Skip to content

Commit

Permalink
Always handle the common tmepfile prefix for license_skips
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bryteise committed Sep 19, 2024
1 parent 185099b commit c8f0eff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion autospec/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion tests/test_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 0 additions & 4 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit c8f0eff

Please sign in to comment.