-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_main.py
More file actions
453 lines (335 loc) · 13.1 KB
/
test_main.py
File metadata and controls
453 lines (335 loc) · 13.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
from unittest.mock import MagicMock
from unittest.mock import patch
def test_parser_defaults():
"""Test argument parser with default values."""
from mxdev.main import parser
args = parser.parse_args([])
assert args.configuration == "mx.ini"
assert args.no_fetch is False
assert args.fetch_only is False
assert args.offline is False
assert args.threads is None
assert args.silent is False
assert args.verbose is False
def test_parser_configuration():
"""Test argument parser with configuration file."""
from mxdev.main import parser
args = parser.parse_args(["-c", "custom.ini"])
assert args.configuration == "custom.ini"
args = parser.parse_args(["--configuration", "another.ini"])
assert args.configuration == "another.ini"
def test_parser_no_fetch():
"""Test argument parser with --no-fetch flag."""
from mxdev.main import parser
args = parser.parse_args(["--no-fetch"])
assert args.no_fetch is True
args = parser.parse_args(["-n"])
assert args.no_fetch is True
def test_parser_fetch_only():
"""Test argument parser with --fetch-only flag."""
from mxdev.main import parser
args = parser.parse_args(["--fetch-only"])
assert args.fetch_only is True
args = parser.parse_args(["-f"])
assert args.fetch_only is True
def test_parser_offline():
"""Test argument parser with --offline flag."""
from mxdev.main import parser
args = parser.parse_args(["--offline"])
assert args.offline is True
args = parser.parse_args(["-o"])
assert args.offline is True
def test_parser_threads():
"""Test argument parser with --threads flag."""
from mxdev.main import parser
args = parser.parse_args(["--threads", "8"])
assert args.threads == 8
args = parser.parse_args(["-t", "16"])
assert args.threads == 16
def test_parser_silent():
"""Test argument parser with --silent flag."""
from mxdev.main import parser
args = parser.parse_args(["--silent"])
assert args.silent is True
args = parser.parse_args(["-s"])
assert args.silent is True
def test_parser_verbose():
"""Test argument parser with --verbose flag."""
from mxdev.main import parser
args = parser.parse_args(["--verbose"])
assert args.verbose is True
args = parser.parse_args(["-v"])
assert args.verbose is True
def test_parser_version(capsys):
"""Test --version prints version and exits."""
from mxdev.main import __version__
from mxdev.main import parser
import pytest
with pytest.raises(SystemExit) as exc_info:
parser.parse_args(["--version"])
# Verify clean exit
assert exc_info.value.code == 0
# Verify output contains version string
captured = capsys.readouterr()
assert __version__ in captured.out
assert "." in captured.out # Version has dots (X.Y.Z)
def test_version_format():
"""Test version format is valid."""
from mxdev.main import __version__
# Version should not be the fallback
assert __version__ != "unknown (not installed)"
# Version should contain dots (semantic versioning)
assert "." in __version__
# Version should be a string
assert isinstance(__version__, str)
def test_supports_unicode_with_utf8():
"""Test supports_unicode returns True for UTF-8 encoding."""
from mxdev.main import supports_unicode
# Mock stdout with UTF-8 encoding
mock_stdout = MagicMock()
mock_stdout.encoding = "utf-8"
with patch("sys.stdout", mock_stdout):
assert supports_unicode() is True
def test_supports_unicode_with_cp1252():
"""Test supports_unicode returns False for cp1252 encoding."""
from mxdev.main import supports_unicode
# Mock stdout with cp1252 encoding
mock_stdout = MagicMock()
mock_stdout.encoding = "cp1252"
with patch("sys.stdout", mock_stdout):
assert supports_unicode() is False
def test_supports_unicode_with_no_encoding():
"""Test supports_unicode returns False when encoding is None."""
from mxdev.main import supports_unicode
# Mock stdout with no encoding
mock_stdout = MagicMock()
mock_stdout.encoding = None
with patch("sys.stdout", mock_stdout):
assert supports_unicode() is False
def test_supports_unicode_with_ascii():
"""Test supports_unicode returns False for ASCII encoding."""
from mxdev.main import supports_unicode
# Mock stdout with ASCII encoding
mock_stdout = MagicMock()
mock_stdout.encoding = "ascii"
with patch("sys.stdout", mock_stdout):
assert supports_unicode() is False
def test_supports_unicode_with_latin1():
"""Test supports_unicode returns False for latin-1 encoding."""
from mxdev.main import supports_unicode
# Mock stdout with latin-1 encoding
mock_stdout = MagicMock()
mock_stdout.encoding = "latin-1"
with patch("sys.stdout", mock_stdout):
assert supports_unicode() is False
def test_main_default_behavior(tmp_path, monkeypatch):
"""Test main() function with default arguments."""
import logging
# Create a minimal mx.ini file
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
constraints-out = constraints-out.txt
"""
)
# Create empty requirements file
req_file = tmp_path / "requirements.txt"
req_file.write_text("")
# Change to temp directory
monkeypatch.chdir(tmp_path)
# Mock command line arguments
test_args = ["-c", str(config_file)]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read") as mock_read,
patch.object(main_module, "read_hooks") as mock_read_hooks,
patch.object(main_module, "fetch") as mock_fetch,
patch.object(main_module, "write") as mock_write,
patch.object(main_module, "write_hooks") as mock_write_hooks,
patch.object(main_module, "setup_logger") as mock_setup_logger,
):
main_module.main()
# Verify logger was set up with INFO level (default)
mock_setup_logger.assert_called_once_with(logging.INFO)
# Verify all processing functions were called
assert mock_read.called
assert mock_read_hooks.called
assert mock_fetch.called
assert mock_write.called
assert mock_write_hooks.called
def test_main_verbose_flag(tmp_path, monkeypatch):
"""Test main() with --verbose flag sets INFO log level."""
import logging
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--verbose"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks"),
patch.object(main_module, "fetch"),
patch.object(main_module, "write"),
patch.object(main_module, "write_hooks"),
patch.object(main_module, "setup_logger") as mock_setup_logger,
):
main_module.main()
# Verify logger was set up with INFO level when verbose flag is used
mock_setup_logger.assert_called_once_with(logging.INFO)
def test_main_silent_flag(tmp_path, monkeypatch):
"""Test main() with --silent flag sets WARNING log level."""
import logging
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--silent"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks"),
patch.object(main_module, "fetch"),
patch.object(main_module, "write"),
patch.object(main_module, "write_hooks"),
patch.object(main_module, "setup_logger") as mock_setup_logger,
):
main_module.main()
# Verify logger was set up with WARNING level when silent flag is used
mock_setup_logger.assert_called_once_with(logging.WARNING)
def test_main_offline_flag(tmp_path, monkeypatch):
"""Test main() with --offline flag skips fetch."""
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--offline"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks"),
patch.object(main_module, "fetch") as mock_fetch,
patch.object(main_module, "write"),
patch.object(main_module, "write_hooks"),
patch.object(main_module, "setup_logger"),
):
main_module.main()
# Verify fetch was NOT called when offline flag is used
assert not mock_fetch.called
def test_main_threads_flag(tmp_path, monkeypatch):
"""Test main() with --threads flag passes value to Configuration."""
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--threads", "8"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "Configuration") as mock_config,
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks"),
patch.object(main_module, "fetch"),
patch.object(main_module, "write"),
patch.object(main_module, "write_hooks"),
patch.object(main_module, "setup_logger"),
):
main_module.main()
# Verify Configuration was called with threads override
mock_config.assert_called_once()
call_kwargs = mock_config.call_args[1]
assert "override_args" in call_kwargs
assert call_kwargs["override_args"]["threads"] == 8
def test_main_no_fetch_flag(tmp_path, monkeypatch):
"""Test main() with --no-fetch flag skips fetch."""
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--no-fetch"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks"),
patch.object(main_module, "fetch") as mock_fetch,
patch.object(main_module, "write"),
patch.object(main_module, "write_hooks"),
patch.object(main_module, "setup_logger"),
):
main_module.main()
# Verify fetch was NOT called when no-fetch flag is used
assert not mock_fetch.called
def test_main_fetch_only_flag(tmp_path, monkeypatch):
"""Test main() with --fetch-only flag skips write and hooks."""
config_file = tmp_path / "mx.ini"
config_file.write_text(
"""[settings]
requirements-in = requirements.txt
requirements-out = requirements-out.txt
"""
)
(tmp_path / "requirements.txt").write_text("")
monkeypatch.chdir(tmp_path)
test_args = ["-c", str(config_file), "--fetch-only"]
import sys
main_module = sys.modules["mxdev.main"]
with (
patch("sys.argv", ["mxdev"] + test_args),
patch.object(main_module, "load_hooks", return_value=[]),
patch.object(main_module, "read"),
patch.object(main_module, "read_hooks") as mock_read_hooks,
patch.object(main_module, "fetch"),
patch.object(main_module, "write") as mock_write,
patch.object(main_module, "write_hooks") as mock_write_hooks,
patch.object(main_module, "setup_logger"),
):
main_module.main()
# Verify read_hooks was NOT called
assert not mock_read_hooks.called
# Verify write and write_hooks were NOT called
assert not mock_write.called
assert not mock_write_hooks.called