#!/usr/bin/env python3 import os import subprocess import sys import unittest from pathlib import Path import run_bazel_with_buildbuddy class RunBazelWithBuildBuddyTest(unittest.TestCase): def test_bazel_command_uses_configured_binary_locally(self) -> None: self.assertEqual( run_bazel_with_buildbuddy.bazel_command( "info", "execution_root", env={"CODEX_BAZEL_BIN": "fake-bazel"}, ), ["fake-bazel", "info", "execution_root"], ) def test_bazel_command_normalizes_github_actions_startup_options(self) -> None: env = { "BAZEL_OUTPUT_USER_ROOT": "/tmp/bazel-output", "GITHUB_ACTIONS": "true", } self.assertEqual( run_bazel_with_buildbuddy.bazel_command("build", "//codex-rs/...", env=env), [ "bazel", "--output_user_root=/tmp/bazel-output", "--noexperimental_remote_repo_contents_cache", "build", "//codex-rs/...", ], ) self.assertEqual( run_bazel_with_buildbuddy.bazel_command( "--experimental_remote_repo_contents_cache", "build", "//codex-rs/...", env=env, ), [ "bazel", "--output_user_root=/tmp/bazel-output", "--experimental_remote_repo_contents_cache", "build", "//codex-rs/...", ], ) def test_bazel_command_uses_configured_local_caches(self) -> None: env = { "BAZEL_REPO_CONTENTS_CACHE": "/tmp/bazel-repo-contents", "BAZEL_REPOSITORY_CACHE": "/tmp/bazel-repository", } self.assertEqual( run_bazel_with_buildbuddy.bazel_command( "build", "--config=local", "//codex-rs/...", env=env, ), [ "bazel", "build", "--config=local", "//codex-rs/...", "--repo_contents_cache=/tmp/bazel-repo-contents", "--repository_cache=/tmp/bazel-repository", ], ) def test_bazel_command_adds_local_caches_before_separator(self) -> None: self.assertEqual( run_bazel_with_buildbuddy.bazel_command( "build", "//codex-rs/...", "--", "--program-arg", env={"BAZEL_REPOSITORY_CACHE": "/tmp/bazel-repository"}, ), [ "bazel", "build", "//codex-rs/...", "--repository_cache=/tmp/bazel-repository", "--", "--program-arg", ], ) def test_main_preserves_spaced_argument_and_child_exit_status(self) -> None: spaced_arg = ( r"--test_env=PATH=C:\Program Files\PowerShell\7;C:\Program Files\Git\bin" ) child_code = ( f"import sys; sys.exit(37 if sys.argv[1] == {spaced_arg!r} else 91)" ) env = os.environ.copy() env["CODEX_BAZEL_BIN"] = sys.executable env.pop("BAZEL_OUTPUT_USER_ROOT", None) env.pop("GITHUB_ACTIONS", None) result = subprocess.run( [ sys.executable, str(Path(run_bazel_with_buildbuddy.__file__)), "-c", child_code, spaced_arg, ], env=env, check=False, capture_output=True, text=True, ) self.assertEqual(result.returncode, 37, result.stderr) if __name__ == "__main__": unittest.main()