diff options
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | srcinfo.lua | 5 | ||||
-rw-r--r-- | srcinfo_spec.lua | 188 |
3 files changed, 194 insertions, 7 deletions
@@ -1,12 +1,12 @@ -LUA_VERSION?=5.4 -PREFIX?=/usr/local +LUA_VERSION=5.4 +PREFIX=/usr/local DATADIR=$(PREFIX)/share LUA_LMOD=$(DATADIR)/lua/$(LUA_VERSION) -all: +all: test test: - busted + busted srcinfo_spec.lua install: install -m644 -Dt $(DESTDIR)$(LUA_LMOD) srcinfo.lua diff --git a/srcinfo.lua b/srcinfo.lua index 40ae7d1..ef40cd9 100644 --- a/srcinfo.lua +++ b/srcinfo.lua @@ -22,7 +22,7 @@ local whitespace = lpeg.S" \t"^0 local newline = lpeg.P"\r\n" + "\n" local rest = (1 - newline)^0 local definition = lpeg.Cc"definition" * whitespace * lpeg.C(identifier) * whitespace * "=" * whitespace * lpeg.C(rest) -local comment = lpeg.Cc"comment" * lpeg.P"#" * whitespace * lpeg.C(rest) +local comment = lpeg.Cc"comment" * whitespace * lpeg.P"#" * lpeg.C(rest) local empty = lpeg.Cc"empty" * whitespace local invalid = lpeg.Cc"invalid" * lpeg.C(rest) local line = lpeg.Carg(1) * (definition + comment + empty + invalid) / apply * newline @@ -32,12 +32,11 @@ local capture = lpeg.Ct(line^0) * -lpeg.P(1) local function srcinfo (text, options) options = options or {} - mode = modes[options.mode] or PKGNAME return capture:match( text, nil, { - mode = mode, + mode = modes[options.mode] or PKGNAME, base = nil, current = nil, definition = function (self, name, value) diff --git a/srcinfo_spec.lua b/srcinfo_spec.lua new file mode 100644 index 0000000..acc32df --- /dev/null +++ b/srcinfo_spec.lua @@ -0,0 +1,188 @@ +local srcinfo = require "srcinfo" +local stx = require "pl.stringx" + + +describe("Parses empty content in", function() + it("pkgname mode (default)", function() + local pkgs = srcinfo"" + assert.are.same({}, pkgs) + end) + + it("pkgbase mode", function() + local bases = srcinfo("", {mode="pkgbase"}) + assert.are.same({}, bases) + end) +end) + + +describe("Parses name-only .SRCINFO in", function() + describe("pkgname mode (default) with", function() + it("one package", function() + local pkgs = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + ]]) + assert.are.same("example", pkgs[1].name) + assert.are.same("example", pkgs[1].base.name) + assert.is_nil(pkgs[2]) + end) + + it("multiple packages", function() + local pkgs = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + pkgname = example-b + pkgname = example-c + ]]) + assert.are.same("example", pkgs[1].name) + assert.are.same("example-b", pkgs[2].name) + assert.are.same("example-c", pkgs[3].name) + assert.are.same("example", pkgs[1].base.name) + assert.are.equal(pkgs[1].base, pkgs[2].base) + assert.are.equal(pkgs[1].base, pkgs[3].base) + assert.is_nil(pkgs[4]) + end) + end) + + describe("pkgbase mode with", function() + it("one package", function() + local bases = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + ]], {mode="pkgbase"}) + assert.are.same("example", bases[1].name) + assert.are.same("example", bases[1].packages[1].name) + assert.is_nil(bases[2]) + assert.is_nil(bases[1].packages[2]) + end) + + it("multiple packages", function() + local bases = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + pkgname = example-b + pkgname = example-c + ]], {mode="pkgbase"}) + assert.are.same("example", bases[1].name) + assert.are.same("example", bases[1].packages[1].name) + assert.are.same("example-b", bases[1].packages[2].name) + assert.are.same("example-c", bases[1].packages[3].name) + assert.is_nil(bases[2]) + assert.is_nil(bases[1].packages[4]) + end) + end) +end) + + +it("Default mode is pkgname", function() + local default = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + ]]) + local explicit = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + ]], {mode="pkgname"}) + assert.are.same(default, explicit) +end) + + +describe("Indentation does not matter", function() + local conventional = srcinfo(stx.dedent[[ + pkgbase = example + pkgdesc = Example package + pkgver = 1.0.0 + pkgrel = 1 + arch = any + license = BSD-3 + + pkgname = example + + pkgname = example-bonus + depends = lua + ]]) + + it("for fields", function() + assert.are.same(conventional, srcinfo(stx.dedent[[ + pkgbase = example + pkgdesc = Example package + pkgver = 1.0.0 + pkgrel = 1 + arch = any + license = BSD-3 + pkgname = example + pkgname = example-bonus + depends = lua + ]])) + end) + + it("between sections", function() + assert.are.same(conventional, srcinfo(stx.dedent[[ + pkgbase = example + pkgdesc = Example package + pkgver = 1.0.0 + pkgrel = 1 + arch = any + license = BSD-3 + pkgname = example + pkgname = example-bonus + depends = lua + ]])) + end) +end) + + +describe("Parses multiple .SRCINFO in the same context in", function() + it("pkgname mode (default)", function() + local pkgs = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + pkgbase = sample + pkgname = sample + ]]) + assert.are.same("example", pkgs[1].name) + assert.are.same("example", pkgs[1].base.name) + assert.are.same("sample", pkgs[2].name) + assert.are.same("sample", pkgs[2].base.name) + end) + + it("pkgbase mode", function() + local bases = srcinfo(stx.dedent[[ + pkgbase = example + pkgname = example + pkgbase = sample + pkgname = sample + ]], {mode="pkgbase"}) + assert.are.same("example", bases[1].name) + assert.are.same("example", bases[1].packages[1].name) + assert.are.same("sample", bases[2].name) + assert.are.same("sample", bases[2].packages[1].name) + end) +end) + + +describe("Comments", function() + it("are ignored", function() + local pkgs = srcinfo(stx.dedent[[ + pkgbase = example + #arch = any + + pkgname = example + + # pkgname = sample + ]]) + assert.are.same("example", pkgs[1].name) + assert.is_nil(pkgs[1].base.arch) + assert.is_nil(pkgs[2]) + end) + + it("work only if they are the first thing in the line", function() + local pkgs = srcinfo(stx.dedent[[ + pkgbase = example + pkgdesc = I like #hiking + + pkgname = example + ]]) + assert.are.same("I like #hiking", pkgs[1].base.pkgdesc) + end) +end) |