| 1 |
|
| 2 |
|
| 3 |
const fs = require('fs'); |
| 4 |
const path = require('path'); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
const fileExtension = '.json'; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
const jsonIndent = 2; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
const testNameSeparator = ', '; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
const description = 'JSONPath Compliance Test Suite. This file is autogenerated, do not edit.'; |
| 26 |
|
| 27 |
build(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
function readTestsFromDir(dir, relativePath = []) { |
| 55 |
const files = fs.readdirSync(dir); |
| 56 |
files.sort(filesBeforeDirs); |
| 57 |
return files.reduce(addTestsFromFile, []); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
function isDir(file) { |
| 107 |
return fs.statSync(path.join(dir, file)).isDirectory(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
function readTestsFromFile(file) { |
| 117 |
const fileData = fs.readFileSync(file, 'utf8'); |
| 118 |
return JSON.parse(fileData).tests; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
function prependToTestName(prefix) { |
| 127 |
return (test) => { |
| 128 |
return { |
| 129 |
...test, name: prefix + testNameSeparator + test.name, |
| 130 |
}; |
| 131 |
}; |
| 132 |
} |
| 133 |
|