Skip to content

Commit

Permalink
[pylint] Convert all tests to f-strings
Browse files Browse the repository at this point in the history
Signed-off-by: Arif Ali <[email protected]>
  • Loading branch information
arif-ali authored and TurboTurtle committed May 2, 2024
1 parent 5a2091c commit 799425b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions tests/report_tests/encryption_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class EncryptedReportTest(StageOneReportTest):
"""

encrypt_pass = 'sostest'
sos_cmd = "-o kernel --encrypt-pass %s" % encrypt_pass
sos_cmd = f"-o kernel --encrypt-pass {encrypt_pass}"

def test_archive_gpg_encrypted(self):
self.assertOutputContains(r'/.*sosreport-.*tar.*\.gpg')
_cmd = "file %s" % self.encrypted_path
_cmd = f"file {self.encrypted_path}"
res = process.run(_cmd)
self.assertTrue(
("GPG symmetrically encrypted data" in res.stdout.decode())
Expand All @@ -41,7 +41,7 @@ class EncryptedCleanedReportTest(EncryptedReportTest):
"""

encrypt_pass = 'sostest'
sos_cmd = "-o host,networking --clean --encrypt-pass %s" % encrypt_pass
sos_cmd = f"-o host,networking --clean --encrypt-pass {encrypt_pass}"

def test_hostname_obfuscated(self):
self.assertFileHasContent('hostname', 'host0')
Expand Down
2 changes: 1 addition & 1 deletion tests/report_tests/help_output_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ def test_no_missing_plugin_descriptions(self):
# Ignore newlines
if not ln:
continue
assert len(ln) > 1, "Plugin '%s' missing description" % ln[0]
assert len(ln) > 1, f"Plugin '{ln[0]}' missing description"
2 changes: 1 addition & 1 deletion tests/report_tests/plugin_tests/networking/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_netdevs_properly_iterated(self):
# https://lwn.net/Articles/142330/
if not dev.startswith('bonding_'):
self.assertFileGlobInArchive(
"sos_commands/networking/ethtool_*_%s" % dev
f"sos_commands/networking/ethtool_*_{dev}"
)


Expand Down
4 changes: 2 additions & 2 deletions tests/report_tests/smoke_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AllPluginSmokeTest(StageOneReportTest):
"""

def pre_sos_setup(self):
_cmd = '%s report --list-plugins' % self.sos_bin
_cmd = f'{self.sos_bin} report --list-plugins'
out = process.run(_cmd, timeout=300).stdout.decode()
reg = DISABLED + '(.*?)' + OPTIONS
self.plugs = []
Expand All @@ -38,7 +38,7 @@ def pre_sos_setup(self):
except Exception:
pass

self.sos_cmd = '-e %s' % ','.join(p for p in self.plugs)
self.sos_cmd = f'-e {",".join(p for p in self.plugs)}'

def test_all_plugins_ran(self):
for plugin in self.plugs:
Expand Down
38 changes: 19 additions & 19 deletions tests/sos_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ def _execute_sos_cmd(self):
if err.result.interrupted:
raise Exception("Timeout exceeded, see output above")
else:
raise Exception("Command failed, see output above: '%s'"
% err.command.split('bin/')[1])
raise Exception("Command failed, see output above: "
f"'{err.command.split('bin/')[1]}'")
with open(os.path.join(self.tmpdir, 'output'), 'wb') as pfile:
pickle.dump(self.cmd_output, pfile)
self.cmd_output.stdout = self.cmd_output.stdout.decode()
Expand Down Expand Up @@ -332,11 +332,11 @@ def pre_sos_setup(self):

def assertFileExists(self, fname):
"""Asserts that fname exists on the filesystem"""
assert os.path.exists(fname), "%s does not exist" % fname
assert os.path.exists(fname), f"{fname} does not exist"

def assertFileNotExists(self, fname):
"""Asserts that fname does not exist on the filesystem"""
assert not os.path.exists(fname), "%s exists" % fname
assert not os.path.exists(fname), f"{fname} exists"

def assertOutputContains(self, content):
"""Ensure that stdout did contain the given content string
Expand All @@ -347,7 +347,7 @@ def assertOutputContains(self, content):
found = re.search(
fr"(.*)?{content}(.*)?",
self.cmd_output.stdout + self.cmd_output.stderr)
assert found, "Content string '%s' not in output" % content
assert found, f"Content string '{content}' not in output"

