From b5b37074fb7c773b3f6305d09d030c74da849eb5 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Fri, 19 Sep 2025 19:33:28 -0400 Subject: [PATCH] fix(ci): validate config after merge --- doc/cp.txt | 4 +- lua/cp/config.lua | 117 +++++++++++++++++++++---------------------- lua/cp/init.lua | 4 +- spec/config_spec.lua | 2 +- 4 files changed, 61 insertions(+), 66 deletions(-) diff --git a/doc/cp.txt b/doc/cp.txt index 3933a70..64f4609 100644 --- a/doc/cp.txt +++ b/doc/cp.txt @@ -104,7 +104,7 @@ Here's an example configuration with lazy.nvim: > }, }, hooks = { - before_test = function(ctx) vim.cmd.w() end, + before_run = function(ctx) vim.cmd.w() end, before_debug = function(ctx) ... end, setup_code = function(ctx) vim.wo.foldmethod = "marker" @@ -186,7 +186,7 @@ Here's an example configuration with lazy.nvim: > *cp.Hooks* Fields: ~ - • {before_test}? (`function`) Called before test panel opens. + • {before_run}? (`function`) Called before test panel opens. `function(ctx: ProblemContext)` • {before_debug}? (`function`) Called before debug compilation. `function(ctx: ProblemContext)` diff --git a/lua/cp/config.lua b/lua/cp/config.lua index 2242fa2..d92c267 100644 --- a/lua/cp/config.lua +++ b/lua/cp/config.lua @@ -27,7 +27,7 @@ ---@field timeout_ms? number ---@class Hooks ----@field before_test? fun(ctx: ProblemContext) +---@field before_run? fun(ctx: ProblemContext) ---@field before_debug? fun(ctx: ProblemContext) ---@field setup_code? fun(ctx: ProblemContext) @@ -72,7 +72,7 @@ M.defaults = { contests = {}, snippets = {}, hooks = { - before_test = nil, + before_run = nil, before_debug = nil, setup_code = nil, }, @@ -112,65 +112,6 @@ function M.setup(user_config) diff = { user_config.diff, { 'table', 'nil' }, true }, }) - if user_config.hooks then - vim.validate({ - before_test = { - user_config.hooks.before_test, - { 'function', 'nil' }, - true, - }, - before_debug = { - user_config.hooks.before_debug, - { 'function', 'nil' }, - true, - }, - setup_code = { - user_config.hooks.setup_code, - { 'function', 'nil' }, - true, - }, - }) - end - - if user_config.run_panel then - vim.validate({ - diff_mode = { - user_config.run_panel.diff_mode, - function(value) - return vim.tbl_contains({ 'vim', 'git' }, value) - end, - "diff_mode must be 'vim' or 'git'", - }, - next_test_key = { - user_config.run_panel.next_test_key, - function(value) - return type(value) == 'string' and value ~= '' - end, - 'next_test_key must be a non-empty string', - }, - prev_test_key = { - user_config.run_panel.prev_test_key, - function(value) - return type(value) == 'string' and value ~= '' - end, - 'prev_test_key must be a non-empty string', - }, - toggle_diff_key = { - user_config.run_panel.toggle_diff_key, - function(value) - return type(value) == 'string' and value ~= '' - end, - 'toggle_diff_key must be a non-empty string', - }, - }) - end - - if user_config.diff then - vim.validate({ - git = { user_config.diff.git, { 'table', 'nil' }, true }, - }) - end - if user_config.contests then for contest_name, contest_config in pairs(user_config.contests) do for lang_name, lang_config in pairs(contest_config) do @@ -214,6 +155,60 @@ function M.setup(user_config) local config = vim.tbl_deep_extend('force', M.defaults, user_config or {}) + -- Validate merged config values + vim.validate({ + before_run = { + config.hooks.before_run, + { 'function', 'nil' }, + true, + }, + before_debug = { + config.hooks.before_debug, + { 'function', 'nil' }, + true, + }, + setup_code = { + config.hooks.setup_code, + { 'function', 'nil' }, + true, + }, + }) + + vim.validate({ + diff_mode = { + config.run_panel.diff_mode, + function(value) + return vim.tbl_contains({ 'vim', 'git' }, value) + end, + "diff_mode must be 'vim' or 'git'", + }, + next_test_key = { + config.run_panel.next_test_key, + function(value) + return type(value) == 'string' and value ~= '' + end, + 'next_test_key must be a non-empty string', + }, + prev_test_key = { + config.run_panel.prev_test_key, + function(value) + return type(value) == 'string' and value ~= '' + end, + 'prev_test_key must be a non-empty string', + }, + toggle_diff_key = { + config.run_panel.toggle_diff_key, + function(value) + return type(value) == 'string' and value ~= '' + end, + 'toggle_diff_key must be a non-empty string', + }, + }) + + vim.validate({ + git = { config.diff.git, { 'table', 'nil' }, true }, + }) + for _, contest_config in pairs(config.contests) do for lang_name, lang_config in pairs(contest_config) do if type(lang_config) == 'table' and not lang_config.extension then diff --git a/lua/cp/init.lua b/lua/cp/init.lua index c91815a..54b75e4 100644 --- a/lua/cp/init.lua +++ b/lua/cp/init.lua @@ -459,8 +459,8 @@ local function toggle_run_panel(is_debug) setup_keybindings_for_buffer(test_buffers.tab_buf) - if config.hooks and config.hooks.before_test then - config.hooks.before_test(ctx) + if config.hooks and config.hooks.before_run then + config.hooks.before_run(ctx) end if is_debug and config.hooks and config.hooks.before_debug then diff --git a/spec/config_spec.lua b/spec/config_spec.lua index f05c07a..cd5b058 100644 --- a/spec/config_spec.lua +++ b/spec/config_spec.lua @@ -66,7 +66,7 @@ describe('cp.config', function() it('validates hook functions', function() local invalid_config = { - hooks = { before_test = 'not_a_function' }, + hooks = { before_run = 'not_a_function' }, } assert.has_error(function()