diff options
author | EuAndreh <eu@euandre.org> | 2024-03-06 11:57:09 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-06 11:57:09 -0300 |
commit | 331738a0f69e00d12d44f27a717b3b73733b485a (patch) | |
tree | e8bc0208326f8ad469bd8cb3ef4fcd395a326d37 | |
parent | tests/runner.mjs: Add missing await in t.test("", () => {}) (diff) | |
download | papod-331738a0f69e00d12d44f27a717b3b73733b485a.tar.gz papod-331738a0f69e00d12d44f27a717b3b73733b485a.tar.xz |
src/hero.mjs: Add rmIf() and mkfifo()
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 19 | ||||
-rw-r--r-- | src/hero.mjs | 24 | ||||
-rw-r--r-- | src/web.mjs | 6 | ||||
-rw-r--r-- | tests/js/hero.mjs | 85 | ||||
-rw-r--r-- | tests/lighttpd.conf | 4 |
6 files changed, 111 insertions, 28 deletions
@@ -1,2 +1,3 @@ /doc/*.[0-9] /doc/*.3js +/tests/hero-0.pipe @@ -55,12 +55,12 @@ derived-assets = \ $(manpages) \ side-assets = \ - tests/hero-0.sock \ - tests/hero-1.sock \ - lighttpd.sock \ - ircd.sock \ - web.sock \ + tests/hero-*.pipe \ + tests/hero-*.socket \ web.pipe \ + lighttpd.socket \ + ircd.socket \ + web.socket \ @@ -71,11 +71,6 @@ all: $(derived-assets) $(manpages): Makefile deps.mk -rm-sock-files: - rm -f tests/hero-0.sock tests/hero-1.sock - -tests/js/hero.mjs-check: rm-sock-files - .SUFFIXES: .mjs .mjs-check @@ -138,10 +133,10 @@ uninstall: run-ircd: all - ./src/bin.mjs ircd ircd.sock + ./src/bin.mjs ircd ircd.socket run-web: all - ./src/bin.mjs web web.sock + ./src/bin.mjs web web.socket web.pipe run-proxy: all lighttpd -Df tests/lighttpd.conf diff --git a/src/hero.mjs b/src/hero.mjs index 53f3086..6b6fa77 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -1,7 +1,10 @@ -import assert from "node:assert/strict"; -import crypto from "node:crypto"; -import http from "node:http"; -import process from "node:process"; +import assert from "node:assert/strict"; +import child_process from "node:child_process"; +import crypto from "node:crypto"; +import fs from "node:fs"; +import http from "node:http"; +import process from "node:process"; +import util from "node:util"; import * as u from "./utils.mjs"; @@ -79,13 +82,13 @@ export const firstParamMatch = (tree, segments, params) => { const subtree = tree[seg]; if (subtree) { const submatch = firstParamMatch(subtree, nextSegments, params); - // propagation of the end of recursion + /// propagation of the end of recursion if (submatch) { return submatch; } } - // literal matching failed, we now look for patterns that might match + /// literal matching failed, we now look for patterns that might match const paramOptions = Object.keys(tree) .filter(s => s.startsWith(":")) .sort(u.strSortFn); @@ -326,6 +329,15 @@ export const lineHandlerFn = ({ export const lineHandler = lineHandlerFn(); +export const rmIf = path => { + if (fs.existsSync(path)) { + fs.unlinkSync(path); + } +}; + +export const mkfifo = path => + child_process.execFileSync("mkfifo", [path]); + export const buildRoutes = (routes, globalInterceptors = []) => routes.reduce( (acc, [methods, path, handlerFn, interceptors = []]) => diff --git a/src/web.mjs b/src/web.mjs index 3ca2544..23ae5ff 100644 --- a/src/web.mjs +++ b/src/web.mjs @@ -12,7 +12,7 @@ const server = hero.buildServer("papo", [ [ "GET", "/api/socket", getSocket ], ]); -export const app = async path => { - await server.listen(path); - hero.log.info({ type: "starting-server", path }); +export const app = async (...args) => { + await server.listen(...args); + hero.log.info({ type: "starting-server", args }); }; diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index 9c7aa6d..ebb2c27 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -1,6 +1,8 @@ -import assert from "node:assert/strict"; -import http from "node:http"; -import procees from "node:process"; +import assert from "node:assert/strict"; +import child_process from "node:child_process"; +import fs from "node:fs"; +import http from "node:http"; +import procees from "node:process"; import * as runner from "../runner.mjs"; import * as u from "../../src/utils.mjs"; @@ -28,6 +30,8 @@ import { wrapHandler, actionsFn, lineHandlerFn, + rmIf, + mkfifo, buildRoutes, promisifyServer, buildServer, @@ -759,6 +763,9 @@ const test_makeLogger = async t => { const writerFn = x => contents.push(x); const log = makeLogger(writerFn); + const previous = process.env.DEBUG; + delete process.env.DEBUG; + log.debug({ x: "ignored" }); process.env.DEBUG = "1"; log.debug({ x: "seen" }); @@ -771,6 +778,8 @@ const test_makeLogger = async t => { level: "DEBUG", x: "seen", }]); + + process.env.DEBUG = previous; }); }; @@ -1118,7 +1127,9 @@ const test_actionsFn = async t => { const logger = { info: x => contents.push(x) }; const actions = actionsFn({logger}); - assert.equal(process.env.DEBUG, undefined); + const previous = process.env.DEBUG; + delete process.env.DEBUG; + actions["toggle-debug-env"]("action-text-1"); assert.equal(process.env.DEBUG, "1"); actions["toggle-debug-env"]("action-text-2"); @@ -1140,6 +1151,8 @@ const test_actionsFn = async t => { after: null, }, ]); + + process.env.DEBUG = previous; }); }; @@ -1216,7 +1229,67 @@ const test_lineHandlerFn = async t => { }); }; -const test_buildRoutes = t => { +const test_rmIf = async t => { + t.start("rmIf()"); + + const path = "tests/hero-0.txt"; + + await t.test("rm when exists", async () => { + fs.writeFileSync(path, " ", { flush: true }); + assert.ok(fs.existsSync(path)); + rmIf(path); + assert.ok(!fs.existsSync(path)); + }); + + await t.test("noop otherwise", async () => { + assert.ok(!fs.existsSync(path)); + rmIf(path); + assert.ok(!fs.existsSync(path)); + }); +}; + +const test_mkfifo = async t => { + t.start("mkfifo()"); + + await t.test("invalid paths", () => { + assert.throws( + () => mkfifo("tests/this/dir/does/not/exist/file.fifo"), + { status: 1 }, + ); + assert.throws( + () => mkfifo(""), + { status: 1 }, + ); + }); + + await t.test("error when path already exists", async () => { + const path = "tests/hero-0.pipe" + + fs.writeFileSync(path, " ", { flush: true }); + + const stats = fs.statSync(path); + assert.ok(!stats.isFIFO()); + + assert.throws( + () => mkfifo(path), + { status: 1 }, + ); + }); + + await t.test("new pipe file", async () => { + const path = "tests/hero-1.pipe" + + rmIf(path); + assert.ok(!fs.existsSync(path)); + mkfifo(path); + assert.ok(fs.existsSync(path)); + + const stats = fs.statSync(path); + assert.ok(stats.isFIFO()); + }); +}; + +const test_buildRoutes = async t => { t.start("buildRoutes()"); await t.test("empty values", () => { @@ -1448,6 +1521,8 @@ await runner.runTests([ test_actionsFn, test_lineHandlerFn, test_buildRoutes, + test_rmIf, + test_mkfifo, test_promisifyServer, test_buildServer, ]); diff --git a/tests/lighttpd.conf b/tests/lighttpd.conf index 5438366..dfe395b 100644 --- a/tests/lighttpd.conf +++ b/tests/lighttpd.conf @@ -3,7 +3,7 @@ server.modules += ( "mod_accesslog", ) -server.bind = var.CWD + "/lighttpd.sock" +server.bind = var.CWD + "/lighttpd.socket" server.document-root = var.CWD + "/src/static/" server.errorlog = "/dev/stderr" @@ -28,5 +28,5 @@ accesslog.format += "}" index-file.names = ( "index.html" ) $HTTP["url"] =~ "^/api/" { - proxy.server = ( "" => (( "host" => var.CWD + "/web.sock" ))) + proxy.server = ( "" => (( "host" => var.CWD + "/web.socket" ))) } |