From 0757ed6189525b808595f43e3230fa2beaeff0ee Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 2 Sep 2023 14:47:20 +0800 Subject: [PATCH] chore: add bun test --- .github/workflows/test.yml | 2 +- package.json | 6 ++- test_bun/bun.test.ts | 47 +++++++++++++++++++++++ test_node/{test-node.mjs => node.test.js} | 19 +++------ 4 files changed, 58 insertions(+), 16 deletions(-) create mode 100644 test_bun/bun.test.ts rename test_node/{test-node.mjs => node.test.js} (71%) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 310d871..c1c6f7a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/package.json b/package.json index 6f875b7..ceac81a 100644 --- a/package.json +++ b/package.json @@ -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": "pnpm@8.7.1", "engines": { "node": ">=16.17.0" }, diff --git a/test_bun/bun.test.ts b/test_bun/bun.test.ts new file mode 100644 index 0000000..d23715c --- /dev/null +++ b/test_bun/bun.test.ts @@ -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 { + 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); + }); +} diff --git a/test_node/test-node.mjs b/test_node/node.test.js similarity index 71% rename from test_node/test-node.mjs rename to test_node/node.test.js index 6737937..159197e 100644 --- a/test_node/test-node.mjs +++ b/test_node/node.test.js @@ -7,21 +7,14 @@ import { fileURLToPath } from "node:url"; await init(); -/** - * @param {string} dir - * @returns {Generator} - */ -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) {