blob: a8538be799969b4dc97fc412f3d038dbfae90d5b [file] [log] [blame]
Philipp Wollermann738e5722017-02-27 17:17:00 +00001#!/bin/bash
2#
3# Copyright 2017 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
Laszlo Csomoraa761ae2018-07-27 08:36:50 -070018# --- begin runfiles.bash initialization ---
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 "io_bazel/src/test/shell/integration_test_setup.sh")" \
Philipp Wollermann738e5722017-02-27 17:17:00 +000041 || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
42
Laszlo Csomoraa761ae2018-07-27 08:36:50 -070043case "$(uname -s | tr [:upper:] [:lower:])" in
44msys*|mingw*|cygwin*)
45 declare -r is_windows=true
46 ;;
47*)
48 declare -r is_windows=false
49 ;;
50esac
51
52if "$is_windows"; then
53 export MSYS_NO_PATHCONV=1
54 export MSYS2_ARG_CONV_EXCL="*"
55 declare -r EXE_EXT=".exe"
56else
57 declare -r EXE_EXT=""
58fi
Philipp Wollermann738e5722017-02-27 17:17:00 +000059
brandjon268b4132018-12-17 13:12:19 -080060#### TESTS #############################################################
61
62# Tests in this file cannot invoke a real Python 3 runtime. This is because this
63# file is shared by both Bazel's public test suite and Google's internal tests,
64# and the internal tests do not have a Python 3 environment.
65#
66# - If you only need a real Python 2 environment and do not use Python 3 at
67# all, you can place your test in this file.
68#
69# - If you need to check Bazel's behavior concerning the *selection* of a
70# Python 2 or 3 runtime, but do not actually need the runtime itself, then
brandjon94078452019-01-18 11:45:54 -080071# you may put your test in python_stub_test.sh (in this directory) instead.
brandjon268b4132018-12-17 13:12:19 -080072#
73# - Otherwise, put your test in //src/test/shell/bazel. That suite can invoke
74# actual Python 2 and 3 interpreters.
75
Philipp Wollermann738e5722017-02-27 17:17:00 +000076function test_python_binary_empty_files_in_runfiles_are_regular_files() {
77 mkdir -p test/mypackage
78 cat > test/BUILD <<'EOF'
79py_test(
80 name = "a",
81 srcs = [
82 "a.py",
83 "mypackage/b.py",
84 ],
85 main = "a.py"
86)
87EOF
88 cat >test/a.py <<'EOF'
Laszlo Csomor3a51b082018-08-08 04:08:43 -070089from __future__ import print_function
Philipp Wollermann738e5722017-02-27 17:17:00 +000090import os.path
91import sys
92
Laszlo Csomor3a51b082018-08-08 04:08:43 -070093print("This is my name: %s" % __file__)
94print("This is my working directory: %s" % os.getcwd())
Philipp Wollermann738e5722017-02-27 17:17:00 +000095os.chdir(os.path.dirname(__file__))
Laszlo Csomor3a51b082018-08-08 04:08:43 -070096print("This is my new working directory: %s" % os.getcwd())
Philipp Wollermann738e5722017-02-27 17:17:00 +000097
98file_to_check = "mypackage/__init__.py"
99
100if not os.path.exists(file_to_check):
Laszlo Csomor3a51b082018-08-08 04:08:43 -0700101 print("mypackage/__init__.py does not exist")
Philipp Wollermann738e5722017-02-27 17:17:00 +0000102 sys.exit(1)
103
104if os.path.islink(file_to_check):
Laszlo Csomor3a51b082018-08-08 04:08:43 -0700105 print("mypackage/__init__.py is a symlink, expected a regular file")
Philipp Wollermann738e5722017-02-27 17:17:00 +0000106 sys.exit(1)
107
108if not os.path.isfile(file_to_check):
Laszlo Csomor3a51b082018-08-08 04:08:43 -0700109 print("mypackage/__init__.py is not a regular file")
Philipp Wollermann738e5722017-02-27 17:17:00 +0000110 sys.exit(1)
111
Laszlo Csomor3a51b082018-08-08 04:08:43 -0700112print("OK")
Philipp Wollermann738e5722017-02-27 17:17:00 +0000113EOF
114 touch test/mypackage/b.py
115
116 bazel test --test_output=streamed //test:a &> $TEST_log || fail "test failed"
117}
118
Benjamin Peterson3b0bcd92017-09-26 08:15:27 -0400119function test_building_transitive_py_binary_runfiles_trees() {
brandjon268b4132018-12-17 13:12:19 -0800120 touch main.py script.sh
121 chmod u+x script.sh
122 cat > BUILD <<'EOF'
Benjamin Peterson3b0bcd92017-09-26 08:15:27 -0400123py_binary(
124 name = 'py-tool',
125 srcs = ['main.py'],
126 main = 'main.py',
127)
128
129sh_binary(
130 name = 'sh-tool',
131 srcs = ['script.sh'],
132 data = [':py-tool'],
133)
134EOF
brandjon268b4132018-12-17 13:12:19 -0800135 bazel build --experimental_build_transitive_python_runfiles :sh-tool
136 [ -d "bazel-bin/py-tool${EXE_EXT}.runfiles" ] || fail "py_binary runfiles tree not built"
137 bazel clean
138 bazel build --noexperimental_build_transitive_python_runfiles :sh-tool
139 [ ! -e "bazel-bin/py-tool${EXE_EXT}.runfiles" ] || fail "py_binary runfiles tree built"
140}
141
Philipp Wollermann738e5722017-02-27 17:17:00 +0000142run_suite "Tests for the Python rules"