Skip to content

Commit cace57a

Browse files
committed
feat(tests): add tests for utils.fs module
1 parent dcf9c7f commit cace57a

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tests/plenary/helpers.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@ local orgmode = require('orgmode')
33

44
local M = {}
55

6+
---Temporarily change a variable.
7+
---@param ctx table<string, any>
8+
---@param name string
9+
---@param value any
10+
---@param inner fun()
11+
function M.with_var(ctx, name, value, inner)
12+
local old = ctx[name]
13+
ctx[name] = value
14+
local ok, err = pcall(inner)
15+
ctx[name] = old
16+
assert(ok, err)
17+
end
18+
19+
---Temporarily change the working directory.
20+
---@param new_path string
21+
---@param inner fun()
22+
function M.with_cwd(new_path, inner)
23+
local old_path = vim.fn.getcwd()
24+
vim.cmd.cd(new_path)
25+
local ok, err = pcall(inner)
26+
vim.cmd.cd(old_path)
27+
assert(ok, err)
28+
end
29+
630
---@param path string
731
function M.load_file(path)
832
vim.cmd.edit(path)

tests/plenary/utils/fs_spec.lua

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
local fs_utils = require('orgmode.utils.fs')
2+
local helpers = require('tests.plenary.helpers')
3+
4+
local uv = vim.uv or vim.loop
5+
local file = helpers.create_file({})
6+
7+
describe('get_current_file_dir', function()
8+
it('gives the dirname of current_file_path', function()
9+
assert.are.same(vim.fs.dirname(file.filename), fs_utils.get_current_file_dir())
10+
end)
11+
12+
it('always gives an absolute path', function()
13+
local tempdir = vim.fs.dirname(file.filename)
14+
helpers.with_cwd(tempdir, function()
15+
assert.are.same('.', vim.fs.dirname(vim.fn.bufname()))
16+
local dir = fs_utils.get_current_file_dir()
17+
assert.are.same(dir .. '/', vim.fn.fnamemodify(dir, ':p'))
18+
end)
19+
end)
20+
end)
21+
22+
describe('substitute_path', function()
23+
it('leaves absolute paths untouched', function()
24+
assert.are.same('/a/b/c', fs_utils.substitute_path('/a/b/c'))
25+
end)
26+
27+
it('expands ~ to HOME', function()
28+
local output
29+
helpers.with_var(vim.env, 'HOME', '/home/org', function()
30+
output = fs_utils.substitute_path('~/foobar')
31+
end)
32+
assert.are.same('/home/org/foobar', output)
33+
end)
34+
35+
it('expands . to the current file dir', function()
36+
local output = fs_utils.substitute_path('./a/b')
37+
assert(output)
38+
assert(vim.startswith(output, vim.fs.dirname(file.filename)))
39+
end)
40+
41+
it('expands .. to the current file dir plus ..', function()
42+
local output = fs_utils.substitute_path('../a/b')
43+
assert(output)
44+
assert(vim.startswith(output, vim.fs.dirname(file.filename)))
45+
local normalized = vim.fs.normalize(output)
46+
assert.are.Not.same(output, normalized, "normalize didn't remove ..")
47+
end)
48+
49+
it('fails on all other relative paths', function()
50+
assert.is.False(fs_utils.substitute_path('a/b/c'))
51+
end)
52+
end)
53+
54+
describe('get_real_path', function()
55+
local link_name = nil
56+
57+
before_each(function()
58+
if not link_name then
59+
link_name = vim.fn.tempname()
60+
assert(uv.fs_symlink(file.filename, link_name))
61+
end
62+
end)
63+
64+
it('resolves symlinks', function()
65+
assert(link_name)
66+
assert.are.same(file.filename, fs_utils.get_real_path(link_name))
67+
end)
68+
69+
it('resolves relative paths', function()
70+
assert(link_name)
71+
local relpath = vim.fs.joinpath('.', vim.fs.basename(link_name))
72+
assert.are.same(file.filename, fs_utils.get_real_path(relpath))
73+
end)
74+
75+
it('keeps trailing slash if it is there', function()
76+
local path = fs_utils.get_real_path('././')
77+
assert.are.same(vim.fs.dirname(file.filename) .. '/', path)
78+
end)
79+
80+
it('keeps trailing slash away if it is not there', function()
81+
local path = fs_utils.get_real_path('./.')
82+
assert.are.same(vim.fs.dirname(file.filename), path)
83+
end)
84+
85+
it('fails on bare dot "."', function()
86+
assert.is.False(fs_utils.get_real_path('.'))
87+
end)
88+
end)

0 commit comments

Comments
 (0)