diff options
Diffstat (limited to 'rules/diff_test.bzl')
-rw-r--r-- | rules/diff_test.bzl | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/rules/diff_test.bzl b/rules/diff_test.bzl index 49a1968..0f27f35 100644 --- a/rules/diff_test.bzl +++ b/rules/diff_test.bzl @@ -18,6 +18,8 @@ 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("//lib:shell.bzl", "shell") + def _runfiles_path(f): if f.root.path: return f.path[len(f.root.path) + 1:] # generated file @@ -44,8 +46,14 @@ for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F1! " "%MF%"`) do ( set RF1=!RF1:/=\\! ) if "!RF1!" equ "" ( - if exist "{file1}" ( - set RF1="{file1}" + if "%RUNFILES_MANIFEST_ONLY%" neq "1" if exist "%RUNFILES_DIR%\\%F1%" ( + set RF1="%RUNFILES_DIR%\\%F1%" + ) else ( + if exist "{file1}" ( + set RF1="{file1}" + ) + ) + if "!RF1!" neq "" ( set RF1=!RF1:/=\\! ) else ( echo>&2 ERROR: !F1! not found @@ -57,8 +65,14 @@ for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F2! " "%MF%"`) do ( set RF2=!RF2:/=\\! ) if "!RF2!" equ "" ( - if exist "{file2}" ( - set RF2="{file2}" + if "%RUNFILES_MANIFEST_ONLY%" neq "1" if exist "%RUNFILES_DIR%\\%F2%" ( + set RF2="%RUNFILES_DIR%\\%F2%" + ) else ( + if exist "{file2}" ( + set RF2="{file2}" + ) + ) + if "!RF2!" neq "" ( set RF2=!RF2:/=\\! ) else ( echo>&2 ERROR: !F2! not found @@ -76,6 +90,7 @@ if %ERRORLEVEL% neq 0 ( ) ) """.format( + # TODO(arostovtsev): use shell.escape_for_bat when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged fail_msg = ctx.attr.failure_message, file1 = _runfiles_path(ctx.file.file1), file2 = _runfiles_path(ctx.file.file2), @@ -106,11 +121,11 @@ else exit 1 fi if ! diff "$RF1" "$RF2"; then - echo >&2 "FAIL: files \"{file1}\" and \"{file2}\" differ. {fail_msg}" + echo >&2 "FAIL: files \"{file1}\" and \"{file2}\" differ. "{fail_msg} exit 1 fi """.format( - fail_msg = ctx.attr.failure_message, + fail_msg = shell.quote(ctx.attr.failure_message), file1 = _runfiles_path(ctx.file.file1), file2 = _runfiles_path(ctx.file.file2), ), @@ -139,21 +154,23 @@ _diff_test = rule( implementation = _diff_test_impl, ) -def diff_test(name, file1, file2, **kwargs): +def diff_test(name, file1, file2, failure_message = None, **kwargs): """A test that compares two files. The test succeeds if the files' contents match. Args: 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>. - **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>. + file1: Label of the file to compare to `file2`. + file2: Label of the file to compare to `file1`. + failure_message: Additional message to log if the files' contents do not match. + **kwargs: The [common attributes for tests](https://bazel.build/reference/be/common-definitions#common-attributes-tests). """ _diff_test( name = name, file1 = file1, file2 = file2, + failure_message = failure_message, is_windows = select({ "@bazel_tools//src/conditions:host_windows": True, "//conditions:default": False, |