|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. 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, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Tests for the exit code of local_yml_mounts.py.""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import os |
| 22 | +import subprocess |
| 23 | +import sys |
| 24 | +import textwrap |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +SCRIPT_PATH = Path(__file__).resolve().parents[3] / "ci" / "prek" / "local_yml_mounts.py" |
| 30 | + |
| 31 | +CALL_LOG_NAME = "breeze_calls.txt" |
| 32 | + |
| 33 | +BREEZE_STUB = textwrap.dedent( |
| 34 | + """\ |
| 35 | + #!/usr/bin/env python3 |
| 36 | + import os |
| 37 | + import sys |
| 38 | +
|
| 39 | + with open(os.environ["BREEZE_STUB_CALL_LOG"], "a") as call_log: |
| 40 | + call_log.write(" ".join(sys.argv[1:]) + "\\n") |
| 41 | +
|
| 42 | + sys.exit(int(os.environ["BREEZE_STUB_RETURNCODE"])) |
| 43 | + """ |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +def run_script(tmp_path: Path, breeze_returncode: int) -> subprocess.CompletedProcess: |
| 48 | + bin_dir = tmp_path / "bin" |
| 49 | + bin_dir.mkdir() |
| 50 | + breeze_stub = bin_dir / "breeze" |
| 51 | + breeze_stub.write_text(BREEZE_STUB) |
| 52 | + breeze_stub.chmod(0o755) |
| 53 | + |
| 54 | + env = { |
| 55 | + **os.environ, |
| 56 | + "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}", |
| 57 | + "BREEZE_STUB_CALL_LOG": os.fspath(tmp_path / CALL_LOG_NAME), |
| 58 | + "BREEZE_STUB_RETURNCODE": str(breeze_returncode), |
| 59 | + } |
| 60 | + # initialize_breeze_prek honours this by exiting 0 before it ever reaches breeze. |
| 61 | + env.pop("SKIP_BREEZE_PREK_HOOKS", None) |
| 62 | + |
| 63 | + # The script exits when imported as a module, so it has to run as a subprocess. |
| 64 | + return subprocess.run( |
| 65 | + [sys.executable, os.fspath(SCRIPT_PATH)], |
| 66 | + env=env, |
| 67 | + capture_output=True, |
| 68 | + text=True, |
| 69 | + check=False, |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("breeze_returncode", [0, 1, 2]) |
| 74 | +def test_hook_exits_with_the_breeze_return_code(tmp_path, breeze_returncode): |
| 75 | + result = run_script(tmp_path, breeze_returncode) |
| 76 | + |
| 77 | + # Without this, an exit from initialize_breeze_prek's own preflight would satisfy the next line. |
| 78 | + assert (tmp_path / CALL_LOG_NAME).exists(), f"breeze was never invoked: {result.stdout}{result.stderr}" |
| 79 | + assert result.returncode == breeze_returncode |
0 commit comments