Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 1 | #!/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 Csomor | aa761ae | 2018-07-27 08:36:50 -0700 | [diff] [blame] | 18 | # --- begin runfiles.bash initialization --- |
| 19 | set -euo pipefail |
| 20 | if [[ ! -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 |
| 28 | fi |
| 29 | if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then |
| 30 | source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash" |
| 31 | elif [[ -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-)" |
| 34 | else |
| 35 | echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash" |
| 36 | exit 1 |
| 37 | fi |
| 38 | # --- end runfiles.bash initialization --- |
| 39 | |
| 40 | source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \ |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 41 | || { echo "integration_test_setup.sh not found!" >&2; exit 1; } |
| 42 | |
Laszlo Csomor | aa761ae | 2018-07-27 08:36:50 -0700 | [diff] [blame] | 43 | case "$(uname -s | tr [:upper:] [:lower:])" in |
| 44 | msys*|mingw*|cygwin*) |
| 45 | declare -r is_windows=true |
| 46 | ;; |
| 47 | *) |
| 48 | declare -r is_windows=false |
| 49 | ;; |
| 50 | esac |
| 51 | |
| 52 | if "$is_windows"; then |
| 53 | export MSYS_NO_PATHCONV=1 |
| 54 | export MSYS2_ARG_CONV_EXCL="*" |
| 55 | declare -r EXE_EXT=".exe" |
| 56 | else |
| 57 | declare -r EXE_EXT="" |
| 58 | fi |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 59 | |
brandjon | 268b413 | 2018-12-17 13:12:19 -0800 | [diff] [blame] | 60 | #### 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 |
brandjon | 9407845 | 2019-01-18 11:45:54 -0800 | [diff] [blame] | 71 | # you may put your test in python_stub_test.sh (in this directory) instead. |
brandjon | 268b413 | 2018-12-17 13:12:19 -0800 | [diff] [blame] | 72 | # |
| 73 | # - Otherwise, put your test in //src/test/shell/bazel. That suite can invoke |
| 74 | # actual Python 2 and 3 interpreters. |
| 75 | |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 76 | function test_python_binary_empty_files_in_runfiles_are_regular_files() { |
| 77 | mkdir -p test/mypackage |
| 78 | cat > test/BUILD <<'EOF' |
| 79 | py_test( |
| 80 | name = "a", |
| 81 | srcs = [ |
| 82 | "a.py", |
| 83 | "mypackage/b.py", |
| 84 | ], |
| 85 | main = "a.py" |
| 86 | ) |
| 87 | EOF |
| 88 | cat >test/a.py <<'EOF' |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 89 | from __future__ import print_function |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 90 | import os.path |
| 91 | import sys |
| 92 | |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 93 | print("This is my name: %s" % __file__) |
| 94 | print("This is my working directory: %s" % os.getcwd()) |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 95 | os.chdir(os.path.dirname(__file__)) |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 96 | print("This is my new working directory: %s" % os.getcwd()) |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 97 | |
| 98 | file_to_check = "mypackage/__init__.py" |
| 99 | |
| 100 | if not os.path.exists(file_to_check): |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 101 | print("mypackage/__init__.py does not exist") |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 102 | sys.exit(1) |
| 103 | |
| 104 | if os.path.islink(file_to_check): |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 105 | print("mypackage/__init__.py is a symlink, expected a regular file") |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 106 | sys.exit(1) |
| 107 | |
| 108 | if not os.path.isfile(file_to_check): |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 109 | print("mypackage/__init__.py is not a regular file") |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 110 | sys.exit(1) |
| 111 | |
Laszlo Csomor | 3a51b08 | 2018-08-08 04:08:43 -0700 | [diff] [blame] | 112 | print("OK") |
Philipp Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 113 | EOF |
| 114 | touch test/mypackage/b.py |
| 115 | |
| 116 | bazel test --test_output=streamed //test:a &> $TEST_log || fail "test failed" |
| 117 | } |
| 118 | |
Benjamin Peterson | 3b0bcd9 | 2017-09-26 08:15:27 -0400 | [diff] [blame] | 119 | function test_building_transitive_py_binary_runfiles_trees() { |
brandjon | 268b413 | 2018-12-17 13:12:19 -0800 | [diff] [blame] | 120 | touch main.py script.sh |
| 121 | chmod u+x script.sh |
| 122 | cat > BUILD <<'EOF' |
Benjamin Peterson | 3b0bcd9 | 2017-09-26 08:15:27 -0400 | [diff] [blame] | 123 | py_binary( |
| 124 | name = 'py-tool', |
| 125 | srcs = ['main.py'], |
| 126 | main = 'main.py', |
| 127 | ) |
| 128 | |
| 129 | sh_binary( |
| 130 | name = 'sh-tool', |
| 131 | srcs = ['script.sh'], |
| 132 | data = [':py-tool'], |
| 133 | ) |
| 134 | EOF |
brandjon | 268b413 | 2018-12-17 13:12:19 -0800 | [diff] [blame] | 135 | 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 Wollermann | 738e572 | 2017-02-27 17:17:00 +0000 | [diff] [blame] | 142 | run_suite "Tests for the Python rules" |