Files
openclaw/test/scripts/check-file-utils.test.ts
T

66 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-03-21 23:07:16 +00:00
import fs from "node:fs";
import path from "node:path";
2026-04-06 05:35:00 +01:00
import { describe, expect, it } from "vitest";
import {
collectFilesSync,
isCodeFile,
relativeToCwd,
toPosixPath,
} from "../../scripts/check-file-utils.js";
2026-04-06 05:35:00 +01:00
import { createScriptTestHarness } from "./test-helpers.js";
2026-03-21 23:07:16 +00:00
2026-04-06 05:35:00 +01:00
const { createTempDir } = createScriptTestHarness();
2026-03-21 23:07:16 +00:00
describe("scripts/check-file-utils isCodeFile", () => {
it("accepts source files and skips declarations", () => {
expect(isCodeFile("example.ts")).toBe(true);
expect(isCodeFile("example.mjs")).toBe(true);
expect(isCodeFile("example.d.ts")).toBe(false);
});
});
describe("scripts/check-file-utils collectFilesSync", () => {
it("collects matching files while skipping common generated dirs", () => {
2026-04-06 05:35:00 +01:00
const rootDir = createTempDir("openclaw-check-file-utils-");
2026-03-21 23:07:16 +00:00
fs.mkdirSync(path.join(rootDir, "src", "nested"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "dist"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "docs", ".generated"), { recursive: true });
2026-03-21 23:07:16 +00:00
fs.writeFileSync(path.join(rootDir, "src", "keep.ts"), "");
fs.writeFileSync(path.join(rootDir, "src", "nested", "keep.test.ts"), "");
fs.writeFileSync(path.join(rootDir, "dist", "skip.ts"), "");
fs.writeFileSync(path.join(rootDir, "docs", ".generated", "skip.ts"), "");
2026-03-21 23:07:16 +00:00
const files = collectFilesSync(rootDir, {
includeFile: (filePath) => filePath.endsWith(".ts"),
}).map((filePath) => toPosixPath(path.relative(rootDir, filePath)));
2026-03-21 23:07:16 +00:00
expect(files.toSorted((left, right) => left.localeCompare(right))).toEqual([
"src/keep.ts",
"src/nested/keep.test.ts",
]);
2026-03-21 23:07:16 +00:00
});
it("supports custom skipped directories", () => {
2026-04-06 05:35:00 +01:00
const rootDir = createTempDir("openclaw-check-file-utils-");
2026-03-21 23:07:16 +00:00
fs.mkdirSync(path.join(rootDir, "fixtures"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "src"), { recursive: true });
fs.writeFileSync(path.join(rootDir, "fixtures", "skip.ts"), "");
fs.writeFileSync(path.join(rootDir, "src", "keep.ts"), "");
const files = collectFilesSync(rootDir, {
includeFile: (filePath) => filePath.endsWith(".ts"),
skipDirNames: new Set(["fixtures"]),
}).map((filePath) => toPosixPath(path.relative(rootDir, filePath)));
2026-03-21 23:07:16 +00:00
expect(files).toEqual(["src/keep.ts"]);
});
});
describe("scripts/check-file-utils relativeToCwd", () => {
it("renders repo-relative paths when possible", () => {
expect(relativeToCwd(path.join(process.cwd(), "scripts", "check-file-utils.ts"))).toBe(
"scripts/check-file-utils.ts",
);
});
});