blob: 9617320ab21c322aacf9e7e4ec49c25144043fc5 [file] [log] [blame]
Laszlo Csomora7f202c2018-04-26 07:39:41 -07001#!/bin/bash
2#
3# Copyright 2016 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# This test exercises Bash utility implementations.
18
Laszlo Csomora7f202c2018-04-26 07:39:41 -070019# --- begin runfiles.bash initialization ---
laszlocsomor64560dd2018-07-27 07:04:22 -070020set -euo pipefail
Laszlo Csomora7f202c2018-04-26 07:39:41 -070021if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
laszlocsomor64560dd2018-07-27 07:04:22 -070022 if [[ -f "$0.runfiles_manifest" ]]; then
23 export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
24 elif [[ -f "$0.runfiles/MANIFEST" ]]; then
25 export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
26 elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
27 export RUNFILES_DIR="$0.runfiles"
28 fi
Laszlo Csomora7f202c2018-04-26 07:39:41 -070029fi
laszlocsomor64560dd2018-07-27 07:04:22 -070030if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
31 source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
Laszlo Csomora7f202c2018-04-26 07:39:41 -070032elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
laszlocsomor64560dd2018-07-27 07:04:22 -070033 source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
Laszlo Csomora7f202c2018-04-26 07:39:41 -070034 "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
35else
laszlocsomor64560dd2018-07-27 07:04:22 -070036 echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
Laszlo Csomora7f202c2018-04-26 07:39:41 -070037 exit 1
38fi
39# --- end runfiles.bash initialization ---
40
laszlocsomor64560dd2018-07-27 07:04:22 -070041source "$(rlocation "io_bazel/src/test/shell/unittest.bash")" \
42 || { echo "Could not source unittest.bash" >&2; exit 1; }
43source "$(rlocation "io_bazel/src/test/shell/shell_utils.sh")" \
44 || { echo "Could not source shell_utils.sh" >&2; exit 1; }
Laszlo Csomora7f202c2018-04-26 07:39:41 -070045
46cd "$TEST_TMPDIR"
47
48function assert_fails_to() {
49 local -r method="$1"
50 local -r arg="${2:-}"
51 if [[ -n "$arg" ]]; then
52 "$method" "$arg" && fail "'$method' should have failed for '$arg'"
53 else
54 "$method" && fail "'$method' should have failed for empty argument"
55 fi
56 true # reset the exit status otherwise the test would be considered failed
57}
58
59function test_resolve_bad_links() {
60 local -r dir="${FUNCNAME[0]}"
61
62 mkdir -p "$dir" || fail "mkdir -p $dir"
63
64 # absolute, non-existent path
65 assert_fails_to resolve_links "$(pwd)/${dir}/non-existent"
66
67 # relative, non-existent path
68 assert_fails_to resolve_links "${dir}/non-existent"
69
70 # symlink with absolute non-existent target
71 ln -s "/non-existent" "${dir}/bad-absolute.sym"
72 assert_fails_to resolve_links "$(pwd)/${dir}/bad-absolute.sym"
73 assert_fails_to resolve_links "${dir}/bad-absolute.sym"
74
75 # symlink with relative non-existent target
76 ln -s "non-existent" "${dir}/bad-relative.sym"
77 assert_fails_to resolve_links "$(pwd)/${dir}/bad-relative.sym"
78 assert_fails_to resolve_links "${dir}/bad-relative.sym"
79
80 # circular symlink
81 ln -s "circular.sym" "${dir}/circular.sym"
82 assert_fails_to resolve_links "$(pwd)/${dir}/circular.sym"
83 assert_fails_to resolve_links "${dir}/circular.sym"
84}
85
86
87function test_resolve_symlinks() {
88 local -r dir="${FUNCNAME[0]}"
89
90 mkdir -p "${dir}/a/b" || fail "mkdir -p ${dir}/a/b"
91 echo hello > "${dir}/hello.txt"
92
93 ln -s "." "${dir}/self"
94 ln -s "../hello.txt" "${dir}/a/sym"
95 ln -s "../sym" "${dir}/a/b/sym"
96 ln -s ".././sym" "${dir}/a/b/sym-not-normalized"
97
98 assert_equals "${dir}/." "$(resolve_links "${dir}/self")"
99 assert_equals "${dir}/a/../hello.txt" "$(resolve_links "${dir}/a/sym")"
100 assert_equals "${dir}/a/b/../../hello.txt" "$(resolve_links "${dir}/a/b/sym")"
101 assert_equals \
102 "${dir}/a/b/.././../hello.txt" \
103 "$(resolve_links "${dir}/a/b/sym-not-normalized")"
104
105 cd "$dir"
106 assert_equals "./." "$(resolve_links "self")"
107 assert_equals "./." "$(resolve_links "./self")"
108 assert_equals "a/../hello.txt" "$(resolve_links "a/sym")"
109 assert_equals "a/b/../../hello.txt" "$(resolve_links "a/b/sym")"
110 assert_equals \
111 "a/b/.././../hello.txt" \
112 "$(resolve_links "a/b/sym-not-normalized")"
113
114 cd a
115 assert_equals "../." "$(resolve_links "../self")"
116 assert_equals "./../hello.txt" "$(resolve_links "sym")"
117 assert_equals "./../hello.txt" "$(resolve_links "./sym")"
118 assert_equals "b/../../hello.txt" "$(resolve_links "b/sym")"
119 assert_equals \
120 "b/.././../hello.txt" \
121 "$(resolve_links "b/sym-not-normalized")"
122
123 cd b
124 assert_equals "../../." "$(resolve_links "../../self")"
125 assert_equals "../../hello.txt" "$(resolve_links "../sym")"
126 assert_equals "./../../hello.txt" "$(resolve_links "sym")"
127 assert_equals "./../../hello.txt" "$(resolve_links "./sym")"
128 assert_equals \
129 "./.././../hello.txt" \
130 "$(resolve_links "sym-not-normalized")"
131}
132
133function test_get_realpath() {
134 local -r dir="${FUNCNAME[0]}"
135
136 mkdir -p "${dir}/a/b" || fail "mkdir -p ${dir}/a/b"
137 echo hello > "${dir}/hello.txt"
138
139 ln -s "." "${dir}/self"
140 ln -s "../hello.txt" "${dir}/a/sym"
141 ln -s "../sym" "${dir}/a/b/sym"
142 ln -s ".././sym" "${dir}/a/b/sym-not-normalized"
143
144 assert_equals "$(pwd)/${dir}" "$(get_real_path "${dir}/self")"
145 assert_equals "$(pwd)/${dir}/hello.txt" "$(get_real_path "${dir}/a/sym")"
146 assert_equals "$(pwd)/${dir}/hello.txt" "$(get_real_path "${dir}/a/b/sym")"
147 assert_equals \
148 "$(pwd)/${dir}/hello.txt" \
149 "$(get_real_path "${dir}/a/b/sym-not-normalized")"
150
151 cd "$dir"
152 local -r abs_dir=$(pwd)
153 assert_equals "${abs_dir}" "$(get_real_path "self")"
154 assert_equals "${abs_dir}" "$(get_real_path "./self")"
155 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "a/sym")"
156 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "a/b/sym")"
157 assert_equals \
158 "${abs_dir}/hello.txt" "$(get_real_path "a/b/sym-not-normalized")"
159
160 cd a
161 assert_equals "${abs_dir}" "$(get_real_path "../self")"
162 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "sym")"
163 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "./sym")"
164 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "b/sym")"
165 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "b/sym-not-normalized")"
166
167 cd b
168 assert_equals "${abs_dir}" "$(get_real_path "../../self")"
169 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "../sym")"
170 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "sym")"
171 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "./sym")"
172 assert_equals "${abs_dir}/hello.txt" "$(get_real_path "sym-not-normalized")"
173
174 assert_fails_to get_real_path "non-existent"
175 ln -s self self
176 assert_fails_to get_real_path "self"
177}
178
179run_suite "Tests for Bash utilities"