github.com/bazelbuild/rules_go@v0.47.2-0.20240515105122-e7ddb9ea474e/go/private/tools/files_equal_test.bzl (about)

     1  # Copyright 2016 The Bazel Go Rules Authors. All rights reserved.
     2  # Copyright 2016 The Closure Rules Authors. All rights reserved.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #    http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  """Tests that two files contain the same data."""
    17  
    18  def files_equal_test(name, golden, actual, error_message = None, **kwargs):
    19      # This genrule creates a Bash script: the source of the actual test.
    20      # The script:
    21      #   1. Initializes the Bash runfiles library (see
    22      #      @bazel_tools//tools/bash/runfiles/runfiles.bash).
    23      #   2. Stores command line arguments into variables.
    24      #   3. Computes runfile paths for the GOLDEN and ACTUAL files.
    25      #   4. Calls "rlocation" from runfiles.bash to locates the runfiles.
    26      #   5. Computes and compares checksums.
    27      native.genrule(
    28          name = name + "_src",
    29          outs = [name + "-src.sh"],
    30          executable = True,
    31          visibility = ["//visibility:private"],
    32          cmd = r"""cat >$@ <<'eof'
    33  #!/usr/bin/env bash
    34  # sh_test() source, generated by @io_bazel_rules_go//go/private/tools/files_equal_test.bzl
    35  
    36  ### 1. initialize the Bash runfiles library
    37  
    38  # --- begin runfiles.bash initialization ---
    39  # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
    40  set -euo pipefail
    41  if [[ ! -d "$${RUNFILES_DIR:-/dev/null}" && ! -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
    42    if [[ -f "$$0.runfiles_manifest" ]]; then
    43      export RUNFILES_MANIFEST_FILE="$$0.runfiles_manifest"
    44    elif [[ -f "$$0.runfiles/MANIFEST" ]]; then
    45      export RUNFILES_MANIFEST_FILE="$$0.runfiles/MANIFEST"
    46    elif [[ -f "$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    47      export RUNFILES_DIR="$$0.runfiles"
    48    fi
    49  fi
    50  if [[ -f "$${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
    51    source "$${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
    52  elif [[ -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
    53    source "$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
    54              "$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
    55  else
    56    echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
    57    exit 1
    58  fi
    59  # --- end runfiles.bash initialization ---
    60  
    61  ### 2. Store command line arguments into variables.
    62  
    63  declare -r GOLDEN="$${1}"
    64  declare -r ACTUAL="$${2}"
    65  declare -r ERROR_MSG="$${3:-FILES DO NOT HAVE EQUAL CONTENTS}"
    66  
    67  ### 3. Compute runfile paths.
    68  
    69  # Strip "external/" prefix OR prepend workspace name and strip "./" prefix.
    70  [[ "$$GOLDEN" =~ external/* ]] && F1="$${GOLDEN#external/}" || F1="$$TEST_WORKSPACE/$${GOLDEN#./}"
    71  [[ "$$ACTUAL" =~ external/* ]] && F2="$${ACTUAL#external/}" || F2="$$TEST_WORKSPACE/$${ACTUAL#./}"
    72  
    73  ### 4. Locate the runfiles.
    74  
    75  F1="$$(rlocation "$$F1")"
    76  F2="$$(rlocation "$$F2")"
    77  
    78  if [[ "$$F1" == "$$F2" ]]; then
    79    echo >&2 "GOLDEN and ACTUAL should be different files"
    80    exit 1
    81  fi
    82  
    83  ### 5. Compute and compare checksums.
    84  
    85  function checksum() {
    86    if command -v openssl >/dev/null; then
    87      openssl sha1 $$1 | cut -f 2 -d ' '
    88    elif command -v sha256sum >/dev/null; then
    89      sha256sum $$1 | cut -f 1 -d ' '
    90    elif command -v shasum >/dev/null; then
    91      cat $$1 | shasum -a 256 | cut -f 1 -d ' '
    92    else
    93      echo please install openssl >&2
    94      exit 1
    95    fi
    96  }
    97  SUM1=$$(checksum "$$F1")
    98  SUM2=$$(checksum "$$F2")
    99  if [[ $${SUM1} != $${SUM2} ]]; then
   100    echo "ERROR: $$ERROR_MSG" >&2
   101    echo "$$GOLDEN $${SUM1}" >&2
   102    echo "$$ACTUAL $${SUM2}" >&2
   103    exit 1
   104  fi
   105  eof""",
   106      )
   107  
   108      native.sh_test(
   109          name = name,
   110          srcs = [name + "-src.sh"],
   111          data = [
   112              "@bazel_tools//tools/bash/runfiles",
   113              actual,
   114              golden,
   115          ],
   116          args = [
   117              "$(location %s)" % golden,
   118              "$(location %s)" % actual,
   119              error_message,
   120          ],
   121          **kwargs
   122      )