@@ -11,50 +11,84 @@ class TestGetCurrentBranch:
11
11
"""Test branch detection logic for different scenarios."""
12
12
13
13
@patch ("subprocess.run" )
14
- def test_master_branch (self , mock_run ):
15
- """Test detection of master branch."""
16
- mock_run .return_value = MagicMock (stdout = "master\n " , returncode = 0 )
14
+ def test_ci_environment_with_original_branch (self , mock_run ):
15
+ """Test detection of original branch in CI environment like Evergreen."""
16
+ # Mock the sequence of git commands
17
+ def side_effect (cmd , ** kwargs ):
18
+ if cmd == ["git" , "rev-parse" , "HEAD" ]:
19
+ return MagicMock (stdout = "4cecea664abcd1234567890\n " , returncode = 0 )
20
+ elif cmd == ["git" , "for-each-ref" , "--format=%(refname:short) %(objectname)" , "refs/remotes/origin" ]:
21
+ return MagicMock (stdout = "origin/master 1234567890abcdef\n origin/add-caching 4cecea664abcd1234567890\n origin/evg-pr-test-12345 4cecea664abcd1234567890\n " , returncode = 0 )
22
+ elif cmd == ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ]:
23
+ return MagicMock (stdout = "evg-pr-test-12345\n " , returncode = 0 )
24
+ return MagicMock (stdout = "" , returncode = 1 )
25
+
26
+ mock_run .side_effect = side_effect
17
27
18
28
result = get_current_branch ()
19
29
20
- assert result == "master"
21
- mock_run .assert_called_once_with (
22
- ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ], capture_output = True , text = True , check = True
23
- )
30
+ assert result == "add-caching"
24
31
25
32
@patch ("subprocess.run" )
26
- def test_feature_branch (self , mock_run ):
27
- """Test detection of feature branch."""
28
- mock_run .return_value = MagicMock (stdout = "feature/new-cache\n " , returncode = 0 )
33
+ def test_master_branch_fallback (self , mock_run ):
34
+ """Test detection of master branch using fallback method."""
35
+ # Mock the sequence where sophisticated method fails but fallback works
36
+ def side_effect (cmd , ** kwargs ):
37
+ if cmd == ["git" , "rev-parse" , "HEAD" ]:
38
+ return MagicMock (stdout = "4cecea664abcd1234567890\n " , returncode = 0 )
39
+ elif cmd == ["git" , "for-each-ref" , "--format=%(refname:short) %(objectname)" , "refs/remotes/origin" ]:
40
+ raise subprocess .CalledProcessError (1 , "git" ) # This fails, triggering fallback
41
+ elif cmd == ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ]:
42
+ return MagicMock (stdout = "master\n " , returncode = 0 )
43
+ return MagicMock (stdout = "" , returncode = 1 )
44
+
45
+ mock_run .side_effect = side_effect
29
46
30
47
result = get_current_branch ()
31
48
32
- assert result == "feature/new-cache"
33
- mock_run .assert_called_once_with (
34
- ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ], capture_output = True , text = True , check = True
35
- )
49
+ assert result == "master"
36
50
37
51
@patch ("subprocess.run" )
38
- def test_detached_head (self , mock_run ):
39
- """Test detection when in detached HEAD state."""
40
- mock_run .return_value = MagicMock (stdout = "HEAD\n " , returncode = 0 )
52
+ def test_detached_head_fallback (self , mock_run ):
53
+ """Test detection when in detached HEAD state using fallback."""
54
+ # Mock the sequence where sophisticated method fails and fallback returns HEAD
55
+ def side_effect (cmd , ** kwargs ):
56
+ if cmd == ["git" , "rev-parse" , "HEAD" ]:
57
+ return MagicMock (stdout = "4cecea664abcd1234567890\n " , returncode = 0 )
58
+ elif cmd == ["git" , "for-each-ref" , "--format=%(refname:short) %(objectname)" , "refs/remotes/origin" ]:
59
+ raise subprocess .CalledProcessError (1 , "git" ) # This fails, triggering fallback
60
+ elif cmd == ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ]:
61
+ return MagicMock (stdout = "HEAD\n " , returncode = 0 )
62
+ return MagicMock (stdout = "" , returncode = 1 )
63
+
64
+ mock_run .side_effect = side_effect
41
65
42
66
result = get_current_branch ()
43
67
44
68
assert result == "master" # fallback to master
45
69
46
70
@patch ("subprocess.run" )
47
- def test_empty_output (self , mock_run ):
48
- """Test detection when git returns empty output."""
49
- mock_run .return_value = MagicMock (stdout = "\n " , returncode = 0 )
71
+ def test_ci_branch_filtered_out_in_fallback (self , mock_run ):
72
+ """Test that CI auto-generated branches are filtered out in fallback."""
73
+ # Mock the sequence where sophisticated method fails and fallback returns CI branch
74
+ def side_effect (cmd , ** kwargs ):
75
+ if cmd == ["git" , "rev-parse" , "HEAD" ]:
76
+ return MagicMock (stdout = "4cecea664abcd1234567890\n " , returncode = 0 )
77
+ elif cmd == ["git" , "for-each-ref" , "--format=%(refname:short) %(objectname)" , "refs/remotes/origin" ]:
78
+ raise subprocess .CalledProcessError (1 , "git" ) # This fails, triggering fallback
79
+ elif cmd == ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ]:
80
+ return MagicMock (stdout = "evg-pr-test-12345\n " , returncode = 0 )
81
+ return MagicMock (stdout = "" , returncode = 1 )
82
+
83
+ mock_run .side_effect = side_effect
50
84
51
85
result = get_current_branch ()
52
86
53
- assert result == "master" # fallback to master
87
+ assert result == "master" # fallback to master when CI branch is detected
54
88
55
89
@patch ("subprocess.run" )
56
90
def test_git_command_fails (self , mock_run ):
57
- """Test fallback when git command fails ."""
91
+ """Test fallback when all git commands fail ."""
58
92
mock_run .side_effect = subprocess .CalledProcessError (1 , "git" )
59
93
60
94
result = get_current_branch ()
0 commit comments