def test_generation_with_no_about_resource(self): location = get_test_loc('gen/inv2.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) expected = OrderedDict([(u'.', None)]) assert abouts[0].about_resource.value == expected assert len(errors) == 1
def test_load_inventory_with_errors(self): location = get_test_loc('test_gen/inv4.csv') base_dir = get_temp_dir() errors, abouts = gen.load_inventory(location, base_dir) expected_errors = [ Error( CRITICAL, "Field name: 'confirmed copyright' contains illegal name characters: 0 to 9, a to z, A to Z and _." ), Error(INFO, 'Field resource is a custom field.'), Error(INFO, 'Field test is a custom field.'), Error(INFO, 'Field about_resource: Path') ] # assert [] == errors for exp, err in zip(expected_errors, errors): assert exp.severity == err.severity assert err.message.startswith(exp.message) expected = ( 'about_resource: .\n' 'name: AboutCode\n' 'version: 0.11.0\n' 'description: |\n' ' multi\n' ' line\n' # 'confirmed copyright: Copyright (c) nexB, Inc.\n' 'resource: this.ABOUT\n' 'test: This is a test\n') result = [a.dumps() for a in abouts] assert expected == result[0]
def test_generation_with_no_about_resource(self): location = get_test_loc('test_gen/inv2.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) expected = OrderedDict([('.', None)]) assert abouts[0].about_resource.value == expected assert len(errors) == 1
def test_load_inventory(self): location = get_test_loc('test_gen/inv.csv') base_dir = get_temp_dir() errors, abouts = gen.load_inventory(location, base_dir) expected_errors = [ Error(INFO, 'Field custom1 is a custom field.'), Error(INFO, 'Field about_resource: Path') ] for exp, err in zip(expected_errors, errors): assert exp.severity == err.severity assert err.message.startswith(exp.message) expected = ('''about_resource: . name: AboutCode version: 0.11.0 description: | multi line custom1: | multi line ''') result = [a.dumps() for a in abouts] assert expected == result[0]
def test_android_module_license(self): path = 'test_model/android/single_license.c.ABOUT' test_file = get_test_loc(path) abouts = model.About(location=test_file, about_file_path=path) parent_dir = get_temp_dir() abouts.android_module_license(parent_dir) assert os.path.exists( os.path.join(parent_dir, 'MODULE_LICENSE_PUBLIC_DOMAIN'))
def test_generation_dir_endswith_space(self): location = get_test_loc( 'inventory/complex/about_file_path_dir_endswith_space.csv') base_dir = get_temp_dir() errors, _abouts = gen.generate(location, base_dir) expected_errors_msg = 'contains directory name ends with spaces which is not allowed. Generation skipped.' assert errors assert len(errors) == 1 assert expected_errors_msg in errors[0].message
def test_generation_with_no_about_resource_reference_no_resource_validation( self): location = get_test_loc('gen/inv3.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) expected = OrderedDict([(u'test.tar.gz', None)]) assert abouts[0].about_resource.value == expected assert len(errors) == 1
def test_generation_dir_endswith_space(self): location = get_test_loc('inventory/complex/about_file_path_dir_endswith_space.csv') base_dir = get_temp_dir() errors, _abouts = gen.generate(location, base_dir) expected_errors_msg1 = 'contains directory name ends with spaces which is not allowed. Generation skipped.' expected_errors_msg2 = 'Field about_resource' assert errors assert len(errors) == 2 assert expected_errors_msg1 in errors[0].message or expected_errors_msg1 in errors[1].message assert expected_errors_msg2 in errors[0].message or expected_errors_msg2 in errors[1].message
def test_generation_with_no_about_resource_reference(self): location = get_test_loc('gen/inv3.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) expected = [u'test.tar.gz'] assert abouts[0].about_resource.value == expected assert len(errors) == 1 msg = u'Field about_resource_path' assert msg in errors[0].message
def test_generation_with_no_about_resource_reference(self): location = get_test_loc('gen/inv3.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) expected = OrderedDict([(u'test.tar.gz', None)]) assert abouts[0].about_resource.value == expected assert len(errors) == 1 msg = u'Field about_resource' assert msg in errors[0].message
def test_android_module_multi_licenses(self): path = 'test_model/android/multi_license.c.ABOUT' test_file = get_test_loc(path) abouts = model.About(location=test_file, about_file_path=path) parent_dir = get_temp_dir() abouts.android_module_license(parent_dir) assert os.path.exists( os.path.join(parent_dir, 'MODULE_LICENSE_BSD_NEW')) assert os.path.exists( os.path.join(parent_dir, 'MODULE_LICENSE_BSD_SIMPLIFIED'))
def test_boolean_value_not_lost(self): location = get_test_loc('test_gen/inv6.csv') base_dir = get_temp_dir() _errors, abouts = gen.generate(location, base_dir) in_mem_result = [a.dumps() for a in abouts][0] expected = (u'about_resource: .\n' u'name: AboutCode\n' u'version: 0.11.0\n' u'redistribute: yes\n' u'attribute: yes\n' u'modified: no\n') assert expected == in_mem_result
def test_load_inventory_simple_xlsx(self): location = get_test_loc('test_util/load/simple_sample.xlsx') base_dir = get_temp_dir() errors, abouts = util.load_inventory(location) assert errors == [] assert abouts[0].name.value == 'cryptohash-sha256' assert abouts[1].name.value == 'some_component' assert abouts[0].version.value == 'v 0.11.100.1' assert abouts[1].version.value == 'v 0.0.1' assert abouts[0].license_expression.value == 'bsd-new and mit' assert abouts[1].license_expression.value == 'mit'
def test_generate_multi_lic_issue_444(self): location = get_test_loc('test_gen/multi_lic_issue_444/test1.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) result = [a.dumps() for a in abouts][0] expected = ('''about_resource: test.c name: test.c licenses: - key: License1 file: LIC1.LICENSE, LIC2.LICENSE ''') assert expected == result
def test_copy_license_notice_files(self): base_dir = get_temp_dir() reference_dir = get_test_loc('test_util/licenses') fields = [(u'license_expression', u'mit or public-domain'), (u'about_resource', u'.'), (u'name', u'test'), (u'license_key', [u'mit', u'public-domain']), (u'license_file', [u'mit.LICENSE, mit2.LICENSE', u'public-domain.LICENSE'])] util.copy_license_notice_files(fields, base_dir, reference_dir, '') licenses = ['mit.LICENSE', 'mit2.LICENSE', 'public-domain.LICENSE'] from os import listdir copied_files = listdir(base_dir) assert len(licenses) == len(copied_files) for license in licenses: assert license in copied_files
def test_load_csv_with_conf(self): location = get_test_loc('test_util/load/simple_sample.csv') base_dir = get_temp_dir() configuration = get_test_loc('test_util/load/key.config') inventory = util.load_csv(location, configuration) expected = [ OrderedDict([('name', 'cryptohash-sha256'), ('version', 'v 0.11.100.1'), ('license_expression', 'bsd-new and mit'), ('resource', '/project/cryptohash-sha256')]), OrderedDict([('name', 'some_component'), ('version', 'v 0.0.1'), ('license_expression', 'mit'), ('resource', '/project/some_component')]) ] assert inventory == expected
def test_generate_not_overwrite_original_license_file(self): location = get_test_loc('gen/inv5.csv') base_dir = get_temp_dir() license_notice_text_location = None fetch_license = ['url', 'lic_key'] _errors, abouts = gen.generate(location, base_dir, license_notice_text_location, fetch_license) in_mem_result = [a.dumps(use_mapping=False, mapping_file=False, with_absent=False, with_empty=False) for a in abouts][0] expected = (u'about_resource: .\n' u'name: AboutCode\n' u'version: 0.11.0\n' u'licenses:\n' u' - file: this.LICENSE\n') assert expected == in_mem_result
def test_generate_not_overwrite_original_license_file(self): location = get_test_loc('test_gen/inv5.csv') base_dir = get_temp_dir() reference_dir = None fetch_license = ['url', 'lic_key'] _errors, abouts = gen.generate(location, base_dir, reference_dir, fetch_license) result = [a.dumps() for a in abouts][0] expected = ('about_resource: .\n' 'name: AboutCode\n' 'version: 0.11.0\n' 'licenses:\n' ' - file: this.LICENSE\n') assert expected == result
def test_android_notice(self): path = 'test_model/android/single_license.c.ABOUT' test_file = get_test_loc(path) abouts = model.About(location=test_file, about_file_path=path) parent_dir = get_temp_dir() notice_path, notice_context = abouts.android_notice(parent_dir) expected_path = os.path.join(parent_dir, 'NOTICE') assert os.path.normpath(notice_path) == expected_path expected_notice = '''Copyright (c) xyz This component is released to the public domain by the author. ''' assert notice_context == expected_notice
def test_load_scancode_json_with_conf(self): location = get_test_loc('test_util/load/clean-text-0.3.0-lceupi.json') base_dir = get_temp_dir() configuration = get_test_loc('test_util/load/key.config') inventory = util.load_scancode_json(location, configuration) expected = { 'resource': 'clean-text-0.3.0', 'type': 'directory', 'name': 'clean-text-0.3.0', 'base_name': 'clean-text-0.3.0', 'extension': '', 'size': 0, 'date': None, 'sha1': None, 'md5': None, 'sha256': None, 'mime_type': None, 'file_type': None, 'programming_language': None, 'is_binary': False, 'is_text': False, 'is_archive': False, 'is_media': False, 'is_source': False, 'is_script': False, 'licenses': [], 'license_expressions': [], 'percentage_of_license_text': 0, 'copyrights': [], 'holders': [], 'authors': [], 'packages': [], 'emails': [], 'urls': [], 'files_count': 9, 'dirs_count': 1, 'size_count': 32826, 'scan_errors': [] } # We will only check the first element in the inventory list assert inventory[0] == expected
def test_generate_not_overwrite_original_license_file(self): location = get_test_loc('gen/inv5.csv') base_dir = get_temp_dir() license_notice_text_location = None fetch_license = ['url', 'lic_key'] errors, abouts = gen.generate(location, base_dir, license_notice_text_location, fetch_license) in_mem_result = [ a.dumps(with_absent=False, with_empty=False) for a in abouts ][0] expected = (u'about_resource: .\n' u'name: AboutCode\n' u'about_resource_path: .\n' u'version: 0.11.0\n' u'license_file: this.LICENSE\n') assert expected == in_mem_result
def test_generate(self): location = get_test_loc('gen/inv.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) msg1 = u'Field custom1 is not a supported field and is ignored.' msg2 = u'Field about_resource' assert msg1 in errors[0].message assert msg2 in errors[1].message in_mem_result = [a.dumps(use_mapping=False, mapping_file=False, with_absent=False, with_empty=False) for a in abouts][0] expected = (u'about_resource: .\n' u'name: AboutCode\n' u'version: 0.11.0\n' u'description: |-\n' u' multi\n' u' line\n') assert expected == in_mem_result
def test_generate(self): location = get_test_loc('gen/inv.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) msg1 = u'Field custom1 is not a supported field and is ignored.' msg2 = u'Field about_resource_path' assert msg1 in errors[0].message assert msg2 in errors[1].message in_mem_result = [ a.dumps(with_absent=False, with_empty=False) for a in abouts ][0] expected = (u'about_resource: .\n' u'name: AboutCode\n' u'about_resource_path: .\n' u'version: 0.11.0\n' u'description: |\n' u' multi\n' u' line\n') assert expected == in_mem_result
def test_generate(self): location = get_test_loc('test_gen/inv.csv') base_dir = get_temp_dir() errors, abouts = gen.generate(location, base_dir) msg1 = 'Field custom1 is a custom field.' msg2 = 'Field about_resource' assert msg1 in errors[0].message assert msg2 in errors[1].message result = [a.dumps() for a in abouts][0] expected = ('''about_resource: . name: AboutCode version: 0.11.0 description: | multi line custom1: | multi line ''') assert expected == result
def test_convert_object_to_dict(self): location = get_test_loc('test_util/load/simple_sample.csv') base_dir = get_temp_dir() errors, abouts = util.load_inventory(location) assert errors == [] expected = { 'name': 'cryptohash-sha256', 'version': 'v 0.11.100.1', 'download_url': '', 'homepage_url': '', 'package_url': '', 'notes': '', 'license_expression': 'bsd-new and mit', 'license_key': ['bsd-new', 'mit'], 'license_name': '', 'license_file': '', 'license_url': '', 'copyright': '', 'notice_file': '', 'path': '/project/cryptohash-sha256' } results = util.convert_object_to_dict(abouts[0]) assert results == expected
def test_about_gen_command_can_run_minimally_without_error(): test_inv = get_test_loc('test_cmd/geninventory.csv') gen_dir = get_temp_dir() run_about_command_test_click(['gen', test_inv, gen_dir])
def test_about_command_fails_with_an_unknown_subcommand(): test_dir = get_temp_dir() result = run_about_command_test_click(['foo', test_dir], expected_rc=2) assert b'Error: No such command "foo".' in result.output_bytes