Skip to content

Commit 8e0e230

Browse files
committed
chore: update test scripts
1 parent 673e7a6 commit 8e0e230

File tree

6 files changed

+61
-73
lines changed

6 files changed

+61
-73
lines changed

.node-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v20.17.0
1+
v22.10.0

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"deno.enable": true,
33
"deno.enablePaths": [
4-
"./crates/ruff_fmt/test_deno"
4+
"./crates/ruff_fmt/test_deno",
5+
"./crates/ruff_fmt/scripts/update_tests.ts"
56
],
67
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
7-
}
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env -S deno run --allow-read --allow-write
2+
import { walk } from "jsr:@std/fs/walk";
3+
4+
import init, { format } from "../pkg/ruff_fmt.js";
5+
6+
await init();
7+
8+
const test_root = new URL(import.meta.resolve("../test_data"));
9+
10+
for await (const entry of walk(test_root, {
11+
includeDirs: false,
12+
exts: ["py", "pyi"],
13+
})) {
14+
if (entry.name.startsWith(".")) {
15+
continue;
16+
}
17+
18+
const expect_path = entry.path + ".expect";
19+
const input = Deno.readTextFileSync(entry.path);
20+
21+
const actual = format(input, entry.path);
22+
Deno.writeTextFileSync(expect_path, actual);
23+
}
24+
console.log("done");

crates/ruff_fmt/test_bun/bun.spec.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
1+
import { Glob } from "bun";
12
import { expect, test } from "bun:test";
2-
import fs from "node:fs/promises";
3-
import path from "node:path";
3+
import { chdir } from "node:process";
4+
import { fileURLToPath } from "node:url";
5+
46
import init, { format } from "../pkg/ruff_fmt";
57

68
await init();
79

8-
async function* walk(dir: string): AsyncGenerator<string> {
9-
for await (const d of await fs.readdir(dir)) {
10-
const entry = path.join(dir, d);
11-
const stat = await fs.stat(entry);
12-
13-
if (stat.isDirectory()) {
14-
yield* walk(entry);
15-
}
16-
17-
if (stat.isFile()) {
18-
yield entry;
19-
}
20-
}
21-
}
22-
23-
const test_root = Bun.fileURLToPath(new URL("../test_data", import.meta.url));
24-
25-
for await (const input_path of walk(test_root)) {
26-
const ext = path.extname(input_path);
27-
28-
switch (ext) {
29-
case ".py":
30-
case ".pyi":
31-
break;
10+
const test_root = fileURLToPath(import.meta.resolve("../test_data"));
11+
chdir(test_root);
3212

33-
default:
34-
continue;
35-
}
13+
const glob = new Glob("**/*.{py,pyi}");
3614

37-
const test_name = path.relative(test_root, input_path);
15+
for await (const input_path of glob.scan(/* default: {cwd: ".", dot: false, absolute: false, onlyFiles: true} */)) {
3816
const [input, expected] = await Promise.all([
3917
Bun.file(input_path).text(),
4018
Bun.file(input_path + ".expect").text(),
4119
]);
4220

43-
test(test_name, () => {
21+
test(input_path, () => {
4422
const actual = format(input, input_path);
4523
expect(actual).toBe(expected);
4624
});
Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
1-
/// <reference lib="deno.ns" />
2-
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
3-
import { walk } from "https://deno.land/[email protected]/fs/walk.ts";
4-
import { relative } from "https://deno.land/[email protected]/path/mod.ts";
1+
import { assertEquals } from "jsr:@std/assert";
2+
import { walk } from "jsr:@std/fs/walk";
3+
54
import init, { format } from "../pkg/ruff_fmt.js";
65

76
await init();
87

9-
const update = Deno.args.includes("--update");
10-
11-
const test_root = new URL("../test_data", import.meta.url);
8+
const test_root = new URL(import.meta.resolve("../test_data"));
9+
Deno.chdir(test_root);
1210

13-
for await (const entry of walk(test_root, {
11+
for await (const entry of walk(".", {
1412
includeDirs: false,
1513
exts: ["py", "pyi"],
1614
})) {
17-
const expect_path = entry.path + ".expect";
18-
const input = Deno.readTextFileSync(entry.path);
15+
if (entry.name.startsWith(".")) {
16+
continue;
17+
}
1918

20-
if (update) {
21-
const actual = format(input, entry.path);
22-
Deno.writeTextFileSync(expect_path, actual);
23-
} else {
24-
const expected = Deno.readTextFileSync(expect_path);
19+
const input_path = entry.path;
20+
const expect_path = input_path + ".expect";
2521

26-
const test_name = relative(test_root.pathname, entry.path);
22+
const input = Deno.readTextFileSync(input_path);
23+
const expected = Deno.readTextFileSync(expect_path);
2724

28-
Deno.test(test_name, () => {
29-
const actual = format(input, entry.path);
30-
assertEquals(actual, expected);
31-
});
32-
}
25+
Deno.test(input_path, () => {
26+
const actual = format(input, entry.path);
27+
assertEquals(actual, expected);
28+
});
3329
}

crates/ruff_fmt/test_node/test-node.mjs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
import assert from "node:assert/strict";
22
import fs from "node:fs/promises";
3-
import path from "node:path";
3+
import { basename } from "node:path";
4+
import { chdir } from "node:process";
45
import { test } from "node:test";
56
import { fileURLToPath } from "node:url";
7+
68
import init, { format } from "../pkg/ruff_fmt_node.js";
79

810
await init();
911

1012
const test_root = fileURLToPath(new URL("../test_data", import.meta.url));
13+
chdir(test_root);
1114

12-
for await (const dirent of await fs.opendir(test_root, { recursive: true })) {
13-
if (!dirent.isFile()) {
15+
for await (const input_path of fs.glob("**/*.{py,pyi}")) {
16+
if (basename(input_path).startsWith(".")) {
1417
continue;
1518
}
1619

17-
const input_path = path.join(dirent.path, dirent.name);
18-
const ext = path.extname(input_path);
19-
20-
switch (ext) {
21-
case ".py":
22-
case ".pyi":
23-
break;
24-
25-
default:
26-
continue;
27-
}
28-
2920
const expect_path = input_path + ".expect";
3021

3122
const [input, expected] = await Promise.all([
3223
fs.readFile(input_path, "utf-8"),
3324
fs.readFile(expect_path, "utf-8"),
3425
]);
3526

36-
const test_name = path.relative(test_root, input_path);
37-
38-
test(test_name, () => {
27+
test(input_path, () => {
3928
const actual = format(input, input_path);
4029
assert.equal(actual, expected);
4130
});

0 commit comments

Comments
 (0)