forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-test-output.js
More file actions
93 lines (83 loc) · 2.92 KB
/
update-test-output.js
File metadata and controls
93 lines (83 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable no-console */
import klawSync from "klaw-sync";
import path from "path";
import fs from "fs-extra";
import prettier from "prettier";
import url from "url";
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const updateTestOutput = async () => {
let samplesDir = path.resolve(__dirname, "../test/unit-test");
let originalSamplesDir = samplesDir;
if (process.argv.indexOf("-single") > -1) {
samplesDir = path.resolve(__dirname, "./single-printer-run");
} else if (process.argv.indexOf("-repository") > -1) {
const testSamples = path.resolve(__dirname, "../test-samples");
originalSamplesDir = path.resolve(
__dirname,
process.argv[process.argv.indexOf("-repository") + 1]
);
samplesDir = path.resolve(testSamples, path.basename(originalSamplesDir));
if (fs.existsSync(samplesDir)) {
fs.removeSync(samplesDir);
}
console.log(`start copy ${originalSamplesDir} to ${samplesDir}`);
fs.copySync(originalSamplesDir, samplesDir);
console.log(`end copy ${originalSamplesDir} to ${samplesDir}`);
}
let numberOfTime = 1;
if (process.argv.indexOf("-times") > -1) {
numberOfTime = process.argv[process.argv.indexOf("-times") + 1];
}
const sampleFiles = klawSync(samplesDir, { nodir: true });
const javaSampleFiles = sampleFiles.filter(fileDesc => {
if (fileDesc.path.includes("node_modules")) {
return false;
}
if (process.argv.indexOf("-repository") > -1) {
return fileDesc.path.endsWith(".java");
}
return fileDesc.path.endsWith("input.java");
});
let failures = 0;
await Promise.all(
javaSampleFiles.map(async fileDesc => {
const javaFileText = fs.readFileSync(fileDesc.path, "utf8");
try {
console.log(`Reading <${fileDesc.path}>`);
let newExpectedText = javaFileText;
const testDir = path.dirname(fileDesc.path);
const optionsPath = path.join(testDir, ".prettierrc.json");
const testOptions = fs.existsSync(optionsPath)
? fs.readJsonSync(optionsPath)
: {};
for (let i = 0; i < numberOfTime; i++) {
newExpectedText = await prettier.format(newExpectedText, {
parser: "java",
plugins: [path.resolve(__dirname, "../dist/index.js")],
tabWidth: 2,
endOfLine: "lf",
...testOptions
});
}
let outputFilePath = fileDesc.path.replace(
/input.java$/,
"output.java"
);
if (process.argv.indexOf("-repository") > -1) {
outputFilePath = fileDesc.path;
}
console.log(`writing <${outputFilePath}>`);
fs.writeFileSync(outputFilePath, newExpectedText);
} catch (e) {
failures++;
console.log(`Failed parsing: <${fileDesc.path}>`);
console.log(e);
}
})
);
if (failures > 0) {
console.log(`fail: ${failures}`);
process.exit(1);
}
};
updateTestOutput();