From 33cb8570ab0dda1ad915347fae62567d9c1390ed Mon Sep 17 00:00:00 2001 From: Nate Jensvold Date: Tue, 17 Sep 2024 22:56:00 -0700 Subject: [PATCH] fix(cspell_multi): skip adding missing config files to merged config --- lua/cspell/helpers.lua | 13 ++++- tests/spec/diagnostics_spec.lua | 97 ++++++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 26 deletions(-) diff --git a/lua/cspell/helpers.lua b/lua/cspell/helpers.lua index 87e71ca..0b1b596 100644 --- a/lua/cspell/helpers.lua +++ b/lua/cspell/helpers.lua @@ -74,7 +74,18 @@ M.create_merged_cspell_json = function(params, cspell_config_mapping) end for _, cspell_config_path in pairs(cspell_config_mapping) do - table.insert(cspell_config_paths, cspell_config_path) + local path_exists = cspell_config_path ~= nil + and cspell_config_path ~= "" + and Path:new(cspell_config_path):exists() + if path_exists then + table.insert(cspell_config_paths, cspell_config_path) + else + local debug_message = M.format( + 'Unable to find file at "${file_path}", skipping adding to merged cspell config.', + { file_path = cspell_config_path } + ) + logger:debug(debug_message) + end end local cspell_json = { diff --git a/tests/spec/diagnostics_spec.lua b/tests/spec/diagnostics_spec.lua index be765c1..d613da2 100644 --- a/tests/spec/diagnostics_spec.lua +++ b/tests/spec/diagnostics_spec.lua @@ -178,35 +178,82 @@ describe("diagnostics", function() Path:new(CSPELL_MERGED_CONFIG_PATH):rm({}) end) - it("includes a suggestions param", function() - args = args_fn({ - ft = "lua", - bufname = "file.txt", - get_config = function() - return { - find_json = function() - return "some/custom/path/cspell.json" - end, - } - end, - }) + describe("not found", function() + before_each(function() + Path:new("some/custom/path/cspell.json"):rm() + end) + it("includes a suggestions param", function() + args = args_fn({ + ft = "lua", + bufname = "file.txt", + get_config = function() + return { + find_json = function() + return "some/custom/path/cspell.json" + end, + } + end, + }) - assert.same({ - "-c", - CSPELL_MERGED_CONFIG_PATH, - "lint", - "--language-id", - "lua", - "stdin://file.txt", - }, args) + assert.same({ + "-c", + CSPELL_MERGED_CONFIG_PATH, + "lint", + "--language-id", + "lua", + "stdin://file.txt", + }, args) + + local merged_config = vim.json.decode(Path:new(CSPELL_MERGED_CONFIG_PATH):read()) - local merged_config = vim.json.decode(Path:new(CSPELL_MERGED_CONFIG_PATH):read()) + assert.is_table(merged_config) + assert.is_not_nil(merged_config["import"]) - assert.is_table(merged_config) - assert.truthy(merged_config["import"] ~= nil) + assert.falsy(vim.tbl_contains(merged_config.import, "some/custom/path/cspell.json")) + end) + end) - assert.is_table(merged_config.import) - assert.truthy(vim.tbl_contains(merged_config.import, "some/custom/path/cspell.json")) + describe("found", function() + local orig_config_str = Path:new(CSPELL_CONFIG_PATH):read() + local config = vim.json.decode(orig_config_str) + local custom_path = Path:new("some/custom/path/cspell.json") + before_each(function() + custom_path:touch({ parents = true }) + custom_path:write(vim.json.encode(config), "w") + end) + + after_each(function() + Path:new("some"):rm({ recursive = true }) + end) + it("includes a suggestions param", function() + args = args_fn({ + ft = "lua", + bufname = "file.txt", + get_config = function() + return { + find_json = function() + return "some/custom/path/cspell.json" + end, + } + end, + }) + + assert.same({ + "-c", + CSPELL_MERGED_CONFIG_PATH, + "lint", + "--language-id", + "lua", + "stdin://file.txt", + }, args) + + local merged_config = vim.json.decode(Path:new(CSPELL_MERGED_CONFIG_PATH):read()) + + assert.is_table(merged_config) + assert.is_not_nil(merged_config["import"]) + + assert.same(merged_config.import, { "some/custom/path/cspell.json" }) + end) end) end)