Skip to content
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
2 changes: 1 addition & 1 deletion lua/copy_with_context/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function M.get_lines(is_visual)
end

function M.get_file_path(absolute)
return absolute and vim.fn.expand("%:p") or vim.fn.expand("%")
return absolute and vim.fn.expand("%:p") or vim.fn.expand("%:.")
end

function M.process_lines(lines)
Expand Down
24 changes: 20 additions & 4 deletions tests/copy_with_context/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,30 @@ describe("Utility Functions", function()
end)

describe("get_file_path", function()
it("returns the absolute file path", function()
it("returns the absolute file path using %:p", function()
local expand_calls = {}
stub(vim.fn, "expand", function(expr)
table.insert(expand_calls, expr)
assert.equals("%:p", expr, "Expected expand to be called with %:p")
return "/absolute/path/to/file.lua"
end)

local path = utils.get_file_path(true)
assert.equals("absolute_test_file.lua", path)
assert.equals("/absolute/path/to/file.lua", path)
assert.same({ "%:p" }, expand_calls)
end)

it("returns the relative file path", function()
it("returns the relative file path using %:.", function()
local expand_calls = {}
stub(vim.fn, "expand", function(expr)
table.insert(expand_calls, expr)
assert.equals("%:.", expr, "Expected expand to be called with %:.")
return "relative/path/to/file.lua"
end)

local path = utils.get_file_path(false)
assert.equals("test_file.lua", path)
assert.equals("relative/path/to/file.lua", path)
assert.same({ "%:." }, expand_calls)
end)
end)

Expand Down