Skip to content

Commit

Permalink
chore: update test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Oct 19, 2024
1 parent 673e7a6 commit 8e0e230
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.17.0
v22.10.0
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"deno.enable": true,
"deno.enablePaths": [
"./crates/ruff_fmt/test_deno"
"./crates/ruff_fmt/test_deno",
"./crates/ruff_fmt/scripts/update_tests.ts"
],
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
}
}
24 changes: 24 additions & 0 deletions crates/ruff_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/ruff_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: ["py", "pyi"],
})) {
if (entry.name.startsWith(".")) {
continue;
}

const expect_path = entry.path + ".expect";
const input = Deno.readTextFileSync(entry.path);

const actual = format(input, entry.path);
Deno.writeTextFileSync(expect_path, actual);
}
console.log("done");
40 changes: 9 additions & 31 deletions crates/ruff_fmt/test_bun/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
import { Glob } from "bun";
import { expect, test } from "bun:test";
import fs from "node:fs/promises";
import path from "node:path";
import { chdir } from "node:process";
import { fileURLToPath } from "node:url";

import init, { format } from "../pkg/ruff_fmt";

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 ".py":
case ".pyi":
break;
const test_root = fileURLToPath(import.meta.resolve("../test_data"));
chdir(test_root);

default:
continue;
}
const glob = new Glob("**/*.{py,pyi}");

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

test(test_name, () => {
test(input_path, () => {
const actual = format(input, input_path);
expect(actual).toBe(expected);
});
Expand Down
38 changes: 17 additions & 21 deletions crates/ruff_fmt/test_deno/deno.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
/// <reference lib="deno.ns" />
import { assertEquals } from "https://deno.land/std@0.201.0/assert/mod.ts";
import { walk } from "https://deno.land/std@0.201.0/fs/walk.ts";
import { relative } from "https://deno.land/std@0.201.0/path/mod.ts";
import { assertEquals } from "jsr:@std/assert";
import { walk } from "jsr:@std/fs/walk";

import init, { format } from "../pkg/ruff_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"));
Deno.chdir(test_root);

for await (const entry of walk(test_root, {
for await (const entry of walk(".", {
includeDirs: false,
exts: ["py", "pyi"],
})) {
const expect_path = entry.path + ".expect";
const input = Deno.readTextFileSync(entry.path);
if (entry.name.startsWith(".")) {
continue;
}

if (update) {
const actual = format(input, entry.path);
Deno.writeTextFileSync(expect_path, actual);
} else {
const expected = Deno.readTextFileSync(expect_path);
const input_path = entry.path;
const expect_path = input_path + ".expect";

const test_name = relative(test_root.pathname, entry.path);
const input = Deno.readTextFileSync(input_path);
const expected = Deno.readTextFileSync(expect_path);

Deno.test(test_name, () => {
const actual = format(input, entry.path);
assertEquals(actual, expected);
});
}
Deno.test(input_path, () => {
const actual = format(input, entry.path);
assertEquals(actual, expected);
});
}
25 changes: 7 additions & 18 deletions crates/ruff_fmt/test_node/test-node.mjs
Original file line number Diff line number Diff line change
@@ -1,41 +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/ruff_fmt_node.js";

await init();

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

for await (const dirent of await fs.opendir(test_root, { recursive: true })) {
if (!dirent.isFile()) {
for await (const input_path of fs.glob("**/*.{py,pyi}")) {
if (basename(input_path).startsWith(".")) {
continue;
}

const input_path = path.join(dirent.path, dirent.name);
const ext = path.extname(input_path);

switch (ext) {
case ".py":
case ".pyi":
break;

default:
continue;
}

const expect_path = input_path + ".expect";

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

const test_name = path.relative(test_root, input_path);

test(test_name, () => {
test(input_path, () => {
const actual = format(input, input_path);
assert.equal(actual, expected);
});
Expand Down

0 comments on commit 8e0e230

Please sign in to comment.