def test_get_package_resources_on_nested_packages_should_include_manifest(
         self):
     from packagedcode import get_package_instance
     from commoncode.resource import VirtualCodebase
     scan_file = self.get_scan('plugin_consolidate/nested-packages',
                               cli_options='-p')
     codebase = VirtualCodebase(scan_file)
     for resource in codebase.walk():
         for package_data in resource.packages:
             package = get_package_instance(package_data)
             package_resources = list(
                 package.get_package_resources(resource, codebase))
             assert any(r.name == 'package.json'
                        for r in package_resources), resource.path
Exemplo n.º 2
0
 def test_Package_get_package_resource_works_with_nested_packages_and_ignores(
         self):
     from packagedcode import get_package_instance
     from packagedcode import npm
     from commoncode.resource import VirtualCodebase
     scan_loc = self.get_test_loc('models/nested-npm-packages.json')
     codebase = VirtualCodebase(scan_loc)
     for resource in codebase.walk():
         for package_data in resource.packages:
             package = get_package_instance(package_data)
             assert isinstance(package, npm.NpmPackage)
             package_resources = list(
                 package.get_package_resources(resource, codebase))
             assert any(r.name == 'package.json'
                        for r in package_resources), resource.path
def test_HtmlOutput_process_codebase_does_not_fail_with_non_ascii_scanned_paths_and_file_opened_in_text_mode_with_utf(
):
    test_scan = '''{
          "scancode_notice": "Generated with ScanCode...",
          "scancode_version": "2.9.7.post137.2e29fe3.dirty.20181120225811",
          "scancode_options": {
            "input": "han/",
            "--json-pp": "-"
          },
          "scan_start": "2018-11-23T123252.191917",
          "files_count": 1,
          "files": [
            {
              "path": "han",
              "type": "directory",
              "scan_errors": []
            },
            {
              "path": "han/\u636e.svg",
              "type": "file",
              "scan_errors": []
            }
          ]
        }'''
    codebase = VirtualCodebase(test_scan)
    result_file = test_env.get_temp_file('html')
    ho = HtmlOutput()
    with io.open(result_file, 'w', encoding='utf-8') as html:
        ho.process_codebase(codebase, html)
    results = io.open(result_file, encoding='utf-8').read()
    assert '<td>han/据.svg</td>' in results
Exemplo n.º 4
0
def get_virtual_codebase(project, input_location):
    """
    Return a ScanCode virtual codebase built from the JSON scan file at
    `input_location`.
    """
    temp_path = project.tmp_path / "scancode-temp-resource-cache"
    temp_path.mkdir(parents=True, exist_ok=True)

    return VirtualCodebase(input_location, temp_dir=str(temp_path), max_in_memory=0)
def test_find_referenced_resource_does_not_find_based_file_name_suffix():
    # Setup: Create a new scan to use for a virtual codebase. This directory has
    # two test file with the same name suffix which is also a referenced
    # filename
    test_dir = test_env.get_test_loc(
        'plugin_license/license_reference/scan/scan-ref-dupe-name-suffix',
        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-dupe-name-suffix/license-notice.txt')
    result = find_referenced_resource(referenced_filename='LICENSE',
                                      resource=resource,
                                      codebase=codebase)
    assert result.path == 'scan-ref-dupe-name-suffix/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',
        '--unknown-licenses',
        '--json',
        scan_loc,
        test_dir,
    ]
    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 = add_referenced_filenames_license_matches(resource=resource,
                                                      codebase=codebase)
    assert len(result.licenses) == 3
def check_plugin(plugin_class, test_file='reuse/vb.json', force_text=False):
    # this is the result of this scan:
    # ./scancode -clip --summary --license-clarity-score --summary-key-files
    # --classify  samples/ --json-pp vb.json -n
    test_file = test_env.get_test_loc(test_file)
    from commoncode.resource import VirtualCodebase

    cb = VirtualCodebase(test_file)

    result_file = test_env.get_temp_file('reuse')
    op = plugin_class()
    if force_text:
        with io.open(result_file, 'w', encoding='utf-8') as out:
            op.process_codebase(cb, out)
    else:
        with io.open(result_file, 'w') as out:
            op.process_codebase(cb, out)

    with io.open(result_file, 'r', encoding='utf-8') as inp:
        assert 'zlib' in inp.read()