Compare commits
No commits in common. "08ae51fad7d744319975630e30ebedcdb0d44e75" and "42c23c94d3f37509e645178737f6fe4accfc9316" have entirely different histories.
08ae51fad7
...
42c23c94d3
|
@ -0,0 +1,10 @@
|
|||
return {
|
||||
_all = {
|
||||
shuffle = true,
|
||||
verbose = true,
|
||||
coverage = true,
|
||||
},
|
||||
default = {
|
||||
ROOT = {"tests/"},
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
luacov.stats.out
|
10
LICENSE
10
LICENSE
|
@ -1,10 +0,0 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
||||
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <http://unlicense.org/>
|
|
@ -1,4 +0,0 @@
|
|||
# nextfile
|
||||
|
||||
nextfile is a small Lua library for generating the next file in a sequence of files.
|
||||
It was developed for use with tools that export sections of a video as images, and need to be able to name files sequentially without having generated all of the previous files themselves.
|
|
@ -0,0 +1,30 @@
|
|||
package = "nextfile"
|
||||
version = "0.0.1"
|
||||
|
||||
source = {
|
||||
url = "https://git.themagician.cc/TheMagician/nextfile",
|
||||
tag = "v0.0.1"
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "A library for getting the next file in a sequence.",
|
||||
detailed = [[
|
||||
nextfile is a small Lua library for generating the next file in a sequence of files.
|
||||
It was developed for use with tools that export sections of a video as images, and need to be able to name files sequentially without having generated all of the previous files themselves.
|
||||
]]
|
||||
homepage = "https://themagician.cc/TheMagician/nextfile",
|
||||
licence = "The Unlicence"
|
||||
}
|
||||
|
||||
dependencies = {
|
||||
"lua >=5.1, <= 5.4",
|
||||
"luafilesystem >= scm-1"
|
||||
}
|
||||
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
prevfile = "src/prevfile.lua",
|
||||
nextfile = "src/nextfile.lua"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
local prevfile = require "prevfile"
|
||||
|
||||
local nextfile = {}
|
||||
|
||||
function nextfile.splitfilename(path)
|
||||
dir, name, ext = path:match("^(.*/)(.*)(%.%w*)")
|
||||
return dir, name, ext
|
||||
end
|
||||
|
||||
function nextfile.nextletter(letter)
|
||||
if letter == "z" then return "za" end
|
||||
|
||||
local ascii = letter:byte()
|
||||
local nextascii = ascii + 1
|
||||
return string.char(nextascii)
|
||||
end
|
||||
|
||||
function nextfile.nextfile(directory, extension)
|
||||
local previous = prevfile.prevfile(directory)
|
||||
if previous == nil then return nil end
|
||||
|
||||
local dir, name, ext = nextfile.splitfilename(previous)
|
||||
|
||||
value = dir
|
||||
if tonumber(name) == nil then
|
||||
local lastletter = name:sub(#name)
|
||||
local nextletter = nextfile.nextletter(lastletter)
|
||||
value = value .. name:gsub(lastletter .. "$", nextletter)
|
||||
else
|
||||
value = value .. name + 1
|
||||
end
|
||||
|
||||
if extension ~= nil then
|
||||
value = value .. "." .. extension
|
||||
else
|
||||
value = value .. ext
|
||||
end
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
return nextfile
|
|
@ -0,0 +1,36 @@
|
|||
local lfs = require "lfs"
|
||||
|
||||
local prevfile = {}
|
||||
|
||||
local function padnum(d)
|
||||
local dec, n = string.match(d, "(%.?)0*(.+)")
|
||||
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n)
|
||||
end
|
||||
|
||||
local function humansort(a, b)
|
||||
return tostring(a):gsub("%.?%d+", padnum) < tostring(b):gsub("%.?%d+", padnum)
|
||||
end
|
||||
|
||||
function prevfile.ls(directory)
|
||||
if not pcall(lfs.dir, directory) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local files = {}
|
||||
for file in lfs.dir(directory) do
|
||||
if string.sub(file, 1, 1) ~= "." then
|
||||
table.insert(files, file)
|
||||
end
|
||||
end
|
||||
|
||||
table.sort(files, humansort)
|
||||
return files
|
||||
end
|
||||
|
||||
function prevfile.prevfile(directory)
|
||||
local files = prevfile.ls(directory)
|
||||
if files == nil then return nil end
|
||||
return files[#files]
|
||||
end
|
||||
|
||||
return prevfile
|
|
@ -0,0 +1,95 @@
|
|||
describe("splitfilename", function()
|
||||
local nextfile = require "nextfile"
|
||||
|
||||
it("should return the filename and extension of an absolute filepath", function()
|
||||
local dir, name, ext = nextfile.splitfilename("/testdir/701.txt")
|
||||
|
||||
assert.are.equal("/testdir/", dir)
|
||||
assert.are.equal("701", name)
|
||||
assert.are.equal(".txt", ext)
|
||||
end)
|
||||
|
||||
it("should return the filename and extension of a relative filepath", function()
|
||||
local dir, name, ext = nextfile.splitfilename("../../foo/testdir/file.mp3")
|
||||
|
||||
assert.are.equal("../../foo/testdir/", dir)
|
||||
assert.are.equal("file", name)
|
||||
assert.are.equal(".mp3", ext)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("nextletter", function()
|
||||
local nextfile = require "nextfile"
|
||||
|
||||
it("should return the next letter when the current letter is not 'z'", function()
|
||||
local letter = nextfile.nextletter("q")
|
||||
|
||||
assert.are.equal("r", letter)
|
||||
end)
|
||||
|
||||
it("should return 'za' when the current letter is 'z'", function()
|
||||
local letter = nextfile.nextletter("z")
|
||||
|
||||
assert.are.equal("za", letter)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("nextfile", function()
|
||||
local prevfile = require "prevfile"
|
||||
local nextfile = require "nextfile"
|
||||
|
||||
insulate("prevfile.prevfile returns nil", function()
|
||||
prevfile.prevfile = function() return nil end
|
||||
|
||||
it("should call prevfile.prevfile", function()
|
||||
spy.on(prevfile, "prevfile")
|
||||
test_directory = "/testdir"
|
||||
|
||||
nextfile.nextfile(test_directory)
|
||||
|
||||
assert.spy(prevfile.prevfile).was_called_with(test_directory)
|
||||
end)
|
||||
|
||||
it("should return nil", function()
|
||||
local file = nextfile.nextfile("/testdir")
|
||||
|
||||
assert.are.equal(nil, file)
|
||||
end)
|
||||
end)
|
||||
|
||||
insulate("prevfile.prevfile returns a numeric filename", function()
|
||||
prevfile.prevfile = function() return "/testdir/701.txt" end
|
||||
|
||||
it("should return the result of incrementing the filename", function()
|
||||
local file = nextfile.nextfile("/testdir")
|
||||
|
||||
assert.are.equal("/testdir/702.txt", file)
|
||||
end)
|
||||
end)
|
||||
|
||||
insulate("prevfile.prevfile returns a non-numeric filename", function()
|
||||
it("should return the result of replacing the last character with the next letter of the alphabet", function()
|
||||
prevfile.prevfile = function() return "/testdir/421b.txt" end
|
||||
|
||||
local file = nextfile.nextfile("/testdir")
|
||||
|
||||
assert.are.equal("/testdir/421c.txt", file)
|
||||
end)
|
||||
|
||||
it("should return the result of replacing z with za when z is the last letter of the filename", function()
|
||||
prevfile.prevfile = function() return "/testdir/421z.txt" end
|
||||
|
||||
local file = nextfile.nextfile("/testdir")
|
||||
|
||||
assert.are.equal("/testdir/421za.txt", file)
|
||||
end)
|
||||
|
||||
it("should use the provided file extension if one is given", function()
|
||||
prevfile.prevfile = function() return "/testdir/900.txt" end
|
||||
|
||||
local file = nextfile.nextfile("/testdir", "yaml")
|
||||
|
||||
assert.are.equal("/testdir/901.yaml", file)
|
||||
end)
|
||||
end)
|
||||
end)
|
|
@ -0,0 +1,108 @@
|
|||
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)
|
Loading…
Reference in New Issue