Skip to content

Commit 9f0457f

Browse files
committed
feat: refactor branch switching logic in hook testing utilities
1 parent d814d63 commit 9f0457f

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

tools/internal-test/lib/hook_testing.py

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,7 @@ def test_branch_validation(self):
4040
self.log.section("🔒 Testing Branch Validation")
4141

4242
# Return to main branch and create final commit
43-
main_branch = None
44-
try:
45-
self.cmd.run_command(
46-
["git", "checkout", "main"], cwd=self.config.target_dir, check=False
47-
)
48-
main_branch = "main"
49-
except:
50-
try:
51-
self.cmd.run_command(
52-
["git", "checkout", "master"],
53-
cwd=self.config.target_dir,
54-
check=False,
55-
)
56-
main_branch = "master"
57-
except:
58-
self.log.error("Could not checkout main or master branch")
59-
sys.exit(1)
43+
main_branch = self._switch_to_default_branch()
6044

6145
if not main_branch:
6246
self.log.error("Could not determine main branch name")
@@ -79,41 +63,13 @@ def test_branch_validation(self):
7963
self._test_valid_branch()
8064

8165
# Return to main branch before testing invalid branch
82-
try:
83-
self.cmd.run_command(
84-
["git", "checkout", "main"], cwd=self.config.target_dir, check=False
85-
)
86-
except:
87-
try:
88-
self.cmd.run_command(
89-
["git", "checkout", "master"],
90-
cwd=self.config.target_dir,
91-
check=False,
92-
)
93-
except:
94-
pass
66+
self._switch_to_default_branch()
9567

9668
# Test invalid branch
9769
self._test_invalid_branch()
9870

9971
# Return to main branch
100-
main_branch = None
101-
try:
102-
self.cmd.run_command(
103-
["git", "checkout", "main"], cwd=self.config.target_dir, check=False
104-
)
105-
main_branch = "main"
106-
except:
107-
try:
108-
self.cmd.run_command(
109-
["git", "checkout", "master"],
110-
cwd=self.config.target_dir,
111-
check=False,
112-
)
113-
main_branch = "master"
114-
except:
115-
self.log.error("Could not checkout main or master branch")
116-
sys.exit(1)
72+
main_branch = self._switch_to_default_branch()
11773

11874
if not main_branch:
11975
self.log.error("Could not determine main branch name")
@@ -171,6 +127,32 @@ def test_branch_validation(self):
171127
self.log.error(f"Error: {e}")
172128
sys.exit(1)
173129

130+
def _switch_to_default_branch(self) -> str:
131+
"""
132+
Switch to the default branch (master or main) and return its name.
133+
Returns the branch name on success, exits on failure.
134+
"""
135+
result = self.cmd.run_command(
136+
["git", "checkout", "master"],
137+
cwd=self.config.target_dir,
138+
check=False,
139+
capture_output=True,
140+
)
141+
if result.returncode == 0:
142+
return "master"
143+
144+
result = self.cmd.run_command(
145+
["git", "checkout", "main"],
146+
cwd=self.config.target_dir,
147+
check=False,
148+
capture_output=True,
149+
)
150+
if result.returncode == 0:
151+
return "main"
152+
153+
self.log.error("Could not checkout master or main branch")
154+
sys.exit(1)
155+
174156
def _test_valid_branch(self):
175157
"""Test valid branch and commit"""
176158
print("")
@@ -179,7 +161,7 @@ def _test_valid_branch(self):
179161
self.log.info("Branch: feature/PRJ-123-test-feature")
180162
print("└──────────────────────────────────────────────────────────────┘")
181163
print("")
182-
164+
183165
self.cmd.run_command(
184166
["git", "checkout", "-b", "feature/PRJ-123-test-feature"],
185167
cwd=self.config.target_dir,
@@ -315,6 +297,20 @@ def _test_invalid_branch(self):
315297
else:
316298
print(result.stderr)
317299
print("═══════════════════════════════════")
300+
301+
# Unstage the test file first
302+
self.cmd.run_command(
303+
["git", "reset", "HEAD", "test_commit2.php"],
304+
cwd=self.config.target_dir,
305+
check=False,
306+
capture_output=True,
307+
)
308+
309+
# Clean up the test file
310+
test_file = self.config.target_dir / "test_commit2.php"
311+
if test_file.exists():
312+
test_file.unlink()
313+
self.log.info("Cleaned up test file")
318314
print("")
319315

320316
def test_github_actions(self):

0 commit comments

Comments
 (0)