Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/config validation #704

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading