feat: test -> run on filenames
This commit is contained in:
parent
1093ff26f6
commit
f8de0207ee
4 changed files with 30 additions and 30 deletions
422
lua/cp/run.lua
Normal file
422
lua/cp/run.lua
Normal file
|
|
@ -0,0 +1,422 @@
|
|||
---@class TestCase
|
||||
---@field index number
|
||||
---@field input string
|
||||
---@field expected string
|
||||
---@field status "pending"|"pass"|"fail"|"running"|"timeout"
|
||||
---@field actual string?
|
||||
---@field actual_highlights table[]?
|
||||
---@field time_ms number?
|
||||
---@field error string?
|
||||
---@field stderr string?
|
||||
---@field selected boolean
|
||||
---@field code number?
|
||||
---@field ok boolean?
|
||||
---@field signal string?
|
||||
---@field timed_out boolean?
|
||||
|
||||
---@class ProblemConstraints
|
||||
---@field timeout_ms number
|
||||
---@field memory_mb number
|
||||
|
||||
---@class RunPanelState
|
||||
---@field test_cases TestCase[]
|
||||
---@field current_index number
|
||||
---@field buffer number?
|
||||
---@field namespace number?
|
||||
---@field is_active boolean
|
||||
---@field saved_layout table?
|
||||
---@field constraints ProblemConstraints?
|
||||
|
||||
local M = {}
|
||||
local constants = require('cp.constants')
|
||||
local logger = require('cp.log')
|
||||
|
||||
---@type RunPanelState
|
||||
local run_panel_state = {
|
||||
test_cases = {},
|
||||
current_index = 1,
|
||||
buffer = nil,
|
||||
namespace = nil,
|
||||
is_active = false,
|
||||
saved_layout = nil,
|
||||
constraints = nil,
|
||||
}
|
||||
|
||||
---@param index number
|
||||
---@param input string
|
||||
---@param expected string
|
||||
---@return TestCase
|
||||
local function create_test_case(index, input, expected)
|
||||
return {
|
||||
index = index,
|
||||
input = input,
|
||||
expected = expected,
|
||||
status = 'pending',
|
||||
actual = nil,
|
||||
time_ms = nil,
|
||||
error = nil,
|
||||
selected = true,
|
||||
}
|
||||
end
|
||||
|
||||
---@param platform string
|
||||
---@param contest_id string
|
||||
---@param problem_id string?
|
||||
---@return TestCase[]
|
||||
local function parse_test_cases_from_cache(platform, contest_id, problem_id)
|
||||
local cache = require('cp.cache')
|
||||
cache.load()
|
||||
local cached_test_cases = cache.get_test_cases(platform, contest_id, problem_id)
|
||||
|
||||
if not cached_test_cases or #cached_test_cases == 0 then
|
||||
return {}
|
||||
end
|
||||
|
||||
local test_cases = {}
|
||||
|
||||
for i, test_case in ipairs(cached_test_cases) do
|
||||
local index = test_case.index or i
|
||||
local expected = test_case.expected or test_case.output or ''
|
||||
table.insert(test_cases, create_test_case(index, test_case.input, expected))
|
||||
end
|
||||
|
||||
return test_cases
|
||||
end
|
||||
|
||||
---@param input_file string
|
||||
---@param expected_file string
|
||||
---@return TestCase[]
|
||||
local function parse_test_cases_from_files(input_file, expected_file)
|
||||
if vim.fn.filereadable(input_file) == 0 or vim.fn.filereadable(expected_file) == 0 then
|
||||
return {}
|
||||
end
|
||||
|
||||
local base_name = vim.fn.fnamemodify(input_file, ':r')
|
||||
local test_cases = {}
|
||||
local i = 1
|
||||
|
||||
while true do
|
||||
local individual_input_file = base_name .. '.' .. i .. '.cpin'
|
||||
local individual_expected_file = base_name .. '.' .. i .. '.cpout'
|
||||
|
||||
if
|
||||
vim.fn.filereadable(individual_input_file) == 1
|
||||
and vim.fn.filereadable(individual_expected_file) == 1
|
||||
then
|
||||
local input_content = table.concat(vim.fn.readfile(individual_input_file), '\n')
|
||||
local expected_content = table.concat(vim.fn.readfile(individual_expected_file), '\n')
|
||||
|
||||
table.insert(test_cases, create_test_case(i, input_content, expected_content))
|
||||
i = i + 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if #test_cases == 0 then
|
||||
local input_content = table.concat(vim.fn.readfile(input_file), '\n')
|
||||
local expected_content = table.concat(vim.fn.readfile(expected_file), '\n')
|
||||
return { create_test_case(1, input_content, expected_content) }
|
||||
end
|
||||
|
||||
return test_cases
|
||||
end
|
||||
|
||||
---@param platform string
|
||||
---@param contest_id string
|
||||
---@param problem_id string?
|
||||
---@return ProblemConstraints?
|
||||
local function load_constraints_from_cache(platform, contest_id, problem_id)
|
||||
local cache = require('cp.cache')
|
||||
cache.load()
|
||||
local timeout_ms, memory_mb = cache.get_constraints(platform, contest_id, problem_id)
|
||||
|
||||
if timeout_ms and memory_mb then
|
||||
return {
|
||||
timeout_ms = timeout_ms,
|
||||
memory_mb = memory_mb,
|
||||
}
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
---@param ctx ProblemContext
|
||||
---@param contest_config ContestConfig
|
||||
---@param test_case TestCase
|
||||
---@return table
|
||||
local function run_single_test_case(ctx, contest_config, cp_config, test_case)
|
||||
local language = vim.fn.fnamemodify(ctx.source_file, ':e')
|
||||
local language_name = constants.filetype_to_language[language] or contest_config.default_language
|
||||
local language_config = contest_config[language_name]
|
||||
|
||||
if not language_config then
|
||||
return {
|
||||
status = 'fail',
|
||||
actual = '',
|
||||
error = 'No language configuration',
|
||||
time_ms = 0,
|
||||
}
|
||||
end
|
||||
|
||||
local function substitute_template(cmd_template, substitutions)
|
||||
local result = {}
|
||||
for _, arg in ipairs(cmd_template) do
|
||||
local substituted = arg
|
||||
for key, value in pairs(substitutions) do
|
||||
substituted = substituted:gsub('{' .. key .. '}', value)
|
||||
end
|
||||
table.insert(result, substituted)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
local function build_command(cmd_template, executable, substitutions)
|
||||
local cmd = substitute_template(cmd_template, substitutions)
|
||||
if executable then
|
||||
table.insert(cmd, 1, executable)
|
||||
end
|
||||
return cmd
|
||||
end
|
||||
|
||||
local substitutions = {
|
||||
source = ctx.source_file,
|
||||
binary = ctx.binary_file,
|
||||
version = tostring(language_config.version or ''),
|
||||
}
|
||||
|
||||
if language_config.compile and vim.fn.filereadable(ctx.binary_file) == 0 then
|
||||
logger.log('binary not found, compiling first...')
|
||||
local compile_cmd = substitute_template(language_config.compile, substitutions)
|
||||
local compile_result = vim.system(compile_cmd, { text = true }):wait()
|
||||
if compile_result.code ~= 0 then
|
||||
return {
|
||||
status = 'fail',
|
||||
actual = '',
|
||||
error = 'Compilation failed: ' .. (compile_result.stderr or 'Unknown error'),
|
||||
stderr = compile_result.stderr or '',
|
||||
time_ms = 0,
|
||||
code = compile_result.code,
|
||||
ok = false,
|
||||
signal = nil,
|
||||
timed_out = false,
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
local run_cmd = build_command(language_config.test, language_config.executable, substitutions)
|
||||
|
||||
local stdin_content = test_case.input .. '\n'
|
||||
|
||||
local start_time = vim.uv.hrtime()
|
||||
local timeout_ms = run_panel_state.constraints and run_panel_state.constraints.timeout_ms or 2000
|
||||
|
||||
if not run_panel_state.constraints then
|
||||
logger.log('no problem constraints available, using default 2000ms timeout')
|
||||
end
|
||||
local result = vim
|
||||
.system(run_cmd, {
|
||||
stdin = stdin_content,
|
||||
timeout = timeout_ms,
|
||||
text = false,
|
||||
})
|
||||
:wait()
|
||||
local execution_time = (vim.uv.hrtime() - start_time) / 1000000
|
||||
|
||||
local ansi = require('cp.ansi')
|
||||
local stdout_str = ansi.bytes_to_string(result.stdout or '')
|
||||
local stderr_str = ansi.bytes_to_string(result.stderr or '')
|
||||
|
||||
local actual_output = stdout_str:gsub('\n$', '')
|
||||
local stderr_output = stderr_str:gsub('\n$', '')
|
||||
|
||||
local actual_highlights = {}
|
||||
|
||||
if stderr_output ~= '' then
|
||||
local stderr_parsed = ansi.parse_ansi_text(stderr_output)
|
||||
local clean_stderr = table.concat(stderr_parsed.lines, '\n')
|
||||
|
||||
local line_offset
|
||||
if actual_output ~= '' then
|
||||
local stdout_lines = vim.split(actual_output, '\n')
|
||||
line_offset = #stdout_lines + 2 -- +1 for empty line, +1 for "--- stderr ---"
|
||||
actual_output = actual_output .. '\n\n--- stderr ---\n' .. clean_stderr
|
||||
else
|
||||
line_offset = 1 -- +1 for "--- stderr ---"
|
||||
actual_output = '--- stderr ---\n' .. clean_stderr
|
||||
end
|
||||
|
||||
for _, highlight in ipairs(stderr_parsed.highlights) do
|
||||
table.insert(actual_highlights, {
|
||||
line = highlight.line + line_offset,
|
||||
col_start = highlight.col_start,
|
||||
col_end = highlight.col_end,
|
||||
highlight_group = highlight.highlight_group,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
local max_lines = cp_config.run_panel.max_output_lines
|
||||
local output_lines = vim.split(actual_output, '\n')
|
||||
if #output_lines > max_lines then
|
||||
local trimmed_lines = {}
|
||||
for i = 1, max_lines do
|
||||
table.insert(trimmed_lines, output_lines[i])
|
||||
end
|
||||
table.insert(trimmed_lines, string.format('... (output trimmed after %d lines)', max_lines))
|
||||
actual_output = table.concat(trimmed_lines, '\n')
|
||||
end
|
||||
|
||||
local expected_output = test_case.expected:gsub('\n$', '')
|
||||
local ok = actual_output == expected_output
|
||||
|
||||
local status
|
||||
local timed_out = result.code == 143 or result.code == 124
|
||||
if timed_out then
|
||||
status = 'timeout'
|
||||
elseif result.code == 0 and ok then
|
||||
status = 'pass'
|
||||
else
|
||||
status = 'fail'
|
||||
end
|
||||
|
||||
local signal = nil
|
||||
if result.code >= 128 then
|
||||
signal = constants.signal_codes[result.code]
|
||||
end
|
||||
|
||||
return {
|
||||
status = status,
|
||||
actual = actual_output,
|
||||
actual_highlights = actual_highlights,
|
||||
error = result.code ~= 0 and result.stderr or nil,
|
||||
stderr = result.stderr or '',
|
||||
time_ms = execution_time,
|
||||
code = result.code,
|
||||
ok = ok,
|
||||
signal = signal,
|
||||
timed_out = timed_out,
|
||||
}
|
||||
end
|
||||
|
||||
---@param ctx ProblemContext
|
||||
---@param state table
|
||||
---@return boolean
|
||||
function M.load_test_cases(ctx, state)
|
||||
local test_cases = parse_test_cases_from_cache(state.platform, state.contest_id, state.problem_id)
|
||||
|
||||
if #test_cases == 0 then
|
||||
test_cases = parse_test_cases_from_files(ctx.input_file, ctx.expected_file)
|
||||
end
|
||||
|
||||
run_panel_state.test_cases = test_cases
|
||||
run_panel_state.current_index = 1
|
||||
run_panel_state.constraints =
|
||||
load_constraints_from_cache(state.platform, state.contest_id, state.problem_id)
|
||||
|
||||
local constraint_info = run_panel_state.constraints
|
||||
and string.format(
|
||||
' with %dms/%dMB limits',
|
||||
run_panel_state.constraints.timeout_ms,
|
||||
run_panel_state.constraints.memory_mb
|
||||
)
|
||||
or ''
|
||||
logger.log(('loaded %d test case(s)%s'):format(#test_cases, constraint_info))
|
||||
return #test_cases > 0
|
||||
end
|
||||
|
||||
---@param ctx ProblemContext
|
||||
---@param contest_config ContestConfig
|
||||
---@param index number
|
||||
---@return boolean
|
||||
function M.run_test_case(ctx, contest_config, cp_config, index)
|
||||
local test_case = run_panel_state.test_cases[index]
|
||||
if not test_case then
|
||||
return false
|
||||
end
|
||||
|
||||
logger.log(('running test case %d'):format(index))
|
||||
test_case.status = 'running'
|
||||
|
||||
local result = run_single_test_case(ctx, contest_config, cp_config, test_case)
|
||||
|
||||
test_case.status = result.status
|
||||
test_case.actual = result.actual
|
||||
test_case.actual_highlights = result.actual_highlights
|
||||
test_case.error = result.error
|
||||
test_case.stderr = result.stderr
|
||||
test_case.time_ms = result.time_ms
|
||||
test_case.code = result.code
|
||||
test_case.ok = result.ok
|
||||
test_case.signal = result.signal
|
||||
test_case.timed_out = result.timed_out
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---@param ctx ProblemContext
|
||||
---@param contest_config ContestConfig
|
||||
---@return TestCase[]
|
||||
function M.run_all_test_cases(ctx, contest_config, cp_config)
|
||||
local results = {}
|
||||
for i, _ in ipairs(run_panel_state.test_cases) do
|
||||
M.run_test_case(ctx, contest_config, cp_config, i)
|
||||
table.insert(results, run_panel_state.test_cases[i])
|
||||
end
|
||||
return results
|
||||
end
|
||||
|
||||
---@return RunPanelState
|
||||
function M.get_run_panel_state()
|
||||
return run_panel_state
|
||||
end
|
||||
|
||||
function M.handle_compilation_failure(compilation_stderr)
|
||||
local ansi = require('cp.ansi')
|
||||
local clean_text = 'Compilation failed'
|
||||
local highlights = {}
|
||||
|
||||
if compilation_stderr and compilation_stderr ~= '' then
|
||||
logger.log('Raw compilation stderr length: ' .. #compilation_stderr)
|
||||
logger.log('Has ANSI codes: ' .. tostring(compilation_stderr:find('\027%[[%d;]*m') ~= nil))
|
||||
|
||||
-- Show first 200 chars to see actual ANSI sequences
|
||||
local sample = compilation_stderr:sub(1, 200):gsub('\027', '\\027')
|
||||
logger.log('Stderr sample: ' .. sample)
|
||||
|
||||
local parsed = ansi.parse_ansi_text(compilation_stderr)
|
||||
clean_text = table.concat(parsed.lines, '\n')
|
||||
highlights = parsed.highlights
|
||||
|
||||
logger.log('Parsed highlights count: ' .. #highlights)
|
||||
for i, hl in ipairs(highlights) do
|
||||
logger.log(
|
||||
'Highlight '
|
||||
.. i
|
||||
.. ': line='
|
||||
.. hl.line
|
||||
.. ' col='
|
||||
.. hl.col_start
|
||||
.. '-'
|
||||
.. hl.col_end
|
||||
.. ' group='
|
||||
.. (hl.highlight_group or 'nil')
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
for _, test_case in ipairs(run_panel_state.test_cases) do
|
||||
test_case.status = 'fail'
|
||||
test_case.actual = clean_text
|
||||
test_case.actual_highlights = highlights
|
||||
test_case.error = 'Compilation failed'
|
||||
test_case.stderr = ''
|
||||
test_case.time_ms = 0
|
||||
test_case.code = 1
|
||||
test_case.ok = false
|
||||
test_case.signal = nil
|
||||
test_case.timed_out = false
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Loading…
Add table
Add a link
Reference in a new issue