99
1010import os
1111
12+ from click .testing import Result
13+ from licensedcode .plugin_license import find_referenced_resource , match_reference_license
14+ from licensedcode .plugin_license import get_referenced_filenames
15+
1216import pytest
1317
1418from commoncode .testcase import FileDrivenTesting
@@ -67,6 +71,7 @@ def test_license_option_reports_license_texts_diag_long_lines():
6771 test_loc = test_env .get_test_loc ('plugin_license/text_long_lines/scan-diag.expected.json' )
6872 check_json_scan (test_loc , result_file , regen = False )
6973
74+
7075def test_license_match_reference ():
7176 test_dir = test_env .get_test_loc ('plugin_license/license_reference/scan/scan-ref' , copy = True )
7277 result_file = test_env .get_temp_file ('json' )
@@ -75,6 +80,7 @@ def test_license_match_reference():
7580 test_loc = test_env .get_test_loc ('plugin_license/license_reference/scan-ref.expected.json' )
7681 check_json_scan (test_loc , result_file , regen = False )
7782
83+
7884def test_license_match_without_reference ():
7985 test_dir = test_env .get_test_loc ('plugin_license/license_reference/scan/scan-without-ref' , copy = True )
8086 result_file = test_env .get_temp_file ('json' )
@@ -83,6 +89,49 @@ def test_license_match_without_reference():
8389 test_loc = test_env .get_test_loc ('plugin_license/license_reference/scan-wref.expected.json' )
8490 check_json_scan (test_loc , result_file , regen = False )
8591
92+
93+ def test_get_referenced_filenames ():
94+ license_matches = [
95+ {'matched_rule' : {'referenced_filenames' : ['LICENSE.txt' ,'COPYING' ]}},
96+ {'matched_rule' : {'referenced_filenames' : ['COPYING' ,'LICENSE.txt' ]}},
97+ {'matched_rule' : {'referenced_filenames' : ['copying' ]}},
98+ {'matched_rule' : {'referenced_filenames' : []}},
99+ ]
100+ expected = ['LICENSE.txt' ,'COPYING' ,'copying' ]
101+ assert get_referenced_filenames (license_matches ) == expected
102+
103+
104+ def test_find_referenced_resource ():
105+ # Setup: Create a new scan to use for a virtual codebase
106+ test_dir = test_env .get_test_loc ('plugin_license/license_reference/scan/scan-ref' , copy = True )
107+ scan_loc = test_env .get_temp_file ('json' )
108+ args = ['--license' , '--license-text' , '--license-text-diagnostics' , test_dir , '--json' , scan_loc ]
109+ run_scan_click (args )
110+
111+ # test proper
112+ from commoncode .resource import VirtualCodebase
113+ codebase = VirtualCodebase (scan_loc )
114+ resource = codebase .get_resource_from_path ('scan-ref/license-notice.txt' )
115+ result = find_referenced_resource (referenced_filename = 'LICENSE' ,resource = resource , codebase = codebase )
116+ assert result .path == 'scan-ref/LICENSE'
117+
118+
119+ def test_match_reference_license ():
120+ # Setup: Create a new scan to use for a virtual codebase
121+ test_dir = test_env .get_test_loc ('plugin_license/license_reference/scan/scan-ref' , copy = True )
122+ scan_loc = test_env .get_temp_file ('json' )
123+ args = ['--license' , '--license-text' , '--license-text-diagnostics' , test_dir , '--json' , scan_loc ]
124+ run_scan_click (args )
125+
126+ # test proper
127+ from commoncode .resource import VirtualCodebase
128+ codebase = VirtualCodebase (scan_loc )
129+ resource = codebase .get_resource_from_path ('scan-ref/license-notice.txt' )
130+ assert len (resource .licenses ) == 2
131+ result = match_reference_license (resource = resource , codebase = codebase )
132+ assert len (result .licenses ) == 3
133+
134+
86135def test_reindex_licenses_works ():
87136 args = ['--reindex-licenses' ]
88137 run_scan_click (args )
0 commit comments