-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
test_parser_environment.py
82 lines (67 loc) · 3.45 KB
/
test_parser_environment.py
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
# encoding: utf-8
# This file is part of CycloneDX Python Lib
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.
import os
from unittest import TestCase
from cyclonedx.model import License, LicenseChoice
from cyclonedx_py.parser.environment import EnvironmentParser
class TestEnvironmentParser(TestCase):
def test_simple(self) -> None:
"""
@todo This test is a vague as it will detect the unique environment where tests are being executed -
so is this valid?
:return:
"""
parser = EnvironmentParser()
self.assertGreater(parser.component_count(), 1)
# We can only be sure that tox is in the environment, for example as we use tox to run tests
c_tox = next(filter(lambda c: c.name == 'tox', parser.get_components()), None)
self.assertIsNotNone(c_tox)
self.assertNotEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
self.assertIsNotNone(c_tox.licenses)
self.assertEqual(len(c_tox.licenses), 2)
self.assertEqual({LicenseChoice(license=License(name="MIT License")),
LicenseChoice(license=License(name="MIT"))}, c_tox.licenses)
def test_simple_use_purl_bom_ref(self) -> None:
"""
@todo This test is a vague as it will detect the unique environment where tests are being executed -
so is this valid?
:return:
"""
parser = EnvironmentParser(use_purl_bom_ref=True)
self.assertGreater(parser.component_count(), 1)
# We can only be sure that tox is in the environment, for example as we use tox to run tests
c_tox = next(filter(lambda c: c.name == 'tox', parser.get_components()), None)
self.assertIsNotNone(c_tox)
self.assertEqual(c_tox.purl.to_string(), c_tox.bom_ref.value)
self.assertIsNotNone(c_tox.licenses)
self.assertEqual(len(c_tox.licenses), 2)
self.assertEqual({LicenseChoice(license=License(name="MIT License")),
LicenseChoice(license=License(name="MIT"))}, c_tox.licenses)
def test_alternative_environment_path(self) -> None:
env_path = os.path.join(os.path.dirname(__file__), "fixtures/venv-simple/lib/python3.10/site-packages")
parser = EnvironmentParser(environment_path=env_path)
self.assertEqual(parser.component_count(), 1)
c_packaging = parser.get_components()[0]
self.assertEqual(c_packaging.name, "packaging")
self.assertEqual(c_packaging.version, "23.1")
self.assertNotEqual(c_packaging.purl.to_string(), c_packaging.bom_ref.value)
self.assertIsNotNone(c_packaging.licenses)
self.assertEqual(len(c_packaging.licenses), 2)
self.assertEqual({LicenseChoice(license=License(name="BSD License")),
LicenseChoice(license=License(name="Apache Software License"))},
c_packaging.licenses)