Skip to content

Commit

Permalink
chore: add bun test
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Sep 2, 2023
1 parent a2bd50d commit 0757ed6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
yes | sudo dpkg -i ${{ runner.temp }}/tinygo_0.28.1_amd64.deb
- run: npm run build
- run: pnpm run /test/
- run: pnpm run /^test:/

- name: Package
run: npm pack
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
"patch": "git apply ./gofmt.patch",
"build": "tinygo build -o=gofmt.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
"postbuild": "npm run gofmt && npm run patch",
"test:node": "node --test",
"test:deno": "deno test --allow-read"
"test:node": "node --test test_node",
"test:deno": "deno test test_deno --allow-read",
"test:bun": "bun test test_bun"
},
"packageManager": "[email protected]",
"engines": {
"node": ">=16.17.0"
},
Expand Down
47 changes: 47 additions & 0 deletions test_bun/bun.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test } from "bun:test";
import init, { format } from "../gofmt.js";
import fs from "node:fs/promises";
import path from "node:path";

await init();

async function* walk(dir: string): AsyncGenerator<string> {
for await (const d of await fs.readdir(dir)) {
const entry = path.join(dir, d);
const stat = await fs.stat(entry);

if (stat.isDirectory()) {
yield* walk(entry);
}

if (stat.isFile()) {
yield entry;
}
}
}

const test_root = Bun.fileURLToPath(new URL("../test_data", import.meta.url));

for await (const input_path of walk(test_root)) {
const ext = path.extname(input_path);

switch (ext) {
case ".input":
break;

default:
continue;
}

const test_name = path.relative(test_root, input_path);
const [input, expected] = await Promise.all([
Bun.file(input_path).text(),
Bun.file(input_path.replace(ext, ".golden")).text(),
]);

const actual = format(input);

test(test_name, () => {
expect(actual).toBe(expected);
});
}
19 changes: 6 additions & 13 deletions test_node/test-node.mjs → test_node/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ import { fileURLToPath } from "node:url";

await init();

/**
* @param {string} dir
* @returns {Generator<string>}
*/
async function* walk(dir) {
for await (const d of await fs.opendir(dir)) {
const entry = path.join(dir, d.name);
if (d.isDirectory()) yield* walk(entry);
else if (d.isFile()) yield entry;
}
}

const test_root = fileURLToPath(new URL("../test_data", import.meta.url));

for await (const input_path of walk(test_root)) {
for await (const dirent of await fs.opendir(test_root, { recursive: true })) {
if (!dirent.isFile()) {
continue;
}

const input_path = dirent.path;
const ext = path.extname(input_path);

switch (ext) {
Expand Down

0 comments on commit 0757ed6

Please sign in to comment.