Debian Codegraph @

cli/tools-cli/test/versioning/index.ts

raw symbols SCIP
1 import fs from "fs";
2 import child from "child_process";
3 import path from "path";
4 import { copyDirectorySync } from "../../src/scripts/version";
5
6 const cliPath = path.join(__dirname, "..", "..", "src", "bin.ts");
7 const fixturesPath = path.join(__dirname, "fixtures");
8 const dirs = fs
9 .readdirSync(fixturesPath, { withFileTypes: true })
10 .filter((file) => file.isDirectory() && file.name !== ".DS_Store");
11
12 function assertEqualFiles(path1: string, path2: string): boolean {
13 if (!fs.statSync(path1).isFile() || !fs.statSync(path2).isFile()) return false;
14 return fs.readFileSync(path1).equals(fs.readFileSync(path2));
15 }
16
17 function assertEqualDirectories(path1: string, path2: string): boolean {
18 if (!fs.statSync(path1).isDirectory() || !fs.statSync(path2).isDirectory()) return false;
19 const dirA = fs
20 .readdirSync(path1, { withFileTypes: true })
21 .filter((file) => file.name !== ".DS_Store");
22 const dirB = fs
23 .readdirSync(path2, { withFileTypes: true })
24 .filter((file) => file.name !== ".DS_Store");
25
26 if (dirA.length !== dirB.length) return false;
27
28 for (const dirent of dirA) {
29 const dirent1Path = path.join(path1, dirent.name);
30 const dirent2Path = path.join(path2, dirent.name);
31 if (dirent.isDirectory() && !assertEqualDirectories(dirent1Path, dirent2Path)) {
32 return false;
33 }
34 if (dirent.isFile() && !assertEqualFiles(dirent1Path, dirent2Path)) {
35 return false;
36 }
37 }
38 return true;
39 }
40
41 export function runFixtures() {
42 let hadErrors = false;
43 for (const dir of dirs) {
44 const fixtureDirPath = path.join(fixturesPath, dir.name);
45 const newSpecPath = path.resolve(fixtureDirPath, "new-spec.ts");
46 const updatedSpecPath = path.join(fixtureDirPath, "updated-spec"); // gitignored
47 const expectedSpecPath = path.join(fixtureDirPath, "expected-spec");
48 const configPath = path.join(fixtureDirPath, "config.json");
49
50 let newVersion: string;
51
52 try {
53 const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
54 newVersion = config.newVersion;
55 // TODO: load other options
56 } catch (error) {
57 throw new Error(`You must provide a config.json file for fixture at ${fixtureDirPath}`);
58 }
59
60 fs.rmSync(updatedSpecPath, { recursive: true, force: true });
61 const cmd = `node -r tsx/cjs ${cliPath} version add-diff old-spec ${newSpecPath} ${newVersion} --cwd ${fixtureDirPath} --new-path ${updatedSpecPath}`;
62
63 try {
64 child.execSync(cmd);
65 } catch (error) {
66 console.warn(`- Encounterd an error when running fixture ${fixtureDirPath}.\n${error}`);
67 }
68
69 if (process.env.OVERWRITE || !fs.existsSync(expectedSpecPath)) {
70 copyDirectorySync(updatedSpecPath, expectedSpecPath);
71 } else if (!assertEqualDirectories(expectedSpecPath, updatedSpecPath)) {
72 hadErrors = true;
73 console.error(`- Fixture ${fixtureDirPath} is failing.`);
74 }
75 }
76 if (hadErrors) {
77 process.exit(1);
78 }
79 }