2021-04-30 13:30:43 +00:00
|
|
|
local ls = require 'zettelkasten.files'
|
2020-10-29 17:17:00 +00:00
|
|
|
-- these tests, I suppose, only work on unix due to the file structure
|
|
|
|
|
2020-10-30 12:48:17 +00:00
|
|
|
local function simple_api_mock(files)
|
|
|
|
return {
|
|
|
|
g = {},
|
|
|
|
b = {},
|
|
|
|
loop = {
|
|
|
|
fs_scandir = function()
|
|
|
|
if #files == 0 then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end,
|
2020-10-30 15:04:23 +00:00
|
|
|
fs_scandir_next = function() return table.remove(files) end
|
2020-10-30 12:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe("get_anchors_and_paths", function()
|
2022-01-21 16:15:32 +00:00
|
|
|
before_each(function() Get_api_mock = simple_api_mock end)
|
2020-10-30 15:04:23 +00:00
|
|
|
after_each(function() _G.vim = nil end)
|
2020-10-29 18:00:23 +00:00
|
|
|
|
|
|
|
it("should return anchor-keyed table pointing to filename of zettel",
|
|
|
|
function()
|
2021-04-30 13:07:36 +00:00
|
|
|
local file_list = {}
|
2021-04-30 14:16:13 +00:00
|
|
|
file_list["someDir/1910291645 this-is-a-testfile.md"] =
|
|
|
|
"1910291645 this-is-a-testfile.md"
|
2022-01-21 16:15:32 +00:00
|
|
|
_G.vim = Get_api_mock(file_list)
|
2020-10-29 18:00:23 +00:00
|
|
|
|
2020-11-06 16:28:05 +00:00
|
|
|
local expected = {
|
|
|
|
["1910291645"] = "someDir/1910291645 this-is-a-testfile.md"
|
|
|
|
}
|
2021-04-30 13:07:36 +00:00
|
|
|
assert.same(expected, ls.get_anchors_and_paths(file_list))
|
2020-10-29 18:00:23 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should ignore any malformed files", function()
|
|
|
|
local file_list = {
|
2021-04-30 13:07:36 +00:00
|
|
|
["someDir/2010261208 this-should-be-picked-up.md"] = "2010261208 this-should-be-picked-up.md",
|
|
|
|
["someDir/1910291645 this-is-a-testfile.md"] = "1910291645 this-is-a-testfile.md",
|
|
|
|
["someDir/this-is-not-a-testfile.md"] = "this-is-not-a-testfile.md",
|
|
|
|
["1910271456 this-is-wrong-extension.txt"] = "1910271456 this-is-wrong-extension.txt",
|
2021-04-30 14:16:13 +00:00
|
|
|
["1812 this-is-ignored.md"] = "1812 this-is-ignored.md"
|
2020-10-29 17:17:00 +00:00
|
|
|
}
|
2022-01-21 16:15:32 +00:00
|
|
|
_G.vim = Get_api_mock(file_list)
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
local expected = {
|
2020-11-06 16:28:05 +00:00
|
|
|
["1910291645"] = "someDir/1910291645 this-is-a-testfile.md",
|
|
|
|
["2010261208"] = "someDir/2010261208 this-should-be-picked-up.md"
|
2020-10-29 18:00:23 +00:00
|
|
|
}
|
2021-04-30 13:07:36 +00:00
|
|
|
assert.same(expected, ls.get_anchors_and_paths(file_list))
|
2020-10-29 18:00:23 +00:00
|
|
|
end)
|
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
it("should adhere to the zettel extension defined in options", function()
|
|
|
|
local file_list = {
|
|
|
|
["mydirectory/1910291645 myfile.wiki"] = "1910291645 myfile.wiki",
|
|
|
|
["mydirectory/2345678901 another.wiki"] = "2345678901 another.wiki"
|
|
|
|
}
|
2022-01-21 16:15:32 +00:00
|
|
|
_G.vim = Get_api_mock(file_list)
|
2021-04-30 13:07:36 +00:00
|
|
|
vim.g['zettel_extension'] = '.wiki'
|
|
|
|
|
|
|
|
local expected = {
|
|
|
|
["1910291645"] = "mydirectory/1910291645 myfile.wiki",
|
|
|
|
["2345678901"] = "mydirectory/2345678901 another.wiki"
|
|
|
|
}
|
|
|
|
|
2021-04-30 14:16:13 +00:00
|
|
|
assert.same(expected, ls.get_anchors_and_paths(file_list, false, vim.g))
|
2021-04-30 13:07:36 +00:00
|
|
|
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe("get_all_files", function()
|
2022-01-21 16:15:32 +00:00
|
|
|
it("should recurse into directories if recursive argument passed in",
|
2020-10-29 18:00:23 +00:00
|
|
|
function()
|
|
|
|
local files = {
|
|
|
|
{"1910271456 testfile.md", "file"},
|
|
|
|
{"more-notes-here", "directory"},
|
|
|
|
{"2010261208 another-testfile.md", "file"}
|
|
|
|
}
|
|
|
|
local vim_api_mock = {
|
|
|
|
g = {},
|
|
|
|
b = {},
|
|
|
|
loop = mock({
|
|
|
|
fs_scandir = function()
|
|
|
|
if #files == 0 then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
fs_scandir_next = function()
|
|
|
|
if #files == 0 then return nil end
|
2022-01-21 16:15:32 +00:00
|
|
|
local fname, ftype = table.unpack(table.remove(files))
|
2020-10-29 18:00:23 +00:00
|
|
|
return fname, ftype
|
|
|
|
end
|
|
|
|
})
|
|
|
|
}
|
2020-10-29 19:13:02 +00:00
|
|
|
_G.vim = vim_api_mock
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2021-04-30 13:07:36 +00:00
|
|
|
ls.get_all_files("path/to/startingdir", true)
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2020-10-29 18:00:23 +00:00
|
|
|
assert.spy(vim_api_mock.loop.fs_scandir).was_called(2)
|
|
|
|
assert.spy(vim_api_mock.loop.fs_scandir).was_called_with(
|
|
|
|
"path/to/startingdir/more-notes-here")
|
|
|
|
end)
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2021-04-30 14:16:13 +00:00
|
|
|
it("should add all files found in subdirectories when recursing", function()
|
2020-10-29 18:00:23 +00:00
|
|
|
local outer_files = {
|
|
|
|
"subdir", "1234567890 myfile.md", "2345678901 another.md"
|
|
|
|
}
|
|
|
|
local inner_files = {
|
|
|
|
"2222222222 should-be-present.md",
|
|
|
|
"3333333333 should-also-be-present.md"
|
|
|
|
}
|
|
|
|
local files = outer_files
|
|
|
|
-- assert.is_true("not implemented")
|
|
|
|
local vim_api_mock = {
|
|
|
|
g = {},
|
|
|
|
b = {},
|
|
|
|
loop = {
|
|
|
|
fs_scandir = function()
|
|
|
|
if #files == 0 then return false end
|
|
|
|
return true
|
|
|
|
end,
|
|
|
|
fs_scandir_next = function()
|
|
|
|
if #files == 0 then return nil end
|
|
|
|
local fname, ftype = table.remove(files), 'file'
|
|
|
|
if fname == "subdir" then
|
|
|
|
files = inner_files
|
|
|
|
ftype = 'directory'
|
|
|
|
end
|
|
|
|
return fname, ftype
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 19:13:02 +00:00
|
|
|
_G.vim = vim_api_mock
|
2020-10-29 18:00:23 +00:00
|
|
|
local expected = {
|
2021-04-30 13:07:36 +00:00
|
|
|
["mydirectory/1234567890 myfile.md"] = "1234567890 myfile.md",
|
|
|
|
["mydirectory/2345678901 another.md"] = "2345678901 another.md",
|
|
|
|
["mydirectory/subdir/2222222222 should-be-present.md"] = "2222222222 should-be-present.md",
|
|
|
|
["mydirectory/subdir/3333333333 should-also-be-present.md"] = "3333333333 should-also-be-present.md"
|
2020-10-29 18:00:23 +00:00
|
|
|
}
|
2021-04-30 13:07:36 +00:00
|
|
|
assert.same(expected, ls.get_all_files('mydirectory', true))
|
2020-10-29 18:00:23 +00:00
|
|
|
end)
|
2020-10-30 12:48:17 +00:00
|
|
|
end)
|
|
|
|
|
2021-04-30 13:52:54 +00:00
|
|
|
describe("get_zettel_by_anchor", function()
|
2020-10-30 12:48:17 +00:00
|
|
|
it("should return the correct zettel by id", function()
|
|
|
|
local file_list = {
|
2021-04-30 14:16:13 +00:00
|
|
|
["aDir/1910291645 myfile.md"] = "1910291645 myfile.md",
|
|
|
|
["dir/2345678901 another.md"] = "2345678901 another.md"
|
2020-10-30 12:48:17 +00:00
|
|
|
}
|
|
|
|
_G.vim = simple_api_mock(file_list)
|
2020-10-29 17:17:00 +00:00
|
|
|
|
2021-04-30 14:16:13 +00:00
|
|
|
assert.same("aDir/1910291645 myfile.md",
|
2021-04-30 13:52:54 +00:00
|
|
|
ls.get_zettel_by_anchor("1910291645", file_list))
|
2020-10-30 12:48:17 +00:00
|
|
|
end)
|
2020-11-06 15:35:36 +00:00
|
|
|
it("should return nil and not break on no all list passed in", function()
|
|
|
|
stub(ls, "get_anchors_and_paths")
|
2021-04-30 14:16:13 +00:00
|
|
|
assert.is_not_error(function()
|
|
|
|
ls.get_zettel_by_anchor("myanchor")
|
|
|
|
end)
|
2020-11-06 15:35:36 +00:00
|
|
|
end)
|
2020-10-30 15:04:23 +00:00
|
|
|
it("should default to the zettel root dir if no list passed in", function()
|
2021-04-30 13:07:36 +00:00
|
|
|
local fc = stub(ls, "get_all_files")
|
2020-11-06 15:35:36 +00:00
|
|
|
local expected = require'zettelkasten.options'.zettel().rootdir
|
2020-10-30 15:04:23 +00:00
|
|
|
|
2021-04-30 13:52:54 +00:00
|
|
|
ls.get_zettel_by_anchor(expected)
|
2020-11-06 15:35:36 +00:00
|
|
|
assert.stub(fc).was_called_with(expected, true)
|
2020-10-30 15:04:23 +00:00
|
|
|
end)
|
2021-04-30 13:52:54 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe("get_zettel_by_ref", function()
|
2021-04-30 14:02:13 +00:00
|
|
|
|
|
|
|
it("should return a full file path for file path linked", function()
|
|
|
|
local file_list = {
|
|
|
|
["link/to/my/file.md"] = "file.md",
|
2021-04-30 14:16:13 +00:00
|
|
|
["link/to/my/target-file.md"] = "target-file.md"
|
2021-04-30 14:02:13 +00:00
|
|
|
}
|
2021-04-30 14:16:13 +00:00
|
|
|
assert.same("link/to/my/target-file.md", ls.get_zettel_by_ref(
|
|
|
|
"link/to/my/target-file.md", file_list))
|
2021-04-30 14:02:13 +00:00
|
|
|
end)
|
|
|
|
|
2021-04-30 14:16:13 +00:00
|
|
|
it("should return path to matching base name if only that is linked",
|
|
|
|
function()
|
2021-04-30 13:52:54 +00:00
|
|
|
local file_list = {
|
|
|
|
["link/to/my/file.md"] = "file.md",
|
2021-04-30 14:16:13 +00:00
|
|
|
["link/to/my/target-file.md"] = "target-file.md"
|
2021-04-30 13:52:54 +00:00
|
|
|
}
|
2021-04-30 14:16:13 +00:00
|
|
|
assert.same("link/to/my/target-file.md",
|
|
|
|
ls.get_zettel_by_ref("target-file.md", file_list))
|
2021-04-30 14:02:13 +00:00
|
|
|
end)
|
2021-04-30 13:26:32 +00:00
|
|
|
|
2021-04-30 14:02:13 +00:00
|
|
|
it("should not return anything if no match exists", function()
|
|
|
|
local file_list = {
|
|
|
|
["link/to/my/file.md"] = "file.md",
|
2021-04-30 14:16:13 +00:00
|
|
|
["link/to/my/no-target-file.md"] = "no-target-file.md"
|
2021-04-30 14:02:13 +00:00
|
|
|
}
|
|
|
|
assert.same(nil, ls.get_zettel_by_ref("target-file.md", file_list))
|
2021-04-30 13:52:54 +00:00
|
|
|
end)
|
2021-04-30 14:02:13 +00:00
|
|
|
|
2020-10-30 15:04:23 +00:00
|
|
|
end)
|