Skip to content

Commit

Permalink
fix: ensure lowercase rock names (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Apr 24, 2024
1 parent 462379d commit 4d4b0a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
19 changes: 18 additions & 1 deletion lua/rocks/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ local function check_external_dependency(dep)
end

local function check_config()
start("Checking config")
start("Checking rocks.nvim config")
if vim.g.rocks_nvim and not config.debug_info.was_g_rocks_nvim_sourced then
warn("unrecognized configs in vim.g.rocks_nvim: " .. vim.inspect(config.debug_info.unrecognized_configs))
end
Expand All @@ -131,12 +131,29 @@ local function check_config()
end
end

local function check_rocks_toml()
start("Checking rocks.toml")
local success, user_rocks_or_err = xpcall(require("rocks.config.internal").get_user_rocks, function(err)
error(err)
end)
if not success then
return
end
for rock_name, _ in pairs(user_rocks_or_err) do
if rock_name:lower() ~= rock_name then
error(("Plugin name is not lowercase: %s"):format(rock_name))
end
ok("No errors found in rocks.toml.")
end
end

function health.check()
start("Checking external dependencies")
for _, dep in ipairs(external_dependencies) do
check_external_dependency(dep)
end
check_config()
check_rocks_toml()
end

return health
31 changes: 23 additions & 8 deletions lua/rocks/operations/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ operations.sync = function(user_rocks, on_complete)
end
if user_rocks == nil then
-- Read or create a new config file and decode it
-- NOTE: This does not use parse_user_rocks because we decode with toml, not toml-edit
-- NOTE: This does not use parse_user_rocks
-- because we decode with toml-edit.parse_as_tbl, not toml-edit.parse
user_rocks = config.get_user_rocks()
end

Expand Down Expand Up @@ -174,16 +175,29 @@ operations.sync = function(user_rocks, on_complete)
return get_percentage(ct, action_count)
end

---@type RockSpec[]
---@class SyncSkippedRock
---@field spec RockSpec
---@field reason string

---@type SyncSkippedRock[]
local skipped_rocks = {}

for _, key in ipairs(to_install) do
nio.scheduler()
-- Save skipped rocks for later, when an external handler may have been bootstrapped
if not user_rocks[key].version then
-- Save it for later, when an external handler may have been bootstrapped
table.insert(skipped_rocks, user_rocks[key])
table.insert(skipped_rocks, {
spec = user_rocks[key],
reason = "No version specified",
})
goto skip_install
elseif key:lower() ~= key then
table.insert(skipped_rocks, {
spec = user_rocks[key],
reason = "Name is not lowercase",
})
goto skip_install
end
nio.scheduler()
progress_handle:report({
message = ("Installing: %s"):format(key),
})
Expand Down Expand Up @@ -218,13 +232,14 @@ operations.sync = function(user_rocks, on_complete)

-- rocks.nvim sync handlers should be installed now.
-- try installing any rocks that rocks.nvim could not handle itself
for _, spec in ipairs(skipped_rocks) do
for _, skipped_rock in ipairs(skipped_rocks) do
local spec = skipped_rock.spec
ct = ct + 1
local callback = handlers.get_sync_handler_callback(spec)
if callback then
callback(report_progress, report_error)
else
report_error(("Failed to install %s."):format(spec.name))
report_error(("Failed to install %s: %s"):format(spec.name, skipped_rock.reason))
end
end

Expand Down Expand Up @@ -529,7 +544,7 @@ operations.add = function(arg_list, callback)
return
end
---@type rock_name
local rock_name = arg_list[1]
local rock_name = arg_list[1]:lower()
-- We can't mutate the arg_list, because we may need it for a recursive add
---@type string[]
local args = #arg_list == 1 and {} or { unpack(arg_list, 2, #arg_list) }
Expand Down
8 changes: 7 additions & 1 deletion spec/operations/install_update_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("install/update", function()
local state = require("rocks.state")
nio.tests.it("install and update rocks", function()
local future = nio.control.future()
operations.add({ "neorg", "7.0.0" }, function()
operations.add({ "Neorg", "7.0.0" }, function() -- ensure lower case
future.set(true)
end)
future.wait()
Expand All @@ -21,6 +21,12 @@ describe("install/update", function()
name = "neorg",
version = "7.0.0",
}, installed_rocks.neorg)
nio.sleep(1000) -- Time to write to rocks.toml
local user_rocks = require("rocks.config.internal").get_user_rocks()
assert.same({
name = "neorg",
version = "7.0.0",
}, user_rocks.neorg)
future = nio.control.future()
operations.update(function()
future.set(true)
Expand Down

0 comments on commit 4d4b0a7

Please sign in to comment.