-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_git.py
More file actions
2655 lines (2071 loc) · 78.1 KB
/
test_git.py
File metadata and controls
2655 lines (2071 loc) · 78.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for libvcs.cmd.git."""
from __future__ import annotations
import os
import pathlib
import typing as t
import pytest
from libvcs._internal.query_list import ObjectDoesNotExist
from libvcs.cmd import git
if t.TYPE_CHECKING:
from libvcs.pytest_plugin import CreateRepoFn, GitCommitEnvVars
from libvcs.sync.git import GitSync
@pytest.mark.parametrize("path_type", [str, pathlib.Path])
def test_git_constructor(
path_type: t.Callable[[str | pathlib.Path], t.Any],
tmp_path: pathlib.Path,
) -> None:
"""Test Git constructor."""
repo = git.Git(path=path_type(tmp_path))
assert repo.path == tmp_path
def test_git_init_basic(tmp_path: pathlib.Path) -> None:
"""Test basic git init functionality."""
repo = git.Git(path=tmp_path)
result = repo.init()
assert "Initialized empty Git repository" in result
assert (tmp_path / ".git").is_dir()
def test_git_run_accepts_scalar_string(tmp_path: pathlib.Path) -> None:
"""Git run() should not split scalar command strings."""
repo = git.Git(path=tmp_path)
result = repo.run("--version")
assert result.startswith("git version ")
def test_git_init_bare(tmp_path: pathlib.Path) -> None:
"""Test git init with bare repository."""
repo = git.Git(path=tmp_path)
result = repo.init(bare=True)
assert "Initialized empty Git repository" in result
# Bare repos have files directly in the directory
assert (tmp_path / "HEAD").exists()
def test_git_init_template(tmp_path: pathlib.Path) -> None:
"""Test git init with template directory."""
template_dir = tmp_path / "template"
template_dir.mkdir()
(template_dir / "hooks").mkdir()
(template_dir / "hooks" / "pre-commit").write_text("#!/bin/sh\nexit 0\n")
repo_dir = tmp_path / "repo"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(template=str(template_dir))
assert "Initialized empty Git repository" in result
assert (repo_dir / ".git" / "hooks" / "pre-commit").exists()
def test_git_init_separate_git_dir(tmp_path: pathlib.Path) -> None:
"""Test git init with separate git directory."""
repo_dir = tmp_path / "repo"
git_dir = tmp_path / "git_dir"
repo_dir.mkdir()
git_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(separate_git_dir=str(git_dir.absolute()))
assert "Initialized empty Git repository" in result
assert git_dir.is_dir()
assert (git_dir / "HEAD").exists()
def test_git_init_initial_branch(tmp_path: pathlib.Path) -> None:
"""Test git init with custom initial branch name."""
repo = git.Git(path=tmp_path)
result = repo.init(branch="main")
assert "Initialized empty Git repository" in result
# Check if HEAD points to the correct branch
head_content = (tmp_path / ".git" / "HEAD").read_text()
assert "ref: refs/heads/main" in head_content
def test_git_init_shared(tmp_path: pathlib.Path) -> None:
"""Test git init with shared repository settings."""
repo = git.Git(path=tmp_path)
# Test boolean shared
result = repo.init(shared=True)
assert "Initialized empty shared Git repository" in result
# Test string shared value
repo_dir = tmp_path / "shared_group"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(shared="group")
assert "Initialized empty shared Git repository" in result
class InitSharedFixture(t.NamedTuple):
"""Test fixture for Git.init() shared parameter behavior."""
test_id: str
shared: bool | str | None
expect_shared_repo: bool # True = "shared Git repository" in output
INIT_SHARED_FIXTURES: list[InitSharedFixture] = [
InitSharedFixture(
test_id="shared-true-passes-flag",
shared=True,
expect_shared_repo=True,
),
InitSharedFixture(
test_id="shared-false-omits-flag",
shared=False,
expect_shared_repo=False, # Key: shared=False should NOT pass --shared
),
InitSharedFixture(
test_id="shared-none-omits-flag",
shared=None,
expect_shared_repo=False,
),
InitSharedFixture(
test_id="shared-group-passes-value",
shared="group",
expect_shared_repo=True,
),
]
@pytest.mark.parametrize(
list(InitSharedFixture._fields),
INIT_SHARED_FIXTURES,
ids=[test.test_id for test in INIT_SHARED_FIXTURES],
)
def test_git_init_shared_boolean_behavior(
tmp_path: pathlib.Path,
test_id: str,
shared: bool | str | None,
expect_shared_repo: bool,
) -> None:
"""Test Git.init() shared parameter behavior.
Verifies commit 03b124c: shared=False should NOT pass --shared flag.
"""
repo_dir = tmp_path / f"init_{test_id}"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(shared=shared)
# Check for "shared Git repository" in output (not just "shared" - matches path)
if expect_shared_repo:
assert "shared git repository" in result.lower()
else:
assert "shared git repository" not in result.lower()
def test_git_init_quiet(tmp_path: pathlib.Path) -> None:
"""Test git init with quiet flag."""
repo = git.Git(path=tmp_path)
result = repo.init(quiet=True)
# Quiet mode should suppress normal output
assert result == "" or "Initialized empty Git repository" not in result
def test_git_init_object_format(tmp_path: pathlib.Path) -> None:
"""Test git init with different object formats."""
repo = git.Git(path=tmp_path)
# Test with sha1 (default)
result = repo.init(object_format="sha1")
assert "Initialized empty Git repository" in result
# Note: sha256 test is commented out as it might not be supported in all
# git versions
# repo_dir = tmp_path / "sha256"
# repo_dir.mkdir()
# repo = git.Git(path=repo_dir)
# result = repo.init(object_format="sha256")
# assert "Initialized empty Git repository" in result
def test_git_reinit(tmp_path: pathlib.Path) -> None:
"""Test reinitializing an existing repository."""
repo = git.Git(path=tmp_path)
# Initial init
first_result = repo.init()
assert "Initialized empty Git repository" in first_result
# Reinit
second_result = repo.init()
assert "Reinitialized existing Git repository" in second_result
def test_git_init_validation_errors(tmp_path: pathlib.Path) -> None:
"""Test validation errors in git init."""
repo = git.Git(path=tmp_path)
# Test invalid template type
with pytest.raises(TypeError, match="template must be a string or Path"):
repo.init(template=123) # type: ignore
# Test non-existent template directory
with pytest.raises(ValueError, match="template directory does not exist"):
repo.init(template=str(tmp_path / "nonexistent"))
# Test invalid object format
with pytest.raises(
ValueError,
match="object_format must be either 'sha1' or 'sha256'",
):
repo.init(object_format="invalid") # type: ignore
# Test specifying both branch and initial_branch
with pytest.raises(
ValueError,
match="Cannot specify both branch and initial_branch",
):
repo.init(branch="main", initial_branch="master")
# Test branch name with whitespace
with pytest.raises(ValueError, match="Branch name cannot contain whitespace"):
repo.init(branch="main branch")
# Test invalid shared value
with pytest.raises(ValueError, match="Invalid shared value"):
repo.init(shared="invalid")
# Test invalid octal number for shared
with pytest.raises(ValueError, match="Invalid shared value"):
repo.init(shared="8888") # Invalid octal number
# Test octal number out of range
with pytest.raises(ValueError, match="Invalid shared value"):
repo.init(shared="1000") # Octal number > 0777
# Test non-existent directory with make_parents=False
non_existent = tmp_path / "non_existent"
with pytest.raises(FileNotFoundError, match="Directory does not exist"):
repo = git.Git(path=non_existent)
repo.init(make_parents=False)
def test_git_init_shared_octal(tmp_path: pathlib.Path) -> None:
"""Test git init with shared octal permissions."""
repo = git.Git(path=tmp_path)
# Test valid octal numbers
for octal in ["0660", "0644", "0755"]:
repo_dir = tmp_path / f"shared_{octal}"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(shared=octal)
assert "Initialized empty shared Git repository" in result
def test_git_init_shared_values(tmp_path: pathlib.Path) -> None:
"""Test git init with all valid shared values."""
valid_values = ["false", "true", "umask", "group", "all", "world", "everybody"]
for value in valid_values:
repo_dir = tmp_path / f"shared_{value}"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
result = repo.init(shared=value)
# The output message varies between git versions and shared values
assert any(
msg in result
for msg in [
"Initialized empty Git repository",
"Initialized empty shared Git repository",
]
)
def test_git_init_ref_format(tmp_path: pathlib.Path) -> None:
"""Test git init with different ref formats."""
repo = git.Git(path=tmp_path)
# Test with files format (default)
result = repo.init()
assert "Initialized empty Git repository" in result
# Test with reftable format (requires git >= 2.37.0)
repo_dir = tmp_path / "reftable"
repo_dir.mkdir()
repo = git.Git(path=repo_dir)
try:
result = repo.init(ref_format="reftable")
assert "Initialized empty Git repository" in result
except Exception as e:
if "unknown option" in str(e):
pytest.skip("ref-format option not supported in this git version")
raise
def test_git_init_make_parents(tmp_path: pathlib.Path) -> None:
"""Test git init with make_parents flag."""
deep_path = tmp_path / "a" / "b" / "c"
# Test with make_parents=True (default)
repo = git.Git(path=deep_path)
result = repo.init()
assert "Initialized empty Git repository" in result
assert deep_path.exists()
assert (deep_path / ".git").is_dir()
# Test with make_parents=False on existing directory
existing_path = tmp_path / "existing"
existing_path.mkdir()
repo = git.Git(path=existing_path)
result = repo.init(make_parents=False)
assert "Initialized empty Git repository" in result
# =============================================================================
# GitBranchCmd Tests
# =============================================================================
class BranchDeleteFixture(t.NamedTuple):
"""Test fixture for GitBranchCmd.delete() operations."""
test_id: str
branch_name: str
force: bool
expect_success: bool
BRANCH_DELETE_FIXTURES: list[BranchDeleteFixture] = [
BranchDeleteFixture(
test_id="delete-merged-branch",
branch_name="feature-branch",
force=False,
expect_success=True,
),
BranchDeleteFixture(
test_id="delete-branch-force",
branch_name="force-delete-branch",
force=True,
expect_success=True,
),
]
@pytest.mark.parametrize(
list(BranchDeleteFixture._fields),
BRANCH_DELETE_FIXTURES,
ids=[test.test_id for test in BRANCH_DELETE_FIXTURES],
)
def test_branch_delete(
git_repo: GitSync,
test_id: str,
branch_name: str,
force: bool,
expect_success: bool,
) -> None:
"""Test GitBranchCmd.delete() with various scenarios."""
# Setup: create and checkout a branch, then switch back
git_repo.cmd.branches.create(branch=branch_name)
git_repo.cmd.checkout(branch="master")
# Get branch via Manager
branch = git_repo.cmd.branches.get(branch_name=branch_name)
assert branch is not None
# Delete the branch
branch.delete(force=force)
# Verify deletion - get() raises ObjectDoesNotExist when not found
with pytest.raises(ObjectDoesNotExist):
git_repo.cmd.branches.get(branch_name=branch_name)
class BranchRenameFixture(t.NamedTuple):
"""Test fixture for GitBranchCmd.rename() operations."""
test_id: str
original_name: str
new_name: str
force: bool
BRANCH_RENAME_FIXTURES: list[BranchRenameFixture] = [
BranchRenameFixture(
test_id="rename-simple",
original_name="old-branch",
new_name="new-branch",
force=False,
),
BranchRenameFixture(
test_id="rename-with-force",
original_name="rename-source",
new_name="rename-target",
force=True,
),
]
@pytest.mark.parametrize(
list(BranchRenameFixture._fields),
BRANCH_RENAME_FIXTURES,
ids=[test.test_id for test in BRANCH_RENAME_FIXTURES],
)
def test_branch_rename(
git_repo: GitSync,
test_id: str,
original_name: str,
new_name: str,
force: bool,
) -> None:
"""Test GitBranchCmd.rename() with various scenarios."""
# Setup: create a branch
git_repo.cmd.branches.create(branch=original_name)
git_repo.cmd.checkout(branch="master")
# Get branch and rename
branch = git_repo.cmd.branches.get(branch_name=original_name)
assert branch is not None
branch.rename(new_name, force=force)
# Verify: old name gone (raises ObjectDoesNotExist), new name exists
with pytest.raises(ObjectDoesNotExist):
git_repo.cmd.branches.get(branch_name=original_name)
assert git_repo.cmd.branches.get(branch_name=new_name) is not None
class BranchCopyFixture(t.NamedTuple):
"""Test fixture for GitBranchCmd.copy() operations."""
test_id: str
source_name: str
copy_name: str
force: bool
BRANCH_COPY_FIXTURES: list[BranchCopyFixture] = [
BranchCopyFixture(
test_id="copy-simple",
source_name="source-branch",
copy_name="copied-branch",
force=False,
),
BranchCopyFixture(
test_id="copy-with-force",
source_name="copy-source",
copy_name="copy-target",
force=True,
),
]
@pytest.mark.parametrize(
list(BranchCopyFixture._fields),
BRANCH_COPY_FIXTURES,
ids=[test.test_id for test in BRANCH_COPY_FIXTURES],
)
def test_branch_copy(
git_repo: GitSync,
test_id: str,
source_name: str,
copy_name: str,
force: bool,
) -> None:
"""Test GitBranchCmd.copy() with various scenarios."""
# Setup: create a branch
git_repo.cmd.branches.create(branch=source_name)
git_repo.cmd.checkout(branch="master")
# Get branch and copy
branch = git_repo.cmd.branches.get(branch_name=source_name)
assert branch is not None
branch.copy(copy_name, force=force)
# Verify: both branches exist
assert git_repo.cmd.branches.get(branch_name=source_name) is not None
assert git_repo.cmd.branches.get(branch_name=copy_name) is not None
class BranchUpstreamFixture(t.NamedTuple):
"""Test fixture for GitBranchCmd upstream operations."""
test_id: str
branch_name: str
upstream: str
BRANCH_UPSTREAM_FIXTURES: list[BranchUpstreamFixture] = [
BranchUpstreamFixture(
test_id="set-upstream-origin-master",
branch_name="tracking-branch",
upstream="origin/master",
),
]
@pytest.mark.parametrize(
list(BranchUpstreamFixture._fields),
BRANCH_UPSTREAM_FIXTURES,
ids=[test.test_id for test in BRANCH_UPSTREAM_FIXTURES],
)
def test_branch_set_upstream(
git_repo: GitSync,
test_id: str,
branch_name: str,
upstream: str,
) -> None:
"""Test GitBranchCmd.set_upstream() with various scenarios."""
# Setup: create a branch
git_repo.cmd.branches.create(branch=branch_name)
git_repo.cmd.checkout(branch="master")
# Get branch and set upstream
branch = git_repo.cmd.branches.get(branch_name=branch_name)
assert branch is not None
result = branch.set_upstream(upstream)
# Verify: should succeed (output contains confirmation)
assert "set up to track" in result.lower() or result == ""
def test_branch_unset_upstream(git_repo: GitSync) -> None:
"""Test GitBranchCmd.unset_upstream()."""
branch_name = "untrack-branch"
# Setup: create a branch and set upstream
git_repo.cmd.branches.create(branch=branch_name)
git_repo.cmd.checkout(branch="master")
branch = git_repo.cmd.branches.get(branch_name=branch_name)
assert branch is not None
# Set then unset upstream
branch.set_upstream("origin/master")
result = branch.unset_upstream()
# unset_upstream typically returns empty string on success
assert result == "" or "upstream" not in result.lower()
def test_branch_track(git_repo: GitSync) -> None:
"""Test GitBranchCmd.track()."""
branch_name = "tracking-test-branch"
branch = git.GitBranchCmd(path=git_repo.path, branch_name=branch_name)
result = branch.track("origin/master")
# Should create branch tracking origin/master
assert "set up to track" in result.lower()
# Verify branch was created
branches = git_repo.cmd.branches.ls()
branch_names = [b.branch_name for b in branches]
assert branch_name in branch_names
def test_branch_ls_filters(git_repo: GitSync) -> None:
"""Test GitBranchManager.ls() with filter parameters."""
# Test basic ls
branches = git_repo.cmd.branches.ls()
assert len(branches) >= 1
assert any(b.branch_name == "master" for b in branches)
# Test with --all (includes remote-tracking branches)
all_branches = git_repo.cmd.branches.ls(_all=True)
assert len(all_branches) >= len(branches)
# Test with --merged (branches merged into HEAD)
merged = git_repo.cmd.branches.ls(merged="HEAD")
assert isinstance(merged, list)
# master should be merged into HEAD
assert any(b.branch_name == "master" for b in merged)
# Test with --contains (branches containing HEAD commit)
contains = git_repo.cmd.branches.ls(contains="HEAD")
assert isinstance(contains, list)
assert any(b.branch_name == "master" for b in contains)
# Test with --sort
sorted_branches = git_repo.cmd.branches.ls(sort="refname")
assert isinstance(sorted_branches, list)
# Test with --verbose (should still extract branch names correctly)
verbose_branches = git_repo.cmd.branches.ls(verbose=True)
assert isinstance(verbose_branches, list)
assert any(b.branch_name == "master" for b in verbose_branches)
class BranchCreateFixture(t.NamedTuple):
"""Test fixture for GitBranchCmd.create() and GitBranchManager.create()."""
test_id: str
checkout: bool
expect_switch: bool
BRANCH_CREATE_FIXTURES: list[BranchCreateFixture] = [
BranchCreateFixture(
test_id="create-without-checkout",
checkout=False,
expect_switch=False,
),
BranchCreateFixture(
test_id="create-with-checkout",
checkout=True,
expect_switch=True,
),
]
@pytest.mark.parametrize(
list(BranchCreateFixture._fields),
BRANCH_CREATE_FIXTURES,
ids=[test.test_id for test in BRANCH_CREATE_FIXTURES],
)
def test_branch_cmd_create_checkout_parameter(
git_repo: GitSync,
test_id: str,
checkout: bool,
expect_switch: bool,
) -> None:
"""Test GitBranchCmd.create() checkout parameter behavior.
Verifies commit 4b8a0f7: create() uses 'git branch' instead of 'checkout -b',
and only switches HEAD when checkout=True.
"""
branch_name = f"test-create-{test_id}"
# Record current branch before creating
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
# Create branch using GitBranchCmd
branch_cmd = git.GitBranchCmd(path=git_repo.path, branch_name=branch_name)
result = branch_cmd.create(checkout=checkout)
# Should succeed (empty string or no fatal error)
assert "fatal" not in result.lower() or "already exists" in result.lower()
# Verify branch was created
branches = git_repo.cmd.branches.ls()
branch_names = [b.branch_name for b in branches]
assert branch_name in branch_names
# Check if HEAD switched
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
if expect_switch:
assert current_after == branch_name
else:
assert current_after == current_before
@pytest.mark.parametrize(
list(BranchCreateFixture._fields),
BRANCH_CREATE_FIXTURES,
ids=[test.test_id for test in BRANCH_CREATE_FIXTURES],
)
def test_branch_manager_create_checkout_parameter(
git_repo: GitSync,
test_id: str,
checkout: bool,
expect_switch: bool,
) -> None:
"""Test GitBranchManager.create() checkout parameter behavior.
Verifies commit 4b8a0f7: create() uses 'git branch' instead of 'checkout -b',
and only switches HEAD when checkout=True.
"""
branch_name = f"test-mgr-create-{test_id}"
# Record current branch before creating
current_before = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
# Create branch using GitBranchManager
result = git_repo.cmd.branches.create(branch=branch_name, checkout=checkout)
# Should succeed (empty string or no fatal error)
assert "fatal" not in result.lower() or "already exists" in result.lower()
# Verify branch was created
branches = git_repo.cmd.branches.ls()
branch_names = [b.branch_name for b in branches]
assert branch_name in branch_names
# Check if HEAD switched
current_after = git_repo.cmd.symbolic_ref(name="HEAD", short=True)
if expect_switch:
assert current_after == branch_name
else:
assert current_after == current_before
# =============================================================================
# GitRemoteCmd Tests
# =============================================================================
class RemoteSetBranchesFixture(t.NamedTuple):
"""Test fixture for GitRemoteCmd.set_branches() operations."""
test_id: str
branches: tuple[str, ...]
add: bool
REMOTE_SET_BRANCHES_FIXTURES: list[RemoteSetBranchesFixture] = [
RemoteSetBranchesFixture(
test_id="set-single-branch",
branches=("master",),
add=False,
),
RemoteSetBranchesFixture(
test_id="set-multiple-branches",
branches=("master", "develop"),
add=False,
),
RemoteSetBranchesFixture(
test_id="add-branch",
branches=("feature",),
add=True,
),
]
@pytest.mark.parametrize(
list(RemoteSetBranchesFixture._fields),
REMOTE_SET_BRANCHES_FIXTURES,
ids=[test.test_id for test in REMOTE_SET_BRANCHES_FIXTURES],
)
def test_remote_set_branches(
git_repo: GitSync,
test_id: str,
branches: tuple[str, ...],
add: bool,
) -> None:
"""Test GitRemoteCmd.set_branches() with various scenarios."""
remote = git_repo.cmd.remotes.get(remote_name="origin")
assert remote is not None
# set_branches should succeed without error
result = remote.set_branches(*branches, add=add)
assert result == ""
class RemoteSetHeadFixture(t.NamedTuple):
"""Test fixture for GitRemoteCmd.set_head() operations."""
test_id: str
branch: str | None
auto: bool
delete: bool
REMOTE_SET_HEAD_FIXTURES: list[RemoteSetHeadFixture] = [
RemoteSetHeadFixture(
test_id="set-head-auto",
branch=None,
auto=True,
delete=False,
),
RemoteSetHeadFixture(
test_id="set-head-explicit",
branch="master",
auto=False,
delete=False,
),
]
@pytest.mark.parametrize(
list(RemoteSetHeadFixture._fields),
REMOTE_SET_HEAD_FIXTURES,
ids=[test.test_id for test in REMOTE_SET_HEAD_FIXTURES],
)
def test_remote_set_head(
git_repo: GitSync,
test_id: str,
branch: str | None,
auto: bool,
delete: bool,
) -> None:
"""Test GitRemoteCmd.set_head() with various scenarios."""
remote = git_repo.cmd.remotes.get(remote_name="origin")
assert remote is not None
result = remote.set_head(branch, auto=auto, delete=delete)
# set_head returns either confirmation message or empty string
if auto:
result_lower = result.lower()
assert "set to" in result_lower or "unchanged" in result_lower or result == ""
else:
assert result == "" or "head" in result.lower()
class RemoteUpdateFixture(t.NamedTuple):
"""Test fixture for GitRemoteCmd.update() operations."""
test_id: str
prune: bool
REMOTE_UPDATE_FIXTURES: list[RemoteUpdateFixture] = [
RemoteUpdateFixture(
test_id="update-simple",
prune=False,
),
RemoteUpdateFixture(
test_id="update-with-prune",
prune=True,
),
]
@pytest.mark.parametrize(
list(RemoteUpdateFixture._fields),
REMOTE_UPDATE_FIXTURES,
ids=[test.test_id for test in REMOTE_UPDATE_FIXTURES],
)
def test_remote_update(
git_repo: GitSync,
test_id: str,
prune: bool,
) -> None:
"""Test GitRemoteCmd.update() with various scenarios."""
remote = git_repo.cmd.remotes.get(remote_name="origin")
assert remote is not None
result = remote.update(prune=prune)
# update typically returns "Fetching <remote>" message
assert "fetching" in result.lower() or result == ""
class RemoteShowNoQueryRemotesFixture(t.NamedTuple):
"""Test fixture for GitRemoteCmd.show() no_query_remotes parameter."""
test_id: str
no_query_remotes: bool | None
expect_n_flag: bool
REMOTE_SHOW_NO_QUERY_REMOTES_FIXTURES: list[RemoteShowNoQueryRemotesFixture] = [
RemoteShowNoQueryRemotesFixture(
test_id="no-query-remotes-none",
no_query_remotes=None,
expect_n_flag=False,
),
RemoteShowNoQueryRemotesFixture(
test_id="no-query-remotes-true",
no_query_remotes=True,
expect_n_flag=True,
),
RemoteShowNoQueryRemotesFixture(
test_id="no-query-remotes-false",
no_query_remotes=False,
expect_n_flag=False,
),
]
@pytest.mark.parametrize(
list(RemoteShowNoQueryRemotesFixture._fields),
REMOTE_SHOW_NO_QUERY_REMOTES_FIXTURES,
ids=[test.test_id for test in REMOTE_SHOW_NO_QUERY_REMOTES_FIXTURES],
)
def test_remote_cmd_show_no_query_remotes(
git_repo: GitSync,
test_id: str,
no_query_remotes: bool | None,
expect_n_flag: bool,
) -> None:
"""Test GitRemoteCmd.show() with no_query_remotes parameter."""
remote = git_repo.cmd.remotes.get(remote_name="origin")
assert remote is not None
# show() should succeed without error
result = remote.show(no_query_remotes=no_query_remotes)
assert isinstance(result, str)
# Result should contain remote name info
assert "origin" in result.lower() or "fetch" in result.lower()
@pytest.mark.parametrize(
list(RemoteShowNoQueryRemotesFixture._fields),
REMOTE_SHOW_NO_QUERY_REMOTES_FIXTURES,
ids=[test.test_id for test in REMOTE_SHOW_NO_QUERY_REMOTES_FIXTURES],
)
def test_remote_manager_show_no_query_remotes(
git_repo: GitSync,
test_id: str,
no_query_remotes: bool | None,
expect_n_flag: bool,
) -> None:
"""Test GitRemoteManager.show() with no_query_remotes parameter."""
# show() should succeed without error
result = git_repo.cmd.remotes.show(name="origin", no_query_remotes=no_query_remotes)
assert isinstance(result, str)
# Result should contain remote name info
assert "origin" in result.lower() or "fetch" in result.lower()
class RemoteAddParamFixture(t.NamedTuple):
"""Test fixture for GitRemoteManager.add() parameter wiring."""
test_id: str
fetch: bool | None
track: str | None
master: str | None
REMOTE_ADD_PARAM_FIXTURES: list[RemoteAddParamFixture] = [
RemoteAddParamFixture(
test_id="fetch-only",
fetch=True,
track=None,
master=None,
),
RemoteAddParamFixture(
test_id="track-branch",
fetch=None,
track="master",
master=None,
),
RemoteAddParamFixture(
test_id="master-branch",
fetch=None,
track=None,
master="master",
),
# Note: fetch=True with track requires the tracked branch to exist.
# We test track and master separately since their flag wiring is independent.
]
@pytest.mark.parametrize(
list(RemoteAddParamFixture._fields),
REMOTE_ADD_PARAM_FIXTURES,
ids=[test.test_id for test in REMOTE_ADD_PARAM_FIXTURES],
)
def test_remote_manager_add_params(
git_repo: GitSync,
create_git_remote_repo: CreateRepoFn,
test_id: str,
fetch: bool | None,
track: str | None,
master: str | None,
) -> None:
"""Test GitRemoteManager.add() parameter wiring.
Verifies commit 5c00880: fetch, track, master params were not wired.
"""
remote_repo = create_git_remote_repo()
remote_name = f"test_remote_{test_id.replace('-', '_')}"
# Add remote with params - should not error
result = git_repo.cmd.remotes.add(
name=remote_name,
url=f"file://{remote_repo}",
fetch=fetch,
track=track,
master=master,
)
# Should succeed (empty string or no fatal error)
assert "fatal" not in result.lower()
# Verify remote was added
remotes = git_repo.cmd.remotes.ls()
remote_names = [r.remote_name for r in remotes]
assert remote_name in remote_names
# =============================================================================
# GitTagCmd / GitTagManager Tests
# =============================================================================
class TagCreateFixture(t.NamedTuple):
"""Test fixture for GitTagManager.create() operations."""
test_id: str
tag_name: str
message: str | None