def assertOutputNotContains(self, content):
"""Ensure that stdout did NOT contain the given content string
Expand All @@ -358,7 +358,7 @@ def assertOutputNotContains(self, content):
found = re.search(
fr"(.*)?{content}(.*)?",
self.cmd_output.stdout + self.cmd_output.stderr)
assert not found, "String '%s' present in stdout" % content
assert not found, f"String '{content}' present in stdout"


class BaseSoSReportTest(BaseSoSTest):
Expand Down Expand Up @@ -394,8 +394,8 @@ def encrypted_path(self):

def _decrypt_archive(self, archive):
_archive = archive.strip('.gpg')
cmd = ("gpg --batch --passphrase %s -o %s --decrypt %s"
% (self.encrypt_pass, _archive, archive))
cmd = (f"gpg --batch --passphrase {self.encrypt_pass} -o {_archive} "
f"--decrypt {archive}")
try:
process.run(cmd, timeout=10)
except Exception as err:
Expand All @@ -415,7 +415,7 @@ def grep_for_content(self, search, regexp=False):
means "grep -F")
"""
fixed_opt = "" if regexp else "F"
cmd = "grep -ril%s '%s' %s" % (fixed_opt, search, self.archive_path)
cmd = f"grep -ril{fixed_opt} '{search}' {self.archive_path}"
try:
out = process.run(cmd)
rc = out.exit_status
Expand Down Expand Up @@ -457,7 +457,7 @@ def _extract_archive(self, arc_path):
archive.extract(arc_path, _extract_path)
self.archive_path = self._get_archive_path()
except Exception as err:
self.cancel("Could not extract archive: %s" % err)
self.cancel(f"Could not extract archive: {err}")

def _get_extracted_tarball_path(self):
"""Based on the klass id setup earlier, provide a name to extract the
Expand Down Expand Up @@ -550,7 +550,7 @@ def assertFileGlobInArchive(self, fname):
files = glob.glob(
os.path.join(self.archive_path, fname.lstrip('/'))
)
assert files, "No files matching %s found" % fname
assert files, f"No files matching {fname} found"

def assertFileGlobNotInArchive(self, fname):
"""Ensure that there are NO files in the archive matching a given fname
Expand Down Expand Up @@ -580,7 +580,7 @@ def assertFileHasContent(self, fname, content):
with open(fname, 'r') as lfile:
_contents = lfile.read()
for line in _contents.splitlines():
if re.match(".*%s.*" % content, line, re.I):
if re.match(f".*{content}.*", line, re.I):
matched = True
break
assert \
Expand All @@ -600,7 +600,7 @@ def assertFileNotHasContent(self, fname, content):
fname = self.get_name_in_archive(fname)
with open(fname, 'r') as mfile:
for line in mfile.read().splitlines():
if re.match(".*%s.*" % content, line, re.I):
if re.match(f".*{content}.*", line, re.I):
matched = True
break
assert \
Expand Down Expand Up @@ -680,11 +680,11 @@ def assertOnlyPluginsIncluded(self, plugins):

# test that all requested plugins did run
for i in plugins:
assert i in _executed, "Requested plugin '%s' did not run" % i
assert i in _executed, f"Requested plugin '{i}' did not run"

# test that no unrequested plugins ran
for j in _executed:
assert j in plugins, "Unrequested plugin '%s' ran as well" % j
assert j in plugins, f"Unrequested plugin '{j}' ran as well"

def get_plugin_manifest(self, plugin):
"""Get the manifest data for the specified plugin
Expand All @@ -696,7 +696,7 @@ def get_plugin_manifest(self, plugin):
:rtype: ``dict``
"""
if not self.manifest['components']['report']['plugins'][plugin]:
raise Exception("Manifest for %s not present" % plugin)
raise Exception(f"Manifest for {plugin} not present")
return self.manifest['components']['report']['plugins'][plugin]


Expand Down Expand Up @@ -896,8 +896,8 @@ def setup_mocked_packages(self):
installed = distro_packages.install_distro_packages(self.packages)
if not installed:
raise Exception(
"Unable to install requested packages %s"
% ', '.join(self.packages[self.local_distro])
f"Unable to install requested packages "
f"{', '.join(self.packages[self.local_distro])}"
)
# save installed package list to our tmpdir to be removed later
self._write_file_to_tmpdir(
Expand Down Expand Up @@ -1037,7 +1037,7 @@ class StageOneOutputTest(BaseSoSTest):
sos_cmd = ''

def _generate_sos_command(self):
return "%s %s" % (self.sos_bin, self.sos_cmd)
return f"{self.sos_bin} {self.sos_cmd}"

@skipIf(lambda x: x._exception_expected, "Non-zero exit code expected")
def test_help_output_successful(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/unittests/conformance_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ def test_plugin_tuples_set_correctly(self):
_attr = getattr(plug, tup)
self.assertIsInstance(
_attr, tuple,
"%s.%s is type %s" % (plug.__name__, tup, type(_attr))
f"{plug.__name__}.{tup} is type {type(_attr)}"
)

def test_plugin_description_is_str(self):
for plug in self.plug_classes:
self.assertIsInstance(plug.short_desc, str,
"%s name not string" % plug.__name__)
f"{plug.__name__} name not string")
# make sure the description is not empty
self.assertNotEqual(plug.short_desc, '',
"%s description unset" % plug.__name__)
f"{plug.__name__} description unset")

def test_plugin_name_is_str(self):
for plug in self.plug_classes:
self.assertIsInstance(plug.plugin_name, str,
"%s name not string" % plug.__name__)
f"{plug.__name__} name not string")
self.assertNotEqual(plug.plugin_name, '',
"%s name unset" % plug.__name__)
f"{plug.__name__} name unset")

def test_plugin_option_list_correct(self):
for plug in self.plug_classes:
Expand Down

0 comments on commit 799425b

Please sign in to comment.