| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.FixtureResult = void 0; |
| 4 |
const assert = require("assert"); |
| 5 |
const buffer_1 = require("buffer"); |
| 6 |
const child_process_1 = require("child_process"); |
| 7 |
const path = require("path"); |
| 8 |
class FixtureResult { |
| 9 |
constructor(executables, maxParallel) { |
| 10 |
this.executables = executables; |
| 11 |
this.maxParallel = maxParallel; |
| 12 |
} |
| 13 |
async check(input, expected, options = {}) { |
| 14 |
const ranges = []; |
| 15 |
const maxParallel = this.maxParallel; |
| 16 |
const rawLength = buffer_1.Buffer.byteLength(input); |
| 17 |
const len = Math.ceil(rawLength / maxParallel); |
| 18 |
if (options.noScan === true) { |
| 19 |
ranges.push({ from: rawLength, to: rawLength + 1 }); |
| 20 |
} |
| 21 |
else if (options.scan) { |
| 22 |
ranges.push({ from: options.scan, to: options.scan + 1 }); |
| 23 |
} |
| 24 |
else { |
| 25 |
for (let i = 1; i <= rawLength; i += len) { |
| 26 |
ranges.push({ |
| 27 |
from: i, |
| 28 |
to: Math.min(i + len, rawLength + 1), |
| 29 |
}); |
| 30 |
} |
| 31 |
} |
| 32 |
await Promise.all(ranges.map(async (range) => { |
| 33 |
for (const single of await this.spawn(range, input)) { |
| 34 |
for (const [index, output] of single.outputs.entries()) { |
| 35 |
this.checkScan(single.name, index + 1, output, expected); |
| 36 |
} |
| 37 |
} |
| 38 |
})); |
| 39 |
} |
| 40 |
async spawn(range, input) { |
| 41 |
return await Promise.all(this.executables.map((executable) => { |
| 42 |
return this.spawnSingle(executable, range, input); |
| 43 |
})); |
| 44 |
} |
| 45 |
async spawnSingle(executable, range, input) { |
| 46 |
const name = path.basename(executable); |
| 47 |
const proc = child_process_1.spawn(executable, [ |
| 48 |
`${range.from}:${range.to}`, |
| 49 |
input, |
| 50 |
], { |
| 51 |
shell: process.platform === 'win32', |
| 52 |
stdio: [null, 'pipe', 'pipe'], |
| 53 |
}); |
| 54 |
const stdout = []; |
| 55 |
proc.stdout.on('data', (chunk) => stdout.push(chunk)); |
| 56 |
const stderr = []; |
| 57 |
proc.stderr.on('data', (chunk) => stderr.push(chunk)); |
| 58 |
const onEnd = new Promise((resolve) => proc.stdout.once('end', () => resolve())); |
| 59 |
const { code, signal } = await new Promise((resolve) => { |
| 60 |
proc.once('exit', (exitCode, exitSignal) => { |
| 61 |
resolve({ code: exitCode, signal: exitSignal }); |
| 62 |
}); |
| 63 |
}); |
| 64 |
await onEnd; |
| 65 |
const stdoutText = buffer_1.Buffer.concat(stdout).toString(); |
| 66 |
const stderrText = buffer_1.Buffer.concat(stderr).toString(); |
| 67 |
const stdOutErr = `stdout: ${stdoutText}\nstderr: ${stderrText}`; |
| 68 |
if (signal) { |
| 69 |
throw new Error(`Test "${name}" killed with signal: "${signal}".\n${stdOutErr}`); |
| 70 |
} |
| 71 |
if (code !== 0) { |
| 72 |
throw new Error(`Test "${name}" exited with code: "${code}".\n${stdOutErr}`); |
| 73 |
} |
| 74 |
const out = stdoutText.split(/===== SCAN \d+ START =====\n/g).slice(1); |
| 75 |
return { |
| 76 |
name, |
| 77 |
outputs: out.map((part) => this.normalizeSpans(part)), |
| 78 |
}; |
| 79 |
} |
| 80 |
checkScan(name, scan, actual, expected) { |
| 81 |
if (typeof expected === 'string') { |
| 82 |
assert.strictEqual(actual, expected, `Executable: ${name}\n` + |
| 83 |
`Scan value: ${scan}`); |
| 84 |
return; |
| 85 |
} |
| 86 |
if (expected instanceof RegExp) { |
| 87 |
expected.lastIndex = 0; |
| 88 |
assert.ok(expected.test(actual), `Executable: ${name}\n` + |
| 89 |
`Scan value: ${scan}\n` + |
| 90 |
` got : ${JSON.stringify(actual)}\n` + |
| 91 |
` against : ${expected}`); |
| 92 |
return; |
| 93 |
} |
| 94 |
assert(Array.isArray(expected) && |
| 95 |
expected.every((line) => { |
| 96 |
return typeof line === 'string' || line instanceof RegExp; |
| 97 |
}), '`expected` must be a string, RegExp, or Array[String|RegExp]'); |
| 98 |
const lines = actual.split('\n'); |
| 99 |
while (lines.length && lines[lines.length - 1]) { |
| 100 |
lines.pop(); |
| 101 |
} |
| 102 |
|
| 103 |
while (lines.length < expected.length) { |
| 104 |
lines.push(''); |
| 105 |
} |
| 106 |
|
| 107 |
const expectedArr = expected.slice(); |
| 108 |
while (expectedArr.length < lines.length) { |
| 109 |
expectedArr.push(/$^/); |
| 110 |
} |
| 111 |
lines.forEach((line, lineNum) => { |
| 112 |
const expectedLine = expectedArr[lineNum]; |
| 113 |
if (typeof expectedLine === 'string') { |
| 114 |
assert.strictEqual(line, expectedLine, `Executable: ${name}\n` + |
| 115 |
`Scan value: ${scan} at line: ${lineNum + 1}\n` + |
| 116 |
` output : ${lines.join('\n')}`); |
| 117 |
return; |
| 118 |
} |
| 119 |
expectedLine.lastIndex = 0; |
| 120 |
assert.ok(expectedLine.test(line), `Executable: ${name}\n` + |
| 121 |
`Scan value: ${scan} at line: ${lineNum + 1}\n` + |
| 122 |
` got : ${JSON.stringify(line)}\n` + |
| 123 |
` against : ${expectedLine}\n` + |
| 124 |
` output : ${lines.join('\n')}`); |
| 125 |
}); |
| 126 |
} |
| 127 |
normalizeSpans(source) { |
| 128 |
const lines = source.split(/\n/g); |
| 129 |
const parse = (line) => { |
| 130 |
const match = line.match(/^off=(\d+)\s+len=(\d+)\s+span\[([^\]]+)\]="(.*)"$/); |
| 131 |
if (!match) { |
| 132 |
return { type: 'raw', value: line }; |
| 133 |
} |
| 134 |
return { |
| 135 |
len: parseInt(match[2], 10), |
| 136 |
off: parseInt(match[1], 10), |
| 137 |
span: match[3], |
| 138 |
type: 'span', |
| 139 |
value: match[4], |
| 140 |
}; |
| 141 |
}; |
| 142 |
const parsed = lines.filter((l) => l).map(parse); |
| 143 |
const lastMap = new Map(); |
| 144 |
const res = []; |
| 145 |
parsed.forEach((obj) => { |
| 146 |
if (obj.type === 'raw') { |
| 147 |
res.push(obj); |
| 148 |
return; |
| 149 |
} |
| 150 |
if (lastMap.has(obj.span)) { |
| 151 |
const last = lastMap.get(obj.span); |
| 152 |
if (last.off + last.len === obj.off) { |
| 153 |
last.len += obj.len; |
| 154 |
last.value += obj.value; |
| 155 |
|
| 156 |
res.splice(res.indexOf(last), 1); |
| 157 |
res.push(last); |
| 158 |
return; |
| 159 |
} |
| 160 |
} |
| 161 |
res.push(obj); |
| 162 |
lastMap.set(obj.span, obj); |
| 163 |
}); |
| 164 |
const stringify = (obj) => { |
| 165 |
if (obj.type === 'raw') { |
| 166 |
return obj.value; |
| 167 |
} |
| 168 |
return `off=${obj.off} len=${obj.len} span[${obj.span}]="${obj.value}"`; |
| 169 |
}; |
| 170 |
return res.map(stringify).join('\n') + '\n'; |
| 171 |
} |
| 172 |
} |
| 173 |
exports.FixtureResult = FixtureResult; |
| 174 |
|