|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Visit https://aboutcode.org and https://github.com/aboutcode-org/ for support and download. |
| 6 | +# ScanCode is a trademark of nexB Inc. |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +import purl_validator |
| 22 | +from packageurl import PackageURL |
| 23 | +from commoncode import fileutils |
| 24 | +from commoncode.testcase import FileBasedTesting |
| 25 | + |
| 26 | + |
| 27 | +class TestPurlValidator(FileBasedTesting): |
| 28 | + def setUp(self): |
| 29 | + self.created_purl_maps = [] |
| 30 | + return super().setUp() |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + for purl_map in self.created_purl_maps: |
| 34 | + fileutils.delete(purl_map.parent) |
| 35 | + return super().tearDown() |
| 36 | + |
| 37 | + def test_purl_validator_create_purl_map_entry(self): |
| 38 | + test_purl1 = PackageURL(type="npm", namespace="@test", name="test", version="1.0") |
| 39 | + test_purl2 = "pkg:npm/test2@2.0" |
| 40 | + test_purl3 = "not-a-purl" |
| 41 | + test_purl4 = [] |
| 42 | + |
| 43 | + self.assertEqual(b'npm/@test/test', purl_validator.create_purl_map_entry(test_purl1)) |
| 44 | + self.assertEqual(b'npm/test2', purl_validator.create_purl_map_entry(test_purl2)) |
| 45 | + |
| 46 | + with self.assertRaises(ValueError): |
| 47 | + purl_validator.create_purl_map_entry(test_purl3) |
| 48 | + |
| 49 | + with self.assertRaises(ValueError): |
| 50 | + purl_validator.create_purl_map_entry(test_purl4) |
| 51 | + |
| 52 | + def test_purl_validator_create_purl_map_entry(self): |
| 53 | + test_purl1 = PackageURL(type="npm", namespace="@test", name="test", version="1.0") |
| 54 | + test_purl2 = "pkg:npm/test2@2.0" |
| 55 | + test_purl3 = "not-a-purl" |
| 56 | + test_purl4 = [] |
| 57 | + purls = [test_purl1, test_purl2] |
| 58 | + |
| 59 | + purl_map_loc = purl_validator.create_purl_map(purls) |
| 60 | + self.created_purl_maps.append(purl_map_loc) |
| 61 | + |
| 62 | + purl_map = purl_validator.PurlValidator.load_map(purl_map_loc) |
| 63 | + expected_results = [(b'npm/@test/test', 1), (b'npm/test2', 1)] |
| 64 | + results = [(k, v) for k, v in purl_map.items()] |
| 65 | + self.assertEqual(expected_results, results) |
| 66 | + |
| 67 | + with self.assertRaises(ValueError): |
| 68 | + purl_validator.create_purl_map([test_purl3]) |
| 69 | + |
| 70 | + with self.assertRaises(ValueError): |
| 71 | + purl_validator.create_purl_map([test_purl4]) |
0 commit comments