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.stub(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)