From 848fcaa853944d620e9dedec27b64c177a1f8777 Mon Sep 17 00:00:00 2001 From: Simon Conseil Date: Sat, 15 Jul 2023 16:27:35 +0200 Subject: [PATCH] Optimize plugins testing without building the full test gallery --- tests/sample/sigal.conf.py | 13 ------ tests/test_cli.py | 13 +++++- tests/test_plugins.py | 84 ++++++++++++++++++++------------------ tox.ini | 4 +- 4 files changed, 58 insertions(+), 56 deletions(-) diff --git a/tests/sample/sigal.conf.py b/tests/sample/sigal.conf.py index d11348e..4d9ae39 100644 --- a/tests/sample/sigal.conf.py +++ b/tests/sample/sigal.conf.py @@ -20,10 +20,8 @@ plugins = [ "sigal.plugins.copyright", "sigal.plugins.extended_caching", "sigal.plugins.feeds", - "sigal.plugins.nomedia", "sigal.plugins.watermark", "sigal.plugins.zip_gallery", - "sigal.plugins.titleregexp", ] copyright = "© An example copyright message" adjust_options = { @@ -42,17 +40,6 @@ thumb_size = (200, 150) rss_feed = {"feed_url": "http://127.0.0.1:8000/feed.rss", "nb_items": 10} atom_feed = {"feed_url": "http://127.0.0.1:8000/feed.atom", "nb_items": 10} -titleregexp = { - "regexp": [ - { - "search": r"test ?(.*)", - "replace": r"titleregexp \1", - "substitute": [["2", "02"]], - "break": 1, - } - ] -} - # theme = 'photoswipe' # theme = 'galleria' # thumb_size = (280, 210) diff --git a/tests/test_cli.py b/tests/test_cli.py index 4c9c9b3..c6f13f3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -40,13 +40,20 @@ def test_build(tmpdir, disconnect_signals): join(tmpdir, "pictures", "KeckObservatory20071020.jpg"), ) - result = runner.invoke(build, ["-n", 1, "--debug"]) + result = runner.invoke(build, ["-n", 1, "--debug", "--verbose"]) + assert ( + result.output + == "Only one option of debug, verbose and quiet should be used\n" + ) + assert result.exit_code == 1 + + result = runner.invoke(build, ["-n", 1, "--verbose"]) assert result.output == "Settings file not found: sigal.conf.py\n" assert result.exit_code == 1 os.chdir(tmpdir) - result = runner.invoke(build, ["foo", "-n", 1, "--debug"]) + result = runner.invoke(build, ["foo", "-n", 1, "--quiet"]) assert result.exit_code == 1 assert "Input directory not found" in result.output @@ -106,9 +113,11 @@ def test_serve(tmpdir): result = runner.invoke(serve) assert result.exit_code == 2 + assert result.output.startswith("The _build directory doesn't exist") result = runner.invoke(serve, ["-c", config_file]) assert result.exit_code == 1 + assert result.output.endswith("maybe try building first?\n") def test_set_meta(tmpdir): diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 4dda019..592a127 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -6,54 +6,58 @@ from sigal.utils import init_plugins CURRENT_DIR = os.path.dirname(__file__) -def test_plugins(settings, tmpdir, disconnect_signals): - settings["destination"] = str(tmpdir) - if "sigal.plugins.nomedia" not in settings["plugins"]: - settings["plugins"] += ["sigal.plugins.nomedia"] - if "sigal.plugins.media_page" not in settings["plugins"]: - settings["plugins"] += ["sigal.plugins.media_page"] +def _build_with_plugin( + settings, input_path, output_path, plugin, **additional_settings +): + settings["source"] = os.path.join(settings["source"], input_path) + settings["destination"] = str(output_path) + settings["plugins"] = [plugin] + settings.update(additional_settings) init_plugins(settings) - gal = Gallery(settings) + gal = Gallery(settings, ncpu=1) gal.build() + return gal - out_html = os.path.join( - settings["destination"], "dir2", "KeckObservatory20071020.jpg.html" - ) - assert os.path.isfile(out_html) - - for path, dirs, files in os.walk(os.path.join(str(tmpdir), "nomedia")): - assert "ignore" not in path - for file in files: - assert "ignore" not in file +def test_media_page(settings, tmp_path, disconnect_signals): + _build_with_plugin(settings, "dir2", tmp_path, "sigal.plugins.media_page") + assert (tmp_path / "KeckObservatory20071020.jpg.html").is_file() -def test_nonmedia_files(settings, tmpdir, disconnect_signals): - settings["destination"] = str(tmpdir) - settings["plugins"] += ["sigal.plugins.nonmedia_files"] - settings["nonmedia_files_options"] = {"thumb_bg_color": "red"} +def test_nomedia(settings, tmp_path, disconnect_signals): + _build_with_plugin(settings, "nomedia", tmp_path, "sigal.plugins.nomedia") - init_plugins(settings) - - gal = Gallery(settings) - gal.build() + for path, dirs, files in os.walk(str(tmp_path)): + assert "ignore" not in path + for file in files: + assert "ignore" not in file - outfile = os.path.join(settings["destination"], "nonmedia_files", "dummy.pdf") - assert os.path.isfile(outfile) - outthumb = os.path.join( - settings["destination"], "nonmedia_files", "thumbnails", "dummy.tn.jpg" +def test_nonmedia_files(settings, tmp_path, disconnect_signals): + _build_with_plugin( + settings, + "nonmedia_files", + tmp_path, + "sigal.plugins.nonmedia_files", + nonmedia_files_options={"thumb_bg_color": "red"}, ) - assert os.path.isfile(outthumb) - - -def test_titleregexp(settings, tmpdir, disconnect_signals): - if "sigal.plugins.titleregexp" not in settings["plugins"]: - settings["plugins"] += ["sigal.plugins.titleregexp"] - - init_plugins(settings) - gal = Gallery(settings) - gal.build() - - assert gal.albums.get("dir1").albums[1].title == "titleregexp 02" + assert (tmp_path / "dummy.pdf").is_file() + assert (tmp_path / "thumbnails" / "dummy.tn.jpg").is_file() + + +def test_titleregexp(settings, tmp_path, disconnect_signals): + conf = { + "regexp": [ + { + "search": r"test ?(.*)", + "replace": r"titleregexp \1", + "substitute": [["2", "02"]], + "break": 1, + } + ] + } + gal = _build_with_plugin( + settings, "dir1", tmp_path, "sigal.plugins.titleregexp", titleregexp=conf + ) + assert gal.albums["test2"].title == "titleregexp 02" diff --git a/tox.ini b/tox.ini index 7a4618d..02be03a 100644 --- a/tox.ini +++ b/tox.ini @@ -19,7 +19,9 @@ deps = extras = all tests -commands = pytest --cov sigal --cov-report term tests/ +commands = + pip list + pytest --cov sigal --cov-report term --durations=10 tests/ [testenv:doc] whitelist_externals = make