Skip to content

Commit 526f00c

Browse files
committed
Fix test_edge_cases.py sys.argv handling for all tests
Add sys.argv mocking to all tests that instantiate Command objects directly. The get_added_files() method reads from sys.argv[1:], so tests must set sys.argv before creating command objects or files won't be detected. Fixed tests: - test_no_diff_flag_removal - test_args_with_double_dash - test_cfg_file_filtering - test_fix_errors_flag_detection - test_fix_flag_detection - test_default_args_added - test_user_args_override_defaults - test_defaults_cfg_creation - test_oclint_version_detection - test_iwyu_correct_includes_message - test_multiple_files_processed Same pattern as test_utils_functions.py fixes in commit bf6c8b7.
1 parent 72faa05 commit 526f00c

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

tests/test_edge_cases.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111
import os
1212
import subprocess as sp
13+
import sys
1314
import tempfile
1415
from pathlib import Path
1516

@@ -47,7 +48,11 @@ def test_no_diff_flag_removal(self):
4748
temp_file = f.name
4849

4950
try:
51+
# Mock sys.argv for get_added_files()
52+
original_argv = sys.argv
53+
sys.argv = ["clang-format-hook", "--no-diff", temp_file]
5054
cmd = ClangFormatCmd(["clang-format-hook", "--no-diff", temp_file])
55+
sys.argv = original_argv
5156
assert "--no-diff" not in cmd.args
5257
assert cmd.no_diff_flag is True
5358
finally:
@@ -62,7 +67,11 @@ def test_args_with_double_dash(self):
6267

6368
try:
6469
# The -- separator should be handled correctly
70+
# Mock sys.argv for get_added_files()
71+
original_argv = sys.argv
72+
sys.argv = ["cppcheck-hook", "--", temp_file]
6573
cmd = CppcheckCmd(["cppcheck-hook", "--", temp_file])
74+
sys.argv = original_argv
6675
assert temp_file in cmd.files
6776
finally:
6877
if os.path.exists(temp_file):
@@ -101,7 +110,11 @@ def test_cfg_file_filtering(self):
101110
c_file = f.name
102111

103112
try:
113+
# Mock sys.argv for get_added_files()
114+
original_argv = sys.argv
115+
sys.argv = ["cppcheck-hook", cfg_file, c_file]
104116
cmd = CppcheckCmd(["cppcheck-hook", cfg_file, c_file])
117+
sys.argv = original_argv
105118
assert cfg_file not in cmd.files
106119
assert c_file in cmd.files
107120
finally:
@@ -121,7 +134,11 @@ def test_fix_errors_flag_detection(self):
121134
temp_file = f.name
122135

123136
try:
137+
# Mock sys.argv for get_added_files()
138+
original_argv = sys.argv
139+
sys.argv = ["clang-tidy-hook", "--fix-errors", temp_file]
124140
cmd = ClangTidyCmd(["clang-tidy-hook", "--fix-errors", temp_file])
141+
sys.argv = original_argv
125142
assert cmd.edit_in_place is True
126143
finally:
127144
if os.path.exists(temp_file):
@@ -134,7 +151,11 @@ def test_fix_flag_detection(self):
134151
temp_file = f.name
135152

136153
try:
154+
# Mock sys.argv for get_added_files()
155+
original_argv = sys.argv
156+
sys.argv = ["clang-tidy-hook", "-fix", temp_file]
137157
cmd = ClangTidyCmd(["clang-tidy-hook", "-fix", temp_file])
158+
sys.argv = original_argv
138159
assert cmd.edit_in_place is True
139160
finally:
140161
if os.path.exists(temp_file):
@@ -151,7 +172,11 @@ def test_default_args_added(self):
151172
temp_file = f.name
152173

153174
try:
175+
# Mock sys.argv for get_added_files()
176+
original_argv = sys.argv
177+
sys.argv = ["cppcheck-hook", temp_file]
154178
cmd = CppcheckCmd(["cppcheck-hook", temp_file])
179+
sys.argv = original_argv
155180
assert "-q" in cmd.args
156181
assert "--error-exitcode=1" in cmd.args
157182
assert "--enable=all" in cmd.args
@@ -166,7 +191,11 @@ def test_user_args_override_defaults(self):
166191
temp_file = f.name
167192

168193
try:
194+
# Mock sys.argv for get_added_files()
195+
original_argv = sys.argv
196+
sys.argv = ["cppcheck-hook", "--enable=warning", temp_file]
169197
cmd = CppcheckCmd(["cppcheck-hook", "--enable=warning", temp_file])
198+
sys.argv = original_argv
170199
# User's --enable=warning should be present
171200
assert "--enable=warning" in cmd.args
172201
# Default --enable=all should not be added because user provided --enable
@@ -190,7 +219,11 @@ def test_defaults_cfg_creation(self):
190219
temp_file = f.name
191220

192221
try:
222+
# Mock sys.argv for get_added_files()
223+
original_argv = sys.argv
224+
sys.argv = ["uncrustify-hook", temp_file]
193225
cmd = UncrustifyCmd(["uncrustify-hook", temp_file])
226+
sys.argv = original_argv
194227
# If no -c arg provided, defaults.cfg should be created
195228
if "-c" in cmd.args:
196229
idx = cmd.args.index("-c")
@@ -216,7 +249,11 @@ def test_oclint_version_detection(self):
216249
temp_file = f.name
217250

218251
try:
252+
# Mock sys.argv for get_added_files()
253+
original_argv = sys.argv
254+
sys.argv = ["oclint-hook", temp_file]
219255
cmd = OCLintCmd(["oclint-hook", temp_file])
256+
sys.argv = original_argv
220257
# Version should be a string
221258
assert isinstance(cmd.version, str)
222259
assert len(cmd.version) > 0
@@ -264,7 +301,11 @@ def test_iwyu_correct_includes_message(self):
264301
# Just verify the command can be instantiated
265302
from hooks.include_what_you_use import IncludeWhatYouUseCmd
266303

304+
# Mock sys.argv for get_added_files()
305+
original_argv = sys.argv
306+
sys.argv = ["include-what-you-use-hook", temp_file]
267307
cmd = IncludeWhatYouUseCmd(["include-what-you-use-hook", temp_file])
308+
sys.argv = original_argv
268309
assert cmd.command == "include-what-you-use"
269310
finally:
270311
if os.path.exists(temp_file):
@@ -309,7 +350,11 @@ def test_multiple_files_processed(self):
309350
with open(file2, "w") as f:
310351
f.write("int func() { return 1; }\n")
311352

353+
# Mock sys.argv for get_added_files()
354+
original_argv = sys.argv
355+
sys.argv = ["cppcheck-hook", file1, file2]
312356
cmd = CppcheckCmd(["cppcheck-hook", file1, file2])
357+
sys.argv = original_argv
313358
assert file1 in cmd.files
314359
assert file2 in cmd.files
315360
assert len(cmd.files) == 2

0 commit comments

Comments
 (0)