From f0799b3789c1efa65cf7e37ffd5b454cfff8f700 Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Tue, 14 Jan 2020 16:39:44 +0100 Subject: [PATCH 1/2] fix: is_license_ok fixed for complex license --- license_sh/helpers.py | 4 ++-- tests/runners/test_helpers.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/license_sh/helpers.py b/license_sh/helpers.py index b9662d8..1ed4df4 100644 --- a/license_sh/helpers.py +++ b/license_sh/helpers.py @@ -42,14 +42,14 @@ def is_license_ok(license_text, whitelist): try: license = licensing.parse(license_text) except: - return license_text in whitelist + return f'{license_text}' in whitelist if license is None: return None if license.isliteral: return license.render() in whitelist - + operator = license.operator.strip() fn = {"OR": any, "AND": all}[operator] diff --git a/tests/runners/test_helpers.py b/tests/runners/test_helpers.py index 7b7a414..80155b9 100644 --- a/tests/runners/test_helpers.py +++ b/tests/runners/test_helpers.py @@ -2,7 +2,7 @@ from anytree import AnyNode from anytree.exporter import DictExporter -from license_sh.helpers import flatten_dependency_tree, annotate_dep_tree +from license_sh.helpers import flatten_dependency_tree, annotate_dep_tree, is_license_ok def get_tree(): @@ -218,6 +218,17 @@ def test_bad_licenses_identified__one_package_ignored_by_package_whitelist(self) _, unknown_licenses = annotate_dep_tree(tree, whitelist, ignored_packages) self.assertSetEqual(set({"GPL"}), unknown_licenses) + def test_is_license_ok_simple(self): + self.assertEqual(is_license_ok("MIT", ["MIT"]), True) + + def test_is_license_ok_negative(self): + self.assertEqual(is_license_ok("MIT", [""]), False) + + def test_is_license_ok_brackets(self): + self.assertEqual(is_license_ok("MIT AND Zlib", ["MIT", "Zlib"]), True) + + def test_is_license_ok_json(self): + self.assertEqual(is_license_ok({'type': 'MIT', 'url': 'https://github.com/thlorenz/bunyan-format/blob/master/LICENSE'}, ["{'type': 'MIT', 'url': 'https://github.com/thlorenz/bunyan-format/blob/master/LICENSE'}"]), True) if __name__ == "__main__": unittest.main() From 915f1b222c4d0d1b52a1f5d0d0ad90c315b9682f Mon Sep 17 00:00:00 2001 From: Bogdan Sikora Date: Tue, 14 Jan 2020 16:40:40 +0100 Subject: [PATCH 2/2] fix: style fix --- license_sh/helpers.py | 4 ++-- tests/runners/test_helpers.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/license_sh/helpers.py b/license_sh/helpers.py index 1ed4df4..39a2dbf 100644 --- a/license_sh/helpers.py +++ b/license_sh/helpers.py @@ -42,14 +42,14 @@ def is_license_ok(license_text, whitelist): try: license = licensing.parse(license_text) except: - return f'{license_text}' in whitelist + return f"{license_text}" in whitelist if license is None: return None if license.isliteral: return license.render() in whitelist - + operator = license.operator.strip() fn = {"OR": any, "AND": all}[operator] diff --git a/tests/runners/test_helpers.py b/tests/runners/test_helpers.py index 80155b9..5b65e64 100644 --- a/tests/runners/test_helpers.py +++ b/tests/runners/test_helpers.py @@ -220,7 +220,7 @@ def test_bad_licenses_identified__one_package_ignored_by_package_whitelist(self) def test_is_license_ok_simple(self): self.assertEqual(is_license_ok("MIT", ["MIT"]), True) - + def test_is_license_ok_negative(self): self.assertEqual(is_license_ok("MIT", [""]), False) @@ -228,7 +228,19 @@ def test_is_license_ok_brackets(self): self.assertEqual(is_license_ok("MIT AND Zlib", ["MIT", "Zlib"]), True) def test_is_license_ok_json(self): - self.assertEqual(is_license_ok({'type': 'MIT', 'url': 'https://github.com/thlorenz/bunyan-format/blob/master/LICENSE'}, ["{'type': 'MIT', 'url': 'https://github.com/thlorenz/bunyan-format/blob/master/LICENSE'}"]), True) + self.assertEqual( + is_license_ok( + { + "type": "MIT", + "url": "https://github.com/thlorenz/bunyan-format/blob/master/LICENSE", + }, + [ + "{'type': 'MIT', 'url': 'https://github.com/thlorenz/bunyan-format/blob/master/LICENSE'}" + ], + ), + True, + ) + if __name__ == "__main__": unittest.main()