aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/runfiles/runfiles_posix_test.sh
blob: 9a90661b2646584f5950af3a75ea75f4991db0d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
#
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

function _log_base() {
  prefix=$1
  shift
  echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $*"
}

function log_fatal() {
  _log_base "ERROR" "$@"
  exit 1
}

function fail() {
  _log_base "FAILED" "$@"
  exit 1
}

stat "$0" >&/dev/null || log_fatal "cannot locate GNU coreutils"

# Unset existing definitions of the functions we want to test.
if type rlocation >&/dev/null; then
  unset is_absolute
  unset is_windows
  unset rlocation
fi
if rlocation >&/dev/null; then
  fail "rlocation is still defined"
fi

# Find runfiles.sh
runfiles_sh=$(dirname $0)/runfiles.sh
[[ -e "$runfiles_sh" ]] || fail "cannot find '$runfiles_sh'"

# Assert that runfiles.sh attempts to look up the runfiles directory.
# It will find the actual runfiles directory of this test.
unset RUNFILES_DIR
source "$runfiles_sh" || fail "cannot source '$runfiles_sh'"
[[ "$RUNFILES_DIR" = *.runfiles ]] \
  || fail "'$runfiles_sh' cannot find the runfiles directory"

# Set a mock $RUNFILES_DIR.
# Unset `rlocation` so runfiles.sh will define it again.
export RUNFILES_DIR="/path/to runfiles"
unset is_absolute
source "$runfiles_sh" || fail "cannot source '$runfiles_sh'"

# Exercise the functions in runfiles.sh.
if is_windows; then
  fail "expected is_windows() to be false"
fi

if is_absolute "d:/foo"; then
  fail "expected d:/foo not to be absolute"
fi
if is_absolute "D:\\foo"; then
  fail "expected D:\\foo not to be absolute"
fi
is_absolute "/foo" || fail "expected /foo to be absolute"

[[ "$(rlocation "some/runfile")" = "/path/to runfiles/some/runfile" ]] \
  || fail "rlocation 1 failed"
[[ "$(rlocation "/some absolute/runfile")" = "/some absolute/runfile" ]] \
  || fail "rlocation 2 failed"