-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractTestDefinitions.js
71 lines (55 loc) · 2.02 KB
/
extractTestDefinitions.js
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
const fs = require("fs");
const path = require("path");
const { v4: uuidv4 } = require("uuid");
const os = require("os");
const execSync = require("child_process").execSync;
const tempDir = os.tmpdir();
const folderId = uuidv4();
const repoDir = path.join(tempDir, folderId);
const outputFileName = process.argv[2]
? process.argv[2] + ".txt"
: "output.txt";
execSync(`git clone https://github.com/NucleoidJS/Nucleoid ${repoDir}`);
const specFilePath = path.join(repoDir, "src", "test", "nucleoid.spec.js");
const testData = fs.readFileSync(specFilePath, "utf8");
function extractTestDescriptions(testData) {
const describeRegex = /describe\((['"`])(.*?)\1,/g;
const itRegex = /it\((['"`])(.*?)\1,/g;
let currentDescribe = "";
let output = "";
let definitionBlock = "";
let previousItDescription = "";
for (const line of testData.split("\n")) {
const describeMatch = describeRegex.exec(line);
const itMatch = itRegex.exec(line);
if (describeMatch) {
currentDescribe = describeMatch[2];
definitionBlock = "";
} else if (itMatch) {
if (previousItDescription) {
let blockLines = definitionBlock.split("\n");
blockLines.pop();
blockLines.pop();
blockLines.pop();
const trimmedDefinitionBlock = blockLines.join("\n");
output += `Nucleoid ${currentDescribe} ${previousItDescription}\n${trimmedDefinitionBlock}\n\n`;
definitionBlock = "";
}
previousItDescription = itMatch[2];
} else {
definitionBlock += line + "\n";
}
}
if (previousItDescription) {
let blockLines = definitionBlock.split("\n");
for (let i = 0; i < 5; i++) {
blockLines.pop();
}
const trimmedDefinitionBlock = blockLines.join("\n");
output += `Nucleoid ${currentDescribe} ${previousItDescription}\n${trimmedDefinitionBlock}\n`;
}
return output;
}
const formattedDescriptions = extractTestDescriptions(testData);
fs.writeFileSync(outputFileName, formattedDescriptions);
console.log(`Output written to ${outputFileName}`);