Skip to content

Commit

Permalink
refactor(test): update test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Nov 8, 2024
1 parent 99aa73a commit 24e0a24
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 164 deletions.
24 changes: 24 additions & 0 deletions crates/biome_fmt/scripts/update_tests.ts
Original file line number Diff line number Diff line change
@@ -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");
48 changes: 10 additions & 38 deletions crates/biome_fmt/test_bun/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -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<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));
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);
});
Expand Down
31 changes: 12 additions & 19 deletions crates/biome_fmt/test_deno/deno.test.ts
Original file line number Diff line number Diff line change
@@ -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/[email protected]/assert/mod.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { relative } from "https://deno.land/[email protected]/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,
Expand All @@ -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);
});
}
36 changes: 11 additions & 25 deletions crates/biome_fmt/test_node/test-node.mjs
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down
24 changes: 24 additions & 0 deletions crates/web_fmt/scripts/update_tests.ts
Original file line number Diff line number Diff line change
@@ -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");
48 changes: 10 additions & 38 deletions crates/web_fmt/test_bun/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -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<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));
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);
});
Expand Down
31 changes: 12 additions & 19 deletions crates/web_fmt/test_deno/deno.test.ts
Original file line number Diff line number Diff line change
@@ -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/[email protected]/assert/mod.ts";
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
import { relative } from "https://deno.land/[email protected]/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,
Expand All @@ -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);
});
}
36 changes: 11 additions & 25 deletions crates/web_fmt/test_node/test-node.mjs
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down

0 comments on commit 24e0a24

Please sign in to comment.