From 9986f128e9c7822e0472afa4815fac6d4fed4899 Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:37:47 +0100 Subject: [PATCH 1/4] refactor(mappings): use `utils.tags_to_string()` --- lua/orgmode/org/mappings.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index e1135001b..b23ccf079 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -57,7 +57,7 @@ function OrgMappings:set_tags(tags) end) end if type(tags) == 'table' then - tags = string.format(':%s:', table.concat(tags, ':')) + tags = utils.tags_to_string(tags) end return tags From f07d8d93b3b5d1023ddb88d2b7fe36991123525e Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:43:48 +0100 Subject: [PATCH 2/4] refactor(tests): define exports same as everywhere else --- tests/plenary/helpers.lua | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/plenary/helpers.lua b/tests/plenary/helpers.lua index 91886eff1..e7d046fed 100644 --- a/tests/plenary/helpers.lua +++ b/tests/plenary/helpers.lua @@ -1,19 +1,25 @@ local OrgFile = require('orgmode.files.file') local orgmode = require('orgmode') -local function load_file(path) - vim.cmd(string.format('e %s', path)) +local M = {} + +---@param path string +function M.load_file(path) + vim.cmd.edit(path) return orgmode.files:get(path) end -local function create_file(lines) +---@param lines string[] +function M.create_file(lines) local fname = vim.fn.tempname() .. '.org' vim.fn.writefile(lines or {}, fname) - return load_file(fname) + return M.load_file(fname) end +---@param lines string[] +---@param config? table ---@return OrgFile -local function create_agenda_file(lines, config) +function M.create_agenda_file(lines, config) local fname = vim.fn.tempname() .. '.org' vim.fn.writefile(lines or {}, fname) @@ -22,11 +28,13 @@ local function create_agenda_file(lines, config) }, config or {}) local org = orgmode.setup(cfg) org:init() - return load_file(fname) + return M.load_file(fname) end +---@param lines string[] +---@param filename string ---@return OrgFile -local function create_file_instance(lines, filename) +function M.create_file_instance(lines, filename) local file = OrgFile:new({ filename = filename or vim.fn.tempname() .. '.org', lines = lines, @@ -36,9 +44,9 @@ local function create_file_instance(lines, filename) end ---@param fixtures {filename: string, content: string[] }[] ----@param config table? +---@param config? table ---@return table -local function create_agenda_files(fixtures, config) +function M.create_agenda_files(fixtures, config) -- NOTE: content is only 1 line for 1 file local temp_fname = vim.fn.tempname() local temp_dir = vim.fn.fnamemodify(temp_fname, ':p:h') @@ -65,10 +73,4 @@ local function create_agenda_files(fixtures, config) return files end -return { - load_file = load_file, - create_file = create_file, - create_file_instance = create_file_instance, - create_agenda_file = create_agenda_file, - create_agenda_files = create_agenda_files, -} +return M From dcf9c7f4f82a6daa589b420add880ab600269be5 Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:50:06 +0100 Subject: [PATCH 3/4] feat(tests): add tests for utils.current_file_path() --- tests/plenary/utils_spec.lua | 46 +++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/plenary/utils_spec.lua b/tests/plenary/utils_spec.lua index b8993efbf..b969eb23b 100644 --- a/tests/plenary/utils_spec.lua +++ b/tests/plenary/utils_spec.lua @@ -1,17 +1,41 @@ local utils = require('orgmode.utils') +local helpers = require('tests.plenary.helpers') -describe('Utils', function() - it('should properly reduce', function() +describe('Util', function() + describe('reduce', function() local nums = { 1, 2, 3 } - local sum = utils.reduce(nums, function(acc, num) - return acc + num - end, 0) - assert.are.same(6, sum) - local multiplied = utils.reduce(nums, function(acc, num) - table.insert(acc, num * 2) - return acc - end, {}) - assert.are.same({ 2, 4, 6 }, multiplied) + it('works on sums', function() + local sum = utils.reduce(nums, function(acc, num) + return acc + num + end, 0) + assert.are.same(6, sum) + end) + + it('works on products', function() + local multiplied = utils.reduce(nums, function(acc, num) + table.insert(acc, num * 2) + return acc + end, {}) + assert.are.same({ 2, 4, 6 }, multiplied) + end) + end) + + describe('current_file_path', function() + it('returns the buffer name', function() + local file = helpers.create_file({}) + assert.are.Not.same('', file.filename) + assert.are.same(file.filename, utils.current_file_path()) + end) + it('always returns the full path', function() + local file = helpers.create_file({}) + local dirname = vim.fs.dirname(file.filename) + helpers.with_cwd(dirname, function() + local relpath = vim.fn.bufname() + local abspath = utils.current_file_path() + assert(vim.endswith(abspath, relpath)) + assert.are.Not.same(abspath, relpath) + end) + end) end) end) From cace57ab35e3321e5b56baaf5a14d2cbee0b668f Mon Sep 17 00:00:00 2001 From: troiganto Date: Sat, 25 Jan 2025 22:57:40 +0100 Subject: [PATCH 4/4] feat(tests): add tests for `utils.fs` module --- tests/plenary/helpers.lua | 24 +++++++++ tests/plenary/utils/fs_spec.lua | 88 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 tests/plenary/utils/fs_spec.lua diff --git a/tests/plenary/helpers.lua b/tests/plenary/helpers.lua index e7d046fed..425bb1808 100644 --- a/tests/plenary/helpers.lua +++ b/tests/plenary/helpers.lua @@ -3,6 +3,30 @@ local orgmode = require('orgmode') local M = {} +---Temporarily change a variable. +---@param ctx table +---@param name string +---@param value any +---@param inner fun() +function M.with_var(ctx, name, value, inner) + local old = ctx[name] + ctx[name] = value + local ok, err = pcall(inner) + ctx[name] = old + assert(ok, err) +end + +---Temporarily change the working directory. +---@param new_path string +---@param inner fun() +function M.with_cwd(new_path, inner) + local old_path = vim.fn.getcwd() + vim.cmd.cd(new_path) + local ok, err = pcall(inner) + vim.cmd.cd(old_path) + assert(ok, err) +end + ---@param path string function M.load_file(path) vim.cmd.edit(path) diff --git a/tests/plenary/utils/fs_spec.lua b/tests/plenary/utils/fs_spec.lua new file mode 100644 index 000000000..18afbad73 --- /dev/null +++ b/tests/plenary/utils/fs_spec.lua @@ -0,0 +1,88 @@ +local fs_utils = require('orgmode.utils.fs') +local helpers = require('tests.plenary.helpers') + +local uv = vim.uv or vim.loop +local file = helpers.create_file({}) + +describe('get_current_file_dir', function() + it('gives the dirname of current_file_path', function() + assert.are.same(vim.fs.dirname(file.filename), fs_utils.get_current_file_dir()) + end) + + it('always gives an absolute path', function() + local tempdir = vim.fs.dirname(file.filename) + helpers.with_cwd(tempdir, function() + assert.are.same('.', vim.fs.dirname(vim.fn.bufname())) + local dir = fs_utils.get_current_file_dir() + assert.are.same(dir .. '/', vim.fn.fnamemodify(dir, ':p')) + end) + end) +end) + +describe('substitute_path', function() + it('leaves absolute paths untouched', function() + assert.are.same('/a/b/c', fs_utils.substitute_path('/a/b/c')) + end) + + it('expands ~ to HOME', function() + local output + helpers.with_var(vim.env, 'HOME', '/home/org', function() + output = fs_utils.substitute_path('~/foobar') + end) + assert.are.same('/home/org/foobar', output) + end) + + it('expands . to the current file dir', function() + local output = fs_utils.substitute_path('./a/b') + assert(output) + assert(vim.startswith(output, vim.fs.dirname(file.filename))) + end) + + it('expands .. to the current file dir plus ..', function() + local output = fs_utils.substitute_path('../a/b') + assert(output) + assert(vim.startswith(output, vim.fs.dirname(file.filename))) + local normalized = vim.fs.normalize(output) + assert.are.Not.same(output, normalized, "normalize didn't remove ..") + end) + + it('fails on all other relative paths', function() + assert.is.False(fs_utils.substitute_path('a/b/c')) + end) +end) + +describe('get_real_path', function() + local link_name = nil + + before_each(function() + if not link_name then + link_name = vim.fn.tempname() + assert(uv.fs_symlink(file.filename, link_name)) + end + end) + + it('resolves symlinks', function() + assert(link_name) + assert.are.same(file.filename, fs_utils.get_real_path(link_name)) + end) + + it('resolves relative paths', function() + assert(link_name) + local relpath = vim.fs.joinpath('.', vim.fs.basename(link_name)) + assert.are.same(file.filename, fs_utils.get_real_path(relpath)) + end) + + it('keeps trailing slash if it is there', function() + local path = fs_utils.get_real_path('././') + assert.are.same(vim.fs.dirname(file.filename) .. '/', path) + end) + + it('keeps trailing slash away if it is not there', function() + local path = fs_utils.get_real_path('./.') + assert.are.same(vim.fs.dirname(file.filename), path) + end) + + it('fails on bare dot "."', function() + assert.is.False(fs_utils.get_real_path('.')) + end) +end)