-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a new public API for checking in golden or snapshot files. If you used the private golden_file_test previously, you need to rename actual to generated, golden to src, and golden_debug to src_dbg Fixes #1893
- Loading branch information
Showing
26 changed files
with
197 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
bzl_library( | ||
name = "bzl", | ||
srcs = glob(["*.bzl"]), | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
exports_files(["bin.js"]) | ||
|
||
filegroup( | ||
name = "package_contents", | ||
srcs = glob(["*"]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); | ||
|
||
function main(args) { | ||
const [mode, golden_no_debug, golden_debug, actual] = args; | ||
const actualPath = runfiles.resolveWorkspaceRelative(actual); | ||
const debugBuild = /\/bazel-out\/[^/\s]*-dbg\//.test(actualPath); | ||
const golden = debugBuild ? golden_debug : golden_no_debug; | ||
const actualContents = fs.readFileSync(actualPath, 'utf-8').replace(/\r\n/g, '\n'); | ||
const goldenContents = | ||
fs.readFileSync(runfiles.resolveWorkspaceRelative(golden), 'utf-8').replace(/\r\n/g, '\n'); | ||
|
||
if (actualContents === goldenContents) { | ||
return 0; | ||
} | ||
if (mode === '--out') { | ||
// Write to golden file | ||
fs.writeFileSync(runfiles.resolveWorkspaceRelative(golden), actualContents); | ||
console.error(`Replaced ${path.join(process.cwd(), golden)}`); | ||
return 0; | ||
} | ||
if (mode === '--verify') { | ||
const unidiff = require('unidiff'); | ||
// Generated does not match golden | ||
const diff = unidiff.diffLines(goldenContents, actualContents); | ||
let prettyDiff = | ||
unidiff.formatLines(diff, {aname: `[workspace]/${golden}`, bname: `[bazel-out]/${actual}`}); | ||
if (prettyDiff.length > 5000) { | ||
prettyDiff = prettyDiff.substr(0, 5000) + '/n...elided...'; | ||
} | ||
console.error(`Generated output doesn't match: | ||
${prettyDiff} | ||
If the bazel-out content is correct, you can update the workspace file by running: | ||
bazel run ${debugBuild ? '--compilation_mode=dbg ' : ''}${ | ||
process.env['TEST_TARGET'].replace(/_bin$/, '')}.update | ||
`); | ||
return 1; | ||
} | ||
throw new Error('unknown mode', mode); | ||
} | ||
|
||
if (require.main === module) { | ||
process.exitCode = main(process.argv.slice(2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"Convenience for testing that an output matches a file" | ||
|
||
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary", "nodejs_test") | ||
|
||
def generated_file_test(name, generated, src, src_dbg = None, **kwargs): | ||
"""Tests that a file generated by Bazel has identical content to a file in the workspace. | ||
This is useful for testing, where a "snapshot" or "golden" file is checked in, | ||
so that you can code review changes to the generated output. | ||
Args: | ||
name: Name of the rule. | ||
generated: a Label of the output file generated by another rule | ||
src: Label of the source file in the workspace | ||
src_dbg: if the build uses `--compilation_mode dbg` then some rules will produce different output. | ||
In this case you can specify what the dbg version of the output should look like | ||
**kwargs: extra arguments passed to the underlying nodejs_test or nodejs_binary | ||
""" | ||
data = [src, generated, "@npm//unidiff"] | ||
|
||
if src_dbg: | ||
data.append(src_dbg) | ||
else: | ||
src_dbg = src | ||
|
||
loc = "$(rootpath %s)" | ||
nodejs_test( | ||
name = name, | ||
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bin.js", | ||
templated_args = ["--verify", loc % src, loc % src_dbg, loc % generated], | ||
data = data, | ||
**kwargs | ||
) | ||
|
||
nodejs_binary( | ||
name = name + ".update", | ||
testonly = True, | ||
entry_point = "@build_bazel_rules_nodejs//internal/generated_file_test:bin.js", | ||
templated_args = ["--out", loc % src, loc % src_dbg, loc % generated], | ||
data = data, | ||
**kwargs | ||
) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.