From dc563de9378993f666f27bef715bc425a7711f45 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 10 Nov 2023 10:26:53 -0300 Subject: tests/assert-install.sh: Always to enforce correct installation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also in this change: - now we call ln(1) in the "install" target without using the -r "relative" flag, as it isn't POSIX; - add `+` as a prefix to the "assert-tests" command, so we tell sub-make calls to also run things in parallel. In this case, the "make DESTDIR=... install" calls; - use the implicit `index.js` entrypoint and do away completly with `package.json`; - change from `import` to `require` as Node.js ESM modules don't support `$NODE_PATH`[0]: "NODE_PATH is not part of resolving import specifiers. Please use symlinks if this behavior is desired." 🤦; The parallel sub-make behaviour isn't available in current specification of make (POSIX issue 7) but is included in the upcoming[1] version 8: > If a rule invokes a sub-make either via the MAKE macro or via a > command line that begins with '+', the sub-make is the same > implementation as the make that invoked the sub-make, and the −j > option is passed to the sub-make via the MAKEFLAGS environment > variable with the same maxjobs value and is not overridden by a > maxjobs value from another source (even if it has the same value), the > sub-make shall use the same token pool as its invoking make rather > than create a new token pool. [0]: https://nodejs.org/api/esm.html#no-node_path [1]: https://www.opengroup.org/austin/restricted/202x-d3/202x_d3.pdf --- .gitignore | 1 + Makefile | 23 ++++++++++++++--------- doc/papo.en.3js.in | 0 package.json | 4 ---- src/api.js | 18 ++++++++++++++++-- src/cli.js | 2 +- src/utils.js | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/assert-deps.sh | 10 +++++++++- tests/assert-install.sh | 35 +++++++++++++++++++++++++++++++++++ tests/lib.sh | 33 --------------------------------- tools/lib.sh | 33 +++++++++++++++++++++++++++++++++ tools/manpages.sh | 2 +- 12 files changed, 153 insertions(+), 51 deletions(-) create mode 100644 doc/papo.en.3js.in delete mode 100644 package.json create mode 100644 src/utils.js create mode 100755 tests/assert-install.sh delete mode 100644 tests/lib.sh create mode 100644 tools/lib.sh diff --git a/.gitignore b/.gitignore index 2907948..e1d9328 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /doc/*.[0-9] +/src/index.js diff --git a/Makefile b/Makefile index faa4c51..feff636 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ TRANSLATIONS = PREFIX = /usr BINDIR = $(PREFIX)/bin LIBDIR = $(PREFIX)/lib -JSLIBDIR = $(LIBDIR)/node_modules/$(NAME) +JSLIBDIR = $(LIBDIR)/node/$(NAME) SHAREDIR = $(PREFIX)/share LOCALEDIR = $(SHAREDIR)/locale MANDIR = $(SHAREDIR)/man @@ -40,6 +40,7 @@ manpages.en.in = \ doc/$(NAME).CHANGELOG.en.7.in \ doc/$(NAME).TODOs.en.7.in \ doc/$(NAME).en.1.in \ + doc/$(NAME).en.3js.in \ doc/$(NAME).tutorial.en.7.in \ doc/$(NAME).recipes.en.7.in \ doc/$(NAME).why.en.7.in @@ -61,7 +62,8 @@ sources = \ derived-assets = \ - $(manpages) \ + $(manpages) \ + src/index.js \ @@ -72,6 +74,9 @@ all: $(derived-assets) $(manpages): Makefile +src/index.js: + ln -fs api.js $@ + .SUFFIXES: .js .js-t @@ -83,10 +88,11 @@ check-t: $(tests.js-t) assert-tests = \ - tests/assert-deps.sh \ + tests/assert-deps.sh \ + tests/assert-install.sh \ $(assert-tests): ALWAYS - sh $@ + +sh $@ check-asserts: $(assert-tests) @@ -108,10 +114,9 @@ clean: install: all mkdir -p \ '$(DESTDIR)$(BINDIR)' \ - '$(DESTDIR)$(JSLIBDIR)'/src - cp package.json '$(DESTDIR)$(JSLIBDIR)' - cp src/*.js '$(DESTDIR)$(JSLIBDIR)'/src - ln -frs '$(DESTDIR)$(JSLIBDIR)'/src/cli.js '$(DESTDIR)$(BINDIR)'/$(NAME) + '$(DESTDIR)$(JSLIBDIR)' + cp src/*.js '$(DESTDIR)$(JSLIBDIR)' + ln -fs '$(DESTDIR)$(JSLIBDIR)'/cli.js '$(DESTDIR)$(BINDIR)'/$(NAME) sh tools/manpages.sh -ip '$(DESTDIR)$(MANDIR)' $(manpages) ## Uninstalls from $(DESTDIR)$(PREFIX). This is a perfect mirror @@ -130,7 +135,7 @@ run-ircd: run-web: $(JSIMPL) src/server/web.js server -l http://localhost:3003 -## Run the web and IRC server locally +## Run the web and IRC server locally. run: $(MAKE) run-ircd & $(MAKE) run-web & wait diff --git a/doc/papo.en.3js.in b/doc/papo.en.3js.in new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json deleted file mode 100644 index 583bc1b..0000000 --- a/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "main": "src/api.js", - "type": "module" -} diff --git a/src/api.js b/src/api.js index 1ce7bcc..99ba01c 100644 --- a/src/api.js +++ b/src/api.js @@ -1,3 +1,17 @@ -export const main = async () => { - console.log({ argv: process.argv }); +const { eq } = require("./utils.js"); + +const main = async () => { + if (process.argv.length !== 2) { + console.log("papo 1970-01-01 0.1.0"); + return; + } + + console.log({ + argv: process.argv, + eq: eq({a: 1}, {a: 1}), + }); +}; + +module.exports = { + main, }; diff --git a/src/cli.js b/src/cli.js index aa602f7..2b486fe 100755 --- a/src/cli.js +++ b/src/cli.js @@ -1,4 +1,4 @@ #!/usr/bin/env node -import { main } from "papo"; +const { main } = require("papo"); (async () => await main())(); diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..1877a58 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,43 @@ +const eq = (a, b) => { + if (a === b) { + return true; + } + + if (a === null || b === null) { + return false; + } + + if (typeof a != "object" || typeof b != "object") { + return false; + } + + if (Array.isArray(a) !== Array.isArray(b)) { + return false; + } + + if (Object.keys(a).length !== Object.keys(b).length) { + return false; + } + + for (const k in a) { + if (!b.hasOwnProperty(k)) { + return false; + } + if (!eq(a[k], b[k])) { + return false; + } + } + return true; +}; + +const keys = (ks, obj) => + ks.reduce( + (ret, k) => + obj.hasOwnProperty(k) ? {...ret, [k]: obj[k]} : ret, + {}, + ); + +module.exports = { + eq, + keys, +}; diff --git a/tests/assert-deps.sh b/tests/assert-deps.sh index b125a7c..e358abd 100755 --- a/tests/assert-deps.sh +++ b/tests/assert-deps.sh @@ -1,9 +1,17 @@ #!/bin/sh set -eu +if [ ! -e .git ]; then + echo "Not in a Git repository, skipping \"$0\"." >&2 + exit +fi + +. tools/lib.sh + F="$(mkstemp)" trap 'rm -f "$F"' EXIT + awk ' $0 == "sources.js = \\" { sources = 1; next } $0 == "tests.js = \\" { tests = 1; next } @@ -28,6 +36,6 @@ awk ' } ' Makefile | LANG=POSIX.UTF-8 sort > "$F" -find src/*.js tests/js/*.js | +git ls-files src/*.js tests/js/*.js | LANG=POSIX.UTF-8 sort | diff -U10 "$F" - diff --git a/tests/assert-install.sh b/tests/assert-install.sh new file mode 100755 index 0000000..f2696bc --- /dev/null +++ b/tests/assert-install.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -eu + +. tools/lib.sh + +D="$(mkdtemp)" +trap 'rm -rf "$D"' EXIT + + +make -s DESTDIR="$D" install +PATH="$D/usr/bin:$PATH" +NODE_PATH="$D/usr/lib/node:$NODE_PATH" + +{ + printf 'Assert that the papo(3js) library is installed correctly...' + node -e 'require("papo");' + printf ' OK.\n' + + printf 'Assert that the papo(1) command is installed correctly...' + papo -V | grep -q '^papo ' + printf ' OK.\n' + + make -s DESTDIR="$D" uninstall + printf 'Assert that the papo(1) command is installed correctly...' + if [ "$(find "$D" -not -type d | wc -l)" != 0 ]; then + printf ' ERR.\n' + cat <<-EOF + Leftover files not removed by "make uninstall": + + $(find "$D" -not -type d) + EOF + exit 1 + fi + printf ' OK.\n' +} >&2 diff --git a/tests/lib.sh b/tests/lib.sh deleted file mode 100644 index 9d183f9..0000000 --- a/tests/lib.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/sh - -assert_arg() { - if [ -z "$1" ]; then - printf 'Missing %s.\n\n' "$2" >&2 - cat <<-'EOF' - usage >&2 - exit 2 - EOF - fi -} - -uuid() { - od -xN20 /dev/urandom | - head -n1 | - awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' -} - -tmpname() { - echo "${TMPDIR:-/tmp}/uuid-tmpname with spaces.$(uuid)" -} - -mkstemp() { - name="$(tmpname)" - touch "$name" - echo "$name" -} - -mkdtemp() { - name="$(tmpname)" - mkdir "$name" - echo "$name" -} diff --git a/tools/lib.sh b/tools/lib.sh new file mode 100644 index 0000000..9d183f9 --- /dev/null +++ b/tools/lib.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s.\n\n' "$2" >&2 + cat <<-'EOF' + usage >&2 + exit 2 + EOF + fi +} + +uuid() { + od -xN20 /dev/urandom | + head -n1 | + awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' +} + +tmpname() { + echo "${TMPDIR:-/tmp}/uuid-tmpname with spaces.$(uuid)" +} + +mkstemp() { + name="$(tmpname)" + touch "$name" + echo "$name" +} + +mkdtemp() { + name="$(tmpname)" + mkdir "$name" + echo "$name" +} diff --git a/tools/manpages.sh b/tools/manpages.sh index 959b1c2..755ff77 100755 --- a/tools/manpages.sh +++ b/tools/manpages.sh @@ -1,7 +1,7 @@ #!/bin/sh set -eu -. tests/lib.sh +. tools/lib.sh usage() { -- cgit v1.2.3