1# Copyright 2024 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 v3 ---
16# Copy-pasted from the Bazel Bash runfiles library v3.
17set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
18source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
19  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
20  source "$0.runfiles/$f" 2>/dev/null || \
21  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
22  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
23  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
24# --- end runfiles.bash initialization v3 ---
25set +e
26
27bin=$(rlocation $BIN_RLOCATION)
28if [[ -z "$bin" ]]; then
29  echo "Unable to locate test binary: $BIN_RLOCATION"
30  exit 1
31fi
32
33
34function expect_match() {
35  local expected_pattern=$1
36  local actual=$2
37  if ! (echo "$actual" | grep "$expected_pattern" ) >/dev/null; then
38    echo "expected to match: $expected_pattern"
39    echo "===== actual START ====="
40    echo "$actual"
41    echo "===== actual END ====="
42    echo
43    touch EXPECTATION_FAILED
44    return 1
45  fi
46}
47
48
49echo "Check inherited and disabled"
50# Verify setting it to empty string disables safe path
51actual=$(PYTHONSAFEPATH= $bin 2>&1)
52expect_match "sys.flags.safe_path: False" "$actual"
53expect_match "PYTHONSAFEPATH: EMPTY" "$actual"
54
55echo "Check inherited and propagated"
56# Verify setting it to any string enables safe path and that
57# value is propagated
58actual=$(PYTHONSAFEPATH=OUTER $bin 2>&1)
59expect_match "sys.flags.safe_path: True" "$actual"
60expect_match "PYTHONSAFEPATH: OUTER" "$actual"
61
62echo "Check enabled by default"
63# Verifying doing nothing leaves safepath enabled by default
64actual=$($bin 2>&1)
65expect_match "sys.flags.safe_path: True" "$actual"
66expect_match "PYTHONSAFEPATH: 1" "$actual"
67
68# Exit if any of the expects failed
69[[ ! -e EXPECTATION_FAILED ]]
70