Skip to content

Commit 5f09a1b

Browse files
committed
!squash more
1 parent 0e18a63 commit 5f09a1b

File tree

1 file changed

+105
-35
lines changed

1 file changed

+105
-35
lines changed

tests/cli/test_add_from_fs.py

Lines changed: 105 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import yaml
1111

1212
from vcspull.cli.add_from_fs import add_from_filesystem, get_git_origin_url
13+
from vcspull.tests.helpers import save_config_yaml
1314

1415
if t.TYPE_CHECKING:
1516
from _pytest.logging import LogCaptureFixture
@@ -524,55 +525,124 @@ def test_add_from_fs_base_dir_key_not_dict(
524525
assert data == initial_config_data
525526

526527

527-
# Next: Integration style test with libvcs create_git_remote_repo
528-
@pytest.mark.skip(
529-
reason="libvcs pytest plugin not available in this test environment yet",
530-
)
528+
# @pytest.mark.skip(
529+
# reason="libvcs pytest plugin not available in this test environment yet"
530+
# )
531531
def test_add_from_fs_integration_with_libvcs(
532-
# create_git_remote_repo, # This fixture would come from libvcs.pytest_plugin
533-
tmp_path: pathlib.Path, # Using tmp_path for manual git setup for now
534-
cwd_path_fs: pathlib.Path, # Mocked CWD
535-
mock_find_home_config_files_fs: t.Any,
532+
tmp_path: pathlib.Path,
533+
cwd_path_fs: pathlib.Path, # Mocked CWD, ensures config saved in predictable temp loc
534+
mock_find_home_config_files_fs: t.Any, # To control config loading behavior
536535
caplog: LogCaptureFixture,
536+
# git_commit_envvars: t.Mapping[str, str], # from libvcs if making commits
537537
) -> None:
538-
"""Test add_from_filesystem with actual git repositories created by libvcs (or manually)."""
539-
mock_find_home_config_files_fs.return_value = []
540-
scan_dir = cwd_path_fs / "integration_scan"
541-
scan_dir.mkdir(exist_ok=True)
542-
config_file = cwd_path_fs / ".vcspull.yaml"
538+
"""Test add_from_filesystem with actual git repos on the filesystem.
543539
544-
# Manually create a git repo as libvcs fixture is not directly usable here
545-
repo1_path = scan_dir / "actual_repo1"
546-
repo1_path.mkdir()
547-
repo1_url = "https://gitserver.com/user/actual_repo1.git"
548-
try:
549-
subprocess.run(["git", "init"], cwd=repo1_path, check=True, capture_output=True)
550-
subprocess.run(
551-
["git", "remote", "add", "origin", repo1_url],
552-
cwd=repo1_path,
553-
check=True,
554-
capture_output=True,
555-
)
556-
except (subprocess.CalledProcessError, FileNotFoundError) as e:
557-
pytest.skip(f"git CLI not available or failed to init repo: {e}")
540+
This test does not mock os.walk or get_git_origin_url.
541+
It relies on conftest.py to set up a clean git environment (home, gitconfig).
542+
"""
543+
mock_find_home_config_files_fs.return_value = [] # Default to CWD config
544+
config_file_path = cwd_path_fs / ".vcspull.yaml"
545+
546+
scan_dir = tmp_path / "actual_projects_integration"
547+
scan_dir.mkdir()
548+
549+
# Repo 1: project_one
550+
repo1_url = "https://example.com/integration/project_one.git"
551+
repo1_name = "project_one"
552+
repo1_dir = scan_dir / repo1_name
553+
repo1_dir.mkdir()
554+
# Environment for subprocess.run should be clean thanks to libvcs fixtures in conftest
555+
subprocess.run(
556+
["git", "init"],
557+
cwd=repo1_dir,
558+
check=True,
559+
capture_output=True,
560+
text=True,
561+
)
562+
subprocess.run(
563+
["git", "remote", "add", "origin", repo1_url],
564+
cwd=repo1_dir,
565+
check=True,
566+
capture_output=True,
567+
text=True,
568+
)
558569

559-
expected_base_key = str(scan_dir.resolve()) + "/"
570+
# Repo 2: project_two (nested)
571+
repo2_url = "git@example.com:integration/project_two.git"
572+
repo2_name = "project_two"
573+
nested_dir = scan_dir / "nested_group"
574+
nested_dir.mkdir()
575+
repo2_dir = nested_dir / repo2_name
576+
repo2_dir.mkdir()
577+
subprocess.run(
578+
["git", "init"],
579+
cwd=repo2_dir,
580+
check=True,
581+
capture_output=True,
582+
text=True,
583+
)
584+
subprocess.run(
585+
["git", "remote", "add", "origin", repo2_url],
586+
cwd=repo2_dir,
587+
check=True,
588+
capture_output=True,
589+
text=True,
590+
)
591+
592+
# Repo 3: project_three (no remote)
593+
repo3_name = "project_three"
594+
repo3_dir = scan_dir / repo3_name
595+
repo3_dir.mkdir()
596+
subprocess.run(
597+
["git", "init"],
598+
cwd=repo3_dir,
599+
check=True,
600+
capture_output=True,
601+
text=True,
602+
)
603+
604+
# Repo 4: not_a_git_repo
605+
not_a_repo_dir = scan_dir / "not_a_git_repo"
606+
not_a_repo_dir.mkdir()
607+
(not_a_repo_dir / "some_file.txt").write_text("hello")
560608

561609
add_from_filesystem(
562610
scan_dir_str=str(scan_dir),
563-
config_file_path_str=None,
611+
config_file_path_str=None, # Uses default (cwd_path_fs / ".vcspull.yaml")
564612
recursive=True,
565-
base_dir_key_arg=None,
566-
yes=True,
613+
base_dir_key_arg=None, # Infer
614+
yes=True, # Auto-confirm
567615
)
568616

617+
assert config_file_path.exists()
618+
with open(config_file_path, encoding="utf-8") as f:
619+
data = yaml.safe_load(f)
620+
621+
expected_base_key = str(scan_dir.resolve()) + "/"
622+
623+
assert expected_base_key in data
624+
assert repo1_name in data[expected_base_key]
625+
assert data[expected_base_key][repo1_name] == repo1_url
626+
627+
assert repo2_name in data[expected_base_key]
628+
assert data[expected_base_key][repo2_name] == repo2_url
629+
630+
assert repo3_name not in data[expected_base_key] # No remote
631+
assert "not_a_git_repo" not in data[expected_base_key]
632+
633+
assert f"Found .git in {repo1_dir}" in caplog.text
634+
assert f"Found .git in {repo2_dir}" in caplog.text
635+
assert f"Found .git in {repo3_dir}" in caplog.text
636+
assert f"Could not get origin URL for {repo3_dir}" in caplog.text
569637
assert (
570-
f"Adding 'actual_repo1' ({repo1_url}) under '{expected_base_key}'"
638+
f"Adding '{repo1_name}' ({repo1_url}) under '{expected_base_key}'"
571639
in caplog.text
572640
)
573-
with open(config_file, encoding="utf-8") as f:
574-
data = yaml.safe_load(f)
575-
assert data[expected_base_key]["actual_repo1"] == repo1_url
641+
assert (
642+
f"Adding '{repo2_name}' ({repo2_url}) under '{expected_base_key}'"
643+
in caplog.text
644+
)
645+
assert f"Successfully updated {config_file_path}" in caplog.text
576646

577647

578648
# - scan_dir being relative to a mocked home directory for key generation

0 commit comments

Comments
 (0)