Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option to pass args to diff command in diff_test on Linux/MacOS #1032

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/diff_test.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/write_source_files.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bzl_library(
deps = [
":directory_path",
"//lib:utils",
"@bazel_skylib//lib:shell",
],
)

Expand Down
10 changes: 9 additions & 1 deletion lib/private/diff_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe
command (fc.exe) on Windows (no Bash is required).
"""

load("@bazel_skylib//lib:shell.bzl", "shell")
load(":directory_path.bzl", "DirectoryPathInfo")

def _runfiles_path(f):
Expand Down Expand Up @@ -71,6 +72,10 @@ def _diff_test_impl(ctx):
"{file1}": file1_path,
"{file2}": file2_path,
"{build_file_path}": ctx.build_file_path,
"{diff_args}": " ".join([
shell.quote(arg)
for arg in ctx.attr.diff_args
]),
},
is_executable = True,
)
Expand All @@ -92,6 +97,7 @@ _diff_test = rule(
allow_files = True,
mandatory = True,
),
"diff_args": attr.string_list(),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
"_diff_test_tmpl_sh": attr.label(
default = ":diff_test_tmpl.sh",
Expand All @@ -106,7 +112,7 @@ _diff_test = rule(
implementation = _diff_test_impl,
)

def diff_test(name, file1, file2, size = "small", **kwargs):
def diff_test(name, file1, file2, diff_args = [], size = "small", **kwargs):
"""A test that compares two files.

The test succeeds if the files' contents match.
Expand All @@ -115,6 +121,7 @@ def diff_test(name, file1, file2, size = "small", **kwargs):
name: The name of the test rule.
file1: Label of the file to compare to <code>file2</code>.
file2: Label of the file to compare to <code>file1</code>.
diff_args: Arguments to pass to the `diff` command. (Ignored on Windows)
size: standard attribute for tests
**kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
"""
Expand All @@ -123,5 +130,6 @@ def diff_test(name, file1, file2, size = "small", **kwargs):
file1 = file1,
file2 = file2,
size = size,
diff_args = diff_args,
**kwargs
)
4 changes: 2 additions & 2 deletions lib/private/diff_test_tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ if [[ ! "$DF1" ]] && [[ "$DF2" ]]; then
exit 1
fi
if [[ "$DF1" ]] || [[ "$DF2" ]]; then
if ! diff -r "$RF1" "$RF2"; then
if ! diff {diff_args} -r "$RF1" "$RF2"; then
fail "directories \"{file1}\" and \"{file2}\" differ. {fail_msg}"
fi
else
if ! diff "$RF1" "$RF2"; then
if ! diff {diff_args} "$RF1" "$RF2"; then
fail "files \"{file1}\" and \"{file2}\" differ. {fail_msg}"
fi
fi
4 changes: 4 additions & 0 deletions lib/private/write_source_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def write_source_file(
diff_test = True,
diff_test_failure_message = "{{DEFAULT_MESSAGE}}",
file_missing_failure_message = "{{DEFAULT_MESSAGE}}",
diff_args = [],
check_that_out_file_exists = True,
**kwargs):
"""Write a file or directory to the source tree.
Expand Down Expand Up @@ -66,6 +67,8 @@ def write_source_file(
file_missing_failure_message: Text to print when the output file is missing. Subject to the same
substitutions as diff_test_failure_message.

diff_args: Arguments to pass to the `diff` command. (Ignored on Windows)

check_that_out_file_exists: Test that the output file exists and print a helpful error message if it doesn't.

If `True`, the output file or directory must be in the same containing Bazel package as the target since the underlying mechanism
Expand Down Expand Up @@ -177,6 +180,7 @@ To update *only* this file, run:
file1 = in_file,
file2 = out_file,
failure_message = message,
diff_args = diff_args,
**kwargs
)

Expand Down
Loading