From b8147d8e6bff84de505f331c81191744d316c927 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Wed, 6 Sep 2023 16:52:54 +0200 Subject: [PATCH] chore(*) update linter, add makefile --- .luacheckrc | 30 +++--- Makefile | 91 +++++++++++++++++++ date-dev-1.rockspec | 37 ++++++++ rockspecs/date-2.1.3-1.rockspec | 31 +++++++ .../date-2.2-2.rockspec | 0 samples/mkcalendar.lua | 50 +++++----- samples/mkisocal.lua | 22 ++--- spec/date_spec.lua | 8 +- 8 files changed, 212 insertions(+), 57 deletions(-) create mode 100644 Makefile create mode 100644 date-dev-1.rockspec create mode 100644 rockspecs/date-2.1.3-1.rockspec rename date-2.2-2.rockspec => rockspecs/date-2.2-2.rockspec (100%) diff --git a/.luacheckrc b/.luacheckrc index 6a352c8..0513ff5 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,35 +1,31 @@ ---std = "ngx_lua+busted" unused_args = false redefined = false max_line_length = false - globals = { - --"_KONG", - --"kong", - --"ngx.IS_CLI", +-- "ngx", } - not_globals = { + -- deprecated Lua 5.0 functions "string.len", "table.getn", } - -ignore = { - --"6.", -- ignore whitespace warnings +include_files = { + "**/*.lua", + "**/*.rockspec", + ".busted", + ".luacheckrc", } -include_files = { - "**/*.lua", - "*.rockspec", - ".busted", - ".luacheckrc", +files["spec/**/*.lua"] = { + std = "+busted", } exclude_files = { - "here/**", - "samples/**", + -- The Github Actions Lua Environment + ".lua", + ".luarocks", + ".install", } - diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8682cfc --- /dev/null +++ b/Makefile @@ -0,0 +1,91 @@ +# additional Busted options to pass +BUSTED:= + +# SCM rockspec label; scm/cvs/dev +SCM_LABEL:=$(shell cat *.rockspec | grep "local package_version" | sed "s/ //g" | sed "s/localpackage_version=//g" | sed "s/\"//g") +ROCK_REV:=$(shell cat *.rockspec | grep "local rockspec_revision" | sed "s/ //g" | sed "s/localrockspec_revision=//g" | sed "s/\"//g") +ROCK_NAME:=$(shell cat *.rockspec | grep "local package_name" | sed "s/ //g" | sed "s/localpackage_name=//g" | sed "s/\"//g") +ROCKSPEC:=${ROCK_NAME}-${SCM_LABEL}-${ROCK_REV}.rockspec +TAB=$(shell printf "\t") + +# dev/test dependencies; versions can be pinned. Example: "ldoc 1.4.6" +DEV_ROCKS = "busted" "luacheck" "luacov" + + +target_not_specified: help + @exit 1 + + +help: + @echo "Available make targets for ${ROCK_NAME}:" + @echo "" + @echo "install: uses LuaRocks to install ${ROCK_NAME}" + @echo "uninstall: uninstalls ALL versions of ${ROCK_NAME} (using LuaRocks with" + @echo " the '--force' flag)" + @echo "clean: removes LuaCov output and packed rocks" + @echo "test: runs the test suite using Busted" + @echo "testinst: installs ${ROCK_NAME} and runs tests using the installed version" + @echo " (this modifies the local installation, but also tests the" + @echo " .rockspec file). This is best used when testing in CI." + @echo "lint: will validate all 'rockspec' files using LuaRocks, and the" + @echo " '.lua' files with LuaCheck" + @echo "dev: installs the development dependencies (Busted, LuaCheck, etc.)" + @echo "help: displays this list of make targets" + @echo "" + + +install: luarocks + luarocks make + + +uninstall: luarocks + if (luarocks list --porcelain ${ROCK_NAME} | grep "^${ROCK_NAME}${TAB}" | grep -q "installed") ; then \ + luarocks remove ${ROCK_NAME} --force; \ + fi; + + +# note: restore the docs to the last committed version +clean: clean_luacov clean_luarocks + + +.PHONY: test +test: clean_luacov dev + busted ${BUSTED} + + +# test while having the code installed; also tests the rockspec, but +# this will modify the local luarocks installation/tree!! +.PHONY: testinst +testinst: clean_luacov dev uninstall install + busted --lpath="" --cpath="" ${BUSTED} + + +.PHONY: lint +lint: dev + @echo "luarocks lint ..." + @for spec in $(shell find . -type f -name "*.rockspec") ; do \ + (luarocks lint $$spec && echo "$$spec [OK]") || (echo "$$spec [NOK]"; exit 1); \ + done + luacheck . + + +.PHONY: dev +dev: luarocks + @for rock in $(DEV_ROCKS) ; do \ + (luarocks list --porcelain $$rock | grep -q "installed") || (luarocks install $$rock || exit 1); \ + done; + + +.PHONY: clean_luarocks +clean_luarocks: + $(RM) *.rock + + +.PHONY: clean_luacov +clean_luacov: + $(RM) luacov.report.out luacov.stats.out + + +.PHONY: luarocks +luarocks: + @which luarocks > /dev/null || (echo "LuaRocks was not found. Please install and/or make available in the path." && exit 1) diff --git a/date-dev-1.rockspec b/date-dev-1.rockspec new file mode 100644 index 0000000..ffa7313 --- /dev/null +++ b/date-dev-1.rockspec @@ -0,0 +1,37 @@ +local package_name = "date" +local package_version = "dev" +local rockspec_revision = "1" +local github_account_name = "Tieske" +local github_repo_name = package_name +local git_checkout = package_version == "dev" and "master" or ("version_"..package_version) + +package = package_name +version = package_version .. "-" .. rockspec_revision + +source = { + url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git", + branch = git_checkout +} + +description = { + summary = "Date & Time module for Lua 5.x", + detailed = [[ + Pure Lua Date & Time module for Lua 5.x featuring date and time string + parsing, time addition & subtraction, time span calculation, support for + ISO 8601 dates, local time support, strftime-like formatting. + ]], + license = "MIT", + homepage = "https://github.com/"..github_account_name.."/"..github_repo_name, +} + +dependencies = { + "lua >= 5.0, < 5.5" +} + +build = { + type = "builtin", + modules = { + date = "src/date.lua" + }, + copy_directories = { "docs" }, +} diff --git a/rockspecs/date-2.1.3-1.rockspec b/rockspecs/date-2.1.3-1.rockspec new file mode 100644 index 0000000..484b8f3 --- /dev/null +++ b/rockspecs/date-2.1.3-1.rockspec @@ -0,0 +1,31 @@ +package = "date" +version = "2.1.3-1" + +description = { + summary = "Date & Time module for Lua 5.x", + detailed = [[ + Pure Lua Date & Time module for Lua 5.x featuring date and time string + parsing, time addition & subtraction, time span calculation, support for + ISO 8601 dates, local time support, strftime-like formatting. + ]], + license = "MIT", + homepage = "https://github.com/Tieske/date", +} + +dependencies = { + "lua >= 5.0, < 5.5" +} + +source = { + url = "git://github.com/Tieske/date/", + tag = "version_2.1.3", +} + +build = { + type = "builtin", + modules = { + date = "src/date.lua" + }, + copy_directories = { "docs" }, +} + diff --git a/date-2.2-2.rockspec b/rockspecs/date-2.2-2.rockspec similarity index 100% rename from date-2.2-2.rockspec rename to rockspecs/date-2.2-2.rockspec diff --git a/samples/mkcalendar.lua b/samples/mkcalendar.lua index eeae938..033d8f6 100644 --- a/samples/mkcalendar.lua +++ b/samples/mkcalendar.lua @@ -8,21 +8,21 @@ arg: local date = require"date" -function makemonth(y,m) - local t = {} - local d = date(y,m,1) - t.name = d:fmt("%B") - t.year = y - -- get back to the nearest sunday - d:adddays(-(d:getweekday()-1)) - repeat - local tt = {} - table.insert(t,tt) - repeat -- insert the week days - table.insert(tt, d:getday()) - until d:adddays(1):getweekday() == 1 - until d:getmonth() ~= m - return t +local function makemonth(y,m) + local t = {} + local d = date(y,m,1) + t.name = d:fmt("%B") + t.year = y + -- get back to the nearest sunday + d:adddays(-(d:getweekday()-1)) + repeat + local tt = {} + table.insert(t,tt) + repeat -- insert the week days + table.insert(tt, d:getday()) + until d:adddays(1):getweekday() == 1 + until d:getmonth() ~= m + return t end local htm_foot = '\n' @@ -39,16 +39,16 @@ local htm_yearhead = '\n' local htm_monhead = '\n' local htm_monweek = '\n' local htm_yearfoot = '\n
%s, %s
sunmontuewedthufrisat
%s%s%s%s%s%s%s
' -function makecalendar(y, iox) - iox:write(htm_yearhead) - for i = 1, 12 do - local tm = makemonth(y, i) - iox:write(string.format(htm_monhead, tm.name, tm.year)) - for k, v in ipairs(tm) do - iox:write(string.format(htm_monweek, v[1], v[2], v[3], v[4], v[5], v[6], v[7])) - end - end - iox:write(htm_yearfoot) +local function makecalendar(y, iox) + iox:write(htm_yearhead) + for i = 1, 12 do + local tm = makemonth(y, i) + iox:write(string.format(htm_monhead, tm.name, tm.year)) + for k, v in ipairs(tm) do + iox:write(string.format(htm_monweek, v[1], v[2], v[3], v[4], v[5], v[6], v[7])) + end + end + iox:write(htm_yearfoot) end diff --git a/samples/mkisocal.lua b/samples/mkisocal.lua index 80604d2..16319f8 100644 --- a/samples/mkisocal.lua +++ b/samples/mkisocal.lua @@ -12,17 +12,17 @@ local htm_foot = [[]] local htm_head = [[]] local htm_yearhead = [[]] local htm_yearfoot = [[
YearWeekMonTueWedThuFriSatSun
]] -function makecalendar(year, iow) - local d = date():setisoyear(year,1,1) - iow(htm_yearhead) - iow("\n") - while d:getisoyear() == year do - iow(d:fmt("%G%V
%Y-%j")) - repeat iow(d:fmt("%u
%b %d %Y")) - until d:adddays(1):getisoweekday() == 1 - iow("\n") - end - iow(htm_yearfoot) +local function makecalendar(year, iow) + local d = date():setisoyear(year,1,1) + iow(htm_yearhead) + iow("\n") + while d:getisoyear() == year do + iow(d:fmt("%G%V
%Y-%j")) + repeat iow(d:fmt("%u
%b %d %Y")) + until d:adddays(1):getisoweekday() == 1 + iow("\n") + end + iow(htm_yearfoot) end diff --git a/spec/date_spec.lua b/spec/date_spec.lua index 33c658f..acf7682 100644 --- a/spec/date_spec.lua +++ b/spec/date_spec.lua @@ -120,8 +120,8 @@ describe("Testing the 'date' module", function() local c = date("20131230 00:57:04") -- same as a assert(a < b) assert(a <= b) - assert(not (a > b)) - assert(not (a >= b)) + assert(not (a > b)) -- luacheck: ignore + assert(not (a >= b)) -- luacheck: ignore assert(a == c) assert(a <= c) assert(a >= c) @@ -140,13 +140,13 @@ describe("Testing the 'date' module", function() assert(a > b and b < a) assert(a >= b and b <= a) - assert(a ~= b and (not(a == b))) + assert(a ~= b and (not(a == b))) -- luacheck: ignore a = b:copy() assert(not (a > b and b < a)) assert(a >= b and b <= a) - assert(a == b and (not(a ~= b))) + assert(a == b and (not(a ~= b))) -- luacheck: ignore assert((a .. 565369) == (b .. 565369)) assert((a .. "????") == (b .. "????"))