From 24e0a24d093cb3ec6c12bfa188d368a56cb3ee45 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Fri, 8 Nov 2024 21:52:34 +0800 Subject: [PATCH] refactor(test): update test scripts --- crates/biome_fmt/scripts/update_tests.ts | 24 ++++++++++++ crates/biome_fmt/test_bun/bun.spec.ts | 48 +++++------------------- crates/biome_fmt/test_deno/deno.test.ts | 31 ++++++--------- crates/biome_fmt/test_node/test-node.mjs | 36 ++++++------------ crates/web_fmt/scripts/update_tests.ts | 24 ++++++++++++ crates/web_fmt/test_bun/bun.spec.ts | 48 +++++------------------- crates/web_fmt/test_deno/deno.test.ts | 31 ++++++--------- crates/web_fmt/test_node/test-node.mjs | 36 ++++++------------ 8 files changed, 114 insertions(+), 164 deletions(-) create mode 100755 crates/biome_fmt/scripts/update_tests.ts create mode 100755 crates/web_fmt/scripts/update_tests.ts diff --git a/crates/biome_fmt/scripts/update_tests.ts b/crates/biome_fmt/scripts/update_tests.ts new file mode 100755 index 0000000..a44735d --- /dev/null +++ b/crates/biome_fmt/scripts/update_tests.ts @@ -0,0 +1,24 @@ +#!/usr/bin/env -S deno run --allow-read --allow-write +import { walk } from "jsr:@std/fs/walk"; + +import init, { format } from "../pkg/biome_fmt.js"; + +await init(); + +const test_root = new URL(import.meta.resolve("../test_data")); + +for await (const entry of walk(test_root, { + includeDirs: false, + exts: ["html", "vue", "json", "tsx"], +})) { + if (entry.name.startsWith(".")) { + continue; + } + + const expect_path = entry.path + ".snap"; + const input = Deno.readTextFileSync(entry.path); + + const actual = format(input, entry.path); + Deno.writeTextFileSync(expect_path, actual); +} +console.log("done"); diff --git a/crates/biome_fmt/test_bun/bun.spec.ts b/crates/biome_fmt/test_bun/bun.spec.ts index e4bdbf1..ba14d0e 100644 --- a/crates/biome_fmt/test_bun/bun.spec.ts +++ b/crates/biome_fmt/test_bun/bun.spec.ts @@ -1,52 +1,24 @@ +import { Glob } from "bun"; import { expect, test } from "bun:test"; -import fs from "node:fs/promises"; -import path from "node:path"; -import init, { format } from "../pkg/biome_fmt.js"; +import { chdir } from "node:process"; +import { fileURLToPath } from "node:url"; -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)); +import init, { format } from "../pkg/biome_fmt"; -for await (const input_path of walk(test_root)) { - if (path.basename(input_path).startsWith(".")) { - continue; - } - - const ext = path.extname(input_path); +await init(); - switch (ext) { - case ".js": - case ".jsx": - case ".ts": - case ".tsx": - break; +const test_root = fileURLToPath(import.meta.resolve("../test_data")); +chdir(test_root); - default: - continue; - } +const glob = new Glob("**/*.{js,jsx,ts,tsx}"); - const test_name = path.relative(test_root, input_path); +for await (const input_path of glob.scan()) { const [input, expected] = await Promise.all([ Bun.file(input_path).text(), Bun.file(input_path + ".snap").text(), ]); - test(test_name, () => { + test(input_path, () => { const actual = format(input, input_path); expect(actual).toBe(expected); }); diff --git a/crates/biome_fmt/test_deno/deno.test.ts b/crates/biome_fmt/test_deno/deno.test.ts index 6eff9c3..0bacee7 100644 --- a/crates/biome_fmt/test_deno/deno.test.ts +++ b/crates/biome_fmt/test_deno/deno.test.ts @@ -1,14 +1,11 @@ -import init, { format } from "../pkg/biome_fmt.js"; +import { assertEquals } from "jsr:@std/assert"; +import { walk } from "jsr:@std/fs/walk"; -import { assertEquals } from "https://deno.land/std@0.217.0/assert/mod.ts"; -import { walk } from "https://deno.land/std@0.217.0/fs/walk.ts"; -import { relative } from "https://deno.land/std@0.217.0/path/mod.ts"; +import init, { format } from "../pkg/biome_fmt.js"; await init(); -const update = Deno.args.includes("--update"); - -const test_root = new URL("../test_data", import.meta.url); +const test_root = new URL(import.meta.resolve("../test_data")); for await (const entry of walk(test_root, { includeDirs: false, @@ -18,18 +15,14 @@ for await (const entry of walk(test_root, { continue; } - const input = Deno.readTextFileSync(entry.path); + const input_path = entry.path; + const expect_path = input_path + ".snap"; - if (update) { - const actual = format(input, entry.name); - Deno.writeTextFileSync(entry.path + ".snap", actual); - } else { - const test_name = relative(test_root.pathname, entry.path); - const expected = Deno.readTextFileSync(entry.path + ".snap"); + const input = Deno.readTextFileSync(input_path); + const expected = Deno.readTextFileSync(expect_path); - Deno.test(test_name, () => { - const actual = format(input, entry.name); - assertEquals(actual, expected); - }); - } + Deno.test(input_path, () => { + const actual = format(input, entry.path); + assertEquals(actual, expected); + }); } diff --git a/crates/biome_fmt/test_node/test-node.mjs b/crates/biome_fmt/test_node/test-node.mjs index 944303f..e425e45 100644 --- a/crates/biome_fmt/test_node/test-node.mjs +++ b/crates/biome_fmt/test_node/test-node.mjs @@ -1,44 +1,30 @@ import assert from "node:assert/strict"; import fs from "node:fs/promises"; -import path from "node:path"; +import { basename } from "node:path"; +import { chdir } from "node:process"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; + import init, { format } from "../pkg/biome_fmt_node.js"; await init(); -const test_root = fileURLToPath(new URL("../test_data", import.meta.url)); - -for await (const dirent of await fs.opendir(test_root, { recursive: true })) { - if (!dirent.isFile()) { - continue; - } +const test_root = fileURLToPath(import.meta.resolve("../test_data")); +chdir(test_root); - if (dirent.name.startsWith(".")) { +for await (const input_path of fs.glob("**/*.{js,jsx,ts,tsx}")) { + if (basename(input_path).startsWith(".")) { continue; } - const input_path = path.join(dirent.path, dirent.name); - const ext = path.extname(input_path); - - switch (ext) { - case ".js": - case ".jsx": - case ".ts": - case ".tsx": - break; - - default: - continue; - } + const expect_path = input_path + ".snap"; - const test_name = path.relative(test_root, input_path); const [input, expected] = await Promise.all([ - fs.readFile(input_path, { encoding: "utf-8" }), - fs.readFile(input_path + ".snap", { encoding: "utf-8" }), + fs.readFile(input_path, "utf-8"), + fs.readFile(expect_path, "utf-8"), ]); - test(test_name, () => { + test(input_path, () => { const actual = format(input, input_path); assert.equal(actual, expected); }); diff --git a/crates/web_fmt/scripts/update_tests.ts b/crates/web_fmt/scripts/update_tests.ts new file mode 100755 index 0000000..a40d42c --- /dev/null +++ b/crates/web_fmt/scripts/update_tests.ts @@ -0,0 +1,24 @@ +#!/usr/bin/env -S deno run --allow-read --allow-write +import { walk } from "jsr:@std/fs/walk"; + +import init, { format } from "../pkg/web_fmt.js"; + +await init(); + +const test_root = new URL(import.meta.resolve("../test_data")); + +for await (const entry of walk(test_root, { + includeDirs: false, + exts: ["html", "vue", "json", "tsx"], +})) { + if (entry.name.startsWith(".")) { + continue; + } + + const expect_path = entry.path + ".snap"; + const input = Deno.readTextFileSync(entry.path); + + const actual = format(input, entry.path); + Deno.writeTextFileSync(expect_path, actual); +} +console.log("done"); diff --git a/crates/web_fmt/test_bun/bun.spec.ts b/crates/web_fmt/test_bun/bun.spec.ts index 6412f89..7fdd7e7 100644 --- a/crates/web_fmt/test_bun/bun.spec.ts +++ b/crates/web_fmt/test_bun/bun.spec.ts @@ -1,52 +1,24 @@ +import { Glob } from "bun"; import { expect, test } from "bun:test"; -import fs from "node:fs/promises"; -import path from "node:path"; -import init, { format } from "../pkg/web_fmt.js"; +import { chdir } from "node:process"; +import { fileURLToPath } from "node:url"; -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)); +import init, { format } from "../pkg/web_fmt"; -for await (const input_path of walk(test_root)) { - if (path.basename(input_path).startsWith(".")) { - continue; - } - - const ext = path.extname(input_path); +await init(); - switch (ext) { - case ".html": - case ".vue": - case ".json": - case ".tsx": - break; +const test_root = fileURLToPath(import.meta.resolve("../test_data")); +chdir(test_root); - default: - continue; - } +const glob = new Glob("**/*.{html,vue,json,tsx}"); - const test_name = path.relative(test_root, input_path); +for await (const input_path of glob.scan()) { const [input, expected] = await Promise.all([ Bun.file(input_path).text(), Bun.file(input_path + ".snap").text(), ]); - test(test_name, () => { + test(input_path, () => { const actual = format(input, input_path); expect(actual).toBe(expected); }); diff --git a/crates/web_fmt/test_deno/deno.test.ts b/crates/web_fmt/test_deno/deno.test.ts index a9c903b..c0566bd 100644 --- a/crates/web_fmt/test_deno/deno.test.ts +++ b/crates/web_fmt/test_deno/deno.test.ts @@ -1,14 +1,11 @@ -import init, { format } from "../pkg/web_fmt.js"; +import { assertEquals } from "jsr:@std/assert"; +import { walk } from "jsr:@std/fs/walk"; -import { assertEquals } from "https://deno.land/std@0.217.0/assert/mod.ts"; -import { walk } from "https://deno.land/std@0.217.0/fs/walk.ts"; -import { relative } from "https://deno.land/std@0.217.0/path/mod.ts"; +import init, { format } from "../pkg/web_fmt.js"; await init(); -const update = Deno.args.includes("--update"); - -const test_root = new URL("../test_data", import.meta.url); +const test_root = new URL(import.meta.resolve("../test_data")); for await (const entry of walk(test_root, { includeDirs: false, @@ -18,18 +15,14 @@ for await (const entry of walk(test_root, { continue; } - const input = Deno.readTextFileSync(entry.path); + const input_path = entry.path; + const expect_path = input_path + ".snap"; - if (update) { - const actual = format(input, entry.name); - Deno.writeTextFileSync(entry.path + ".snap", actual); - } else { - const test_name = relative(test_root.pathname, entry.path); - const expected = Deno.readTextFileSync(entry.path + ".snap"); + const input = Deno.readTextFileSync(input_path); + const expected = Deno.readTextFileSync(expect_path); - Deno.test(test_name, () => { - const actual = format(input, entry.name); - assertEquals(actual, expected); - }); - } + Deno.test(input_path, () => { + const actual = format(input, entry.path); + assertEquals(actual, expected); + }); } diff --git a/crates/web_fmt/test_node/test-node.mjs b/crates/web_fmt/test_node/test-node.mjs index 3e3bbc9..992347d 100644 --- a/crates/web_fmt/test_node/test-node.mjs +++ b/crates/web_fmt/test_node/test-node.mjs @@ -1,44 +1,30 @@ import assert from "node:assert/strict"; import fs from "node:fs/promises"; -import path from "node:path"; +import { basename } from "node:path"; +import { chdir } from "node:process"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; + import init, { format } from "../pkg/web_fmt_node.js"; await init(); -const test_root = fileURLToPath(new URL("../test_data", import.meta.url)); - -for await (const dirent of await fs.opendir(test_root, { recursive: true })) { - if (!dirent.isFile()) { - continue; - } +const test_root = fileURLToPath(import.meta.resolve("../test_data")); +chdir(test_root); - if (dirent.name.startsWith(".")) { +for await (const input_path of fs.glob("**/*.{html,vue,json,tsx}")) { + if (basename(input_path).startsWith(".")) { continue; } - const input_path = path.join(dirent.path, dirent.name); - const ext = path.extname(input_path); - - switch (ext) { - case ".html": - case ".vue": - case ".json": - case ".tsx": - break; - - default: - continue; - } + const expect_path = input_path + ".snap"; - const test_name = path.relative(test_root, input_path); const [input, expected] = await Promise.all([ - fs.readFile(input_path, { encoding: "utf-8" }), - fs.readFile(input_path + ".snap", { encoding: "utf-8" }), + fs.readFile(input_path, "utf-8"), + fs.readFile(expect_path, "utf-8"), ]); - test(test_name, () => { + test(input_path, () => { const actual = format(input, input_path); assert.equal(actual, expected); });