Skip to content

Commit 3583fac

Browse files
troigantotroiganto
and
troiganto
authored
feat(tests): add tests for utils.fs and utils.current_file_path() (#890)
* refactor(mappings): use `utils.tags_to_string()` * refactor(tests): define exports same as everywhere else * feat(tests): add tests for utils.current_file_path() * feat(tests): add tests for `utils.fs` module --------- Co-authored-by: troiganto <[email protected]>
1 parent c6d3dc8 commit 3583fac

File tree

4 files changed

+166
-28
lines changed

4 files changed

+166
-28
lines changed

lua/orgmode/org/mappings.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function OrgMappings:set_tags(tags)
5757
end)
5858
end
5959
if type(tags) == 'table' then
60-
tags = string.format(':%s:', table.concat(tags, ':'))
60+
tags = utils.tags_to_string(tags)
6161
end
6262

6363
return tags

tests/plenary/helpers.lua

+42-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,49 @@
11
local OrgFile = require('orgmode.files.file')
22
local orgmode = require('orgmode')
33

4-
local function load_file(path)
5-
vim.cmd(string.format('e %s', path))
4+
local M = {}
5+
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+
30+
---@param path string
31+
function M.load_file(path)
32+
vim.cmd.edit(path)
633
return orgmode.files:get(path)
734
end
835

9-
local function create_file(lines)
36+
---@param lines string[]
37+
function M.create_file(lines)
1038
local fname = vim.fn.tempname() .. '.org'
1139
vim.fn.writefile(lines or {}, fname)
12-
return load_file(fname)
40+
return M.load_file(fname)
1341
end
1442

43+
---@param lines string[]
44+
---@param config? table
1545
---@return OrgFile
16-
local function create_agenda_file(lines, config)
46+
function M.create_agenda_file(lines, config)
1747
local fname = vim.fn.tempname() .. '.org'
1848
vim.fn.writefile(lines or {}, fname)
1949

@@ -22,11 +52,13 @@ local function create_agenda_file(lines, config)
2252
}, config or {})
2353
local org = orgmode.setup(cfg)
2454
org:init()
25-
return load_file(fname)
55+
return M.load_file(fname)
2656
end
2757

58+
---@param lines string[]
59+
---@param filename string
2860
---@return OrgFile
29-
local function create_file_instance(lines, filename)
61+
function M.create_file_instance(lines, filename)
3062
local file = OrgFile:new({
3163
filename = filename or vim.fn.tempname() .. '.org',
3264
lines = lines,
@@ -36,9 +68,9 @@ local function create_file_instance(lines, filename)
3668
end
3769

3870
---@param fixtures {filename: string, content: string[] }[]
39-
---@param config table?
71+
---@param config? table
4072
---@return table
41-
local function create_agenda_files(fixtures, config)
73+
function M.create_agenda_files(fixtures, config)
4274
-- NOTE: content is only 1 line for 1 file
4375
local temp_fname = vim.fn.tempname()
4476
local temp_dir = vim.fn.fnamemodify(temp_fname, ':p:h')
@@ -65,10 +97,4 @@ local function create_agenda_files(fixtures, config)
6597
return files
6698
end
6799

68-
return {
69-
load_file = load_file,
70-
create_file = create_file,
71-
create_file_instance = create_file_instance,
72-
create_agenda_file = create_agenda_file,
73-
create_agenda_files = create_agenda_files,
74-
}
100+
return M

tests/plenary/utils/fs_spec.lua

+88
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)

tests/plenary/utils_spec.lua

+35-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
local utils = require('orgmode.utils')
2+
local helpers = require('tests.plenary.helpers')
23

3-
describe('Utils', function()
4-
it('should properly reduce', function()
4+
describe('Util', function()
5+
describe('reduce', function()
56
local nums = { 1, 2, 3 }
6-
local sum = utils.reduce(nums, function(acc, num)
7-
return acc + num
8-
end, 0)
9-
assert.are.same(6, sum)
107

11-
local multiplied = utils.reduce(nums, function(acc, num)
12-
table.insert(acc, num * 2)
13-
return acc
14-
end, {})
15-
assert.are.same({ 2, 4, 6 }, multiplied)
8+
it('works on sums', function()
9+
local sum = utils.reduce(nums, function(acc, num)
10+
return acc + num
11+
end, 0)
12+
assert.are.same(6, sum)
13+
end)
14+
15+
it('works on products', function()
16+
local multiplied = utils.reduce(nums, function(acc, num)
17+
table.insert(acc, num * 2)
18+
return acc
19+
end, {})
20+
assert.are.same({ 2, 4, 6 }, multiplied)
21+
end)
22+
end)
23+
24+
describe('current_file_path', function()
25+
it('returns the buffer name', function()
26+
local file = helpers.create_file({})
27+
assert.are.Not.same('', file.filename)
28+
assert.are.same(file.filename, utils.current_file_path())
29+
end)
30+
it('always returns the full path', function()
31+
local file = helpers.create_file({})
32+
local dirname = vim.fs.dirname(file.filename)
33+
helpers.with_cwd(dirname, function()
34+
local relpath = vim.fn.bufname()
35+
local abspath = utils.current_file_path()
36+
assert(vim.endswith(abspath, relpath))
37+
assert.are.Not.same(abspath, relpath)
38+
end)
39+
end)
1640
end)
1741
end)

0 commit comments

Comments
 (0)