• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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
15import json
16import os
17import platform
18import re
19import shutil
20import sys
21import tempfile
22import textwrap
23from pathlib import Path
24from subprocess import Popen
25
26from rules_python.python.runfiles import runfiles
27
28r = runfiles.Create()
29
30
31def main(conf_file):
32    with open(conf_file) as j:
33        config = json.load(j)
34
35    isWindows = platform.system() == "Windows"
36    bazelBinary = r.Rlocation(
37        os.path.join(
38            config["bazelBinaryWorkspace"], "bazel.exe" if isWindows else "bazel"
39        )
40    )
41
42    workspacePath = config["workspaceRoot"]
43    # Canonicalize bazel external/some_repo/foo
44    if workspacePath.startswith("external/"):
45        workspacePath = ".." + workspacePath[len("external") :]
46
47    with tempfile.TemporaryDirectory(dir=os.environ["TEST_TMPDIR"]) as tmp_homedir:
48        home_bazel_rc = Path(tmp_homedir) / ".bazelrc"
49        home_bazel_rc.write_text(
50            textwrap.dedent(
51                """\
52                startup --max_idle_secs=1
53                common --announce_rc
54                """
55            )
56        )
57
58        with tempfile.TemporaryDirectory(dir=os.environ["TEST_TMPDIR"]) as tmpdir:
59            workdir = os.path.join(tmpdir, "wksp")
60            print("copying workspace under test %s to %s" % (workspacePath, workdir))
61            shutil.copytree(workspacePath, workdir)
62
63            for command in config["bazelCommands"]:
64                bazel_args = command.split(" ")
65                bazel_args.append(
66                    "--override_repository=rules_python=%s/rules_python"
67                    % os.environ["TEST_SRCDIR"]
68                )
69                bazel_args.append(
70                    "--override_repository=rules_python_gazelle_plugin=%s/rules_python_gazelle_plugin"
71                    % os.environ["TEST_SRCDIR"]
72                )
73
74                # TODO: --override_module isn't supported in the current BAZEL_VERSION (5.2.0)
75                # This condition and attribute can be removed when bazel is updated for
76                # the rest of rules_python.
77                if config["bzlmod"]:
78                    bazel_args.append(
79                        "--override_module=rules_python=%s/rules_python"
80                        % os.environ["TEST_SRCDIR"]
81                    )
82                    bazel_args.append("--enable_bzlmod")
83
84                # Bazel's wrapper script needs this or you get
85                # 2020/07/13 21:58:11 could not get the user's cache directory: $HOME is not defined
86                os.environ["HOME"] = str(tmp_homedir)
87
88                bazel_args.insert(0, bazelBinary)
89                bazel_process = Popen(bazel_args, cwd=workdir)
90                bazel_process.wait()
91                error = bazel_process.returncode != 0
92
93                if platform.system() == "Windows":
94                    # Cleanup any bazel files
95                    bazel_process = Popen([bazelBinary, "clean"], cwd=workdir)
96                    bazel_process.wait()
97                    error |= bazel_process.returncode != 0
98
99                    # Shutdown the bazel instance to avoid issues cleaning up the workspace
100                    bazel_process = Popen([bazelBinary, "shutdown"], cwd=workdir)
101                    bazel_process.wait()
102                    error |= bazel_process.returncode != 0
103
104                if error != 0:
105                    # Test failure in Bazel is exit 3
106                    # https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44
107                    sys.exit(3)
108
109
110if __name__ == "__main__":
111    main(sys.argv[1])
112