| 1 | |
| 2 | |
| 3 |
|
| 4 | import unittest
|
| 5 | from pathlib import Path
|
| 6 | import tempfile
|
| 7 | from sbom.spdx_graph.kernel_file import _parse_spdx_license_identifier |
| 8 |
|
| 9 |
|
| 10 | class TestKernelFile(unittest.TestCase):
|
| 11 | def setUp(self):
|
| 12 | self.tmpdir = tempfile.TemporaryDirectory()
|
| 13 | self.src_tree = Path(self.tmpdir.name)
|
| 14 |
|
| 15 | def tearDown(self):
|
| 16 | self.tmpdir.cleanup()
|
| 17 |
|
| 18 | def test_parse_spdx_license_identifier(self):
|
| 19 | |
| 20 | test_cases: list[tuple[str, str | None]] = [
|
| 21 | ("/* SPDX-License-Identifier: MIT*/", "MIT"),
|
| 22 | ("// SPDX-License-Identifier: GPL-2.0-only", "GPL-2.0-only"),
|
| 23 | ("# SPDX-License-Identifier: GPL-2.0-only", "GPL-2.0-only"),
|
| 24 | ("#!/bin/bash\n# SPDX-License-Identifier: GPL-2.0-only", "GPL-2.0-only"),
|
| 25 | ("/* SPDX-License-Identifier: GPL-2.0-or-later OR MIT */", "GPL-2.0-or-later OR MIT"),
|
| 26 | ("/* SPDX-License-Identifier: Apache-2.0 */\n extra text", "Apache-2.0"),
|
| 27 | ("<!-- SPDX-License-Identifier: GPL-2.0 -->", "GPL-2.0"),
|
| 28 | ("int main() { return 0; }", None),
|
| 29 | ]
|
| 30 | |
| 31 |
|
| 32 | for i, (file_content, expected_identifier) in enumerate(test_cases):
|
| 33 | file_path = self.src_tree / f"file_{i}.c"
|
| 34 | file_path.write_text(file_content)
|
| 35 | self.assertEqual(_parse_spdx_license_identifier(str(file_path)), expected_identifier)
|