Skip to content

Commit

Permalink
refactor(tests): reduce fake type errors in tests
Browse files Browse the repository at this point in the history
I recently added the types of luassert to my neovim config. It looks
like they disallow many values that actually exist at runtime, so they
are not very good. I will remove them for now anyway, since it's easy.

https://github.com/mikavilpas/dotfiles/blob/b9f67f74ff4075d78fada7ec064e6234a02a2b71/.config/nvim/lua/plugins/luadev.lua?plain=1#L4-L5
  • Loading branch information
mikavilpas committed Nov 30, 2024
1 parent 70c3ef2 commit 1016059
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 79 deletions.
1 change: 1 addition & 0 deletions integration-tests/test-environment/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ vim.env.YAZI_NVIM_LOG_PATH =
vim.fn.fnamemodify(vim.uv.os_environ().HOME .. "/../../.repro/yazi.log", ":p")

-- install the following plugins
---@module "lazy"
---@type LazySpec
local plugins = {
{
Expand Down
4 changes: 4 additions & 0 deletions lua/yazi/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ local M = {}
---@return string
function M.relative_path(config, current_file_dir, selected_file)
local command = config.integrations.resolve_relative_path_application
assert(
command ~= nil,
"resolve_relative_path_application must be set. Please report this as a bug."
)

if vim.fn.executable(command) == 0 then
local msg = string.format(
Expand Down
6 changes: 3 additions & 3 deletions spec/yazi/command_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ describe("the Yazi commands", function()
commands.create_yazi_commands()
vim.cmd(":Yazi")

assert.stub(yazi_stub).was_called_with()
assert.stub(yazi_stub).called_with()
end)

it("creates the `:Yazi cwd` command", function()
commands.create_yazi_commands()
vim.cmd(":Yazi cwd")

assert.stub(yazi_stub).was_called_with(nil, "/tmp")
assert.stub(yazi_stub).called_with(nil, "/tmp")
end)

it("creates the `:Yazi toggle` command", function()
commands.create_yazi_commands()
vim.cmd(":Yazi toggle")

assert.stub(yazi_toggle_stub).was_called_with()
assert.stub(yazi_toggle_stub).called_with()
end)
end)
8 changes: 4 additions & 4 deletions spec/yazi/dir_of_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe("dir_of helper function", function()
function()
it("can detect the dir of a file", function()
local d = utils.dir_of("/my-tmp/file1")
assert.is_equal("/my-tmp", d.filename)
assert.equal("/my-tmp", d.filename)
end)

it("can detect the dir of a directory", function()
-- I think it just thinks directories are files because it cannot know
-- better. But this is still a good default.
local d = utils.dir_of("/my-tmp/dir1")
assert.is_equal("/my-tmp", d.filename)
assert.equal("/my-tmp", d.filename)
end)
end
)
Expand All @@ -38,15 +38,15 @@ describe("dir_of helper function", function()
it("can get the directory of a file", function()
local file = vim.fs.joinpath(base_dir, "abc.txt")
local d = utils.dir_of(file)
assert.is_equal(base_dir, d.filename)
assert.equal(base_dir, d.filename)
end)

it("can get the directory of a directory", function()
local dir = vim.fs.joinpath(base_dir, "dir1")
vim.fn.mkdir(dir)
local d = utils.dir_of(dir)

assert.is_equal(base_dir, d.filename)
assert.equal(base_dir, d.filename)
end)
end)
end)
2 changes: 1 addition & 1 deletion spec/yazi/grug_far_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("the grug-far integration (search and replace)", function()

config.default().integrations.replace_in_directory(tmp_path)

assert.spy(mock_grug_far.open).was_called_with({
assert.spy(mock_grug_far.open).called_with({
prefills = {
paths = "/tmp/folder\\ with\\ spaces",
},
Expand Down
26 changes: 13 additions & 13 deletions spec/yazi/keybinding_helpers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("keybinding_helpers", function()

keybinding_helpers.grep_in_directory(config, "/tmp/file")

assert.stub(s).was_called_with("/tmp")
assert.stub(s).called_with("/tmp")
end)

it("should grep in the directory when a directory is passed", function()
Expand All @@ -38,7 +38,7 @@ describe("keybinding_helpers", function()

keybinding_helpers.grep_in_directory(config, "/tmp")

assert.stub(s).was_called_with("/")
assert.stub(s).called_with("/")
end)

it("should not crash if the integration is disabled", function()
Expand All @@ -63,7 +63,7 @@ describe("keybinding_helpers", function()
{ "/tmp/file1", "/tmp/file2" }
)

assert.equals(2, #results)
assert.equal(2, #results)
assert.are.same(
{ "/tmp/file1", "/tmp/file2" },
vim
Expand All @@ -86,8 +86,8 @@ describe("keybinding_helpers", function()

keybinding_helpers.replace_in_directory(config, "/tmp/file")

assert.stub(stub_replace).was_called_with(match.is_truthy())
assert.equals("/tmp", stub_replace.calls[1].vals[1].filename)
assert.stub(stub_replace).called_with(match.is_truthy())
assert.equal("/tmp", stub_replace.calls[1].vals[1].filename)
end
)

Expand All @@ -103,8 +103,8 @@ describe("keybinding_helpers", function()

keybinding_helpers.replace_in_directory(config, "/tmp")

assert.stub(stub_replace).was_called_with(match.is_truthy())
assert.equals("/", stub_replace.calls[1].vals[1].filename)
assert.stub(stub_replace).called_with(match.is_truthy())
assert.equal("/", stub_replace.calls[1].vals[1].filename)
end
)
end)
Expand All @@ -124,7 +124,7 @@ describe("keybinding_helpers", function()
{ "/tmp/file1", "/tmp/file2" }
)

assert.equals(2, #results)
assert.equal(2, #results)

local paths = vim
.iter(results)
Expand Down Expand Up @@ -152,9 +152,9 @@ describe("keybinding_helpers", function()

assert
.stub(vim_cmd_stub)
.was_called_with({ cmd = "cd", args = { "/tmp" } })
.called_with({ cmd = "cd", args = { "/tmp" } })

assert.stub(vim_notify_stub).was_called_with('cwd changed to "/tmp"')
assert.stub(vim_notify_stub).called_with('cwd changed to "/tmp"')
end
)

Expand All @@ -170,8 +170,8 @@ describe("keybinding_helpers", function()

assert
.stub(vim_cmd_stub)
.was_called_with({ cmd = "cd", args = { "/tmp" } })
assert.stub(vim_notify_stub).was_called_with('cwd changed to "/tmp"')
.called_with({ cmd = "cd", args = { "/tmp" } })
assert.stub(vim_notify_stub).called_with('cwd changed to "/tmp"')
end)

it(
Expand All @@ -187,7 +187,7 @@ describe("keybinding_helpers", function()
},
})

assert.stub(vim_fn_stub).was_called_with()
assert.stub(vim_fn_stub).called_with()
assert.stub(vim_cmd_stub).was_not_called()
assert.stub(vim_notify_stub).was_not_called()
end
Expand Down
10 changes: 5 additions & 5 deletions spec/yazi/move_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
move_event
)

assert.is_equal(vim.tbl_count(instructions), 1)
assert.equal(vim.tbl_count(instructions), 1)

local result1 = instructions[1]
assert.is_equal("/my-tmp/file2", result1.path.filename)
assert.equal("/my-tmp/file2", result1.path.filename)
assert.is_number(result1.bufnr)
end)

Expand All @@ -47,9 +47,9 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
move_event
)

assert.is_equal(vim.tbl_count(instructions), 1)
assert.equal(vim.tbl_count(instructions), 1)
local result = instructions[1]
assert.is_equal("/my-tmp/dir2/file", result.path.filename)
assert.equal("/my-tmp/dir2/file", result.path.filename)
end
)

Expand All @@ -68,6 +68,6 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
move_event
)

assert.is_equal(vim.tbl_count(instructions), 0)
assert.equal(vim.tbl_count(instructions), 0)
end)
end)
12 changes: 6 additions & 6 deletions spec/yazi/open_multiple_files_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe("the default configuration", function()

local buffers = vim.api.nvim_list_bufs()

assert.equals(2, #buffers)
assert.equals("/abc/test-file.txt", vim.api.nvim_buf_get_name(buffers[1]))
assert.equals("/abc/test-file2.txt", vim.api.nvim_buf_get_name(buffers[2]))
assert.equal(2, #buffers)
assert.equal("/abc/test-file.txt", vim.api.nvim_buf_get_name(buffers[1]))
assert.equal("/abc/test-file2.txt", vim.api.nvim_buf_get_name(buffers[2]))
end)

it("can display multiple files in the quickfix list", function()
Expand All @@ -41,8 +41,8 @@ describe("the default configuration", function()

local quickfix_list = vim.fn.getqflist()

assert.equals(2, #quickfix_list)
assert.equals("/abc/test-$@file.txt", quickfix_list[1].text)
assert.equals("/abc/test-file2.txt", quickfix_list[2].text)
assert.equal(2, #quickfix_list)
assert.equal("/abc/test-$@file.txt", quickfix_list[1].text)
assert.equal("/abc/test-file2.txt", quickfix_list[2].text)
end)
end)
4 changes: 2 additions & 2 deletions spec/yazi/plugin_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe("installing a plugin", function()
name = "test-plugin-2",
}, { yazi_dir = yazi_dir })

assert.is_equal(result.error, "source directory does not exist")
assert.is_equal(result.from, plugin_dir)
assert.equal(result.error, "source directory does not exist")
assert.equal(result.from, plugin_dir)
end)

it("can install a plugin from a monorepo subdirectory", function()
Expand Down
10 changes: 5 additions & 5 deletions spec/yazi/rename_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
rename_event
)

assert.is_equal(vim.tbl_count(rename_instructions), 1)
assert.equal(vim.tbl_count(rename_instructions), 1)

local result1 = rename_instructions[1]
assert.is_equal("/my-tmp/file2", result1.path.filename)
assert.equal("/my-tmp/file2", result1.path.filename)
assert.is_number(result1.bufnr)
end)

Expand All @@ -48,9 +48,9 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
rename_event
)

assert.is_equal(vim.tbl_count(rename_instructions), 1)
assert.equal(vim.tbl_count(rename_instructions), 1)
local result = rename_instructions[1]
assert.is_equal("/my-tmp/dir2/file", result.path.filename)
assert.equal("/my-tmp/dir2/file", result.path.filename)
end
)

Expand All @@ -69,7 +69,7 @@ describe("get_buffers_that_need_renaming_after_yazi_exited", function()
rename_event
)

assert.is_equal(vim.tbl_count(rename_instructions), 0)
assert.equal(vim.tbl_count(rename_instructions), 0)
end)
end)

Expand Down
16 changes: 8 additions & 8 deletions spec/yazi/selected_files_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ describe("choosing the correct files when starting yazi", function()

local result = utils.selected_file_path("/my-tmp/file1")

assert.is_equal("/my-tmp/file1", result.filename)
assert.equal("/my-tmp/file1", result.filename)
end)

it("when no file is loaded, returns the current directory", function()
local result = utils.selected_file_path()

assert.is_equal(result.filename, vim.fn.getcwd())
assert.equal(result.filename, vim.fn.getcwd())
end)
end)

describe(" selected_files", function()
it("when no file is loaded, returns the current directory", function()
local result = utils.selected_file_paths()
assert.is_equal(result[1].filename, vim.fn.getcwd())
assert.equal(result[1].filename, vim.fn.getcwd())
end)

it("when given a file, returns that file", function()
vim.fn.bufadd("/my-tmp/file1")

local result = utils.selected_file_paths("/my-tmp/file1")

assert.is_equal("/my-tmp/file1", result[1].filename)
assert.is_equal(1, #result)
assert.equal("/my-tmp/file1", result[1].filename)
assert.equal(1, #result)
end)

it(
Expand All @@ -46,10 +46,10 @@ describe("choosing the correct files when starting yazi", function()

local result = utils.selected_file_paths("/my-tmp/file1")

assert.is_equal(#result, 2)
assert.equal(#result, 2)

assert.is_equal("/my-tmp/file1", result[1].filename)
assert.is_equal("/my-tmp/file2", result[2].filename)
assert.equal("/my-tmp/file1", result[1].filename)
assert.equal("/my-tmp/file2", result[2].filename)
end
)
end)
Expand Down
Loading

0 comments on commit 1016059

Please sign in to comment.