• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2
3# Copyright 2019 The Bazel Authors. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#    http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# --- begin runfiles.bash initialization ---
18# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
19set -euo pipefail
20if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
21  if [[ -f "$0.runfiles_manifest" ]]; then
22    export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
23  elif [[ -f "$0.runfiles/MANIFEST" ]]; then
24    export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
25  elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
26    export RUNFILES_DIR="$0.runfiles"
27  fi
28fi
29if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
30  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
31elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
32  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
33            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
34else
35  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
36  exit 1
37fi
38# --- end runfiles.bash initialization ---
39
40source "$(rlocation bazel_skylib/tests/unittest.bash)" \
41  || { echo "Could not source bazel_skylib/tests/unittest.bash" >&2; exit 1; }
42
43function assert_empty_file() {
44  local -r path="$1"
45  # Not using 'du' to check the file is empty, because it doesn't work on CI.
46  [[ "$(echo -n "($(cat "$path"))")" = "()" ]]
47}
48
49function test_write_empty_text() {
50  assert_empty_file "$(rlocation bazel_skylib/tests/write_file/out/empty.txt)"
51}
52
53function test_write_nonempty_text() {
54  cat "$(rlocation bazel_skylib/tests/write_file/out/nonempty.txt)" >"$TEST_log"
55  expect_log '^aaa'
56  expect_log '^bbb'
57}
58
59function test_write_empty_bin() {
60  assert_empty_file "$(rlocation bazel_skylib/tests/write_file/empty-bin-out.txt)"
61}
62
63function test_write_nonempty_bin() {
64  cat "$(rlocation bazel_skylib/tests/write_file/nonempty-bin-out.txt)" >"$TEST_log"
65  expect_log '^potato'
66}
67
68run_suite "write_file_tests test suite"
69