Debian Codegraph @

merge/test/index.ts

raw symbols SCIP
1 import fs from "node:fs";
2 import path from "node:path";
3 import { merge } from "../src/merge";
4
5 const fixturesPath = path.join(__dirname, "fixtures");
6 const dirs = fs
7 .readdirSync(fixturesPath, { withFileTypes: true })
8 .filter((file) => file.isDirectory() && file.name !== ".DS_Store");
9
10 async function runFixtures() {
11 let hadErrors = false;
12 for (const dir of dirs) {
13 const fixtureDirPath = path.join(fixturesPath, dir.name);
14 const oldSpecPath = path.join(fixtureDirPath, "old.ts");
15 const newSpecPath = path.join(fixtureDirPath, "new.ts");
16 const updatedSpecPath = path.join(fixtureDirPath, "updated.ts"); // gitignored
17 const expectedSpecPath = path.join(fixtureDirPath, "expected.ts");
18 const configPath = path.join(fixtureDirPath, "config.json");
19
20 let ignoreProps;
21 let preset;
22 if (fs.existsSync(configPath)) {
23 const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
24 preset = config.preset;
25 ignoreProps = config.ignoreProps;
26 }
27
28 const oldSpec = fs.readFileSync(oldSpecPath, { encoding: "utf-8" });
29 const newSpec = fs.readFileSync(newSpecPath, { encoding: "utf-8" });
30 // eslint-disable-next-line no-await-in-loop
31 const updatedSpec = await merge(oldSpec, newSpec, {
32 ...(preset && { preset }),
33 ...(ignoreProps && Array.isArray(ignoreProps) && { ignore: { commonProps: ignoreProps } }),
34 });
35 fs.writeFileSync(updatedSpecPath, updatedSpec);
36
37 if (process.env.OVERWRITE || !fs.existsSync(expectedSpecPath)) {
38 fs.copyFileSync(updatedSpecPath, expectedSpecPath);
39 } else {
40 const expectedSpec = fs.readFileSync(expectedSpecPath);
41 if (!Buffer.from(updatedSpec).equals(expectedSpec)) {
42 hadErrors = true;
43 console.error(`- Fixture ${fixtureDirPath} is failing.`);
44 }
45 }
46 }
47 if (hadErrors) {
48 process.exit(1);
49 }
50 }
51
52 runFixtures();