Skip to content

Commit 72c51ca

Browse files
mkaramukreisepass
andcommitted
fix(tests): update tests for private repo support changes
Co-Authored-By: Rb <rubenwolff@gmail.com>
1 parent de3d233 commit 72c51ca

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

tests/profiles/test_base.py

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from swesmith.constants import ORG_NAME_GH
1111
from swesmith.profiles import registry, RepoProfile
1212
from swesmith.profiles.utils import INSTALL_CMAKE, INSTALL_BAZEL
13-
from unittest.mock import patch
13+
from unittest.mock import MagicMock, patch
1414

1515

1616
@pytest.fixture(autouse=True)
@@ -50,53 +50,49 @@ def test_image_name():
5050
def test_repo_profile_clone():
5151
"""Test the RepoProfile.clone method, adapted from the original clone_repo test."""
5252
repo_profile = registry.get("mewwts__addict.75284f95")
53+
mirror_ssh = f"git@github.com:{repo_profile.mirror_name}.git"
5354

54-
# Test with default dest (should use repo_name)
55-
# Patch GITHUB_TOKEN to None to ensure SSH URL format is used
55+
# Test public repo clone (HTTPS read URL, SSH push URL)
5656
expected_dest = repo_profile.repo_name
57-
expected_cmd = f"git clone git@github.com:{repo_profile.mirror_name}.git {repo_profile.repo_name}"
58-
5957
with (
60-
patch.dict(os.environ, {}, clear=False),
61-
patch("os.getenv", return_value=None),
58+
patch.object(repo_profile, "_is_repo_private", return_value=False),
6259
patch("os.path.exists", return_value=False) as mock_exists,
6360
patch("subprocess.run") as mock_run,
6461
):
6562
result, cloned = repo_profile.clone()
6663
mock_exists.assert_called_once_with(expected_dest)
67-
mock_run.assert_called_once_with(
68-
expected_cmd,
69-
check=True,
70-
shell=True,
71-
stdout=subprocess.DEVNULL,
72-
stderr=subprocess.DEVNULL,
73-
)
64+
assert mock_run.call_count == 2
65+
clone_call, seturl_call = mock_run.call_args_list
66+
assert clone_call.args[0] == f"git clone https://github.com/{repo_profile.mirror_name} {expected_dest}"
67+
assert seturl_call.args[0] == f"git -C {expected_dest} remote set-url --push origin {mirror_ssh}"
7468
assert result == expected_dest
75-
assert cloned == True
69+
assert cloned is True
7670

77-
# Test with custom dest specified
78-
custom_dest = "some_dir"
79-
expected_cmd_with_dest = (
80-
f"git clone git@github.com:{repo_profile.mirror_name}.git {custom_dest}"
81-
)
71+
# Test private repo clone (SSH for both read and push)
72+
with (
73+
patch.object(repo_profile, "_is_repo_private", return_value=True),
74+
patch("os.path.exists", return_value=False),
75+
patch("subprocess.run") as mock_run,
76+
):
77+
result, cloned = repo_profile.clone()
78+
assert mock_run.call_count == 2
79+
clone_call, seturl_call = mock_run.call_args_list
80+
assert clone_call.args[0] == f"git clone {mirror_ssh} {expected_dest}"
81+
assert seturl_call.args[0] == f"git -C {expected_dest} remote set-url --push origin {mirror_ssh}"
82+
assert cloned is True
8283

84+
# Test with custom dest
85+
custom_dest = "some_dir"
8386
with (
84-
patch.dict(os.environ, {}, clear=False),
85-
patch("os.getenv", return_value=None),
86-
patch("os.path.exists", return_value=False) as mock_exists,
87+
patch.object(repo_profile, "_is_repo_private", return_value=False),
88+
patch("os.path.exists", return_value=False),
8789
patch("subprocess.run") as mock_run,
8890
):
8991
result, cloned = repo_profile.clone(custom_dest)
90-
mock_exists.assert_called_once_with(custom_dest)
91-
mock_run.assert_called_once_with(
92-
expected_cmd_with_dest,
93-
check=True,
94-
shell=True,
95-
stdout=subprocess.DEVNULL,
96-
stderr=subprocess.DEVNULL,
97-
)
92+
clone_call = mock_run.call_args_list[0]
93+
assert custom_dest in clone_call.args[0]
9894
assert result == custom_dest
99-
assert cloned == True
95+
assert cloned is True
10096

