Skip to content

Commit

Permalink
fix(cspell_multi): skip adding missing config files to merged config
Browse files Browse the repository at this point in the history
  • Loading branch information
JateNensvold authored and davidmh committed Sep 21, 2024
1 parent 882b520 commit 33cb857
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 26 deletions.
13 changes: 12 additions & 1 deletion lua/cspell/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
97 changes: 72 additions & 25 deletions tests/spec/diagnostics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 33cb857

Please sign in to comment.