18
18
# Copyright (c) OWASP Foundation. All Rights Reserved.
19
19
20
20
import json
21
- import os
22
21
from unittest import TestCase
23
22
from xml .etree import ElementTree
24
23
24
+ import pkg_resources
25
+
25
26
from cyclonedx .model .bom import Bom
26
27
from cyclonedx .output import get_instance , OutputFormat
27
28
from cyclonedx .output .json import Json
28
29
from cyclonedx .output .xml import Xml
29
30
from cyclonedx .parser .environment import EnvironmentParser
30
31
32
+ OUR_PACKAGE_NAME : str = 'cyclonedx-python-lib'
33
+ OUR_PACKAGE_VERSION : str = pkg_resources .get_distribution (OUR_PACKAGE_NAME ).version
31
34
32
- class TestE2EEnvironment (TestCase ):
33
- _our_package_version : str
34
-
35
- @classmethod
36
- def setUpClass (cls ) -> None :
37
- with open (os .path .join (os .path .realpath (os .path .join (os .path .dirname (os .path .realpath (__file__ )), '../' )),
38
- 'VERSION' )) as _our_version :
39
- cls ._our_package_version = _our_version .read ()
40
35
41
- _our_version . close ()
36
+ class TestE2EEnvironment ( TestCase ):
42
37
43
38
def test_json_defaults (self ):
44
39
outputter : Json = get_instance (bom = Bom .from_parser (EnvironmentParser ()), output_format = OutputFormat .JSON )
45
40
bom_json = json .loads (outputter .output_as_string ())
46
41
component_this_library = next (
47
42
(x for x in bom_json ['components' ] if
48
- x ['purl' ] == 'pkg:pypi/cyclonedx-python-lib @{}' .format (TestE2EEnvironment . _our_package_version )), None
43
+ x ['purl' ] == 'pkg:pypi/{} @{}' .format (OUR_PACKAGE_NAME , OUR_PACKAGE_VERSION )), None
49
44
)
50
45
51
46
self .assertTrue ('author' in component_this_library .keys (), 'author is missing from JSON BOM' )
52
47
self .assertEqual (component_this_library ['author' ], 'Sonatype Community' )
53
- self .assertEqual (component_this_library ['name' ], 'cyclonedx-python-lib' )
54
- self .assertEqual (component_this_library ['version' ], TestE2EEnvironment . _our_package_version )
48
+ self .assertEqual (component_this_library ['name' ], OUR_PACKAGE_NAME )
49
+ self .assertEqual (component_this_library ['version' ], OUR_PACKAGE_VERSION )
55
50
56
51
def test_xml_defaults (self ):
57
52
outputter : Xml = get_instance (bom = Bom .from_parser (EnvironmentParser ()))
58
53
59
54
# Check we have cyclonedx-python-lib with Author, Name and Version
60
55
bom_xml_e = ElementTree .fromstring (outputter .output_as_string ())
61
56
component_this_library = bom_xml_e .find ('./{{{}}}components/{{{}}}component[@bom-ref=\' pkg:pypi/{}\' ]' .format (
62
- outputter .get_target_namespace (), outputter .get_target_namespace (), 'cyclonedx-python-lib @{}' .format (
63
- TestE2EEnvironment . _our_package_version
57
+ outputter .get_target_namespace (), outputter .get_target_namespace (), '{} @{}' .format (
58
+ OUR_PACKAGE_NAME , OUR_PACKAGE_VERSION
64
59
)
65
60
))
66
61
@@ -70,8 +65,8 @@ def test_xml_defaults(self):
70
65
71
66
name = component_this_library .find ('./{{{}}}name' .format (outputter .get_target_namespace ()))
72
67
self .assertIsNotNone (name , 'No name element but one was expected.' )
73
- self .assertEqual (name .text , 'cyclonedx-python-lib' )
68
+ self .assertEqual (name .text , OUR_PACKAGE_NAME )
74
69
75
70
version = component_this_library .find ('./{{{}}}version' .format (outputter .get_target_namespace ()))
76
71
self .assertIsNotNone (version , 'No version element but one was expected.' )
77
- self .assertEqual (version .text , TestE2EEnvironment . _our_package_version )
72
+ self .assertEqual (version .text , OUR_PACKAGE_VERSION )
0 commit comments