11# This file is part of CycloneDX Python
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+ #
215# SPDX-License-Identifier: Apache-2.0
316# Copyright (c) OWASP Foundation. All Rights Reserved.
417
@@ -16,36 +29,36 @@ class TestUtilsPEP621(unittest.TestCase):
1629
1730 def test_license_dict_text_pep621 (self ) -> None :
1831 lfac = LicenseFactory ()
19- fpath = tempfile .mktemp ()
20- project = {
21- 'name' : 'testpkg' ,
22- 'license' : {'text' : 'This is the license text.' },
23- }
24- licenses = list (project2licenses (project , lfac , fpath = fpath ))
25- self .assertEqual (len (licenses ), 1 )
26- lic = licenses [0 ]
27- self .assertIsInstance (lic , DisjunctiveLicense )
28- self .assertIsNone (lic .id )
29- self .assertEqual (lic .text .content , 'This is the license text.' )
30- self .assertEqual (lic .acknowledgement , LicenseAcknowledgement .DECLARED )
32+ with tempfile .TemporaryDirectory () as tmpdir :
33+ fpath = tmpdir # Use the temp directory as the base for any temp files
34+ project = {
35+ 'name' : 'testpkg' ,
36+ 'license' : {'text' : 'This is the license text.' },
37+ }
38+ licenses = list (project2licenses (project , lfac , fpath = fpath ))
39+ self .assertEqual (len (licenses ), 1 )
40+ lic = licenses [0 ]
41+ self .assertIsInstance (lic , DisjunctiveLicense )
42+ self .assertIsNone (lic .id )
43+ self .assertEqual (lic .text .content , 'This is the license text.' )
44+ self .assertEqual (lic .acknowledgement , LicenseAcknowledgement .DECLARED )
3145
3246 def test_license_dict_file_pep621 (self ) -> None :
3347 lfac = LicenseFactory ()
34- with tempfile .NamedTemporaryFile ('w+' , delete = True ) as tf :
35- tf .write ('File license text' )
36- tf .flush ()
48+ with tempfile .TemporaryDirectory () as tmpdir :
49+ file_path = os .path .join (tmpdir , 'license.txt' )
50+ with open (file_path , 'w' ) as tf :
51+ tf .write ('File license text' )
3752 project = {
3853 'name' : 'testpkg' ,
39- 'license' : {'file' : os . path . basename ( tf . name ) },
54+ 'license' : {'file' : 'license.txt' },
4055 }
41- # fpath should be the file path so dirname(fpath) resolves to the correct directory
42- licenses = list (project2licenses (project , lfac , fpath = tf .name ))
43-
44- self .assertEqual (len (licenses ), 1 )
45- lic = licenses [0 ]
46- self .assertIsInstance (lic , DisjunctiveLicense )
47- self .assertIsNotNone (lic .text .content )
48- self .assertEqual (lic .acknowledgement , LicenseAcknowledgement .DECLARED )
56+ licenses = list (project2licenses (project , lfac , fpath = file_path ))
57+ self .assertEqual (len (licenses ), 1 )
58+ lic = licenses [0 ]
59+ self .assertIsInstance (lic , DisjunctiveLicense )
60+ self .assertIsNotNone (lic .text .content )
61+ self .assertEqual (lic .acknowledgement , LicenseAcknowledgement .DECLARED )
4962
5063 def test_license_non_dict_pep621 (self ) -> None :
5164 lfac = LicenseFactory ()
0 commit comments