Skip to content

Commit eddc34c

Browse files
authored
Fail the local yml mounts prek hook when synchronization fails (#73785)
* Fail the local yml mounts prek hook when synchronization fails The hook printed a red error and exited zero, so a failed synchronization passed as green and left the generated mounts in local.yml stale. The failure it is most likely to hide is a broken docker_command_utils.py, which is one of the two files that trigger the hook in the first place. * Run the real prek helper in the local yml mounts test Stubbing common_prek_utils made the test carry a second copy of the script's imports and a copy of the script itself, and left the helper the hook actually runs untested. A fake breeze on PATH is enough.
1 parent 2fab095 commit eddc34c

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎scripts/ci/prek/local_yml_mounts.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import annotations
1919

2020
import subprocess
21+
import sys
2122

2223
from common_prek_utils import console, initialize_breeze_prek
2324

@@ -29,3 +30,4 @@
2930
)
3031
if res.returncode != 0:
3132
console.print("\n[red]Error when running local mounts synchronization.\n")
33+
sys.exit(res.returncode)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)