From 8aff48f64b8a317dc68290df87ee8917f2074271 Mon Sep 17 00:00:00 2001 From: Dean Lindqvist Todevski Date: Fri, 17 Jan 2025 07:40:35 +0100 Subject: [PATCH 1/2] add option to pass args to `diff` in `diff_test` --- lib/private/BUILD.bazel | 1 + lib/private/diff_test.bzl | 10 +++++++++- lib/private/diff_test_tmpl.sh | 4 ++-- lib/private/write_source_file.bzl | 4 ++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/private/BUILD.bazel b/lib/private/BUILD.bazel index 7f8a79985..29bf9615f 100644 --- a/lib/private/BUILD.bazel +++ b/lib/private/BUILD.bazel @@ -104,6 +104,7 @@ bzl_library( deps = [ ":directory_path", "//lib:utils", + "@bazel_skylib//lib:shell", ], ) diff --git a/lib/private/diff_test.bzl b/lib/private/diff_test.bzl index e1ea14ee6..ffe5d3db3 100644 --- a/lib/private/diff_test.bzl +++ b/lib/private/diff_test.bzl @@ -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): @@ -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, ) @@ -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", @@ -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. @@ -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 file2. file2: Label of the file to compare to file1. + diff_args: Arguments to pass to the `diff` command. (Ignored on Windows) size: standard attribute for tests **kwargs: The common attributes for tests. """ @@ -123,5 +130,6 @@ def diff_test(name, file1, file2, size = "small", **kwargs): file1 = file1, file2 = file2, size = size, + diff_args = diff_args, **kwargs ) diff --git a/lib/private/diff_test_tmpl.sh b/lib/private/diff_test_tmpl.sh index 22da03806..d72f04465 100644 --- a/lib/private/diff_test_tmpl.sh +++ b/lib/private/diff_test_tmpl.sh @@ -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 diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index bfdd51da9..3d2eff624 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -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. @@ -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 @@ -177,6 +180,7 @@ To update *only* this file, run: file1 = in_file, file2 = out_file, failure_message = message, + diff_args = diff_args, **kwargs ) From 50ec9e04bc2537ae5d564314caffda6eb727a6db Mon Sep 17 00:00:00 2001 From: Dean Lindqvist Todevski Date: Fri, 17 Jan 2025 07:43:06 +0100 Subject: [PATCH 2/2] update docs --- docs/diff_test.md | 3 ++- docs/write_source_files.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/diff_test.md b/docs/diff_test.md index 1eb1dda4b..700e9bd77 100644 --- a/docs/diff_test.md +++ b/docs/diff_test.md @@ -15,7 +15,7 @@ See also: [rules_diff](https://gitlab.arm.com/bazel/rules_diff) ## diff_test
-diff_test(name, file1, file2, size, kwargs)
+diff_test(name, file1, file2, diff_args, size, kwargs)
 
A test that compares two files. @@ -31,6 +31,7 @@ The test succeeds if the files' contents match. | name | The name of the test rule. | none | | file1 | Label of the file to compare to file2. | none | | file2 | Label of the file to compare to file1. | none | +| diff_args | Arguments to pass to the `diff` command. (Ignored on Windows) | `[]` | | size | standard attribute for tests | `"small"` | | kwargs | The common attributes for tests. | none | diff --git a/docs/write_source_files.md b/docs/write_source_files.md index 2db201290..1ff20c8a6 100644 --- a/docs/write_source_files.md +++ b/docs/write_source_files.md @@ -123,7 +123,7 @@ Provider for write_source_file targets
 write_source_file(name, in_file, out_file, executable, additional_update_targets,
                   suggested_update_target, diff_test, diff_test_failure_message,
-                  file_missing_failure_message, check_that_out_file_exists, kwargs)
+                  file_missing_failure_message, diff_args, check_that_out_file_exists, kwargs)
 
Write a file or directory to the source tree. @@ -147,6 +147,7 @@ To disable the exists check and up-to-date test set `diff_test` to `False`. | diff_test | Test that the source tree file or directory exist and is up to date. | `True` | | diff_test_failure_message | Text to print when the diff test fails, with templating options for relevant targets.

Substitutions are performed on the failure message, with the following substitutions being available:

`{{DEFAULT_MESSAGE}}`: Prints the default error message, listing the target(s) that may be run to update the file(s).

`{{TARGET}}`: The target to update the individual file that does not match in the diff test.

`{{SUGGESTED_UPDATE_TARGET}}`: The suggested_update_target if specified. | `"{{DEFAULT_MESSAGE}}"` | | file_missing_failure_message | Text to print when the output file is missing. Subject to the same substitutions as diff_test_failure_message. | `"{{DEFAULT_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 for this check is limited to files in the same Bazel package. | `True` | | kwargs | Other common named parameters such as `tags` or `visibility` | none |