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# --- begin runfiles.bash initialization --- 16# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). 17set -euo pipefail 18if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 19 if [[ -f "$0.runfiles_manifest" ]]; then 20 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest" 21 elif [[ -f "$0.runfiles/MANIFEST" ]]; then 22 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST" 23 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 24 export RUNFILES_DIR="$0.runfiles" 25 fi 26fi 27if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then 28 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" 29elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then 30 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \ 31 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)" 32else 33 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" 34 exit 1 35fi 36# --- end runfiles.bash initialization --- 37 38source "$(rlocation bazel_skylib/tests/unittest.bash)" \ 39 || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; } 40 41function assert_empty_file() { 42 local -r path="$1" 43 # Not using 'du' to check the file is empty, because it doesn't work on CI. 44 [[ "$(echo -n "($(cat "$path"))")" = "()" ]] 45} 46 47function test_write_empty_text() { 48 assert_empty_file "$(rlocation bazel_skylib/tests/write_file/out/empty.txt)" 49} 50 51function test_write_nonempty_text() { 52 cat "$(rlocation bazel_skylib/tests/write_file/out/nonempty.txt)" >"$TEST_log" 53 expect_log '^aaa$' 54 expect_log '^bbb$' 55} 56 57function test_write_empty_bin() { 58 assert_empty_file "$(rlocation bazel_skylib/tests/write_file/empty-bin-out.txt)" 59} 60 61function test_write_nonempty_bin() { 62 cat "$(rlocation bazel_skylib/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log" 63 expect_log '^potato$' 64} 65 66run_suite "write_file_tests test suite" 67