diff options
Diffstat (limited to 'tests/js')
-rw-r--r-- | tests/js/hero.mjs | 85 |
1 files changed, 80 insertions, 5 deletions
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, ]); |