1+ # Copyright 2025 Open Source Robotics Foundation, Inc.
2+ # Licensed under the Apache License, Version 2.0
3+
14import pytest
25from pallet_patcher .solver import _parse_rust_specifier , solve_dependency
36
7+
48# Test code generated with LLM help
5- @pytest .mark .parametrize ("rust_input , expected_matches, expected_non_matches" , [
9+ @pytest .mark .parametrize ("input , expected_matches, expected_non_matches" , [
610 # 1. Explicit Equals (=)
711 ("=1.2.3" , ["1.2.3" ], ["1.2.4" , "1.2.2" ]),
812
3842 ("^0.0" , ["0.0.0" , "0.0.5" ], ["0.1.0" ]),
3943 ("0.0" , ["0.0.1" ], ["0.1.0" ]),
4044])
41- def test_rust_specifier_logic (rust_input , expected_matches , expected_non_matches ):
45+ def test_rust_specifier_logic (input , expected_matches , expected_non_matches ):
4246 """
4347 Tests that the converted Python SpecifierSet correctly matches
4448 and excludes the versions dictated by Rust SemVer logic.
4549 """
46- spec_set = _parse_rust_specifier (rust_input )
50+ spec_set = _parse_rust_specifier (input )
4751
4852 for version in expected_matches :
4953 assert version in spec_set , \
50- f"Rust spec '{ rust_input } ' should MATCH { version } , but Python spec { spec_set } did not."
54+ f"""Rust spec '{ input } ' should MATCH { version } ,
55+ but Python spec { spec_set } did not."""
5156
5257 for version in expected_non_matches :
5358 assert version not in spec_set , \
54- f"Rust spec '{ rust_input } ' should NOT match { version } , but Python spec { spec_set } did."
59+ f"""Rust spec '{ input } ' should NOT match { version } ,
60+ but Python spec { spec_set } did."""
5561
5662
5763@pytest .mark .parametrize ("input_str, expected_str_repr" , [
@@ -62,34 +68,36 @@ def test_rust_specifier_logic(rust_input, expected_matches, expected_non_matches
6268])
6369def test_standard_python_fallback (input_str , expected_str_repr ):
6470 """
65- Tests that standard Python specifiers or other strings
71+ Tests that standard Python specifiers or other strings
6672 are passed through to SpecifierSet mostly unchanged.
6773 """
6874 spec = _parse_rust_specifier (input_str )
69- # Note: SpecifierSet normalization might change string spacing,
75+ # Note: SpecifierSet normalization might change string spacing,
7076 # but the logic checks if it parses without crashing.
7177 assert str (spec ) == expected_str_repr or not input_str
7278
79+
7380def test_invalid_version_strings ():
7481 """
75- Ensure the function handles malformed strings gracefully
76- (falling back to SpecifierSet which might raise its own error or handle it).
82+ Ensure the function handles malformed strings gracefully
83+ (falling back to SpecifierSet wich might raise or handle it).
7784 """
7885 # This falls through to "Bare" logic, fails try/except, hits fallback
79- # SpecifierSet("invalid") is technically valid in packaging but matches nothing usually
80- # or raises InvalidSpecifier depending on version.
86+ # SpecifierSet("invalid") is technically valid in packaging but matches
87+ # nothing or raises InvalidSpecifier depending on version.
8188 # Here we just want to ensure your code doesn't crash internally.
8289 try :
8390 _parse_rust_specifier ("invalid_string_with_char" )
8491 except Exception as e :
85- # It is acceptable if SpecifierSet raises, but your parsing logic shouldn't
92+ # It is acceptable if SpecifierSet raises, but your parsing logic
93+ # shouldn't
8694 assert "Invalid specifier" in str (e ) or isinstance (e , ValueError )
8795
8896
8997@pytest .mark .parametrize ("spec, available, expected" , [
9098 # 1. Basic Caret Priority
9199 # ^1.2.0 allows >=1.2.0, <2.0.0.
92- # It should pick 1.9.9 (highest), ignore 2.0.0 (too high) and 1.1.0 (too low).
100+ # It should pick 1.9.9 (highest), ignore 2.0.0 (high) and 1.1.0 (low).
93101 ("^1.2.0" , ["1.1.0" , "1.2.0" , "1.2.5" , "1.9.9" , "2.0.0" ], "1.9.9" ),
94102
95103 # 2. Basic Tilde Priority
@@ -98,7 +106,7 @@ def test_invalid_version_strings():
98106 ("~1.2.0" , ["1.2.0" , "1.2.5" , "1.2.9" , "1.3.0" , "1.4.0" ], "1.2.9" ),
99107
100108 # 3. Rust "0.x.y" Semantics (Major 0 breaking changes)
101- # ^0.2.0 allows >=0.2.0, <0.3.0.
109+ # ^0.2.0 allows >=0.2.0, <0.3.0.
102110 # Should ignore 0.3.0 even though it's higher.
103111 ("^0.2.0" , ["0.1.9" , "0.2.0" , "0.2.5" , "0.3.0" ], "0.2.5" ),
104112
@@ -127,7 +135,8 @@ def test_solve_dependency_logic(spec, available, expected):
127135 """
128136 result = solve_dependency (spec , available )
129137 assert result == expected , \
130- f"For spec '{ spec } ' and versions { available } , expected '{ expected } ' but got '{ result } '"
138+ f"""For spec '{ spec } ' and versions { available } , expected '{ expected } '
139+ but got '{ result } '"""
131140
132141
133142def test_solve_dependency_sorting_correctness ():
@@ -146,7 +155,7 @@ def test_solve_dependency_sorting_correctness():
146155
147156def test_solve_dependency_invalid_input_handling ():
148157 """
149- Test how the function handles non-integer version strings
158+ Test how the function handles non-integer version strings
150159 given the lambda sorting logic provided.
151160 """
152161 # NOTE: The provided function uses `int(part)` which will crash on "beta".
@@ -159,4 +168,4 @@ def test_solve_dependency_invalid_input_handling():
159168 solve_dependency (spec , available )
160169
161170 # Confirm it failed where we expected (in the sort lambda)
162- assert "invalid literal for int()" in str (excinfo .value )
171+ assert "invalid literal for int()" in str (excinfo .value )
0 commit comments