Skip to content

Commit

Permalink
Use for f-string formatting, and updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
wummel committed Nov 9, 2023
1 parent f202e31 commit d3a319e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
16 changes: 8 additions & 8 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
A lot of these tests need an external compression program.
See https://github.com/wummel/patool/blob/debian/debian/control
See https://salsa.debian.org/debian/patool/-/blob/master/debian/control
at the "Suggests:" for a list of packages with supported compression
programs.
The file type detection uses the file(1) program which uses a library
of "magic" patterns. The contents of the magic pattern library can vary
between distributions so the tests might not run on all Linux systems.
between distributions so the tests might not run on all systems.
"""
import os
import patoolib
Expand All @@ -39,7 +39,7 @@ def _need_func(testfunc, name, description):
def check_func(func):
def newfunc(*args, **kwargs):
if not testfunc(name):
pytest.skip("%s %r is not available" % (description, name))
pytest.skip(f"{description} {name!r} is not available")
return func(*args, **kwargs)
setattr(newfunc, fnameattr, getattr(func, fnameattr))
return newfunc
Expand Down Expand Up @@ -72,14 +72,14 @@ def has_module(module):
return _need_func(has_module, name, 'Python module')


def needs_codec (program, codec):
def needs_codec(program, codec):
"""Decorator skipping test if given program codec is not available."""
def check_prog (f):
def newfunc (*args, **kwargs):
def check_prog(f):
def newfunc(*args, **kwargs):
if not patoolib.util.find_program(program):
pytest.skip("program `%s' not available" % program)
pytest.skip(f"program `{program}' not available")
if not has_codec(program, codec):
pytest.skip("codec `%s' for program `%s' not available" % (codec, program))
pytest.skip(f"codec `{codec}' for program `{program}' not available")
return f(*args, **kwargs)
setattr(newfunc, fnameattr, getattr(f, fnameattr))
return newfunc
Expand Down
18 changes: 6 additions & 12 deletions tests/archives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ def archive_commands (self, filename, **kwargs):
if not kwargs.get('skip_create'):
self.archive_create(filename, **kwargs)

def archive_extract (self, filename, check=Content.Recursive):
def archive_extract(self, filename, check=Content.Recursive):
"""Test archive extraction."""
archive = os.path.join(datadir, filename)
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
self.assertTrue(os.path.isabs(archive), f"archive path is not absolute: {archive!r}")
self._archive_extract(archive, check)
# archive name relative to tmpdir
relarchive = os.path.join("..", archive[len(basedir)+1:])
Expand Down Expand Up @@ -128,7 +128,7 @@ def archive_test (self, filename):
for verbosity in (-1, 0, 1, 2):
patoolib.test_archive(archive, program=self.program, verbosity=verbosity, interactive=False, password=self.password)

def archive_create (self, archive, srcfiles=None, check=Content.Recursive):
def archive_create(self, archive, srcfiles=None, check=Content.Recursive):
"""Test archive creation."""
if srcfiles is None:
if check == Content.Recursive:
Expand All @@ -138,7 +138,7 @@ def archive_create (self, archive, srcfiles=None, check=Content.Recursive):
elif check == Content.Multifile:
srcfiles = ('t.txt', 't2.txt',)
else:
raise ValueError('invalid check value %r' % check)
raise ValueError('invalid check value {check!r}')
olddir = patoolib.fileutil.chdir(datadir)
try:
# The format and compression arguments are needed for creating
Expand All @@ -158,7 +158,7 @@ def _archive_create (self, archive, srcfiles, program=None, verbosity=0):
tmpdir = patoolib.fileutil.tmpdir(dir=basedir)
try:
archive = os.path.join(tmpdir, archive)
self.assertTrue(os.path.isabs(archive), "archive path is not absolute: %r" % archive)
self.assertTrue(os.path.isabs(archive), f"archive path is not absolute: {archive!r}")
patoolib.create_archive(archive, srcfiles, verbosity=verbosity, interactive=False, program=program, password=self.password)
self.assertTrue(os.path.isfile(archive))
self.check_created_archive_with_test(archive)
Expand Down Expand Up @@ -235,10 +235,4 @@ def get_filecontent(filename):

def get_nonexisting_directory(basedir):
"""Note: this is _not_ intended to be used to create a directory."""
d = os.path.join(basedir, "foo")
while os.path.exists(d):
d += 'a'
if len(d) > 100:
# wtf
raise ValueError('could not construct unique directory name at %r' % basedir)
return d
return patoolib.fileutil.get_single_outfile(basedir, "foo")
2 changes: 1 addition & 1 deletion tests/archives/test_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestRpm (ArchiveTest):

program = 'rpm'

@needs_program('rpm')
@needs_program(program)
def test_rpm(self):
self.archive_list('t.rpm')
# The rpm test fails on non-rpm system with missing dependencies.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def mime_test(self, func, filename, mime, encoding, info):
file_mime, file_encoding = func(filename)
fail_msg = "%s for archive `%s' should be %s, but was %s. Info: %s"
if isinstance(mime, tuple):
self.assertTrue(file_mime in mime, fail_msg % ("MIME type", filename, "in %s" % str(mime), file_mime, info))
self.assertTrue(file_mime in mime, fail_msg % ("MIME type", filename, f"in {mime}", file_mime, info))
else:
self.assertEqual(file_mime, mime, fail_msg % ("MIME type", filename, mime, file_mime, info))
self.assertEqual(file_encoding, encoding, fail_msg % ("Encoding", filename, encoding, file_encoding, info))
Expand Down

0 comments on commit d3a319e

Please sign in to comment.