[tests] use f-strings (##7671)

This commit is contained in:
Mike Fährmann
2025-08-14 10:22:42 +02:00
parent a87c447af2
commit b9429de774
10 changed files with 117 additions and 111 deletions

View File

@@ -229,7 +229,7 @@ class TestConfigFiles(unittest.TestCase):
with open(path) as fp:
return util.json_loads(fp.read())
except FileNotFoundError:
raise unittest.SkipTest(path + " not available")
raise unittest.SkipTest(f"{path} not available")
if __name__ == "__main__":

View File

@@ -158,7 +158,7 @@ class TestCookieUtils(unittest.TestCase):
extr.cookies.set("cd_a", "1", domain=extr.cookies_domain)
self.assertTrue(extr.cookies_check(("cd_a",)))
extr.cookies.set("wd_a", "1", domain="www" + extr.cookies_domain)
extr.cookies.set("wd_a", "1", domain=f"www{extr.cookies_domain}")
self.assertFalse(extr.cookies_check(("wd_a",)))
self.assertEqual(len(extr.cookies), 3)
@@ -183,7 +183,7 @@ class TestCookieUtils(unittest.TestCase):
extr.cookies.set("cd_a", "1", domain=extr.cookies_domain)
self.assertTrue(extr.cookies_check(("cd_a",), subdomains=True))
extr.cookies.set("wd_a", "1", domain="www" + extr.cookies_domain)
extr.cookies.set("wd_a", "1", domain=f"www{extr.cookies_domain}")
self.assertTrue(extr.cookies_check(("wd_a",), subdomains=True))
extr.cookies.set("cd_b", "2", domain=extr.cookies_domain)

View File

@@ -183,7 +183,7 @@ class TestDownloaderBase(unittest.TestCase):
@classmethod
def _prepare_destination(cls, content=None, part=True, extension=None):
name = "file-{}".format(cls.fnum)
name = f"file-{cls.fnum}"
cls.fnum += 1
kwdict = {
@@ -199,7 +199,7 @@ class TestDownloaderBase(unittest.TestCase):
pathfmt.build_path()
if content:
mode = "w" + ("b" if isinstance(content, bytes) else "")
mode = "wb" if isinstance(content, bytes) else "w"
with pathfmt.open(mode) as fp:
fp.write(content)
@@ -211,10 +211,10 @@ class TestDownloaderBase(unittest.TestCase):
success = self.downloader.download(url, pathfmt)
# test successful download
self.assertTrue(success, "downloading '{}' failed".format(url))
self.assertTrue(success, f"downloading '{url}' failed")
# test content
mode = "r" + ("b" if isinstance(output, bytes) else "")
mode = "rb" if isinstance(output, bytes) else "r"
with pathfmt.open(mode) as fp:
content = fp.read()
self.assertEqual(content, output)
@@ -245,16 +245,16 @@ class TestHTTPDownloader(TestDownloaderBase):
server = http.server.HTTPServer((host, port), HttpRequestHandler)
except OSError as exc:
raise unittest.SkipTest(
"cannot spawn local HTTP server ({})".format(exc))
f"cannot spawn local HTTP server ({exc})")
host, port = server.server_address
cls.address = "http://{}:{}".format(host, port)
cls.address = f"http://{host}:{port}"
threading.Thread(target=server.serve_forever, daemon=True).start()
def _run_test(self, ext, input, output,
extension, expected_extension=None):
TestDownloaderBase._run_test(
self, self.address + "/" + ext, input, output,
self, f"{self.address}/{ext}", input, output,
extension, expected_extension)
def tearDown(self):
@@ -281,7 +281,7 @@ class TestHTTPDownloader(TestDownloaderBase):
self._run_test("gif", None, DATA["gif"], "jpg", "gif")
def test_http_filesize_min(self):
url = self.address + "/gif"
url = f"{self.address}/gif"
pathfmt = self._prepare_destination(None, extension=None)
self.downloader.minsize = 100
with self.assertLogs(self.downloader.log, "WARNING"):
@@ -290,7 +290,7 @@ class TestHTTPDownloader(TestDownloaderBase):
self.assertEqual(pathfmt.temppath, "")
def test_http_filesize_max(self):
url = self.address + "/jpg"
url = f"{self.address}/jpg"
pathfmt = self._prepare_destination(None, extension=None)
self.downloader.maxsize = 100
with self.assertLogs(self.downloader.log, "WARNING"):
@@ -334,8 +334,8 @@ class HttpRequestHandler(http.server.BaseHTTPRequestHandler):
match = re.match(r"bytes=(\d+)-", self.headers["Range"])
start = int(match[1])
headers["Content-Range"] = "bytes {}-{}/{}".format(
start, len(output)-1, len(output))
headers["Content-Range"] = \
f"bytes {start}-{len(output) - 1}/{len(output)}"
output = output[start:]
else:
status = 200
@@ -408,7 +408,7 @@ for ext, content in SAMPLES:
DATA[ext] = content
for idx, (_, content) in enumerate(SAMPLES):
DATA["S{:>02}".format(idx)] = content
DATA[f"S{idx:>02}"] = content
# reverse mime types mapping
@@ -421,8 +421,8 @@ MIME_TYPES = {
def generate_tests():
def generate_test(idx, ext, content):
def test(self):
self._run_test("S{:>02}".format(idx), None, content, "bin", ext)
test.__name__ = "test_http_ext_{:>02}_{}".format(idx, ext)
self._run_test(f"S{idx:>02}", None, content, "bin", ext)
test.__name__ = f"test_http_ext_{idx:>02}_{ext}"
return test
for idx, (ext, content) in enumerate(SAMPLES):

View File

@@ -110,7 +110,7 @@ class TestExtractorModule(unittest.TestCase):
except AssertionError:
pass
else:
self.fail(result["#url"] + ": Test did not fail")
self.fail(f"{result['#url']}: Test did not fail")
else:
self.assertCategories(result)
@@ -167,8 +167,7 @@ class TestExtractorModule(unittest.TestCase):
extr.finalize()
except ImportError as exc:
if exc.name in ("youtube_dl", "yt_dlp"):
raise unittest.SkipTest("cannot import module '{}'".format(
exc.name))
raise unittest.SkipTest(f"cannot import module '{exc.name}'")
raise
def test_docstrings(self):
@@ -179,7 +178,7 @@ class TestExtractorModule(unittest.TestCase):
self.assertNotEqual(
extr1.__doc__,
extr2.__doc__,
"{} <-> {}".format(extr1, extr2),
f"{extr1} <-> {extr2}",
)
def test_names(self):
@@ -191,12 +190,10 @@ class TestExtractorModule(unittest.TestCase):
for extr in extractor.extractors():
if extr.category not in ("", "oauth", "ytdl"):
expected = "{}{}Extractor".format(
capitalize(extr.category),
capitalize(extr.subcategory),
)
expected = (f"{capitalize(extr.category)}"
f"{capitalize(extr.subcategory)}Extractor")
if expected[0].isdigit():
expected = "_" + expected
expected = f"_{expected}"
self.assertEqual(expected, extr.__name__)
@@ -225,7 +222,7 @@ class TestExtractorWait(unittest.TestCase):
calls = sleep.mock_calls
self.assertEqual(len(calls), 1)
self.assertAlmostEqual(calls[0][1][0], 6.0, places=1)
self.assertAlmostEqual(calls[0][1][0], 6.0, places=0)
calls = log.info.mock_calls
self.assertEqual(len(calls), 1)
@@ -266,7 +263,7 @@ class TextExtractorOAuth(unittest.TestCase):
def test_oauth1(self):
for category in ("flickr", "smugmug", "tumblr"):
extr = extractor.find("oauth:" + category)
extr = extractor.find(f"oauth:{category}")
with patch.object(extr, "_oauth1_authorization_flow") as m:
for msg in extr:
@@ -275,7 +272,7 @@ class TextExtractorOAuth(unittest.TestCase):
def test_oauth2(self):
for category in ("deviantart", "reddit"):
extr = extractor.find("oauth:" + category)
extr = extractor.find(f"oauth:{category}")
with patch.object(extr, "_oauth2_authorization_code_grant") as m:
for msg in extr:

View File

@@ -73,8 +73,8 @@ class TestFormatter(unittest.TestCase):
self._run_test("{u!H}", "'< / >'")
self._run_test("{n!H}", "")
self._run_test("{a!s}", self.kwdict["a"])
self._run_test("{a!r}", "'" + self.kwdict["a"] + "'")
self._run_test("{a!a}", "'" + self.kwdict["a"] + "'")
self._run_test("{a!r}", f"'{self.kwdict['a']}'")
self._run_test("{a!a}", f"'{self.kwdict['a']}'")
self._run_test("{b!a}", "'\\xe4\\xf6\\xfc'")
self._run_test("{a!S}", self.kwdict["a"])
self._run_test("{l!S}", "a, b, c")
@@ -139,7 +139,7 @@ class TestFormatter(unittest.TestCase):
self._run_test("{missing}" , replacement, default)
self._run_test("{missing.attr}", replacement, default)
self._run_test("{missing[key]}", replacement, default)
self._run_test("{missing:?a//}", "a" + default, default)
self._run_test("{missing:?a//}", f"a{default}", default)
def test_fmt_func(self):
self._run_test("{t}" , self.kwdict["t"] , None, int)
@@ -444,11 +444,11 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{a}")
fmt1 = formatter.parse("\fT " + path1)
fmt1 = formatter.parse(f"\fT {path1}")
with open(path2, "w") as fp:
fp.write("{a!u:Rh/C/}\nFooBar")
fmt2 = formatter.parse("\fT " + path2)
fmt2 = formatter.parse(f"\fT {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict), "HELLO WORLD\nFooBar")
@@ -458,15 +458,18 @@ class TestFormatter(unittest.TestCase):
def test_expression(self):
self._run_test("\fE a", self.kwdict["a"])
self._run_test("\fE name * 2 + ' ' + a", "{}{} {}".format(
self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
self._run_test(
"\fE name * 2 + ' ' + a",
f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
def test_fstring(self):
self._run_test("\fF {a}", self.kwdict["a"])
self._run_test("\fF {name}{name} {a}", "{}{} {}".format(
self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
self._run_test("\fF foo-'\"{a.upper()}\"'-bar",
"""foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
self._run_test(
"\fF {name}{name} {a}",
f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
self._run_test(
"\fF foo-'\"{a.upper()}\"'-bar",
f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
def test_template_fstring(self):
with tempfile.TemporaryDirectory() as tmpdirname:
@@ -475,15 +478,15 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{a}")
fmt1 = formatter.parse("\fTF " + path1)
fmt1 = formatter.parse(f"\fTF {path1}")
with open(path2, "w") as fp:
fp.write("foo-'\"{a.upper()}\"'-bar")
fmt2 = formatter.parse("\fTF " + path2)
fmt2 = formatter.parse(f"\fTF {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict),
"""foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
with self.assertRaises(OSError):
formatter.parse("\fTF /")
@@ -493,10 +496,12 @@ class TestFormatter(unittest.TestCase):
formatter.JinjaFormatter.env = None
self._run_test("\fJ {{a}}", self.kwdict["a"])
self._run_test("\fJ {{name}}{{name}} {{a}}", "{}{} {}".format(
self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
self._run_test("\fJ foo-'\"{{a | upper}}\"'-bar",
"""foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
self._run_test(
"\fJ {{name}}{{name}} {{a}}",
f"{self.kwdict['name']}{self.kwdict['name']} {self.kwdict['a']}")
self._run_test(
"\fJ foo-'\"{{a | upper}}\"'-bar",
f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
@unittest.skipIf(jinja2 is None, "no jinja2")
def test_template_jinja(self):
@@ -508,15 +513,15 @@ class TestFormatter(unittest.TestCase):
with open(path1, "w") as fp:
fp.write("{{a}}")
fmt1 = formatter.parse("\fTJ " + path1)
fmt1 = formatter.parse(f"\fTJ {path1}")
with open(path2, "w") as fp:
fp.write("foo-'\"{{a | upper}}\"'-bar")
fmt2 = formatter.parse("\fTJ " + path2)
fmt2 = formatter.parse(f"\fTJ {path2}")
self.assertEqual(fmt1.format_map(self.kwdict), self.kwdict["a"])
self.assertEqual(fmt2.format_map(self.kwdict),
"""foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
f"""foo-'"{self.kwdict['a'].upper()}"'-bar""")
with self.assertRaises(OSError):
formatter.parse("\fTJ /")
@@ -562,7 +567,7 @@ Present Time is ((( dt | dt_fmt("%H:%M:%S") )))
Hello ((( s | sanitize_whitespace ))).
I hope there is enough "(((S|sanitize_whitespace)))" for you.
""")
fmt = formatter.parse("\fTJ " + path_template)
fmt = formatter.parse(f"\fTJ {path_template}")
self.assertEqual(fmt.format_map(self.kwdict), """\
Present Day is January 01, 2010
@@ -607,8 +612,8 @@ def noarg():
finally:
sys.path.pop(0)
fmt3 = formatter.parse("\fM " + path + ":gentext")
fmt4 = formatter.parse("\fM " + path + ":lengths")
fmt3 = formatter.parse(f"\fM {path}:gentext")
fmt4 = formatter.parse(f"\fM {path}:lengths")
self.assertEqual(fmt1.format_map(self.kwdict), "'Title' by Name")
self.assertEqual(fmt2.format_map(self.kwdict), "168")

View File

@@ -299,7 +299,7 @@ class TestDataJob(TestJob):
for i in range(1, 4):
self.assertEqual(
tjob.data[i][2]["_fallback"],
("https://example.org/alt/{}.jpg".format(i),),
(f"https://example.org/alt/{i}.jpg",),
)
def test_sleep(self):
@@ -382,13 +382,13 @@ class TestExtractor(Extractor):
}
for i in range(1, 4):
url = "{}/{}.jpg".format(root, i)
url = f"{root}/{i}.jpg"
yield Message.Url, url, text.nameext_from_url(url, {
"num" : i,
"tags": ["foo", "bar", "テスト"],
"user": user,
"author": user,
"_fallback": ("{}/alt/{}.jpg".format(root, i),),
"_fallback": (f"{root}/alt/{i}.jpg",),
})

View File

@@ -52,7 +52,7 @@ class TestPostprocessorModule(unittest.TestCase):
def test_find(self):
for name in (postprocessor.modules):
cls = postprocessor.find(name)
self.assertEqual(cls.__name__, name.capitalize() + "PP")
self.assertEqual(cls.__name__, f"{name.capitalize()}PP")
self.assertIs(cls.__base__, PostProcessor)
self.assertEqual(postprocessor.find("foo"), None)
@@ -129,15 +129,15 @@ class ClassifyTest(BasePostprocessorTest):
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "Pictures")
self.assertEqual(self.pathfmt.path, path + "/file.jpg")
self.assertEqual(self.pathfmt.realpath, path + "/file.jpg")
self.assertEqual(self.pathfmt.path, f"{path}/file.jpg")
self.assertEqual(self.pathfmt.realpath, f"{path}/file.jpg")
self.pathfmt.set_extension("mp4")
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "Video")
self.assertEqual(self.pathfmt.path, path + "/file.mp4")
self.assertEqual(self.pathfmt.realpath, path + "/file.mp4")
self.assertEqual(self.pathfmt.path, f"{path}/file.mp4")
self.assertEqual(self.pathfmt.realpath, f"{path}/file.mp4")
def test_classify_noop(self):
pp = self._create()
@@ -169,8 +169,8 @@ class ClassifyTest(BasePostprocessorTest):
self._trigger(("prepare",))
self.pathfmt.build_path()
path = os.path.join(self.dir.name, "test", "foo", "bar")
self.assertEqual(self.pathfmt.path, path + "/file.foo")
self.assertEqual(self.pathfmt.realpath, path + "/file.foo")
self.assertEqual(self.pathfmt.path, f"{path}/file.foo")
self.assertEqual(self.pathfmt.realpath, f"{path}/file.foo")
class DirectoryTest(BasePostprocessorTest):
@@ -179,16 +179,16 @@ class DirectoryTest(BasePostprocessorTest):
self._create()
path = os.path.join(self.dir.name, "test")
self.assertEqual(self.pathfmt.realdirectory, path + "/")
self.assertEqual(self.pathfmt.realpath, path + "/file.ext")
self.assertEqual(self.pathfmt.realdirectory, f"{path}/")
self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext")
self.pathfmt.kwdict["category"] = "custom"
self._trigger()
path = os.path.join(self.dir.name, "custom")
self.assertEqual(self.pathfmt.realdirectory, path + "/")
self.assertEqual(self.pathfmt.realdirectory, f"{path}/")
self.pathfmt.build_path()
self.assertEqual(self.pathfmt.realpath, path + "/file.ext")
self.assertEqual(self.pathfmt.realpath, f"{path}/file.ext")
class ExecTest(BasePostprocessorTest):
@@ -205,10 +205,12 @@ class ExecTest(BasePostprocessorTest):
self._trigger(("after",))
p.assert_called_once_with(
"echo {0} {0} {1} {2} && rm {0};".format(
self.pathfmt.realpath,
self.pathfmt.realdirectory,
self.pathfmt.filename),
(f"echo "
f"{self.pathfmt.realpath} "
f"{self.pathfmt.realpath} "
f"{self.pathfmt.realdirectory} "
f"{self.pathfmt.filename} "
f"&& rm {self.pathfmt.realpath};"),
shell=True,
creationflags=0,
start_new_session=False,
@@ -254,10 +256,12 @@ class ExecTest(BasePostprocessorTest):
self.assertEqual(p.call_args_list, [
call(
"echo {0} {0} {1} {2} && rm {0};".format(
self.pathfmt.realpath,
self.pathfmt.realdirectory,
self.pathfmt.filename),
(f"echo "
f"{self.pathfmt.realpath} "
f"{self.pathfmt.realpath} "
f"{self.pathfmt.realdirectory} "
f"{self.pathfmt.filename} "
f"&& rm {self.pathfmt.realpath};"),
shell=True,
creationflags=0,
start_new_session=False,
@@ -287,8 +291,9 @@ class ExecTest(BasePostprocessorTest):
with self.assertLogs() as log:
self._trigger(("after",))
msg = ("WARNING:postprocessor.exec:'echo {}' returned with "
"non-zero exit status (123)".format(self.pathfmt.realpath))
msg = (f"WARNING:postprocessor.exec:"
f"'echo {self.pathfmt.realpath}' "
f"returned with non-zero exit status (123)")
self.assertEqual(log.output[0], msg)
def test_async(self):
@@ -426,7 +431,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realpath + ".JSON"
path = f"{self.pathfmt.realpath}.JSON"
m.assert_called_once_with(path, "w", encoding="utf-8")
self.assertEqual(self._output(m), """{
@@ -460,7 +465,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realpath + ".JSON"
path = f"{self.pathfmt.realpath}.JSON"
m.assert_called_once_with(path, "a", encoding="UTF-8")
self.assertEqual(self._output(m), """{\
"_private" : "foo \\u30d0\\u30fc",\
@@ -481,7 +486,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realpath + ".txt"
path = f"{self.pathfmt.realpath}.txt"
m.assert_called_once_with(path, "w", encoding="utf-8")
self.assertEqual(self._output(m), "foo\nbar\nbaz\n")
@@ -561,7 +566,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "file.json"
path = f"{self.pathfmt.realdirectory}file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_extfmt_2(self):
@@ -573,7 +578,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "file.2.EXT-data:tESt"
path = f"{self.pathfmt.realdirectory}file.2.EXT-data:tESt"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory(self):
@@ -584,7 +589,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "metadata/file.ext.json"
path = f"{self.pathfmt.realdirectory}metadata/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_2(self):
@@ -596,7 +601,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "metadata/file.json"
path = f"{self.pathfmt.realdirectory}metadata/file.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_format(self):
@@ -608,7 +613,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "../json/12500/file.ext.json"
path = f"{self.pathfmt.realdirectory}../json/12500/file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_directory_empty(self):
@@ -619,7 +624,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "./file.ext.json"
path = f"{self.pathfmt.realdirectory}./file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory(self):
@@ -628,7 +633,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.basedirectory + "file.ext.json"
path = f"{self.pathfmt.basedirectory}file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_basedirectory_custom(self):
@@ -652,7 +657,7 @@ class MetadataTest(BasePostprocessorTest):
with patch("builtins.open", mock_open()) as m:
self._trigger()
path = self.pathfmt.realdirectory + "test_file__meta_.data"
path = f"{self.pathfmt.realdirectory}test_file__meta_.data"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_meta_path(self):
@@ -663,7 +668,7 @@ class MetadataTest(BasePostprocessorTest):
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_meta_path"],
self.pathfmt.realpath + ".json")
f"{self.pathfmt.realpath}.json")
def test_metadata_stdout(self):
self._create({"filename": "-", "indent": None, "sort": True})
@@ -752,7 +757,7 @@ class MetadataTest(BasePostprocessorTest):
self.assertTrue(m.called)
self.assertGreater(len(self._output(m)), 0)
path = self.pathfmt.realdirectory + "file.ext.json"
path = f"{self.pathfmt.realdirectory}file.ext.json"
m.assert_called_once_with(path, "w", encoding="utf-8")
def test_metadata_option_skip_false(self):
@@ -856,7 +861,7 @@ class PythonTest(BasePostprocessorTest):
path = os.path.join(self.dir.name, "module.py")
self._write_module(path)
self._create({"function": path + ":calc"}, {"_value": 12})
self._create({"function": f"{path}:calc"}, {"_value": 12})
self.assertNotIn("_result", self.pathfmt.kwdict)
self._trigger()
@@ -913,7 +918,7 @@ class RenameTest(BasePostprocessorTest):
def test_rename_skip(self):
self._create({"from": "{id}.{extension}"}, {"id": 12345})
path = self._prepare("12345.ext")
with open(path + "file.ext", "w"):
with open(f"{path}file.ext", "w"):
pass
with self.assertLogs("postprocessor.rename", level="WARNING") as cm:
@@ -932,7 +937,7 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1])
self.assertEqual(pp.delete, True)
self.assertEqual(pp.args, (
pp.path + ".zip", "a", zipfile.ZIP_STORED, True,
f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True,
))
self.assertTrue(pp.args[0].endswith("/test.zip"))
@@ -942,7 +947,7 @@ class ZipTest(BasePostprocessorTest):
self.assertEqual(pp.path, self.pathfmt.realdirectory[:-1])
self.assertEqual(pp.delete, True)
self.assertEqual(pp.args, (
pp.path + ".zip", "a", zipfile.ZIP_STORED, True,
f"{pp.path}.zip", "a", zipfile.ZIP_STORED, True,
))
self.assertTrue(pp.args[0].endswith("/test.zip"))
@@ -954,7 +959,7 @@ class ZipTest(BasePostprocessorTest):
})
self.assertEqual(pp.delete, False)
self.assertEqual(pp.args, (
pp.path + ".cbz", "a", zipfile.ZIP_DEFLATED, True,
f"{pp.path}.cbz", "a", zipfile.ZIP_DEFLATED, True,
))
self.assertTrue(pp.args[0].endswith("/test.cbz"))
@@ -968,7 +973,7 @@ class ZipTest(BasePostprocessorTest):
# write dummy file with 3 different names
for i in range(3):
name = "file{}.ext".format(i)
name = f"file{i}.ext"
self.pathfmt.temppath = file.name
self.pathfmt.filename = name
@@ -1015,8 +1020,8 @@ class ZipTest(BasePostprocessorTest):
# write 3 files
for i in range(3):
self.pathfmt.temppath = self.pathfmt.realdirectory + "file.ext"
self.pathfmt.filename = "file{}.ext".format(i)
self.pathfmt.temppath = f"{self.pathfmt.realdirectory}file.ext"
self.pathfmt.filename = f"file{i}.ext"
self._trigger()
# write the last file a second time (should be skipped)

View File

@@ -234,7 +234,7 @@ class TestExtractorResults(unittest.TestCase):
if isinstance(count, str):
self.assertRegex(
count, r"^ *(==|!=|<|<=|>|>=) *\d+ *$", msg="#count")
expr = "{} {}".format(len_urls, count)
expr = f"{len_urls} {count}"
self.assertTrue(eval(expr), msg=expr)
elif isinstance(count, range):
self.assertRange(len_urls, count, msg="#count")
@@ -284,7 +284,7 @@ class TestExtractorResults(unittest.TestCase):
else:
subtest = False
path = "{}.{}".format(parent, key) if parent else key
path = f"{parent}.{key}" if parent else key
if key.startswith("!"):
self.assertNotIn(key[1:], kwdict, msg=path)
@@ -296,7 +296,7 @@ class TestExtractorResults(unittest.TestCase):
if subtest:
self.assertNotIsInstance(value, str, msg=path)
for idx, item in enumerate(value):
subpath = "{}[{}]".format(path, idx)
subpath = f"{path}[{idx}]"
self._test_kwdict_value(item, test, subpath)
else:
self._test_kwdict_value(value, test, path)
@@ -318,7 +318,7 @@ class TestExtractorResults(unittest.TestCase):
for idx, item in enumerate(test):
if isinstance(item, dict):
subtest = True
subpath = "{}[{}]".format(path, idx)
subpath = f"{path}[{idx}]"
try:
obj = value[idx]
except Exception as exc:
@@ -340,7 +340,7 @@ class TestExtractorResults(unittest.TestCase):
cls, _, length = test[4:].rpartition(":")
if cls:
self.assertEqual(
cls, type(value).__name__, msg=path + "/type")
cls, type(value).__name__, msg=f"{path}/type")
try:
len_value = len(value)
except Exception:
@@ -519,8 +519,7 @@ def load_test_config():
except FileNotFoundError:
pass
except Exception as exc:
sys.exit("Error when loading {}: {}: {}".format(
path, exc.__class__.__name__, exc))
sys.exit(f"Error when loading {path}: {exc.__class__.__name__}: {exc}")
def result_categories(result):
@@ -583,12 +582,12 @@ def generate_tests():
enum = collections.defaultdict(int)
for result in tests:
base, cat, sub = result_categories(result)
name = "{}_{}".format(cat, sub)
name = f"{cat}_{sub}"
enum[name] += 1
method = _generate_method(result)
method.__doc__ = result["#url"]
method.__name__ = "test_{}_{}".format(name, enum[name])
method.__name__ = f"test_{name}_{enum[name]}"
setattr(TestExtractorResults, method.__name__, method)

View File

@@ -385,7 +385,7 @@ class TestCompileExpression(unittest.TestCase):
self.assertEqual(expr(value), result)
with tempfile.TemporaryDirectory() as path:
file = path + "/module_sha1.py"
file = f"{path}/module_sha1.py"
with open(file, "w") as fp:
fp.write("""
import hashlib
@@ -638,7 +638,7 @@ class TestOther(unittest.TestCase):
self.assertIs(module, datetime)
with tempfile.TemporaryDirectory() as path:
file = path + "/module_test.py"
file = f"{path}/module_test.py"
with open(file, "w") as fp:
fp.write("""
import datetime

View File

@@ -23,8 +23,8 @@ class Test_CommandlineArguments(unittest.TestCase):
try:
cls.module = __import__(cls.module_name)
except (ImportError, SyntaxError):
raise unittest.SkipTest("cannot import module '{}'".format(
cls.module_name))
raise unittest.SkipTest(
f"cannot import module '{cls.module_name}'")
cls.default = ytdl.parse_command_line(cls.module, [])
cls.ytdlp = hasattr(cls.module, "cookies")