Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: akugarg <akanksha.garg2k@gmail.com>
  • Loading branch information
akugarg committed Aug 26, 2021
1 parent a84b1ed commit 466e375
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/licensedcode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,10 @@ def validate(self, licensing=None):
except InvalidRule as e:
yield f'Failed to parse and validate license_expression: {e}'

if self.referenced_filenames:
if len(set(self.referenced_filenames)) != len(self.referenced_filenames):
yield 'referenced_filenames cannot contain duplicates.'

def license_keys(self, unique=True):
"""
Return a list of license keys for this rule.
Expand Down
40 changes: 28 additions & 12 deletions src/licensedcode/plugin_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def process_codebase(self, codebase, **kwargs):

def match_reference_license(resource, codebase):
"""
Return the updated license matches having reference to another files
Return the ``resource`` Resource updating and saving it in place, after adding new
icense matches (licenses and license_expressions) following their Rule
``referenced_filenames`` if any. Return None if this is not a file Resource.
"""
if not resource.is_file:
return
Expand All @@ -141,29 +143,43 @@ def match_reference_license(resource, codebase):

referenced_licenses = []
referenced_license_expressions = []
referenced_filenames = []

for license in licenses:
referenced_files = license['matched_rule']['referenced_filenames']
for referenced_filename in referenced_files:
if not referenced_filename in referenced_filenames:
referenced_filenames.append(referenced_filename)
referenced_filenames = get_referenced_filenames(licenses)
modified = False

for referenced_filename in referenced_filenames:
new_resource = find_reference_licenses(referenced_filename=referenced_filename, resource=resource, codebase=codebase)
new_resource = find_referenced_resource(referenced_filename=referenced_filename, resource=resource, codebase=codebase)
if new_resource:
modified = True
referenced_licenses.extend(new_resource.licenses)
referenced_license_expressions.extend(new_resource.license_expressions)

licenses.extend(referenced_licenses)
license_expressions.extend(referenced_license_expressions)
codebase.save_resource(resource)

if modified:
codebase.save_resource(resource)
return resource


def find_reference_licenses(referenced_filename, resource, codebase, **kwargs):
def get_referenced_filenames(license_matches):
"""
Return a list of unique referenced filenames found in the rules of a list of ``license_matches``
"""
referenced_filenames = []
for license_match in license_matches:
referenced_files = license_match['matched_rule']['referenced_filenames']
for referenced_filename in referenced_files:
if not referenced_filename in referenced_filenames:
referenced_filenames.append(referenced_filename)

return referenced_filenames


def find_referenced_resource(referenced_filename, resource, codebase, **kwargs):
"""
Return the resource object for matching referenced-filename given a resource in codebase
Return a Resource matching the ``referenced_filename`` path or filename given a ``resource`` in ``codebase``.
Return None if the ``referenced_filename`` cannot be found in the same directory as the base ``resource``.
``referenced_filename`` is the path or filename referenced in a LicenseMatch of ``resource``,
"""
parent = resource.parent(codebase)

Expand Down
49 changes: 49 additions & 0 deletions tests/licensedcode/test_plugin_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import os

from click.testing import Result
from licensedcode.plugin_license import find_referenced_resource, match_reference_license
from licensedcode.plugin_license import get_referenced_filenames

import pytest

from commoncode.testcase import FileDrivenTesting
Expand Down Expand Up @@ -67,6 +71,7 @@ def test_license_option_reports_license_texts_diag_long_lines():
test_loc = test_env.get_test_loc('plugin_license/text_long_lines/scan-diag.expected.json')
check_json_scan(test_loc, result_file, regen=False)


def test_license_match_reference():
test_dir = test_env.get_test_loc('plugin_license/license_reference/scan/scan-ref', copy=True)
result_file = test_env.get_temp_file('json')
Expand All @@ -75,6 +80,7 @@ def test_license_match_reference():
test_loc = test_env.get_test_loc('plugin_license/license_reference/scan-ref.expected.json')
check_json_scan(test_loc, result_file, regen=False)


def test_license_match_without_reference():
test_dir = test_env.get_test_loc('plugin_license/license_reference/scan/scan-without-ref', copy=True)
result_file = test_env.get_temp_file('json')
Expand All @@ -83,6 +89,49 @@ def test_license_match_without_reference():
test_loc = test_env.get_test_loc('plugin_license/license_reference/scan-wref.expected.json')
check_json_scan(test_loc, result_file, regen=False)


def test_get_referenced_filenames():
license_matches = [
{'matched_rule': {'referenced_filenames' : ['LICENSE.txt','COPYING']}},
{'matched_rule': {'referenced_filenames' : ['COPYING','LICENSE.txt']}},
{'matched_rule': {'referenced_filenames' : ['copying']}},
{'matched_rule': {'referenced_filenames' : []}},
]
expected = ['LICENSE.txt','COPYING','copying']
assert get_referenced_filenames(license_matches) == expected


def test_find_referenced_resource():
# Setup: Create a new scan to use for a virtual codebase
test_dir = test_env.get_test_loc('plugin_license/license_reference/scan/scan-ref', copy=True)
scan_loc = test_env.get_temp_file('json')
args = ['--license', '--license-text', '--license-text-diagnostics', test_dir, '--json', scan_loc]
run_scan_click(args)

# test proper
from commoncode.resource import VirtualCodebase
codebase = VirtualCodebase(scan_loc)
resource = codebase.get_resource_from_path('scan-ref/license-notice.txt')
result = find_referenced_resource(referenced_filename='LICENSE',resource=resource, codebase=codebase)
assert result.path == 'scan-ref/LICENSE'


def test_match_reference_license():
# Setup: Create a new scan to use for a virtual codebase
test_dir = test_env.get_test_loc('plugin_license/license_reference/scan/scan-ref', copy=True)
scan_loc = test_env.get_temp_file('json')
args = ['--license', '--license-text', '--license-text-diagnostics', test_dir, '--json', scan_loc]
run_scan_click(args)

# test proper
from commoncode.resource import VirtualCodebase
codebase = VirtualCodebase(scan_loc)
resource = codebase.get_resource_from_path('scan-ref/license-notice.txt')
assert len(resource.licenses) == 2
result = match_reference_license(resource=resource, codebase=codebase)
assert len(result.licenses) == 3


def test_reindex_licenses_works():
args = ['--reindex-licenses']
run_scan_click(args)
Expand Down

0 comments on commit 466e375

Please sign in to comment.