13
13
import os
14
14
from conan.errors import ConanException
15
15
from conan.api.output import ConanOutput
16
- from conan.tools.sbom.cyclonedx import cyclonedx_1_4
16
+ from conan.tools.sbom import cyclonedx_1_4
17
17
18
18
def post_package(conanfile):
19
- sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile.subgraph)
19
+ sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile.subgraph, add_build=%s, add_tests=%s )
20
20
metadata_folder = conanfile.package_metadata_folder
21
21
file_name = "sbom.cdx.json"
22
22
with open(os.path.join(metadata_folder, file_name), 'w') as f:
@@ -28,14 +28,28 @@ def post_package(conanfile):
28
28
def hook_setup_post_package ():
29
29
tc = TestClient ()
30
30
hook_path = os .path .join (tc .paths .hooks_path , "hook_sbom.py" )
31
- save (hook_path , sbom_hook_post_package )
31
+ save (hook_path , sbom_hook_post_package % ("True" , "True" ))
32
+ return tc
33
+
34
+ @pytest .fixture ()
35
+ def hook_setup_post_package_no_tool_requires ():
36
+ tc = TestClient ()
37
+ hook_path = os .path .join (tc .paths .hooks_path , "hook_sbom.py" )
38
+ save (hook_path , sbom_hook_post_package % ("False" , "True" ))
39
+ return tc
40
+
41
+ @pytest .fixture ()
42
+ def hook_setup_post_package_no_test ():
43
+ tc = TestClient ()
44
+ hook_path = os .path .join (tc .paths .hooks_path , "hook_sbom.py" )
45
+ save (hook_path , sbom_hook_post_package % ("True" , "False" ))
32
46
return tc
33
47
34
48
@pytest .fixture ()
35
49
def hook_setup_post_package_tl (transitive_libraries ):
36
50
tc = transitive_libraries
37
51
hook_path = os .path .join (tc .paths .hooks_path , "hook_sbom.py" )
38
- save (hook_path , sbom_hook_post_package )
52
+ save (hook_path , sbom_hook_post_package % ( "True" , "True" ) )
39
53
return tc
40
54
41
55
@@ -65,14 +79,63 @@ def test_sbom_generation_skipped_dependencies(hook_setup_post_package):
65
79
# A skipped dependency also shows up in the sbom
66
80
assert "pkg:conan/[email protected] ?rref=6a99f55e933fb6feeb96df134c33af44" in content
67
81
82
+ def test_sbom_generation_no_tool_requires (hook_setup_post_package_no_tool_requires ):
83
+ tc = hook_setup_post_package_no_tool_requires
84
+ tc .save ({"app/conanfile.py" : GenConanfile ("app" , "1.0" )
85
+ .with_package_type ("application" ),
86
+ "conanfile.py" : GenConanfile ("foo" , "1.0" ).with_tool_requires ("app/1.0" )})
87
+ tc .run ("create app" )
88
+ tc .run ("create ." )
89
+ create_layout = tc .created_layout ()
90
+
91
+ cyclone_path = os .path .join (create_layout .metadata (), "sbom.cdx.json" )
92
+ content = tc .load (cyclone_path )
93
+
94
+ assert "pkg:conan/app" not in content
95
+
96
+ def test_sbom_generation_transitive_test_requires (hook_setup_post_package_no_test ):
97
+ tc = hook_setup_post_package_no_test
98
+ tc .save ({"test_re/conanfile.py" : GenConanfile ("test_re" , "1.0" ),
99
+ "app/conanfile.py" : GenConanfile ("app" , "1.0" )
100
+ .with_package_type ("application" )
101
+ .with_test_requires ("test_re/1.0" ),
102
+ "conanfile.py" : GenConanfile ("foo" , "1.0" ).with_tool_requires ("app/1.0" )})
103
+ tc .run ("create test_re" )
104
+
105
+ tc .run ("create app" )
106
+ create_layout = tc .created_layout ()
107
+ cyclone_path = os .path .join (create_layout .metadata (), "sbom.cdx.json" )
108
+ content = tc .load (cyclone_path )
109
+ assert "pkg:conan/[email protected] " not in content
110
+
111
+ tc .run ("create ." )
112
+ create_layout = tc .created_layout ()
113
+ cyclone_path = os .path .join (create_layout .metadata (), "sbom.cdx.json" )
114
+ content = tc .load (cyclone_path )
115
+ assert "pkg:conan/[email protected] " not in content
116
+
117
+ def test_sbom_generation_dependency_test_require (hook_setup_post_package_no_test ):
118
+ tc = hook_setup_post_package_no_test
119
+ tc .save ({"special/conanfile.py" : GenConanfile ("special" , "1.0" ),
120
+ "foo/conanfile.py" : GenConanfile ("foo" , "1.0" )
121
+ .with_test_requires ("special/1.0" ),
122
+ "conanfile.py" : GenConanfile ("bar" , "1.0" ).with_tool_requires ("foo/1.0" ).with_require ("special/1.0" )})
123
+ tc .run ("create special" )
124
+ tc .run ("create foo" )
125
+
126
+ tc .run ("create ." )
127
+ create_layout = tc .created_layout ()
128
+ cyclone_path = os .path .join (create_layout .metadata (), "sbom.cdx.json" )
129
+ content = tc .load (cyclone_path )
130
+ assert "pkg:conan/[email protected] " in content
68
131
69
132
# Using the sbom tool with "conan install"
70
133
sbom_hook_post_generate = """
71
134
import json
72
135
import os
73
136
from conan.errors import ConanException
74
137
from conan.api.output import ConanOutput
75
- from conan.tools.sbom.cyclonedx import cyclonedx_1_4
138
+ from conan.tools.sbom import cyclonedx_1_4
76
139
77
140
def post_generate(conanfile):
78
141
sbom_cyclonedx_1_4 = cyclonedx_1_4(conanfile.subgraph)
0 commit comments