1818import saneyaml
1919
2020from packagedcode import debian_copyright
21+ from itertools import chain
2122
2223
2324def check_expected_parse_copyright_file (
@@ -31,15 +32,15 @@ def check_expected_parse_copyright_file(
3132 at `expected_loc` location. Regen the expected file if `regen` is True.
3233 """
3334 if with_details :
34- filter_licenses = False
35- skip_debian_packaging = False
36- simplify_licenses = False
37- unique_copyrights = False
35+ filter_licenses = False
36+ skip_debian_packaging = False
37+ simplify_licenses = False
38+ unique_copyrights = False
3839 else :
39- filter_licenses = True
40- skip_debian_packaging = True
41- simplify_licenses = False
42- unique_copyrights = True
40+ filter_licenses = True
41+ skip_debian_packaging = True
42+ simplify_licenses = False
43+ unique_copyrights = True
4344
4445 dc = debian_copyright .parse_copyright_file (location = test_loc , check_consistency = False )
4546 declared_license = dc .get_declared_license (
@@ -58,7 +59,7 @@ def check_expected_parse_copyright_file(
5859 )
5960
6061 parsed = declared_license , license_expression , copyright
61-
62+
6263 result = saneyaml .dump (list (parsed ))
6364 if regen :
6465 with io .open (expected_loc , 'w' , encoding = 'utf-8' ) as reg :
@@ -67,17 +68,56 @@ def check_expected_parse_copyright_file(
6768 with io .open (expected_loc , encoding = 'utf-8' ) as ex :
6869 expected = ex .read ()
6970
70- if result != expected :
71+ if result != expected or 'unknown' in result :
72+
73+ if isinstance (dc , debian_copyright .UnstructuredCopyrightProcessor ):
74+ matches = dc .license_matches
75+
76+ elif isinstance (dc , debian_copyright .StructuredCopyrightProcessor ):
77+ matches = (
78+ ld .license_matches
79+ for ld in dc .license_detections
80+ if ld .license_matches
81+ )
82+ matches = chain .from_iterable (matches )
83+ matches = [lm for lm in matches if lm ]
7184
85+ match_details = list (map (get_match_details , matches ))
86+ match_details = saneyaml .dump (match_details )
7287 expected = '\n ' .join ([
7388 'file://' + test_loc ,
7489 'file://' + expected_loc ,
75- expected
90+ expected ,
91+ match_details ,
7692 ])
7793
7894 assert result == expected
7995
8096
97+ def get_match_details (match ):
98+ """
99+ Return a mapping of match details for LicenseMatch ``match``.
100+ """
101+ details = {}
102+ details ['score' ] = match .score ()
103+ details ['start_line' ] = match .start_line
104+ details ['end_line' ] = match .end_line
105+ details ['matcher' ] = match .matcher
106+ details ['rule_length' ] = match .rule .length
107+ details ['matched_length' ] = match .len ()
108+ details ['match_coverage' ] = match .coverage ()
109+ details ['rule_relevance' ] = match .rule .relevance
110+ details ['identifier' ] = match .rule .identifier
111+ details ['license_expression' ] = match .rule .license_expression
112+ details ['is_license_text' ] = match .rule .is_license_text
113+ details ['is_license_notice' ] = match .rule .is_license_notice
114+ details ['is_license_reference' ] = match .rule .is_license_reference
115+ details ['is_license_tag' ] = match .rule .is_license_tag
116+ details ['is_license_intro' ] = match .rule .is_license_intro
117+ details ['matched_text' ] = match .matched_text (whole_lines = False , highlight = True )
118+ return details
119+
120+
81121def relative_walk (dir_path ):
82122 """
83123 Walk path and yield files paths relative to dir_path.
@@ -181,14 +221,14 @@ class TestDebianDetector(FileBasedTesting):
181221 test_data_dir = path .join (path .dirname (__file__ ), 'data/debian/copyright/' )
182222
183223 def test_add_unknown_matches (self ):
184-
185- matches = debian_copyright .add_unknown_matches (name = 'foo' ,text = 'bar' )
224+
225+ matches = debian_copyright .add_unknown_matches (name = 'foo' , text = 'bar' )
186226 assert len (matches ) == 1
187-
227+
188228
189229class TestEnhancedDebianCopyright (FileBasedTesting ):
190230 test_data_dir = path .join (path .dirname (__file__ ), 'data/debian/copyright/' )
191-
231+
192232 def test_is_paragraph_debian_packaging (self ):
193233 test_file = self .get_test_loc ("debian-slim-2021-04-07/usr/share/doc/libhogweed6/copyright" )
194234 edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
@@ -208,14 +248,14 @@ def test_get_header_para(self):
208248 assert header_para .license .name == "LGPL-3+ or GPL-2+"
209249 assert header_para .upstream_name .value == "Nettle"
210250 assert header_para .source .text == "http://www.lysator.liu.se/~nisse/nettle/"
211-
251+
212252 def test_get_files_paras (self ):
213253 test_file = self .get_test_loc ("debian-2019-11-15/main/c/cryptsetup/stable_copyright" )
214254 edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
215255 files_paras = edebian_copyright .file_paragraphs
216256 assert len (files_paras ) == 15
217257 assert files_paras [1 ].license .name == "GPL-2+"
218-
258+
219259 def test_get_license_paras (self ):
220260 test_file = self .get_test_loc ("debian-2019-11-15/main/c/cryptsetup/stable_copyright" )
221261 edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
@@ -229,70 +269,21 @@ def test_get_paras_with_license_text(self):
229269 paras_with_license = edebian_copyright .paragraphs_with_license_text
230270 assert isinstance (paras_with_license [0 ], debian_copyright .CopyrightHeaderParagraph )
231271 assert isinstance (paras_with_license [1 ], debian_copyright .CopyrightFilesParagraph )
232-
233272
234273 def test_get_other_paras (self ):
235274 test_file = self .get_test_loc ("crafted_for_tests/test_other_paras" )
236275 edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
237276 other_paras = edebian_copyright .other_paragraphs
238277 assert len (other_paras ) == 1
239278 assert other_paras [0 ].extra_data ["unknown" ].text == "Example of other paras."
240-
279+
241280 def test_get_duplicate_license_paras (self ):
242281 test_file = self .get_test_loc ("crafted_for_tests/test_duplicate_license_para_name" )
243282 edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
244283 duplicate_paras = edebian_copyright .duplicate_license_paragraphs
245284 assert len (duplicate_paras ) == 1
246285 duplicate_paras [0 ].license .name == "GPL-2+"
247286
248- def test_get_license_nameless_paras_with_name (self ):
249- test_file = self .get_test_loc ("crafted_for_tests/test_license_with_names" )
250- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
251- nameless_paras = edebian_copyright .license_nameless_paragraphs
252- assert len (nameless_paras ) == 0
253-
254- def test_get_license_nameless_paras_without_name (self ):
255- test_file = self .get_test_loc ("crafted_for_tests/test_license_nameless" )
256- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
257- nameless_paras = edebian_copyright .license_nameless_paragraphs
258- assert len (nameless_paras ) == 1
259-
260- def test_is_all_licenses_used_all_used (self ):
261- test_file = self .get_test_loc ("crafted_for_tests/test_license_with_names" )
262- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
263- assert edebian_copyright .is_all_licenses_used
264-
265- def test_is_all_licenses_used_all_not_used (self ):
266- test_file = self .get_test_loc ("crafted_for_tests/test_all_licenses_not_used" )
267- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
268- assert not edebian_copyright .is_all_licenses_used
269-
270- def test_is_all_licenses_expressions_parsable_case_parsable (self ):
271- test_file = self .get_test_loc ("crafted_for_tests/test_license_with_names" )
272- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
273- assert edebian_copyright .is_all_licenses_expressions_parsable
274-
275- def test_is_all_licenses_expressions_parsable_case_unparsable (self ):
276- test_file = self .get_test_loc ("crafted_for_tests/test_licenses_unparsable" )
277- edebian_copyright = debian_copyright .EnhancedDebianCopyright (debian_copyright = DebianCopyright .from_file (test_file ))
278- assert not edebian_copyright .is_all_licenses_expressions_parsable
279-
280- def test_consistency_structured_copyright_file_inconsistent (self ):
281- test_file = self .get_test_loc ("debian-slim-2021-04-07/usr/share/doc/perl-base/copyright" )
282- try :
283- debian_copyright .parse_copyright_file (location = test_file , check_consistency = True )
284- self .fail (msg = "Exception not raised" )
285- except debian_copyright .DebianCopyrightStructureError :
286- pass
287-
288- def test_consistency_unstructured_copyright_file (self ):
289- test_file = self .get_test_loc ("debian-2019-11-15/main/p/pulseaudio/stable_copyright" )
290- try :
291- debian_copyright .parse_copyright_file (location = test_file , check_consistency = True )
292- self .fail (msg = "Exception not raised" )
293- except debian_copyright .DebianCopyrightStructureError :
294- pass
295-
296287 def test_if_structured_copyright_file (self ):
297288 test_file = self .get_test_loc ("debian-slim-2021-04-07/usr/share/doc/libhogweed6/copyright" )
298289 content = debian_copyright .unicode_text (test_file )
@@ -309,4 +300,4 @@ def test_multiple_blank_lines_is_valid_paragraph(self):
309300 other_paras = edebian_copyright .other_paragraphs
310301 print (other_paras )
311302 assert len (other_paras ) == 1
312- assert other_paras [0 ].extra_data ["unknown" ].text == "This is a catchall para."
303+ assert other_paras [0 ].extra_data ["unknown" ].text == "This is a catchall para."
0 commit comments