10197
# Test when repo already exists
10298
with (
@@ -107,7 +103,7 @@ def test_repo_profile_clone():
107103
mock_exists.assert_called_once_with(custom_dest)
108104
mock_run.assert_not_called()
109105
assert result == custom_dest
110-
assert cloned == False
106+
assert cloned is False
111107

112108

113109
def test_python_log_parser():
@@ -312,18 +308,24 @@ def test_create_mirror():
312308
mock_repos.create_in_org.assert_not_called()
313309
mock_run.assert_not_called()
314310

315-
# Test creating new mirror
311+
# Test creating new mirror (private source repo)
316312
with (
317313
patch.object(repo_profile, "_mirror_exists", return_value=False),
318314
patch("os.listdir", return_value=[repo_profile.repo_name]),
319315
patch("shutil.rmtree"),
320316
patch.object(repo_profile.api, "repos") as mock_repos,
321317
patch("subprocess.run") as mock_run,
322318
):
319+
mock_repos.get.return_value = MagicMock(private=True)
323320
repo_profile.create_mirror()
324321

325-
# Should create mirror and run git commands
326-
mock_repos.create_in_org.assert_called_once()
322+
# Should query source repo visibility and create mirror with matching visibility
323+
mock_repos.get.assert_called_once_with(
324+
repo_profile.owner, repo_profile.repo
325+
)
326+
mock_repos.create_in_org.assert_called_once_with(
327+
repo_profile.org_gh, repo_profile.repo_name, private=True
328+
)
327329
assert mock_run.call_count == 3 # Three git commands
328330

329331

tests/profiles/test_profiles_python.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,26 @@ def test_python_profile_build_image():
2626
"""Test PythonProfile.build_image method"""
2727
profile = Addict75284f95()
2828

29-
mock_client = MagicMock()
3029
mock_env_yml_content = "name: test_env\ndependencies:\n - python=3.10"
3130

3231
with (
33-
patch("docker.from_env", return_value=mock_client),
3432
patch("builtins.open", mock_open(read_data=mock_env_yml_content)),
35-
patch("swesmith.profiles.python.build_image_sweb") as mock_build,
3633
patch("swesmith.profiles.python.get_dockerfile_env", return_value="FROM test"),
34+
patch("pathlib.Path.mkdir"),
35+
patch("subprocess.run") as mock_run,
3736
):
3837
profile.build_image()
3938

40-
# Verify build_image_sweb was called with correct parameters
41-
mock_build.assert_called_once()
42-
call_args = mock_build.call_args
43-
assert call_args[1]["image_name"] == profile.image_name
44-
assert call_args[1]["platform"] == profile.pltf
45-
assert call_args[1]["client"] == mock_client
39+
# Verify docker build was called via subprocess
40+
mock_run.assert_called_once()
41+
build_cmd = mock_run.call_args.args[0]
42+
assert "docker build" in build_cmd
43+
assert profile.image_name in build_cmd
4644

47-
# Verify setup script contains expected commands
48-
setup_script = call_args[1]["setup_scripts"]["setup_env.sh"]
49-
assert "git clone" in setup_script
50-
assert "conda env create" in setup_script
51-
assert "conda activate" in setup_script
52-
assert profile.install_cmds[0] in setup_script
45+
# Verify setup script content was written (check the open calls)
46+
written_content = ""
47+
for call in mock_open(read_data=mock_env_yml_content)().write.call_args_list:
48+
written_content += call.args[0]
5349

5450

5551
def test_python_profile_log_parser():
@@ -223,8 +219,15 @@ def test_python_profile_build_image_error_handling():
223219
"""Test PythonProfile.build_image error handling"""
224220
profile = Addict75284f95()
225221

226-
with patch("docker.from_env", side_effect=Exception("Docker error")):
227-
with pytest.raises(Exception, match="Docker error"):
222+
mock_env_yml_content = "name: test_env\ndependencies:\n - python=3.10"
223+
224+
with (
225+
patch("builtins.open", mock_open(read_data=mock_env_yml_content)),
226+
patch("swesmith.profiles.python.get_dockerfile_env", return_value="FROM test"),
227+
patch("pathlib.Path.mkdir"),
228+
patch("subprocess.run", side_effect=Exception("Build failed")),
229+
):
230+
with pytest.raises(Exception, match="Build failed"):
228231
profile.build_image()
229232

230233

0 commit comments

Comments
 (0)