109 lines
2.7 KiB
Lua
109 lines
2.7 KiB
Lua
function get_directory_iterator_from_table(table)
|
|
return function(directory)
|
|
local index = 0
|
|
local filenames = table
|
|
|
|
return function()
|
|
index = index + 1
|
|
return filenames[index]
|
|
end
|
|
end
|
|
end
|
|
|
|
describe("ls", function()
|
|
local lfs = require "lfs"
|
|
local prevfile = require "prevfile"
|
|
|
|
insulate("if directory does not exist", function()
|
|
lfs.dir = function(directory) error("No such file or directory") end
|
|
|
|
it("should return nil", function()
|
|
local files = prevfile.ls("/testdir")
|
|
|
|
assert.equals(nil, files)
|
|
end)
|
|
end)
|
|
|
|
insulate("if directory is empty", function()
|
|
lfs.dir = get_directory_iterator_from_table({})
|
|
|
|
it("should return empty table", function()
|
|
local files = prevfile.ls("/testdir")
|
|
|
|
assert.are.same({}, files)
|
|
end)
|
|
|
|
it("should call lfs.dir with target directory", function()
|
|
spy.on(lfs, "dir")
|
|
local test_directory = "/testdir"
|
|
|
|
prevfile.ls(test_directory)
|
|
|
|
assert.spy(lfs.dir).was_called_with(test_directory)
|
|
end)
|
|
end)
|
|
|
|
insulate("if directory contains files", function()
|
|
lfs.dir = get_directory_iterator_from_table({"a.txt", "b.txt", "z.txt", "c.txt"})
|
|
|
|
it("should return the files in lexographical order", function()
|
|
local files = prevfile.ls("/testdir")
|
|
|
|
assert.are.same({"a.txt", "b.txt", "c.txt", "z.txt"}, files)
|
|
end)
|
|
end)
|
|
|
|
insulate("if directory contains numerically-named files", function()
|
|
lfs.dir = get_directory_iterator_from_table({"/testdir/709.txt", "/testdir/71.txt", "/testdir/710.txt"})
|
|
|
|
it("should return the files in numerical order", function()
|
|
local files = prevfile.ls("/testdir")
|
|
|
|
assert.are.same({"/testdir/71.txt", "/testdir/709.txt", "/testdir/710.txt"}, files)
|
|
end)
|
|
end)
|
|
|
|
insulate("if directory contains hidden files", function()
|
|
lfs.dir = get_directory_iterator_from_table({"a.txt", ".b.txt.swp", "c.txt"})
|
|
|
|
it("should exclude them from the listing", function()
|
|
local files = prevfile.ls("/testdir")
|
|
|
|
assert.are.same({"a.txt", "c.txt"}, files)
|
|
end)
|
|
end)
|
|
end)
|
|
|
|
describe("prevfile", function()
|
|
local prevfile = require "prevfile"
|
|
|
|
insulate("if ls returns nil", function()
|
|
prevfile.ls = function() return nil end
|
|
|
|
it("should return nil", function()
|
|
local file = prevfile.prevfile()
|
|
|
|
assert.are.same(nil, file)
|
|
end)
|
|
end)
|
|
|
|
insulate("if ls returns files", function()
|
|
prevfile.ls = function() return {"a.txt", "b.txt", "c.txt", "z.txt"} end
|
|
|
|
it("should call prevfile.ls to get files", function()
|
|
spy.on(prevfile, "ls")
|
|
test_directory = "/testdir"
|
|
|
|
prevfile.prevfile(test_directory)
|
|
|
|
assert.spy(prevfile.ls).was_called_with(test_directory)
|
|
end)
|
|
|
|
it("should return the last file in the list", function()
|
|
local file = prevfile.prevfile("/testdir")
|
|
|
|
assert.are.same("z.txt", file)
|
|
end)
|
|
end)
|
|
end)
|