László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 1 | # Copyright 2019 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """A test rule that compares two binary files. |
| 16 | |
| 17 | The rule uses a Bash command (diff) on Linux/macOS/non-Windows, and a cmd.exe |
| 18 | command (fc.exe) on Windows (no Bash is required). |
| 19 | """ |
| 20 | |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 21 | load("//lib:shell.bzl", "shell") |
| 22 | |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 23 | def _runfiles_path(f): |
| 24 | if f.root.path: |
| 25 | return f.path[len(f.root.path) + 1:] # generated file |
| 26 | else: |
| 27 | return f.path # source file |
| 28 | |
| 29 | def _diff_test_impl(ctx): |
| 30 | if ctx.attr.is_windows: |
| 31 | test_bin = ctx.actions.declare_file(ctx.label.name + "-test.bat") |
| 32 | ctx.actions.write( |
| 33 | output = test_bin, |
László Csomor | 58068fe | 2019-09-17 14:03:22 +0200 | [diff] [blame] | 34 | content = """@rem Generated by diff_test.bzl, do not edit. |
| 35 | @echo off |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 36 | SETLOCAL ENABLEEXTENSIONS |
| 37 | SETLOCAL ENABLEDELAYEDEXPANSION |
László Csomor | 58068fe | 2019-09-17 14:03:22 +0200 | [diff] [blame] | 38 | set MF=%RUNFILES_MANIFEST_FILE:/=\\% |
| 39 | set PATH=%SYSTEMROOT%\\system32 |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 40 | set F1={file1} |
| 41 | set F2={file2} |
| 42 | if "!F1:~0,9!" equ "external/" (set F1=!F1:~9!) else (set F1=!TEST_WORKSPACE!/!F1!) |
| 43 | if "!F2:~0,9!" equ "external/" (set F2=!F2:~9!) else (set F2=!TEST_WORKSPACE!/!F2!) |
| 44 | for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F1! " "%MF%"`) do ( |
| 45 | set RF1=%%i |
László Csomor | 58068fe | 2019-09-17 14:03:22 +0200 | [diff] [blame] | 46 | set RF1=!RF1:/=\\! |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 47 | ) |
| 48 | if "!RF1!" equ "" ( |
Alexandre Rostovtsev | 872e9b0 | 2022-07-15 13:07:09 -0400 | [diff] [blame] | 49 | if "%RUNFILES_MANIFEST_ONLY%" neq "1" if exist "%RUNFILES_DIR%\\%F1%" ( |
| 50 | set RF1="%RUNFILES_DIR%\\%F1%" |
| 51 | ) else ( |
| 52 | if exist "{file1}" ( |
| 53 | set RF1="{file1}" |
| 54 | ) |
| 55 | ) |
| 56 | if "!RF1!" neq "" ( |
Thaler Benedek | 43a545a | 2021-09-24 16:49:07 +0200 | [diff] [blame] | 57 | set RF1=!RF1:/=\\! |
| 58 | ) else ( |
| 59 | echo>&2 ERROR: !F1! not found |
| 60 | exit /b 1 |
| 61 | ) |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 62 | ) |
| 63 | for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F2! " "%MF%"`) do ( |
| 64 | set RF2=%%i |
László Csomor | 58068fe | 2019-09-17 14:03:22 +0200 | [diff] [blame] | 65 | set RF2=!RF2:/=\\! |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 66 | ) |
| 67 | if "!RF2!" equ "" ( |
Alexandre Rostovtsev | 872e9b0 | 2022-07-15 13:07:09 -0400 | [diff] [blame] | 68 | if "%RUNFILES_MANIFEST_ONLY%" neq "1" if exist "%RUNFILES_DIR%\\%F2%" ( |
| 69 | set RF2="%RUNFILES_DIR%\\%F2%" |
| 70 | ) else ( |
| 71 | if exist "{file2}" ( |
| 72 | set RF2="{file2}" |
| 73 | ) |
| 74 | ) |
| 75 | if "!RF2!" neq "" ( |
Thaler Benedek | 43a545a | 2021-09-24 16:49:07 +0200 | [diff] [blame] | 76 | set RF2=!RF2:/=\\! |
| 77 | ) else ( |
| 78 | echo>&2 ERROR: !F2! not found |
| 79 | exit /b 1 |
| 80 | ) |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 81 | ) |
| 82 | fc.exe 2>NUL 1>NUL /B "!RF1!" "!RF2!" |
| 83 | if %ERRORLEVEL% neq 0 ( |
| 84 | if %ERRORLEVEL% equ 1 ( |
Alex Eagle | df3c9e2 | 2021-08-18 08:23:43 -0700 | [diff] [blame] | 85 | echo>&2 FAIL: files "{file1}" and "{file2}" differ. {fail_msg} |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 86 | exit /b 1 |
| 87 | ) else ( |
| 88 | fc.exe /B "!RF1!" "!RF2!" |
| 89 | exit /b %errorlevel% |
| 90 | ) |
| 91 | ) |
| 92 | """.format( |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 93 | # TODO(arostovtsev): use shell.escape_for_bat when https://github.com/bazelbuild/bazel-skylib/pull/363 is merged |
Alex Eagle | df3c9e2 | 2021-08-18 08:23:43 -0700 | [diff] [blame] | 94 | fail_msg = ctx.attr.failure_message, |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 95 | file1 = _runfiles_path(ctx.file.file1), |
| 96 | file2 = _runfiles_path(ctx.file.file2), |
| 97 | ), |
| 98 | is_executable = True, |
| 99 | ) |
| 100 | else: |
| 101 | test_bin = ctx.actions.declare_file(ctx.label.name + "-test.sh") |
| 102 | ctx.actions.write( |
| 103 | output = test_bin, |
Yesudeep Mangalapilly | 8e923ca | 2021-10-25 06:12:41 -0700 | [diff] [blame] | 104 | content = r"""#!/usr/bin/env bash |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 105 | set -euo pipefail |
| 106 | F1="{file1}" |
| 107 | F2="{file2}" |
Robbert van Ginkel | feb5296 | 2020-04-14 10:04:34 -0700 | [diff] [blame] | 108 | [[ "$F1" =~ ^external/* ]] && F1="${{F1#external/}}" || F1="$TEST_WORKSPACE/$F1" |
| 109 | [[ "$F2" =~ ^external/* ]] && F2="${{F2#external/}}" || F2="$TEST_WORKSPACE/$F2" |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 110 | if [[ -d "${{RUNFILES_DIR:-/dev/null}}" && "${{RUNFILES_MANIFEST_ONLY:-}}" != 1 ]]; then |
| 111 | RF1="$RUNFILES_DIR/$F1" |
| 112 | RF2="$RUNFILES_DIR/$F2" |
| 113 | elif [[ -f "${{RUNFILES_MANIFEST_FILE:-/dev/null}}" ]]; then |
| 114 | RF1="$(grep -F -m1 "$F1 " "$RUNFILES_MANIFEST_FILE" | sed 's/^[^ ]* //')" |
| 115 | RF2="$(grep -F -m1 "$F2 " "$RUNFILES_MANIFEST_FILE" | sed 's/^[^ ]* //')" |
c-parsons | 84a12d1 | 2019-05-07 16:25:43 -0400 | [diff] [blame] | 116 | elif [[ -f "$TEST_SRCDIR/$F1" && -f "$TEST_SRCDIR/$F2" ]]; then |
| 117 | RF1="$TEST_SRCDIR/$F1" |
| 118 | RF2="$TEST_SRCDIR/$F2" |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 119 | else |
| 120 | echo >&2 "ERROR: could not find \"{file1}\" and \"{file2}\"" |
| 121 | exit 1 |
| 122 | fi |
| 123 | if ! diff "$RF1" "$RF2"; then |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 124 | echo >&2 "FAIL: files \"{file1}\" and \"{file2}\" differ. "{fail_msg} |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 125 | exit 1 |
| 126 | fi |
| 127 | """.format( |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 128 | fail_msg = shell.quote(ctx.attr.failure_message), |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 129 | file1 = _runfiles_path(ctx.file.file1), |
| 130 | file2 = _runfiles_path(ctx.file.file2), |
| 131 | ), |
| 132 | is_executable = True, |
| 133 | ) |
| 134 | return DefaultInfo( |
| 135 | executable = test_bin, |
| 136 | files = depset(direct = [test_bin]), |
| 137 | runfiles = ctx.runfiles(files = [test_bin, ctx.file.file1, ctx.file.file2]), |
| 138 | ) |
| 139 | |
| 140 | _diff_test = rule( |
| 141 | attrs = { |
Alex Eagle | df3c9e2 | 2021-08-18 08:23:43 -0700 | [diff] [blame] | 142 | "failure_message": attr.string(), |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 143 | "file1": attr.label( |
| 144 | allow_single_file = True, |
| 145 | mandatory = True, |
| 146 | ), |
| 147 | "file2": attr.label( |
| 148 | allow_single_file = True, |
| 149 | mandatory = True, |
| 150 | ), |
| 151 | "is_windows": attr.bool(mandatory = True), |
| 152 | }, |
| 153 | test = True, |
| 154 | implementation = _diff_test_impl, |
| 155 | ) |
| 156 | |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 157 | def diff_test(name, file1, file2, failure_message = None, **kwargs): |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 158 | """A test that compares two files. |
| 159 | |
| 160 | The test succeeds if the files' contents match. |
| 161 | |
| 162 | Args: |
| 163 | name: The name of the test rule. |
Alexandre Rostovtsev | bc112d4 | 2022-10-03 05:13:46 -0400 | [diff] [blame^] | 164 | file1: Label of the file to compare to `file2`. |
| 165 | file2: Label of the file to compare to `file1`. |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 166 | failure_message: Additional message to log if the files' contents do not match. |
Alexandre Rostovtsev | bc112d4 | 2022-10-03 05:13:46 -0400 | [diff] [blame^] | 167 | **kwargs: The [common attributes for tests](https://bazel.build/reference/be/common-definitions#common-attributes-tests). |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 168 | """ |
| 169 | _diff_test( |
| 170 | name = name, |
| 171 | file1 = file1, |
| 172 | file2 = file2, |
Alexandre Rostovtsev | de3035d | 2022-04-06 15:16:14 -0400 | [diff] [blame] | 173 | failure_message = failure_message, |
László Csomor | be3b1fc | 2019-04-12 19:35:29 +0200 | [diff] [blame] | 174 | is_windows = select({ |
| 175 | "@bazel_tools//src/conditions:host_windows": True, |
| 176 | "//conditions:default": False, |
| 177 | }), |
| 178 | **kwargs |
| 179 | ) |