Debian Codegraph @

jsonpath-compliance-test-suite/build.js

raw symbols SCIP
1 // @ts-check
2
3 const fs = require('fs');
4 const path = require('path');
5 /**
6 * The file extension of test files.
7 * @type {string}
8 */
9 const fileExtension = '.json';
10 /**
11 * The number of spaces to use for indentation in the output.
12 * @type {number}
13 */
14 const jsonIndent = 2;
15 /**
16 * The separator to use when combining test names.
17 * @type {string}
18 */
19 const testNameSeparator = ', ';
20
21 /**
22 * The description of the test suite.
23 * @type {string}
24 */
25 const description = 'JSONPath Compliance Test Suite. This file is autogenerated, do not edit.';
26
27 build();
28
29 /**
30 * Build a combined test suite from a directory of test files.
31 * The output is written to stdout.
32 */
33 function build() {
34 if (process.argv.length < 3) {
35 console.error('Usage: node build.js <test-folder-path>');
36 process.exit(1);
37 }
38
39 const testsFolder = process.argv[2];
40 const tests = readTestsFromDir(testsFolder);
41 const cts = {'description': description, 'tests': tests};
42 console.log(JSON.stringify(cts, null, jsonIndent));
43 console.error(`Wrote ${tests.length} tests to stdout.`);
44 }
45
46
47 /**
48 * Read all test files from a directory and its subdirectories.
49 * The directory name is prepended to the test name.
50 * @param dir {string} - The directory to read.
51 * @param relativePath {string[]} - The path to the directory.
52 * @returns {*[]} - An array of test objects.
53 */
54 function readTestsFromDir(dir, relativePath = []) {
55 const files = fs.readdirSync(dir);
56 files.sort(filesBeforeDirs);
57 return files.reduce(addTestsFromFile, []);
58
59 /**
60 * Add tests from a file to the list of all tests.
61 * @param allTests - The list of all tests.
62 * @param file - The file to read.
63 * @returns {*} - The updated list of all tests.
64 */
65 function addTestsFromFile(allTests, file) {
66 const fullPath = path.join(dir, file);
67
68 if (isDir(file)) {
69 console.error(`Processing dir ${fullPath}`);
70 const tests = readTestsFromDir(fullPath, [...relativePath, file]);
71 return allTests.concat(tests);
72 }
73
74 if (path.extname(file) === fileExtension) {
75 console.error(`Processing file ${fullPath}`);
76 const basename = path.basename(file, fileExtension);
77 const testNamePrefix = [...relativePath, basename]
78 .join(testNameSeparator)
79 .replace('_', ' ');
80 const tests = readTestsFromFile(fullPath).map(prependToTestName(testNamePrefix));
81 return allTests.concat(tests);
82 }
83
84 return allTests;
85 }
86
87 /**
88 * Sort entries so that directories are listed after files.
89 * @param first - The first file.
90 * @param second - The second file.
91 * @returns {number}
92 */
93 function filesBeforeDirs(first, second) {
94 const firstIsDir = isDir(first);
95 const secondIsDir = isDir(second);
96 if (firstIsDir && !secondIsDir) return 1;
97 if (!firstIsDir && secondIsDir) return -1;
98 return first.localeCompare(second, 'en', {sensitivity: 'base'});
99 }
100
101 /**
102 * Check if a file is a directory.
103 * @param file {string} - The file name.
104 * @returns {boolean} - True if the file is a directory.
105 */
106 function isDir(file) {
107 return fs.statSync(path.join(dir, file)).isDirectory();
108 }
109 }
110
111 /**
112 * Read the tests from a file.
113 * @param file - The file to read.
114 * @returns {*[]}
115 */
116 function readTestsFromFile(file) {
117 const fileData = fs.readFileSync(file, 'utf8');
118 return JSON.parse(fileData).tests;
119 }
120
121 /**
122 * Prepend the prefix to the test name.
123 * @param prefix {string} - The file name.
124 * @returns {function(*): *&{name: string}} - A function that prepends the file name to the test name.
125 */
126 function prependToTestName(prefix) {
127 return (test) => {
128 return {
129 ...test, name: prefix + testNameSeparator + test.name,
130 };
131 };
132 }
133