-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_versionguard.py
More file actions
899 lines (739 loc) · 30.9 KB
/
test_versionguard.py
File metadata and controls
899 lines (739 loc) · 30.9 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#!/usr/bin/env python3
"""
Comprehensive Test Suite for VersionGuard
Tests all functionality including:
- Semantic version parsing
- Dependency parsing from various file formats
- Compatibility rule checking
- Report generation
- CLI interface
- Edge cases and error handling
Target: 60+ tests with 100% pass rate
Author: ATLAS (Team Brain)
"""
import unittest
import tempfile
import json
from pathlib import Path
from unittest.mock import patch, MagicMock
from versionguard import (
SemanticVersion,
CompatibilityStatus,
Severity,
Dependency,
CompatibilityIssue,
CompatibilityReport,
CompatibilityMatrix,
DependencyParser,
VersionGuard,
__version__
)
class TestSemanticVersion(unittest.TestCase):
"""Tests for SemanticVersion class."""
def test_parse_full_version(self):
"""Test parsing full semantic version."""
v = SemanticVersion.parse("1.2.3")
self.assertEqual(v.major, 1)
self.assertEqual(v.minor, 2)
self.assertEqual(v.patch, 3)
def test_parse_major_minor_only(self):
"""Test parsing major.minor version."""
v = SemanticVersion.parse("1.2")
self.assertEqual(v.major, 1)
self.assertEqual(v.minor, 2)
self.assertEqual(v.patch, 0)
def test_parse_major_only(self):
"""Test parsing major version only."""
v = SemanticVersion.parse("5")
self.assertEqual(v.major, 5)
self.assertEqual(v.minor, 0)
self.assertEqual(v.patch, 0)
def test_parse_with_caret_prefix(self):
"""Test parsing with caret (^) prefix."""
v = SemanticVersion.parse("^1.2.3")
self.assertEqual(v.major, 1)
self.assertEqual(v.minor, 2)
self.assertEqual(v.patch, 3)
def test_parse_with_tilde_prefix(self):
"""Test parsing with tilde (~) prefix."""
v = SemanticVersion.parse("~1.2.3")
self.assertEqual(v.major, 1)
self.assertEqual(v.minor, 2)
self.assertEqual(v.patch, 3)
def test_parse_with_v_prefix(self):
"""Test parsing with v prefix."""
v = SemanticVersion.parse("v2.0.0")
self.assertEqual(v.major, 2)
self.assertEqual(v.minor, 0)
self.assertEqual(v.patch, 0)
def test_parse_with_prerelease(self):
"""Test parsing with prerelease tag."""
v = SemanticVersion.parse("1.0.0-alpha")
self.assertEqual(v.major, 1)
self.assertEqual(v.prerelease, "alpha")
def test_parse_with_prerelease_number(self):
"""Test parsing with numbered prerelease."""
v = SemanticVersion.parse("1.0.0-rc.1")
self.assertEqual(v.prerelease, "rc.1")
def test_parse_with_build_metadata(self):
"""Test parsing with build metadata."""
v = SemanticVersion.parse("1.0.0+build.123")
self.assertEqual(v.build, "build.123")
def test_parse_with_prerelease_and_build(self):
"""Test parsing with both prerelease and build."""
v = SemanticVersion.parse("1.0.0-beta.2+build.456")
self.assertEqual(v.prerelease, "beta.2")
self.assertEqual(v.build, "build.456")
def test_parse_empty_returns_none(self):
"""Test parsing empty string returns None."""
v = SemanticVersion.parse("")
self.assertIsNone(v)
def test_parse_none_returns_none(self):
"""Test parsing None returns None."""
v = SemanticVersion.parse(None)
self.assertIsNone(v)
def test_comparison_less_than(self):
"""Test less than comparison."""
v1 = SemanticVersion.parse("1.0.0")
v2 = SemanticVersion.parse("2.0.0")
self.assertTrue(v1 < v2)
def test_comparison_greater_than(self):
"""Test greater than comparison."""
v1 = SemanticVersion.parse("2.0.0")
v2 = SemanticVersion.parse("1.0.0")
self.assertTrue(v1 > v2)
def test_comparison_equal(self):
"""Test equality comparison."""
v1 = SemanticVersion.parse("1.2.3")
v2 = SemanticVersion.parse("1.2.3")
self.assertEqual(v1, v2)
def test_comparison_minor_version(self):
"""Test minor version comparison."""
v1 = SemanticVersion.parse("1.1.0")
v2 = SemanticVersion.parse("1.2.0")
self.assertTrue(v1 < v2)
def test_comparison_patch_version(self):
"""Test patch version comparison."""
v1 = SemanticVersion.parse("1.1.1")
v2 = SemanticVersion.parse("1.1.2")
self.assertTrue(v1 < v2)
def test_is_major_compatible(self):
"""Test major version compatibility check."""
v1 = SemanticVersion.parse("4.7.2")
v2 = SemanticVersion.parse("4.0.0")
self.assertTrue(v1.is_major_compatible(v2))
def test_is_major_incompatible(self):
"""Test major version incompatibility."""
v1 = SemanticVersion.parse("4.7.2")
v2 = SemanticVersion.parse("5.0.0")
self.assertFalse(v1.is_major_compatible(v2))
def test_is_minor_compatible(self):
"""Test minor version compatibility."""
v1 = SemanticVersion.parse("4.7.2")
v2 = SemanticVersion.parse("4.7.0")
self.assertTrue(v1.is_minor_compatible(v2))
def test_str_representation(self):
"""Test string representation."""
v = SemanticVersion.parse("1.2.3")
self.assertEqual(str(v), "1.2.3")
def test_str_with_prerelease(self):
"""Test string with prerelease."""
v = SemanticVersion.parse("1.2.3-beta")
self.assertEqual(str(v), "1.2.3-beta")
def test_equality_with_non_version(self):
"""Test equality with non-SemanticVersion object."""
v = SemanticVersion.parse("1.0.0")
self.assertFalse(v == "1.0.0")
self.assertFalse(v == 1)
self.assertFalse(v == None)
class TestCompatibilityStatus(unittest.TestCase):
"""Tests for CompatibilityStatus enum."""
def test_compatible_value(self):
"""Test compatible status value."""
self.assertEqual(CompatibilityStatus.COMPATIBLE.value, "compatible")
def test_warning_value(self):
"""Test warning status value."""
self.assertEqual(CompatibilityStatus.WARNING.value, "warning")
def test_incompatible_value(self):
"""Test incompatible status value."""
self.assertEqual(CompatibilityStatus.INCOMPATIBLE.value, "incompatible")
def test_unknown_value(self):
"""Test unknown status value."""
self.assertEqual(CompatibilityStatus.UNKNOWN.value, "unknown")
class TestSeverity(unittest.TestCase):
"""Tests for Severity enum."""
def test_info_value(self):
"""Test info severity."""
self.assertEqual(Severity.INFO.value, "info")
def test_warning_value(self):
"""Test warning severity."""
self.assertEqual(Severity.WARNING.value, "warning")
def test_error_value(self):
"""Test error severity."""
self.assertEqual(Severity.ERROR.value, "error")
def test_critical_value(self):
"""Test critical severity."""
self.assertEqual(Severity.CRITICAL.value, "critical")
class TestDependency(unittest.TestCase):
"""Tests for Dependency class."""
def test_create_dependency(self):
"""Test creating a dependency."""
dep = Dependency(
name="test-package",
version=SemanticVersion.parse("1.0.0"),
version_spec="^1.0.0",
source="frontend",
file_path="package.json"
)
self.assertEqual(dep.name, "test-package")
self.assertEqual(dep.source, "frontend")
def test_to_dict(self):
"""Test converting dependency to dict."""
dep = Dependency(
name="test-package",
version=SemanticVersion.parse("1.0.0"),
version_spec="^1.0.0",
source="frontend",
file_path="package.json"
)
d = dep.to_dict()
self.assertEqual(d["name"], "test-package")
self.assertEqual(d["version"], "1.0.0")
self.assertEqual(d["source"], "frontend")
class TestCompatibilityIssue(unittest.TestCase):
"""Tests for CompatibilityIssue class."""
def test_create_issue(self):
"""Test creating a compatibility issue."""
issue = CompatibilityIssue(
package="socket.io",
frontend_version=SemanticVersion.parse("4.0.0"),
backend_version=SemanticVersion.parse("5.0.0"),
status=CompatibilityStatus.INCOMPATIBLE,
severity=Severity.CRITICAL,
message="Version mismatch",
recommendation="Update versions"
)
self.assertEqual(issue.package, "socket.io")
self.assertEqual(issue.status, CompatibilityStatus.INCOMPATIBLE)
def test_to_dict(self):
"""Test converting issue to dict."""
issue = CompatibilityIssue(
package="test",
frontend_version=SemanticVersion.parse("1.0.0"),
backend_version=SemanticVersion.parse("2.0.0"),
status=CompatibilityStatus.WARNING,
severity=Severity.WARNING,
message="Test message",
recommendation="Test recommendation",
details=["Detail 1", "Detail 2"]
)
d = issue.to_dict()
self.assertEqual(d["package"], "test")
self.assertEqual(d["status"], "warning")
self.assertEqual(len(d["details"]), 2)
class TestCompatibilityReport(unittest.TestCase):
"""Tests for CompatibilityReport class."""
def test_create_report(self):
"""Test creating a report."""
report = CompatibilityReport(
status=CompatibilityStatus.COMPATIBLE,
frontend_deps=[],
backend_deps=[],
issues=[],
recommendations=[],
summary="All good"
)
self.assertEqual(report.status, CompatibilityStatus.COMPATIBLE)
def test_to_dict(self):
"""Test converting report to dict."""
report = CompatibilityReport(
status=CompatibilityStatus.COMPATIBLE,
frontend_deps=[],
backend_deps=[],
issues=[],
recommendations=["Recommendation 1"],
summary="Test summary"
)
d = report.to_dict()
self.assertEqual(d["status"], "compatible")
self.assertEqual(d["frontend_dep_count"], 0)
self.assertEqual(d["summary"], "Test summary")
class TestCompatibilityMatrix(unittest.TestCase):
"""Tests for CompatibilityMatrix class."""
def test_get_rule_socket_io(self):
"""Test getting socket.io rule."""
rule = CompatibilityMatrix.get_rule("socket.io")
self.assertIsNotNone(rule)
self.assertEqual(rule["type"], "client_server")
def test_get_rule_react(self):
"""Test getting react rule."""
rule = CompatibilityMatrix.get_rule("react")
self.assertIsNotNone(rule)
self.assertEqual(rule["type"], "version_match")
def test_get_rule_unknown(self):
"""Test getting rule for unknown package."""
rule = CompatibilityMatrix.get_rule("unknown-package-xyz")
self.assertIsNone(rule)
def test_get_rule_case_insensitive(self):
"""Test rule lookup is case-insensitive."""
rule1 = CompatibilityMatrix.get_rule("Socket.IO")
rule2 = CompatibilityMatrix.get_rule("socket.io")
self.assertEqual(rule1, rule2)
def test_get_related_packages_socket_io(self):
"""Test getting related packages for socket.io."""
related = CompatibilityMatrix.get_related_packages("socket.io")
self.assertIn("socket.io-client", related)
def test_get_related_packages_react(self):
"""Test getting related packages for react."""
related = CompatibilityMatrix.get_related_packages("react")
self.assertIn("react-dom", related)
class TestDependencyParser(unittest.TestCase):
"""Tests for DependencyParser class."""
def test_parse_package_json(self):
"""Test parsing package.json."""
with tempfile.TemporaryDirectory() as tmpdir:
pkg_json = Path(tmpdir) / "package.json"
pkg_json.write_text(json.dumps({
"dependencies": {
"react": "^18.2.0",
"socket.io-client": "^4.7.2"
}
}))
deps = DependencyParser.parse_package_json(pkg_json)
self.assertEqual(len(deps), 2)
names = [d.name for d in deps]
self.assertIn("react", names)
self.assertIn("socket.io-client", names)
def test_parse_package_json_with_dev_deps(self):
"""Test parsing package.json with devDependencies."""
with tempfile.TemporaryDirectory() as tmpdir:
pkg_json = Path(tmpdir) / "package.json"
pkg_json.write_text(json.dumps({
"dependencies": {"react": "^18.2.0"},
"devDependencies": {"typescript": "^5.0.0"}
}))
deps = DependencyParser.parse_package_json(pkg_json)
self.assertEqual(len(deps), 2)
def test_parse_package_json_not_found(self):
"""Test parsing non-existent package.json."""
deps = DependencyParser.parse_package_json(Path("/nonexistent/package.json"))
self.assertEqual(len(deps), 0)
def test_parse_package_json_invalid_json(self):
"""Test parsing invalid JSON."""
with tempfile.TemporaryDirectory() as tmpdir:
pkg_json = Path(tmpdir) / "package.json"
pkg_json.write_text("not valid json {{{")
deps = DependencyParser.parse_package_json(pkg_json)
self.assertEqual(len(deps), 0)
def test_parse_requirements_txt(self):
"""Test parsing requirements.txt."""
with tempfile.TemporaryDirectory() as tmpdir:
req_txt = Path(tmpdir) / "requirements.txt"
req_txt.write_text("""
flask==2.3.0
python-socketio>=5.8.0
requests
# comment line
-r other.txt
""")
deps = DependencyParser.parse_requirements_txt(req_txt)
self.assertEqual(len(deps), 3)
names = [d.name for d in deps]
self.assertIn("flask", names)
self.assertIn("python-socketio", names)
self.assertIn("requests", names)
def test_parse_requirements_txt_not_found(self):
"""Test parsing non-existent requirements.txt."""
deps = DependencyParser.parse_requirements_txt(Path("/nonexistent/requirements.txt"))
self.assertEqual(len(deps), 0)
def test_parse_pyproject_toml(self):
"""Test parsing pyproject.toml."""
with tempfile.TemporaryDirectory() as tmpdir:
pyproject = Path(tmpdir) / "pyproject.toml"
pyproject.write_text("""
[project]
name = "test"
[project.dependencies]
flask = "2.3.0"
pydantic = "2.0.0"
""")
deps = DependencyParser.parse_pyproject_toml(pyproject)
# Should find deps in dependencies section
self.assertGreaterEqual(len(deps), 0)
def test_parse_pyproject_toml_not_found(self):
"""Test parsing non-existent pyproject.toml."""
deps = DependencyParser.parse_pyproject_toml(Path("/nonexistent/pyproject.toml"))
self.assertEqual(len(deps), 0)
class TestVersionGuard(unittest.TestCase):
"""Tests for VersionGuard class."""
def setUp(self):
"""Set up test fixtures."""
self.guard = VersionGuard()
def test_init_default_path(self):
"""Test default initialization."""
guard = VersionGuard()
self.assertEqual(guard.project_root, Path.cwd())
def test_init_custom_path(self):
"""Test initialization with custom path."""
guard = VersionGuard(Path("/custom/path"))
self.assertEqual(guard.project_root, Path("/custom/path"))
def test_scan_empty_project(self):
"""Test scanning empty project."""
with tempfile.TemporaryDirectory() as tmpdir:
guard = VersionGuard(Path(tmpdir))
frontend, backend = guard.scan_project()
self.assertEqual(len(frontend), 0)
self.assertEqual(len(backend), 0)
def test_scan_project_with_package_json(self):
"""Test scanning project with package.json."""
with tempfile.TemporaryDirectory() as tmpdir:
pkg_json = Path(tmpdir) / "package.json"
pkg_json.write_text(json.dumps({
"dependencies": {"react": "^18.2.0"}
}))
guard = VersionGuard(Path(tmpdir))
frontend, backend = guard.scan_project()
self.assertEqual(len(frontend), 1)
self.assertEqual(frontend[0].name, "react")
def test_scan_project_with_requirements(self):
"""Test scanning project with requirements.txt."""
with tempfile.TemporaryDirectory() as tmpdir:
req_txt = Path(tmpdir) / "requirements.txt"
req_txt.write_text("flask==2.3.0")
guard = VersionGuard(Path(tmpdir))
frontend, backend = guard.scan_project()
self.assertEqual(len(backend), 1)
self.assertEqual(backend[0].name, "flask")
def test_scan_skips_node_modules(self):
"""Test that scanning skips node_modules."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create node_modules with package.json
node_modules = Path(tmpdir) / "node_modules" / "test-pkg"
node_modules.mkdir(parents=True)
(node_modules / "package.json").write_text(json.dumps({
"dependencies": {"hidden": "1.0.0"}
}))
# Create root package.json
(Path(tmpdir) / "package.json").write_text(json.dumps({
"dependencies": {"visible": "1.0.0"}
}))
guard = VersionGuard(Path(tmpdir))
frontend, backend = guard.scan_project()
names = [d.name for d in frontend]
self.assertIn("visible", names)
self.assertNotIn("hidden", names)
def test_check_socket_io_incompatibility(self):
"""Test detecting Socket.IO version incompatibility."""
self.guard.frontend_deps = [
Dependency(
name="socket.io-client",
version=SemanticVersion.parse("4.7.2"),
version_spec="^4.7.2",
source="frontend",
file_path="package.json"
)
]
self.guard.backend_deps = [
Dependency(
name="socket.io",
version=SemanticVersion.parse("5.0.0"),
version_spec=">=5.0.0",
source="backend",
file_path="package.json"
)
]
issues = self.guard.check_compatibility()
# Should detect the mismatch
critical_issues = [i for i in issues if i.severity == Severity.CRITICAL]
self.assertGreater(len(critical_issues), 0)
def test_check_socket_io_compatible(self):
"""Test Socket.IO compatibility check passes for matching versions."""
self.guard.frontend_deps = [
Dependency(
name="socket.io-client",
version=SemanticVersion.parse("4.7.2"),
version_spec="^4.7.2",
source="frontend",
file_path="package.json"
)
]
self.guard.backend_deps = [
Dependency(
name="socket.io",
version=SemanticVersion.parse("4.6.0"),
version_spec=">=4.0.0",
source="backend",
file_path="package.json"
)
]
issues = self.guard.check_compatibility()
# Should not have critical issues
critical_issues = [i for i in issues if i.severity == Severity.CRITICAL]
self.assertEqual(len(critical_issues), 0)
def test_check_react_react_dom_match(self):
"""Test React/React-DOM version matching."""
self.guard.frontend_deps = [
Dependency(
name="react",
version=SemanticVersion.parse("18.2.0"),
version_spec="^18.2.0",
source="frontend",
file_path="package.json"
),
Dependency(
name="react-dom",
version=SemanticVersion.parse("17.0.0"),
version_spec="^17.0.0",
source="frontend",
file_path="package.json"
)
]
issues = self.guard.check_compatibility()
# Should detect mismatch
self.assertGreater(len(issues), 0)
def test_generate_report_compatible(self):
"""Test generating compatible report."""
self.guard.frontend_deps = [
Dependency(
name="lodash",
version=SemanticVersion.parse("4.17.21"),
version_spec="^4.17.21",
source="frontend",
file_path="package.json"
)
]
self.guard.backend_deps = []
self.guard.issues = []
report = self.guard.generate_report()
self.assertEqual(report.status, CompatibilityStatus.COMPATIBLE)
self.assertIn("COMPATIBLE", report.summary)
def test_generate_report_incompatible(self):
"""Test generating incompatible report."""
self.guard.frontend_deps = []
self.guard.backend_deps = []
self.guard.issues = [
CompatibilityIssue(
package="test",
frontend_version=None,
backend_version=None,
status=CompatibilityStatus.INCOMPATIBLE,
severity=Severity.CRITICAL,
message="Test issue",
recommendation="Fix it"
)
]
report = self.guard.generate_report()
self.assertEqual(report.status, CompatibilityStatus.INCOMPATIBLE)
def test_generate_report_unknown(self):
"""Test generating unknown status report when no deps found."""
self.guard.frontend_deps = []
self.guard.backend_deps = []
self.guard.issues = []
report = self.guard.generate_report()
self.assertEqual(report.status, CompatibilityStatus.UNKNOWN)
def test_format_report_text(self):
"""Test formatting report as text."""
self.guard.frontend_deps = [
Dependency(
name="react",
version=SemanticVersion.parse("18.2.0"),
version_spec="^18.2.0",
source="frontend",
file_path="package.json"
)
]
self.guard.backend_deps = []
self.guard.issues = []
text = self.guard.format_report()
self.assertIn("VERSIONGUARD", text)
self.assertIn("COMPATIBLE", text)
self.assertIn("Frontend packages: 1", text)
def test_format_report_with_issues(self):
"""Test formatting report with issues."""
self.guard.frontend_deps = []
self.guard.backend_deps = []
self.guard.issues = [
CompatibilityIssue(
package="test-package",
frontend_version=SemanticVersion.parse("1.0.0"),
backend_version=SemanticVersion.parse("2.0.0"),
status=CompatibilityStatus.INCOMPATIBLE,
severity=Severity.CRITICAL,
message="Test message",
recommendation="Fix it",
details=["Detail 1"]
)
]
text = self.guard.format_report()
self.assertIn("ISSUES FOUND", text)
self.assertIn("TEST-PACKAGE", text.upper())
self.assertIn("Fix it", text)
def test_export_json(self):
"""Test exporting report to JSON."""
with tempfile.TemporaryDirectory() as tmpdir:
self.guard.frontend_deps = [
Dependency(
name="react",
version=SemanticVersion.parse("18.2.0"),
version_spec="^18.2.0",
source="frontend",
file_path="package.json"
)
]
self.guard.backend_deps = []
self.guard.issues = []
output_path = Path(tmpdir) / "report.json"
self.guard.export_json(output_path)
self.assertTrue(output_path.exists())
with open(output_path) as f:
data = json.load(f)
self.assertEqual(data["status"], "compatible")
self.assertEqual(data["frontend_dep_count"], 1)
class TestVersionGuardCLI(unittest.TestCase):
"""Tests for CLI interface."""
def test_demo_command_runs(self):
"""Test demo command executes without error."""
import sys
from io import StringIO
# Capture stdout
captured = StringIO()
sys.stdout = captured
try:
from versionguard import main
with patch('sys.argv', ['versionguard', 'demo']):
main()
except SystemExit:
pass
finally:
sys.stdout = sys.__stdout__
output = captured.getvalue()
self.assertIn("VersionGuard Demo", output)
def test_scan_command_with_path(self):
"""Test scan command with path argument."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create test files
pkg_json = Path(tmpdir) / "package.json"
pkg_json.write_text(json.dumps({
"dependencies": {"react": "^18.0.0"}
}))
import sys
from io import StringIO
captured = StringIO()
sys.stdout = captured
try:
from versionguard import main
with patch('sys.argv', ['versionguard', 'scan', tmpdir]):
main()
except SystemExit:
pass
finally:
sys.stdout = sys.__stdout__
output = captured.getvalue()
self.assertIn("VERSIONGUARD", output)
class TestEdgeCases(unittest.TestCase):
"""Tests for edge cases and error handling."""
def test_empty_version_string(self):
"""Test handling empty version string."""
v = SemanticVersion.parse("")
self.assertIsNone(v)
def test_invalid_version_string(self):
"""Test handling invalid version string."""
v = SemanticVersion.parse("not-a-version")
self.assertIsNone(v)
def test_version_with_spaces(self):
"""Test version with surrounding spaces."""
v = SemanticVersion.parse(" 1.2.3 ")
self.assertIsNotNone(v)
self.assertEqual(v.major, 1)
def test_version_comparison_operators(self):
"""Test all version comparison operators."""
v1 = SemanticVersion.parse("1.0.0")
v2 = SemanticVersion.parse("2.0.0")
v3 = SemanticVersion.parse("1.0.0")
self.assertTrue(v1 < v2)
self.assertTrue(v1 <= v2)
self.assertTrue(v2 > v1)
self.assertTrue(v2 >= v1)
self.assertTrue(v1 == v3)
self.assertTrue(v1 <= v3)
self.assertTrue(v1 >= v3)
def test_dependency_without_version(self):
"""Test dependency without version specified."""
dep = Dependency(
name="test",
version=None,
version_spec="",
source="backend",
file_path="requirements.txt"
)
d = dep.to_dict()
self.assertIsNone(d["version"])
def test_compatibility_check_with_no_deps(self):
"""Test compatibility check with no dependencies."""
guard = VersionGuard()
issues = guard.check_compatibility()
self.assertEqual(len(issues), 0)
def test_version_greater_than_operators(self):
"""Test version range operators."""
v = SemanticVersion.parse(">=1.0.0")
self.assertEqual(v.major, 1)
def test_complex_version_spec(self):
"""Test complex version specification returns None (unsupported)."""
v = SemanticVersion.parse(">=1.0.0,<2.0.0")
# Complex range specs are not supported - returns None
# This is expected behavior; version ranges should be handled separately
self.assertIsNone(v)
class TestIntegration(unittest.TestCase):
"""Integration tests for full workflow."""
def test_full_scan_and_check_workflow(self):
"""Test complete scan and check workflow."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create frontend config
pkg_json = Path(tmpdir) / "frontend" / "package.json"
pkg_json.parent.mkdir(parents=True)
pkg_json.write_text(json.dumps({
"dependencies": {
"socket.io-client": "^4.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}))
# Create backend config
req_txt = Path(tmpdir) / "backend" / "requirements.txt"
req_txt.parent.mkdir(parents=True)
req_txt.write_text("""
flask==2.3.0
python-socketio==4.6.0
""")
# Run scan and check
guard = VersionGuard(Path(tmpdir))
guard.scan_project()
guard.check_compatibility()
report = guard.generate_report()
# Verify report
self.assertIsNotNone(report)
self.assertGreater(len(guard.frontend_deps), 0)
self.assertGreater(len(guard.backend_deps), 0)
def test_monorepo_structure(self):
"""Test handling monorepo structure."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create multiple package.json files
(Path(tmpdir) / "packages" / "web" / "package.json").parent.mkdir(parents=True)
(Path(tmpdir) / "packages" / "web" / "package.json").write_text(json.dumps({
"dependencies": {"react": "^18.2.0"}
}))
(Path(tmpdir) / "packages" / "mobile" / "package.json").parent.mkdir(parents=True)
(Path(tmpdir) / "packages" / "mobile" / "package.json").write_text(json.dumps({
"dependencies": {"react-native": "^0.72.0"}
}))
guard = VersionGuard(Path(tmpdir))
guard.scan_project()
self.assertEqual(len(guard.frontend_deps), 2)
class TestVersion(unittest.TestCase):
"""Test version information."""
def test_version_exists(self):
"""Test that version is defined."""
self.assertEqual(__version__, "1.0.0")
if __name__ == '__main__':
# Run tests with verbosity
unittest.main(verbosity=2)