From: Ardis Lu Date: Fri, 10 Jan 2025 05:48:27 +0000 (-0800) Subject: Add test suite to test all examples X-Git-Tag: v0.1.0~132 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=b9779b1390ac98626d9cc2c659d1bcb1b2f2db9f;p=csvio Add test suite to test all examples --- diff --git a/test/examples.test.js b/test/examples.test.js new file mode 100644 index 0000000..cdb4728 --- /dev/null +++ b/test/examples.test.js @@ -0,0 +1,44 @@ +import { suite, after, test } from 'node:test'; +import { ok } from 'node:assert/strict'; +import { mkdir, cp, opendir, readFile, rm } from 'node:fs/promises'; +import { pathToFileURL } from 'node:url'; + +import { createTempFolder } from './utils.js'; + +const dir = await createTempFolder(); +const tempExamples = pathToFileURL(`${dir}/examples/`); // Trailing forward slash is REQUIRED for using this URL as a URL base later +const tempSrc = pathToFileURL(`${dir}/src/`); +const actualExamples = new URL('../examples/', import.meta.url); +const actualSrc = new URL('../src/', import.meta.url); +await Promise.all([ + mkdir(tempExamples, { recursive: true }), + mkdir(tempSrc, { recursive: true }) +]); +await Promise.all([ + cp(actualExamples, tempExamples, { recursive: true }), + cp(actualSrc, tempSrc, { recursive: true }) +]); + +const tests = []; +const files = await opendir(tempExamples, { recursive: true }); +for await (const file of files) { + const { name } = /(?ex\d+).js/.exec(file.name)?.groups ?? { name: null }; // Assuming naming convention "ex*.js" + if (name === null) { continue; } + tests.push({ + name, + code: new URL(`${name}.js`, tempExamples), + output: new URL(`./data/${name}-out.csv`, tempExamples) + }); +} + +suite('examples', { concurrency: true }, () => { + after(async () => await rm(dir, { recursive: true, force: true })); + for (const { name, code, output } of tests) { + test(name, { concurrency: true }, async () => { + const expected = await readFile(output); + await import(code); + const actual = await readFile(output); + ok(actual.equals(expected)); + }); + } +}); diff --git a/test/utils.js b/test/utils.js index 8d725c9..a62a477 100644 --- a/test/utils.js +++ b/test/utils.js @@ -129,6 +129,16 @@ export function createCSVMockStream(data) { }); } +/** + * Create a new temporary folder for testing purposes. + * + * @returns {Promise} A `Promise` for the full path to a newly-created temporary folder. + */ +export async function createTempFolder() { + const path = normalize(`${tmpdir()}/scsv_test_`); + return mkdtemp(path); +} + /** * Create a new empty temporary file for testing purposes. *