|
| 1 | +""" |
| 2 | +SPDX-License-Identifier: MIT |
| 3 | +
|
| 4 | + Copyright (c) 2026, SCANOSS |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | +import unittest |
| 27 | + |
| 28 | +from scanoss.scancodedeps import REQUIREMENT_NAME_PREFIX_RE, ScancodeDeps |
| 29 | + |
| 30 | +TEST_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 31 | + |
| 32 | + |
| 33 | +def _sanitize(rq): |
| 34 | + return REQUIREMENT_NAME_PREFIX_RE.sub('', rq) |
| 35 | + |
| 36 | + |
| 37 | +class TestSanitizeRequirement(unittest.TestCase): |
| 38 | + """Test the regex that strips package names from requirements""" |
| 39 | + |
| 40 | + def test_pip_exact_version(self): |
| 41 | + """pip exact match: strip name, keep '=='""" |
| 42 | + self.assertEqual(_sanitize('gtest==1.17.0'), '==1.17.0') |
| 43 | + |
| 44 | + def test_pip_less_equal(self): |
| 45 | + self.assertEqual(_sanitize('boost<=1.83.0'), '<=1.83.0') |
| 46 | + |
| 47 | + def test_pip_greater_equal(self): |
| 48 | + self.assertEqual(_sanitize('requests>=2.25.1'), '>=2.25.1') |
| 49 | + |
| 50 | + def test_pip_range(self): |
| 51 | + self.assertEqual(_sanitize('requests>=2.25.1,<3'), '>=2.25.1,<3') |
| 52 | + |
| 53 | + def test_pip_not_equal(self): |
| 54 | + self.assertEqual(_sanitize('foo!=1.0'), '!=1.0') |
| 55 | + |
| 56 | + def test_npm_caret_unchanged(self): |
| 57 | + """npm ^: no operator after name, unchanged""" |
| 58 | + self.assertEqual(_sanitize('^4.18.0'), '^4.18.0') |
| 59 | + |
| 60 | + def test_npm_tilde_unchanged(self): |
| 61 | + self.assertEqual(_sanitize('~4.18.0'), '~4.18.0') |
| 62 | + |
| 63 | + def test_npm_greater_equal(self): |
| 64 | + self.assertEqual(_sanitize('>=1.0.0'), '>=1.0.0') |
| 65 | + |
| 66 | + def test_bare_version_unchanged(self): |
| 67 | + """Plain version number with no operator: unchanged""" |
| 68 | + self.assertEqual(_sanitize('1.17.0'), '1.17.0') |
| 69 | + |
| 70 | + def test_name_prefix_of_another_package(self): |
| 71 | + """'requests-toolbelt>=1.0' strips full name, not partial""" |
| 72 | + self.assertEqual(_sanitize('requests-toolbelt>=1.0'), '>=1.0') |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + unittest.main() |
0 commit comments