Skip to content

Commit

Permalink
Merge pull request #704 from treatybreaker/fix/config-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey authored Aug 4, 2023
2 parents d1e2b34 + 6eb2c33 commit 8ba948f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
53 changes: 44 additions & 9 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local util = require("neogit.lib.util")
local M = {}

---@return table<string, string[]>
Expand Down Expand Up @@ -261,10 +262,29 @@ function M.validate_config()
---Checks if a variable is the correct, type if not it calls err with an error string
---@param value any
---@param name string
---@param expected_type type
local function validate_type(value, name, expected_type)
if type(value) ~= expected_type then
err(name, string.format("Expected `%s` to be of type '%s', got '%s'", name, expected_type, type(value)))
---@param expected_types type | type[]
local function validate_type(value, name, expected_types)
if type(expected_types) == "table" then
if not vim.tbl_contains(expected_types, type(value)) then
err(
name,
string.format(
"Expected `%s` to be one of types '%s', got '%s'",
name,
table.concat(expected_types, ", "),
type(value)
)
)
return false
end
return true
end

if type(value) ~= expected_types then
err(
name,
string.format("Expected `%s` to be of type '%s', got '%s'", name, expected_types, type(value))
)
return false
end
return true
Expand Down Expand Up @@ -401,23 +421,33 @@ function M.validate_config()
end

-- Validate mappings.finder
local valid_finder_commands = {}
local valid_finder_commands = {
false,
}

for _, cmd in pairs(M.get_default_values().mappings.finder) do
table.insert(valid_finder_commands, cmd)
end

local function validate_finder_map(command, key)
if
not validate_type(key, string.format("mappings.finder[%s]", vim.inspect(key)), "string")
or not validate_type(command, string.format("mappings.finder[%s]", vim.inspect(command)), "string")
not validate_type(key, string.format("mappings.finder -> %s", vim.inspect(key)), "string")
or not validate_type(
command,
string.format("mappings.finder[%s]", vim.inspect(command)),
{ "string", "boolean" }
)
then
return
end

if not vim.tbl_contains(valid_finder_commands, command) then
valid_finder_commands = util.map(valid_finder_commands, function(command)
return vim.inspect(command)
end)
err(
string.format("mappings.finder[%s] -> %s", vim.inspect(key), vim.inspect(command)),

string.format(
"Expected a valid finder command, got %s. Valid finder commands: { %s }",
vim.inspect(command),
Expand All @@ -434,7 +464,9 @@ function M.validate_config()
end

-- Validate mappings.status
local valid_status_commands = {}
local valid_status_commands = {
false,
}

for _, cmd in pairs(M.get_default_values().mappings.status) do
table.insert(valid_status_commands, cmd)
Expand All @@ -444,9 +476,12 @@ function M.validate_config()
for key, command in pairs(config.mappings.status) do
if
validate_type(key, "mappings.status -> " .. vim.inspect(key), "string")
and validate_type(command, string.format("mappings.status['%s']", key), "string")
and validate_type(command, string.format("mappings.status['%s']", key), { "string", "boolean" })
then
if not vim.tbl_contains(valid_status_commands, command) then
valid_status_commands = util.map(valid_status_commands, function(command)
return vim.inspect(command)
end)
err(
string.format("mappings.status['%s']", key),
string.format(
Expand Down
30 changes: 14 additions & 16 deletions tests/specs/neogit/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,16 @@ describe("Neogit config", function()
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when the mapping is not a table", function()
config.values.mappings.finder = {
["Close"] = "c",
}
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when a individual mapping is not a string", function()
config.values.mappings.finder = {
["Select"] = { { "c" } },
}
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)

config.values.mappings.finder = {
["Next"] = { false },
["c"] = { { "Close" } },
}
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when a command mapping is not known", function()
config.values.mappings.finder = {
["Invalid Command"] = { "c" },
["c"] = { "Invalid Command" },
}
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)
Expand All @@ -480,9 +468,9 @@ describe("Neogit config", function()
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when a mapping is not a string", function()
it("should return invalid when a mapping is not a string or boolean", function()
config.values.mappings.status = {
["Close"] = false,
["Close"] = {},
}
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)
Expand Down Expand Up @@ -555,6 +543,16 @@ describe("Neogit config", function()
config.values.ignored_settings = { "Valid--setting-format" }
assert.True(vim.tbl_count(require("neogit.config").validate_config()) == 0)
end)

it("should return valid when a command mappings.status is a boolean", function()
config.values.mappings.status["c"] = false
assert.True(vim.tbl_count(require("neogit.config").validate_config()) == 0)
end)

it("should return valid when a command mappings.finder is a boolean", function()
config.values.mappings.finder["c"] = false
assert.True(vim.tbl_count(require("neogit.config").validate_config()) == 0)
end)
end)
end)
end)

0 comments on commit 8ba948f

Please sign in to comment.