|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); |
| 4 | + |
| 5 | +function main(args) { |
| 6 | + const [mode, golden_no_debug, golden_debug, actual] = args; |
| 7 | + const actualPath = runfiles.resolveWorkspaceRelative(actual); |
| 8 | + const debugBuild = /\/bazel-out\/[^/\s]*-dbg\//.test(actualPath); |
| 9 | + const golden = debugBuild ? golden_debug : golden_no_debug; |
| 10 | + const actualContents = fs.readFileSync(actualPath, 'utf-8').replace(/\r\n/g, '\n'); |
| 11 | + const goldenContents = |
| 12 | + fs.readFileSync(runfiles.resolveWorkspaceRelative(golden), 'utf-8').replace(/\r\n/g, '\n'); |
| 13 | + |
| 14 | + if (actualContents === goldenContents) { |
| 15 | + return 0; |
| 16 | + } |
| 17 | + if (mode === '--out') { |
| 18 | + // Write to golden file |
| 19 | + fs.writeFileSync(runfiles.resolveWorkspaceRelative(golden), actualContents); |
| 20 | + console.error(`Replaced ${path.join(process.cwd(), golden)}`); |
| 21 | + return 0; |
| 22 | + } |
| 23 | + if (mode === '--verify') { |
| 24 | + const unidiff = require('unidiff'); |
| 25 | + // Generated does not match golden |
| 26 | + const diff = unidiff.diffLines(goldenContents, actualContents); |
| 27 | + let prettyDiff = |
| 28 | + unidiff.formatLines(diff, {aname: `[workspace]/${golden}`, bname: `[bazel-out]/${actual}`}); |
| 29 | + if (prettyDiff.length > 5000) { |
| 30 | + prettyDiff = prettyDiff.substr(0, 5000) + '/n...elided...'; |
| 31 | + } |
| 32 | + console.error(`Generated output doesn't match: |
| 33 | +
|
| 34 | +${prettyDiff} |
| 35 | +
|
| 36 | +If the bazel-out content is correct, you can update the workspace file by running: |
| 37 | +
|
| 38 | + bazel run ${debugBuild ? '--compilation_mode=dbg ' : ''}${ |
| 39 | + process.env['TEST_TARGET'].replace(/_bin$/, '')}.update |
| 40 | +`); |
| 41 | + return 1; |
| 42 | + } |
| 43 | + throw new Error('unknown mode', mode); |
| 44 | +} |
| 45 | + |
| 46 | +if (require.main === module) { |
| 47 | + process.exitCode = main(process.argv.slice(2)); |
| 48 | +} |
0 commit comments