1
- import os
2
1
import subprocess
3
2
from unittest .mock import MagicMock , patch
4
3
5
- import pytest
6
-
7
4
from scripts .release .branch_detection import (
8
5
get_cache_scope ,
9
6
get_current_branch ,
10
- get_version_id ,
11
- is_evg_patch ,
12
- is_running_in_evg ,
13
7
)
14
8
15
9
16
10
class TestGetCurrentBranch :
17
11
"""Test branch detection logic for different scenarios."""
18
12
19
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
20
13
@patch ("subprocess.run" )
21
- def test_local_development_master_branch (self , mock_run , mock_is_evg ):
22
- """Test local development on master branch."""
23
- mock_is_evg .return_value = False
14
+ def test_master_branch (self , mock_run ):
15
+ """Test detection of master branch."""
24
16
mock_run .return_value = MagicMock (stdout = "master\n " , returncode = 0 )
25
17
26
18
result = get_current_branch ()
27
19
28
20
assert result == "master"
29
21
mock_run .assert_called_once_with (
30
- ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ], capture_output = True , text = True , check = True
22
+ ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ],
23
+ capture_output = True ,
24
+ text = True ,
25
+ check = True
31
26
)
32
27
33
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
34
28
@patch ("subprocess.run" )
35
- def test_local_development_feature_branch (self , mock_run , mock_is_evg ):
36
- """Test local development on feature branch."""
37
- mock_is_evg .return_value = False
29
+ def test_feature_branch (self , mock_run ):
30
+ """Test detection of feature branch."""
38
31
mock_run .return_value = MagicMock (stdout = "feature/new-cache\n " , returncode = 0 )
39
32
40
33
result = get_current_branch ()
41
34
42
35
assert result == "feature/new-cache"
36
+ mock_run .assert_called_once_with (
37
+ ["git" , "rev-parse" , "--abbrev-ref" , "HEAD" ],
38
+ capture_output = True ,
39
+ text = True ,
40
+ check = True
41
+ )
43
42
44
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
45
43
@patch ("subprocess.run" )
46
- def test_local_development_detached_head (self , mock_run , mock_is_evg ):
47
- """Test local development in detached HEAD state."""
48
- mock_is_evg .return_value = False
44
+ def test_detached_head (self , mock_run ):
45
+ """Test detection when in detached HEAD state."""
49
46
mock_run .return_value = MagicMock (stdout = "HEAD\n " , returncode = 0 )
50
47
51
48
result = get_current_branch ()
52
49
53
50
assert result == "master" # fallback to master
54
51
55
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
56
52
@patch ("subprocess.run" )
57
- def test_local_development_git_error (self , mock_run , mock_is_evg ):
58
- """Test local development when git command fails."""
59
- mock_is_evg .return_value = False
60
- mock_run .side_effect = subprocess .CalledProcessError (1 , "git" )
53
+ def test_empty_output (self , mock_run ):
54
+ """Test detection when git returns empty output."""
55
+ mock_run .return_value = MagicMock (stdout = "\n " , returncode = 0 )
61
56
62
57
result = get_current_branch ()
63
58
64
59
assert result == "master" # fallback to master
65
60
66
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
67
- @patch ("scripts.release.branch_detection.is_evg_patch" )
68
- @patch ("subprocess.run" )
69
- def test_evergreen_non_patch_build (self , mock_run , mock_is_patch , mock_is_evg ):
70
- """Test Evergreen non-patch build."""
71
- mock_is_evg .return_value = True
72
- mock_is_patch .return_value = False
73
- mock_run .return_value = MagicMock (stdout = "master\n " , returncode = 0 )
74
-
75
- result = get_current_branch ()
76
-
77
- assert result == "master"
78
-
79
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
80
- @patch ("scripts.release.branch_detection.is_evg_patch" )
81
61
@patch ("subprocess.run" )
82
- def test_evergreen_patch_build_branch_detection (self , mock_run , mock_is_patch , mock_is_evg ):
83
- """Test Evergreen patch build with successful branch detection."""
84
- mock_is_evg .return_value = True
85
- mock_is_patch .return_value = True
86
-
87
- # Mock git for-each-ref output
88
- mock_run .side_effect = [
89
- MagicMock (
90
- stdout = "origin/feature/cache-improvement abc123\n origin/evg-pr-test-123 abc123\n origin/main def456\n " ,
91
- returncode = 0 ,
92
- ),
93
- MagicMock (stdout = "abc123\n " , returncode = 0 ),
94
- ]
62
+ def test_git_command_fails (self , mock_run ):
63
+ """Test fallback when git command fails."""
64
+ mock_run .side_effect = subprocess .CalledProcessError (1 , "git" )
95
65
96
66
result = get_current_branch ()
97
67
98
- assert result == "feature/cache-improvement"
68
+ assert result == "master" # fallback to master
99
69
100
- @patch ("scripts.release.branch_detection.is_running_in_evg" )
101
- @patch ("scripts.release.branch_detection.is_evg_patch" )
102
70
@patch ("subprocess.run" )
103
- def test_evergreen_patch_build_fallback (self , mock_run , mock_is_patch , mock_is_evg ):
104
- """Test Evergreen patch build fallback when branch detection fails."""
105
- mock_is_evg .return_value = True
106
- mock_is_patch .return_value = True
107
- mock_run .side_effect = subprocess .CalledProcessError (1 , "git" )
71
+ def test_git_not_found (self , mock_run ):
72
+ """Test fallback when git is not found."""
73
+ mock_run .side_effect = FileNotFoundError ("git not found" )
108
74
109
75
result = get_current_branch ()
110
76
@@ -114,124 +80,31 @@ def test_evergreen_patch_build_fallback(self, mock_run, mock_is_patch, mock_is_e
114
80
class TestGetCacheScope :
115
81
"""Test cache scope generation for different scenarios."""
116
82
117
- @patch ("scripts.release.branch_detection.get_current_branch" )
118
- @patch ("scripts.release.branch_detection.is_evg_patch" )
119
- @patch ("scripts.release.branch_detection.get_version_id" )
120
- def test_master_branch_non_patch (self , mock_version_id , mock_is_patch , mock_branch ):
121
- """Test cache scope for master branch non-patch build."""
122
- mock_branch .return_value = "master"
123
- mock_is_patch .return_value = False
124
- mock_version_id .return_value = None
125
-
126
- result = get_cache_scope ()
127
-
128
- assert result == "master"
129
83
130
84
@patch ("scripts.release.branch_detection.get_current_branch" )
131
- @patch ("scripts.release.branch_detection.is_evg_patch" )
132
- @patch ("scripts.release.branch_detection.get_version_id" )
133
- def test_feature_branch_non_patch (self , mock_version_id , mock_is_patch , mock_branch ):
134
- """Test cache scope for feature branch non-patch build."""
85
+ def test_feature_branch (self , mock_branch ):
86
+ """Test cache scope for feature branch."""
135
87
mock_branch .return_value = "feature/new-cache"
136
- mock_is_patch .return_value = False
137
- mock_version_id .return_value = None
138
88
139
89
result = get_cache_scope ()
140
90
141
91
assert result == "feature-new-cache"
142
92
143
93
@patch ("scripts.release.branch_detection.get_current_branch" )
144
- @patch ("scripts.release.branch_detection.is_evg_patch" )
145
- @patch ("scripts.release.branch_detection.get_version_id" )
146
- def test_patch_build_with_version_id (self , mock_version_id , mock_is_patch , mock_branch ):
147
- """Test cache scope for patch build with version ID."""
148
- mock_branch .return_value = "feature/new-cache"
149
- mock_is_patch .return_value = True
150
- mock_version_id .return_value = "6899b7e35bfaee00077db986"
151
-
152
- result = get_cache_scope ()
153
-
154
- assert result == "feature-new-cache-6899b7e3"
155
-
156
- @patch ("scripts.release.branch_detection.get_current_branch" )
157
- @patch ("scripts.release.branch_detection.is_evg_patch" )
158
- @patch ("scripts.release.branch_detection.get_version_id" )
159
- def test_patch_build_without_version_id (self , mock_version_id , mock_is_patch , mock_branch ):
160
- """Test cache scope for patch build without version ID."""
161
- mock_branch .return_value = "feature/new-cache"
162
- mock_is_patch .return_value = True
163
- mock_version_id .return_value = None
164
-
165
- result = get_cache_scope ()
166
-
167
- assert result == "feature-new-cache"
168
-
169
- @patch ("scripts.release.branch_detection.get_current_branch" )
170
- @patch ("scripts.release.branch_detection.is_evg_patch" )
171
- @patch ("scripts.release.branch_detection.get_version_id" )
172
- def test_branch_name_sanitization (self , mock_version_id , mock_is_patch , mock_branch ):
94
+ def test_branch_name_sanitization (self , mock_branch ):
173
95
"""Test branch name sanitization for cache scope."""
174
96
mock_branch .return_value = "Feature/NEW_cache@123"
175
- mock_is_patch .return_value = False
176
- mock_version_id .return_value = None
177
97
178
98
result = get_cache_scope ()
179
99
180
100
assert result == "feature-new_cache-123"
181
101
102
+
182
103
@patch ("scripts.release.branch_detection.get_current_branch" )
183
- @patch ("scripts.release.branch_detection.is_evg_patch" )
184
- @patch ("scripts.release.branch_detection.get_version_id" )
185
- def test_dependabot_branch (self , mock_version_id , mock_is_patch , mock_branch ):
186
- """Test cache scope for dependabot branch."""
187
- mock_branch .return_value = "dependabot/npm_and_yarn/lodash-4.17.21"
188
- mock_is_patch .return_value = False
189
- mock_version_id .return_value = None
104
+ def test_complex_branch_name (self , mock_branch ):
105
+ """Test cache scope for complex branch name with special characters."""
106
+ mock_branch .return_value = "user/feature-123_test.branch"
190
107
191
108
result = get_cache_scope ()
192
109
193
- assert result == "dependabot-npm_and_yarn-lodash-4.17.21"
194
-
195
-
196
- class TestEnvironmentDetection :
197
- """Test environment detection functions."""
198
-
199
- def test_is_evg_patch_true (self ):
200
- """Test is_evg_patch returns True when is_patch is 'true'."""
201
- with patch .dict (os .environ , {"is_patch" : "true" }):
202
- assert is_evg_patch () is True
203
-
204
- def test_is_evg_patch_false (self ):
205
- """Test is_evg_patch returns False when is_patch is 'false'."""
206
- with patch .dict (os .environ , {"is_patch" : "false" }):
207
- assert is_evg_patch () is False
208
-
209
- def test_is_evg_patch_default (self ):
210
- """Test is_evg_patch returns False when is_patch is not set."""
211
- with patch .dict (os .environ , {}, clear = True ):
212
- assert is_evg_patch () is False
213
-
214
- def test_is_running_in_evg_true (self ):
215
- """Test is_running_in_evg returns True when RUNNING_IN_EVG is 'true'."""
216
- with patch .dict (os .environ , {"RUNNING_IN_EVG" : "true" }):
217
- assert is_running_in_evg () is True
218
-
219
- def test_is_running_in_evg_false (self ):
220
- """Test is_running_in_evg returns False when RUNNING_IN_EVG is 'false'."""
221
- with patch .dict (os .environ , {"RUNNING_IN_EVG" : "false" }):
222
- assert is_running_in_evg () is False
223
-
224
- def test_is_running_in_evg_default (self ):
225
- """Test is_running_in_evg returns False when RUNNING_IN_EVG is not set."""
226
- with patch .dict (os .environ , {}, clear = True ):
227
- assert is_running_in_evg () is False
228
-
229
- def test_get_version_id_set (self ):
230
- """Test get_version_id returns value when version_id is set."""
231
- with patch .dict (os .environ , {"version_id" : "6899b7e35bfaee00077db986" }):
232
- assert get_version_id () == "6899b7e35bfaee00077db986"
233
-
234
- def test_get_version_id_not_set (self ):
235
- """Test get_version_id returns None when version_id is not set."""
236
- with patch .dict (os .environ , {}, clear = True ):
237
- assert get_version_id () is None
110
+ assert result == "user-feature-123_test.branch"
0 commit comments