aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_downloader.py16
-rw-r--r--test/test_formatter.py27
-rw-r--r--test/test_job.py8
-rw-r--r--test/test_postprocessor.py18
-rw-r--r--test/test_results.py39
5 files changed, 74 insertions, 34 deletions
diff --git a/test/test_downloader.py b/test/test_downloader.py
index 8027af5..9f9fb3b 100644
--- a/test/test_downloader.py
+++ b/test/test_downloader.py
@@ -174,9 +174,17 @@ class TestHTTPDownloader(TestDownloaderBase):
TestDownloaderBase.setUpClass()
cls.downloader = downloader.find("http")(cls.job)
- port = 8088
- cls.address = "http://127.0.0.1:{}".format(port)
- server = http.server.HTTPServer(("", port), HttpRequestHandler)
+ host = "127.0.0.1"
+ port = 0 # select random not-in-use port
+
+ try:
+ server = http.server.HTTPServer((host, port), HttpRequestHandler)
+ except OSError as exc:
+ raise unittest.SkipTest(
+ "cannot spawn local HTTP server ({})".format(exc))
+
+ host, port = server.server_address
+ cls.address = "http://{}:{}".format(host, port)
threading.Thread(target=server.serve_forever, daemon=True).start()
def _run_test(self, ext, input, output,
@@ -303,7 +311,7 @@ SAMPLES = {
("mp4" , b"????ftypmp4"),
("mp4" , b"????ftypavc1"),
("mp4" , b"????ftypiso3"),
- ("mp4" , b"????ftypM4V"),
+ ("m4v" , b"????ftypM4V"),
("mov" , b"????ftypqt "),
("webm", b"\x1A\x45\xDF\xA3"),
("ogg" , b"OggS"),
diff --git a/test/test_formatter.py b/test/test_formatter.py
index 89cb1aa..73e958c 100644
--- a/test/test_formatter.py
+++ b/test/test_formatter.py
@@ -336,14 +336,14 @@ class TestFormatter(unittest.TestCase):
def test_literals(self):
value = "foo"
- self._run_test("{'foo'}" , value)
- self._run_test("{'foo'!u}" , value.upper())
- self._run_test("{'f00':R0/o/}" , value)
- self._run_test("{'foobar'[:3]}", value)
- self._run_test("{z|'foo'}" , value)
- self._run_test("{z|''|'foo'}" , value)
- self._run_test("{z|''}" , "")
- self._run_test("{''|''}" , "")
+ self._run_test("{'foo'}" , value)
+ self._run_test("{'foo'!u}" , value.upper())
+ self._run_test("{'f00':R0/o/}", value)
+
+ self._run_test("{z|'foo'}" , value)
+ self._run_test("{z|''|'foo'}" , value)
+ self._run_test("{z|'foo'!u}" , value.upper())
+ self._run_test("{z|'f00':R0/o/}", value)
self._run_test("{_lit[foo]}" , value)
self._run_test("{_lit[foo]!u}" , value.upper())
@@ -351,6 +351,17 @@ class TestFormatter(unittest.TestCase):
self._run_test("{_lit[foobar][:3]}", value)
self._run_test("{z|_lit[foo]}" , value)
+ # empty (#4492)
+ self._run_test("{z|''}" , "")
+ self._run_test("{''|''}", "")
+
+ # special characters (dots, brackets, singlee quotes) (#5539)
+ self._run_test("{'f.o.o'}" , "f.o.o")
+ self._run_test("{_lit[f.o.o]}", "f.o.o")
+ self._run_test("{_lit[f'o'o]}", "f'o'o")
+ self._run_test("{'f.[].[]'}" , "f.[].[]")
+ self._run_test("{z|'f.[].[]'}", "f.[].[]")
+
def test_template(self):
with tempfile.TemporaryDirectory() as tmpdirname:
path1 = os.path.join(tmpdirname, "tpl1")
diff --git a/test/test_job.py b/test/test_job.py
index 141b1b2..3e6f85b 100644
--- a/test/test_job.py
+++ b/test/test_job.py
@@ -13,7 +13,6 @@ import unittest
from unittest.mock import patch
import io
-import contextlib
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gallery_dl import job, config, text # noqa E402
@@ -32,8 +31,13 @@ class TestJob(unittest.TestCase):
jobinstance = extr_or_job
with io.StringIO() as buffer:
- with contextlib.redirect_stdout(buffer):
+ stdout = sys.stdout
+ sys.stdout = buffer
+ try:
jobinstance.run()
+ finally:
+ sys.stdout = stdout
+
return buffer.getvalue()
diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py
index 0ee7cdb..d509052 100644
--- a/test/test_postprocessor.py
+++ b/test/test_postprocessor.py
@@ -172,7 +172,7 @@ class ExecTest(BasePostprocessorTest):
"command": "echo {} {_path} {_directory} {_filename} && rm {};",
})
- with patch("subprocess.Popen") as p:
+ with patch("gallery_dl.util.Popen") as p:
i = Mock()
i.wait.return_value = 0
p.return_value = i
@@ -192,7 +192,7 @@ class ExecTest(BasePostprocessorTest):
"\fE _directory.upper()"],
})
- with patch("subprocess.Popen") as p:
+ with patch("gallery_dl.util.Popen") as p:
i = Mock()
i.wait.return_value = 0
p.return_value = i
@@ -212,7 +212,7 @@ class ExecTest(BasePostprocessorTest):
"command": "echo {}",
})
- with patch("subprocess.Popen") as p:
+ with patch("gallery_dl.util.Popen") as p:
i = Mock()
i.wait.return_value = 123
p.return_value = i
@@ -230,7 +230,7 @@ class ExecTest(BasePostprocessorTest):
"command": "echo {}",
})
- with patch("subprocess.Popen") as p:
+ with patch("gallery_dl.util.Popen") as p:
i = Mock()
p.return_value = i
self._trigger(("after",))
@@ -573,6 +573,16 @@ class MtimeTest(BasePostprocessorTest):
self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)
+ def test_mtime_none(self):
+ self._create(None, {"date": None})
+ self._trigger()
+ self.assertNotIn("_mtime", self.pathfmt.kwdict)
+
+ def test_mtime_undefined(self):
+ self._create(None, {})
+ self._trigger()
+ self.assertNotIn("_mtime", self.pathfmt.kwdict)
+
def test_mtime_key(self):
self._create({"key": "foo"}, {"foo": 315532800})
self._trigger()
diff --git a/test/test_results.py b/test/test_results.py
index 0594618..aaa71ec 100644
--- a/test/test_results.py
+++ b/test/test_results.py
@@ -54,6 +54,7 @@ AUTH_CONFIG = (
"cookies",
"api-key",
"client-id",
+ "access-token",
"refresh-token",
)
@@ -88,6 +89,19 @@ class TestExtractorResults(unittest.TestCase):
result.pop("#comment", None)
only_matching = (len(result) <= 3)
+ auth = result.get("#auth")
+ if auth is None:
+ auth = (result["#category"][1] in AUTH)
+ elif not auth:
+ for key in AUTH_CONFIG:
+ config.set((), key, None)
+
+ if auth:
+ extr = result["#class"].from_url(result["#url"])
+ if not any(extr.config(key) for key in AUTH_CONFIG):
+ self._skipped.append((result["#url"], "no auth"))
+ only_matching = True
+
if only_matching:
content = False
else:
@@ -95,21 +109,6 @@ class TestExtractorResults(unittest.TestCase):
for key, value in result["#options"].items():
key = key.split(".")
config.set(key[:-1], key[-1], value)
-
- auth = result.get("#auth")
- if auth is None:
- auth = (result["#category"][1] in AUTH)
- elif not auth:
- for key in AUTH_CONFIG:
- config.set((), key, None)
-
- if auth:
- extr = result["#class"].from_url(result["#url"])
- if not any(extr.config(key) for key in AUTH_CONFIG):
- msg = "no auth"
- self._skipped.append((result["#url"], msg))
- self.skipTest(msg)
-
if "#range" in result:
config.set((), "image-range" , result["#range"])
config.set((), "chapter-range", result["#range"])
@@ -442,7 +441,15 @@ def generate_tests():
tests = results.category(category)
if subcategory:
- tests = [t for t in tests if t["#category"][-1] == subcategory]
+ if subcategory.startswith("+"):
+ url = subcategory[1:]
+ tests = [t for t in tests if url in t["#url"]]
+ elif subcategory.startswith("~"):
+ com = subcategory[1:]
+ tests = [t for t in tests
+ if "#comment" in t and com in t["#comment"].lower()]
+ else:
+ tests = [t for t in tests if t["#category"][-1] == subcategory]
else:
tests = results.all()