Skip to content

Commit 62859a5

Browse files
Clean git package repo in tmp
The presence of a git repo for a package in the tmp folder gives the impression that is has been compiled. We clean it and then move on.
1 parent 005bc69 commit 62859a5

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/deploy/build.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ def checkout(self) -> None:
8484
if not isinstance(gitconf := self.config.src, GitConfig) or self.src is None:
8585
return
8686

87-
try:
88-
self.src.mkdir(parents=True)
89-
except FileExistsError:
90-
return
91-
9287
env = os.environ.copy()
9388

9489
if gitconf.ssh_key_path is not None:
@@ -99,6 +94,13 @@ def checkout(self) -> None:
9994
def git(*args: str | Path) -> None:
10095
subprocess.run(("git", *args), check=True, cwd=self.src, env=env)
10196

97+
try:
98+
self.src.mkdir(parents=True)
99+
except FileExistsError:
100+
git("reset", "--hard")
101+
git("clean", "-xdf")
102+
return
103+
102104
git("init", "-b", "main")
103105
git("remote", "add", "origin", gitconf.url)
104106
git("fetch", "origin", gitconf.ref)

tests/test_build.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from deploy.config import Config
44
from pathlib import Path
55
import pytest
6+
from unittest.mock import patch
67

78

89
@pytest.fixture
@@ -174,3 +175,51 @@ def test_build_script_validity(
174175
# Check that the correct configuration is correct
175176
(tmp_path / "build_B.sh").chmod(0o755)
176177
Build(Path("/dummy"), config, extra_scripts=tmp_path, prefix=tmp_path)
178+
179+
180+
@pytest.mark.parametrize(
181+
"script_content,config_update",
182+
[
183+
pytest.param(
184+
"content",
185+
{"src": {"type": "git", "url": "https://example.com", "ref": "abcdefg"}},
186+
id="git source",
187+
),
188+
],
189+
)
190+
def test_clean_package_cache_on_rebuild(
191+
tmp_path, base_config, monkeypatch, config_update, script_content
192+
):
193+
with patch("deploy.build.subprocess") as mocked_subprocess:
194+
monkeypatch.setenv("XDG_CACHE_HOME", tmp_path)
195+
(tmp_path / "build_A.sh").write_text(script_content)
196+
(tmp_path / "build_A.sh").chmod(0o755)
197+
base_config["builds"].append(
198+
{
199+
"name": "A",
200+
"version": "0.0",
201+
"depends": [],
202+
**config_update,
203+
}
204+
)
205+
206+
config = Config.model_validate(base_config)
207+
build = Build(tmp_path, config, extra_scripts=tmp_path, prefix=tmp_path)
208+
pck = list(build.packages.values())[0]
209+
210+
pck.checkout()
211+
212+
# Checkout will use subpross run on the git command
213+
# We verify that it first is called with git init
214+
git_commands = [
215+
call_args[0][0][1] for call_args in mocked_subprocess.run.call_args_list
216+
]
217+
assert "init" in git_commands
218+
219+
pck.checkout()
220+
221+
# And when we do it again, we want a git clean
222+
git_commands = [
223+
call_args[0][0][1] for call_args in mocked_subprocess.run.call_args_list
224+
]
225+
assert "clean" in git_commands

0 commit comments

Comments
 (0)