1111
1212import responses
1313
14+ from cyclonedx .model import ExternalReferenceType
15+
16+ from capycli .common .capycli_bom_support import CaPyCliBom , CycloneDxSupport
1417from capycli .bom .download_sources import BomDownloadSources
1518from capycli .main .result_codes import ResultCode
1619from tests .test_base import AppArguments , TestBase
1720
1821
19- class TestShowBom (TestBase ):
22+ class TestBomDownloadsources (TestBase ):
2023 INPUTFILE = "sbom_for_download.json"
2124 INPUTERROR = "plaintext.txt"
2225 OUTPUTFILE = "output.json"
@@ -74,7 +77,7 @@ def test_error_loading_file(self) -> None:
7477 args .command = []
7578 args .command .append ("bom" )
7679 args .command .append ("downloadsources" )
77- args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestShowBom .INPUTERROR )
80+ args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .INPUTERROR )
7881
7982 sut .run (args )
8083 self .assertTrue (False , "Failed to report invalid file" )
@@ -90,7 +93,7 @@ def test_source_folder_does_not_exist(self) -> None:
9093 args .command = []
9194 args .command .append ("bom" )
9295 args .command .append ("downloadsources" )
93- args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestShowBom .INPUTFILE )
96+ args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .INPUTFILE )
9497 args .source = "XXX"
9598
9699 sut .run (args )
@@ -107,8 +110,8 @@ def test_simple_bom(self) -> None:
107110 args .command = []
108111 args .command .append ("bom" )
109112 args .command .append ("downloadsources" )
110- args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestShowBom .INPUTFILE )
111- args .outputfile = TestShowBom .OUTPUTFILE
113+ args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .INPUTFILE )
114+ args .outputfile = TestBomDownloadsources .OUTPUTFILE
112115
113116 with tempfile .TemporaryDirectory () as tmpdirname :
114117 args .source = tmpdirname
@@ -126,20 +129,68 @@ def test_simple_bom(self) -> None:
126129
127130 try :
128131 out = self .capture_stdout (sut .run , args )
132+ out_bom = CaPyCliBom .read_sbom (args .outputfile )
129133 # capycli.common.json_support.write_json_to_file(out, "STDOUT.TXT")
130134 self .assertTrue ("Loading SBOM file" in out )
131135 self .assertTrue ("sbom_for_download.json" in out ) # path may vary
136+ self .assertIn ("SBOM file is not relative to" , out )
132137 self .assertTrue ("Downloading source files to folder" in out )
133138 self .assertTrue ("Downloading file certifi-2022.12.7.tar.gz" in out )
134139
135140 resultfile = os .path .join (tmpdirname , "certifi-2022.12.7.tar.gz" )
136141 self .assertTrue (os .path .isfile (resultfile ))
137142
143+ ext_ref = CycloneDxSupport .get_ext_ref (
144+ out_bom .components [0 ], ExternalReferenceType .DISTRIBUTION , CaPyCliBom .SOURCE_FILE_COMMENT )
145+ self .assertEqual (ext_ref .url , resultfile )
146+
138147 self .delete_file (args .outputfile )
139148 return
140- except : # noqa
149+ except Exception as e : # noqa
141150 # catch all exception to let Python cleanup the temp folder
142- pass
151+ print (e )
152+
153+ self .assertTrue (False , "Error: we must never arrive here" )
154+
155+ @responses .activate
156+ def test_simple_bom_relative_path (self ) -> None :
157+ sut = BomDownloadSources ()
158+
159+ # create argparse command line argument object
160+ args = AppArguments ()
161+ args .command = []
162+ args .command .append ("bom" )
163+ args .command .append ("downloadsources" )
164+ args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .INPUTFILE )
165+
166+ with tempfile .TemporaryDirectory () as tmpdirname :
167+ args .source = tmpdirname
168+ args .outputfile = os .path .join (tmpdirname , TestBomDownloadsources .OUTPUTFILE )
169+
170+ # fake file content
171+ responses .add (
172+ responses .GET ,
173+ url = "https://files.pythonhosted.org/packages/37/f7/2b1b/certifi-2022.12.7.tar.gz" ,
174+ body = """
175+ SOME DUMMY DATA
176+ """ ,
177+ status = 200 ,
178+ content_type = "application/json" ,
179+ )
180+
181+ try :
182+ sut .run (args )
183+ out_bom = CaPyCliBom .read_sbom (args .outputfile )
184+
185+ ext_ref = CycloneDxSupport .get_ext_ref (
186+ out_bom .components [0 ], ExternalReferenceType .DISTRIBUTION , CaPyCliBom .SOURCE_FILE_COMMENT )
187+ self .assertEqual (ext_ref .url , "file://certifi-2022.12.7.tar.gz" )
188+
189+ self .delete_file (args .outputfile )
190+ return
191+ except Exception as e : # noqa
192+ # catch all exception to let Python cleanup the temp folder
193+ print (e )
143194
144195 self .assertTrue (False , "Error: we must never arrive here" )
145196
@@ -152,8 +203,8 @@ def test_simple_bom_error_download(self) -> None:
152203 args .command = []
153204 args .command .append ("bom" )
154205 args .command .append ("downloadsources" )
155- args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestShowBom .INPUTFILE )
156- args .outputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestShowBom .OUTPUTFILE )
206+ args .inputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .INPUTFILE )
207+ args .outputfile = os .path .join (os .path .dirname (__file__ ), "fixtures" , TestBomDownloadsources .OUTPUTFILE )
157208
158209 with tempfile .TemporaryDirectory () as tmpdirname :
159210 args .source = tmpdirname
0 commit comments