From c8f0eff03b7ecc9e5f8b6a576281d66a7fcc677a Mon Sep 17 00:00:00 2001 From: William Douglas Date: Thu, 19 Sep 2024 13:39:16 -0700 Subject: [PATCH] Always handle the common tmepfile prefix for license_skips Modify license skips to handle the common tempfile prefix of '/tmp/*' and normalize the case where lines lead with a slash vs not. Also remove a duplicate test for globlike match. Signed-off-by: William Douglas --- README.rst | 6 +++--- autospec/license.py | 6 +++++- tests/test_license.py | 30 +++++++++++++++++++++++++++++- tests/test_util.py | 4 ---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.rst b/README.rst index 55175667..455b815a 100644 --- a/README.rst +++ b/README.rst @@ -129,9 +129,9 @@ release respectively. This results in less manual work via automatic management. license_skips - Each line in the file should be a full path, that path is prefixed into a - tempfile directory + the package tarfile prefix. Requires using '*' to be - effective (e.g. /tmp/*/pkgname-*/path/to/license). + Each line in the file should be the path to a license file. That path needs + to account for the package tarfile prefix. Likely requires using '*' to be + effective (e.g. ``pkgname-*/path/to/license`` where ``*`` handles the version). Files paths can contain a single '*' per directory such that a line of ``/foo*/bar*`` is allowed but ``/foo*bar*`` is not. diff --git a/autospec/license.py b/autospec/license.py index 3ce56632..57be1fc6 100644 --- a/autospec/license.py +++ b/autospec/license.py @@ -151,7 +151,11 @@ def skip_license(license_path, config): """Check if a given license file path should be skipped.""" skip_name = False for skip in config.license_skips: - if util.globlike_match(license_path, skip): + # handle the common tempfile prefix and normalize for + # skip lines without a starting '/' + skip = skip if skip[0] != '' else skip[1:] + skip_path = ['', 'tmp', '*'] + skip + if util.globlike_match(license_path, skip_path): util.print_warning(f"Skip license detected for file at {license_path}") skip_name = True break diff --git a/tests/test_license.py b/tests/test_license.py index 9557e71c..b6f748a5 100644 --- a/tests/test_license.py +++ b/tests/test_license.py @@ -239,7 +239,35 @@ def test_scan_for_licenses_skip(self): """ conf = config.Config("") conf.setup_patterns() - conf.license_skips = [['', 'tmp', '*', 'COPYING']] + conf.license_skips = [['COPYING']] + with open('tests/COPYING_TEST', 'rb') as copyingf: + content = copyingf.read() + + with tempfile.TemporaryDirectory() as tmpd: + # create the copying file + with open(os.path.join(tmpd, 'COPYING'), 'w') as newcopyingf: + newcopyingf.write(content.decode('utf-8')) + # create some cruft for testing + for testf in ['testlib.c', 'testmain.c', 'testheader.h']: + with open(os.path.join(tmpd, testf), 'w') as newtestf: + newtestf.write('test content') + # let's check that the proper thing is being printed as well + out = StringIO() + with redirect_stdout(out): + with self.assertRaises(SystemExit) as thread: + license.scan_for_licenses(tmpd, conf, '') + + self.assertEqual(thread.exception.code, 1) + self.assertIn("Cannot find any license", out.getvalue()) + self.assertEqual(license.licenses, []) + + def test_scan_for_licenses_skip_prefix_slash(self): + """ + Test scan_for_licenses in temporary directory with licenses to skip + """ + conf = config.Config("") + conf.setup_patterns() + conf.license_skips = [['', 'COPYING']] with open('tests/COPYING_TEST', 'rb') as copyingf: content = copyingf.read() diff --git a/tests/test_util.py b/tests/test_util.py index b9227108..7ff18497 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -106,10 +106,6 @@ def test_globlike_match(self): file_path = 'a/ab' self.assertFalse(util.globlike_match(file_path, match_name)) - match_name = ['a', 'b*'] - file_path = 'a/ab' - self.assertFalse(util.globlike_match(file_path, match_name)) - match_name = ['a', '*a'] file_path = 'a/ab' self.assertFalse(util.globlike_match(file_path, match_name))