Skip to content

Commit acdcbd7

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

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

tests/plenary/helpers.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
local OrgFile = require('orgmode.files.file')
22
local orgmode = require('orgmode')
33

4+
local uv = vim.uv or vim.loop
5+
46
local M = {}
57

8+
---Temporarily change a variable.
9+
---@param ctx table<string, any>
10+
---@param name string
11+
---@param value any
12+
---@param inner fun()
13+
function M.with_var(ctx, name, value, inner)
14+
local old = ctx[name]
15+
ctx[name] = value
16+
local ok, err = pcall(inner)
17+
ctx[name] = old
18+
assert(ok, err)
19+
end
20+
21+
---Temporarily change the working directory.
22+
---@param new_path string
23+
---@param inner fun()
24+
function M.with_cwd(new_path, inner)
25+
local old_path = vim.fn.getcwd()
26+
vim.cmd.cd(new_path)
27+
local ok, err = pcall(inner)
28+
vim.cmd.cd(old_path)
29+
assert(ok, err)
30+
-- local old_path = assert(uv.cwd())
31+
-- assert(uv.chdir(new_path))
32+
-- local ok, err = pcall(inner)
33+
-- local ok_back, err2 = uv.chdir(old_path)
34+
-- assert(ok, err)
35+
-- assert(ok_back, err2)
36+
end
37+
638
---@param path string
739
function M.load_file(path)
840
vim.cmd.edit(path)

tests/plenary/utils/fs_spec.lua

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
53+
it('allows passing a custom base', function()
54+
local output = fs_utils.substitute_path('./b/c', 'a/')
55+
assert(output)
56+
assert.are.same(output, 'a//b/c')
57+
end)
58+
end)
59+
60+
describe('get_real_path', function()
61+
local link_name = nil
62+
63+
before_each(function()
64+
if not link_name then
65+
link_name = vim.fn.tempname()
66+
assert(uv.fs_symlink(file.filename, link_name))
67+
end
68+
end)
69+
70+
it('resolves symlinks', function()
71+
assert(link_name)
72+
assert.are.same(file.filename, fs_utils.get_real_path(link_name))
73+
end)
74+
75+
it('resolves relative paths', function()
76+
assert(link_name)
77+
local relpath = vim.fs.joinpath('.', vim.fs.basename(link_name))
78+
assert.are.same(file.filename, fs_utils.get_real_path(relpath))
79+
end)
80+
81+
it('keeps trailing slash if it is there', function()
82+
local path = fs_utils.get_real_path('././')
83+
assert.are.same(vim.fs.dirname(file.filename) .. '/', path)
84+
end)
85+
86+
it('keeps trailing slash away if it is not there', function()
87+
local path = fs_utils.get_real_path('./.')
88+
assert.are.same(vim.fs.dirname(file.filename), path)
89+
end)
90+
91+
it('fails on bare dot "."', function()
92+
assert.is.False(fs_utils.get_real_path('.'))
93+
end)
94+
end)
95+
96+
describe('make_relative', function()
97+
local path = fs_utils.get_real_path(file.filename) ---@cast path string
98+
local basename = vim.fs.basename(path)
99+
local dirname = vim.fs.dirname(path)
100+
local dirname_slash = vim.fs.joinpath(dirname, '')
101+
local root = path
102+
for parent in vim.fs.parents(path) do
103+
root = parent
104+
end
105+
106+
it('gets the basename', function()
107+
local expected = vim.fs.joinpath('.', basename)
108+
local actual = fs_utils.make_relative(path, dirname)
109+
assert.are.same(expected, actual)
110+
end)
111+
112+
it('gets the basename with trailing slash', function()
113+
local expected = vim.fs.joinpath('.', basename)
114+
local actual = fs_utils.make_relative(path, dirname_slash)
115+
assert.are.same(expected, actual)
116+
end)
117+
118+
it('works one level up', function()
119+
local parent_name = vim.fs.basename(dirname)
120+
local expected = vim.fs.joinpath('.', parent_name, basename)
121+
local actual = fs_utils.make_relative(path, vim.fs.dirname(dirname))
122+
assert.are.same(expected, actual)
123+
end)
124+
125+
it('works one level up with trailing slash', function()
126+
local parent_name = vim.fs.basename(dirname)
127+
local expected = vim.fs.joinpath('.', parent_name, basename)
128+
local actual = fs_utils.make_relative(path, vim.fs.dirname(dirname) .. '/')
129+
assert.are.same(expected, actual)
130+
end)
131+
132+
it('produces a relative path even at the root', function()
133+
local relpath = fs_utils.make_relative(path, root)
134+
assert(vim.endswith(path, relpath))
135+
end)
136+
137+
it('climbs up via ..', function()
138+
local relpath = fs_utils.make_relative(root, path)
139+
local only_cdup = vim.regex('\\V\\(../\\)\\+')
140+
assert(only_cdup:match_str(relpath))
141+
end)
142+
end)

0 commit comments

Comments
 (0)