From b6bce8d78b72bb2569667709e589d7227057214a Mon Sep 17 00:00:00 2001 From: egberts Date: Mon, 22 Jul 2024 19:12:40 -0500 Subject: [PATCH] Fix test_pelican.py #3374 Make it work from any directory within the Pelican package. Also fixes parallelism. --- pelican/__init__.py | 9 +++++---- pelican/tests/settings/pelicanconf.py | 1 + pelican/tests/test_pelican.py | 29 +++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 pelican/tests/settings/pelicanconf.py diff --git a/pelican/__init__.py b/pelican/__init__.py index 2e2b735d6..c2ee0da98 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -550,11 +550,12 @@ def get_instance(args): config_settings_file = args.settings if config_settings_file is None: if os.path.isfile(DEFAULT_CONFIG_NAME): - config_settings_file = DEFAULT_CONFIG_NAME - args.settings = DEFAULT_CONFIG_NAME + config_settings_file = DEFAULT_CONFIG_NAME # relative path to $CWD + args.settings = DEFAULT_CONFIG_NAME # relative path to $CWD else: - config_settings_file = "pelicanconf.py" - args.settings = "pelicanconf.py" + err_msg = f"Configuration {config_settings_file} file is not found." + logger.error(err_msg) + raise FileNotFoundError(err_msg) settings = read_settings(config_settings_file, override=get_config(args)) diff --git a/pelican/tests/settings/pelicanconf.py b/pelican/tests/settings/pelicanconf.py new file mode 100644 index 000000000..5828ae6b4 --- /dev/null +++ b/pelican/tests/settings/pelicanconf.py @@ -0,0 +1 @@ +PATH = "content" diff --git a/pelican/tests/test_pelican.py b/pelican/tests/test_pelican.py index 39f6fef68..17512c583 100644 --- a/pelican/tests/test_pelican.py +++ b/pelican/tests/test_pelican.py @@ -272,11 +272,19 @@ def test_parse_errors(self): def test_module_load(self): """Test loading via python -m pelican --help displays the help""" + tests_subdir = os.path.dirname(__file__) # anchor to 'tests' directory + pelican_srcdir = os.path.dirname(tests_subdir) + pelican_pkgdir = os.path.dirname(pelican_srcdir) + original_cwd = os.getcwd() + os.chdir(pelican_pkgdir) output = subprocess.check_output( [sys.executable, "-m", "pelican", "--help"] ).decode("ascii", "replace") + assert "usage:" in output + os.chdir(original_cwd) + def test_main_version(self): """Run main --version.""" out = io.StringIO() @@ -295,19 +303,32 @@ def test_main_help(self): def test_main_on_content(self): """Invoke main on simple_content directory.""" - content_subdir = "pelican/tests/simple_content" + tests_subdir = os.path.dirname(__file__) # anchor to 'tests' directory + pelican_srcdir = os.path.dirname(tests_subdir) + pelican_pkgdir = os.path.dirname(pelican_srcdir) + original_cwd = os.getcwd() + os.chdir(pelican_pkgdir) + content_subdir = "pelican/tests/simple_content" # from pelican_pkgdir POV out, err = io.StringIO(), io.StringIO() + with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err): with TemporaryDirectory() as temp_dir: # Don't highlight anything. # See https://rich.readthedocs.io/en/stable/highlighting.html with patch("pelican.console", new=Console(highlight=False)): main(["-o", temp_dir, content_subdir]) + self.assertIn("Processed 1 article", out.getvalue()) self.assertEqual("", err.getvalue()) + os.chdir(original_cwd) + def test_main_on_content_markdown_disabled(self): """Invoke main on simple_content directory.""" + tests_subdir = os.path.dirname(__file__) # anchor to 'tests' directory + settings_subdir = tests_subdir + os.sep + "settings" + settings_file = settings_subdir + os.sep + "pelicanconf.py" + content_subdir = tests_subdir + os.sep + "simple_content" with patch.object( pelican.readers.MarkdownReader, "enabled", new_callable=PropertyMock ) as attr_mock: @@ -318,7 +339,7 @@ def test_main_on_content_markdown_disabled(self): # Don't highlight anything. # See https://rich.readthedocs.io/en/stable/highlighting.html with patch("pelican.console", new=Console(highlight=False)): - main(["-o", temp_dir, "pelican/tests/simple_content"]) + main(["-o", temp_dir, "-s", settings_file, content_subdir]) self.assertIn("Processed 0 articles", out.getvalue()) self.assertLogCountEqual( 1, @@ -326,3 +347,7 @@ def test_main_on_content_markdown_disabled(self): "Could not import 'markdown.Markdown'. " "Have you installed the 'markdown' package?", ) + + +if __file__ == "main": + unittest.main()