Skip to content

Commit 67a7252

Browse files
committed
feat(tests): add tests for utils.fs module
1 parent 8a7b87f commit 67a7252

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-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: 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)