Skip to content

Commit

Permalink
Fix test_pelican.py getpelican#3374
Browse files Browse the repository at this point in the history
Make it work from any directory within the Pelican package.

Also fixes parallelism.
  • Loading branch information
egberts committed Jul 24, 2024
1 parent 16e17ea commit 2d63d20
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
9 changes: 5 additions & 4 deletions pelican/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
1 change: 1 addition & 0 deletions pelican/tests/settings/pelicanconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PATH = "content"
29 changes: 27 additions & 2 deletions pelican/tests/test_pelican.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -318,11 +339,15 @@ 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,
".*article_with_md_extension.md: "
"Could not import 'markdown.Markdown'. "
"Have you installed the 'markdown' package?",
)


if __file__ == "main":
unittest.main()

0 comments on commit 2d63d20

Please sign in to comment.