]> git.ardis.lu - csvio/commitdiff
Add test suite to test all examples
authorArdis Lu <ardisglu@gmail.com>
Fri, 10 Jan 2025 05:48:27 +0000 (21:48 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 10 Jan 2025 05:48:27 +0000 (21:48 -0800)
test/examples.test.js [new file with mode: 0644]
test/utils.js

diff --git a/test/examples.test.js b/test/examples.test.js
new file mode 100644 (file)
index 0000000..cdb4728
--- /dev/null
@@ -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 } = /(?<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));
+    });
+  }
+});
index 8d725c9858a553ecb6a6b4e7d41a9d3be2b2572b..a62a4772123733eee3ac00f9f2feea05a2a2b169 100644 (file)
@@ -129,6 +129,16 @@ export function createCSVMockStream(data) {
   });
 }
 
+/**
+ * Create a new temporary folder for testing purposes.
+ * 
+ * @returns {Promise<string>} A `Promise<string>` 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.
  * 
This page took 0.053834 seconds and 4 git commands to generate.