Skip to content

Commit

Permalink
Fix false-positive in content gen. test failures
Browse files Browse the repository at this point in the history
Assert equal dirs by return value of diff subprocess, rather than its output.

This prevents tests from failing when file contents are the same but the
file modes are different.

Fix #3042
  • Loading branch information
copperchin committed Nov 6, 2022
1 parent f015ab8 commit c31503f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
17 changes: 17 additions & 0 deletions pelican/tests/support.py
Expand Up @@ -218,6 +218,23 @@ def count_formatted_logs(self, msg=None, level=None):
])


def diff_subproc(first, second):
"""
Return a subprocess that runs a diff on the two paths.
Check results with::
>>> out_stream, err_stream = proc.communicate()
>>> didCheckFail = proc.returnCode != 0
"""
return subprocess.Popen(
['git', '--no-pager', 'diff', '--no-ext-diff', '--exit-code',
'-w', first, second],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)


class LoggedTestCase(unittest.TestCase):
"""A test case that captures log messages."""

Expand Down
49 changes: 22 additions & 27 deletions pelican/tests/test_pelican.py
@@ -1,17 +1,18 @@
from collections.abc import Sequence
import locale
import logging
import os
from shutil import rmtree
import subprocess
import sys
from collections.abc import Sequence
from shutil import rmtree
from tempfile import mkdtemp
import unittest

from pelican import Pelican
from pelican.generators import StaticGenerator
from pelican.settings import read_settings
from pelican.tests.support import (LoggedTestCase, locale_available,
mute, unittest)
mute, diff_subproc)

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
SAMPLES_PATH = os.path.abspath(os.path.join(
Expand Down Expand Up @@ -54,28 +55,19 @@ def tearDown(self):
locale.setlocale(locale.LC_ALL, self.old_locale)
super().tearDown()

def assertDirsEqual(self, left_path, right_path):
out, err = subprocess.Popen(
['git', '--no-pager', 'diff', '--no-ext-diff', '--exit-code',
'-w', left_path, right_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()
def assertDirsEqual(self, left_path, right_path, msg=None):
"""
Check if the files are the same (ignoring whitespace) below both paths.
"""
proc = diff_subproc(left_path, right_path)

def ignorable_git_crlf_errors(line):
# Work around for running tests on Windows
for msg in [
"LF will be replaced by CRLF",
"CRLF will be replaced by LF",
"The file will have its original line endings"]:
if msg in line:
return True
return False
if err:
err = '\n'.join([line for line in err.decode('utf8').splitlines()
if not ignorable_git_crlf_errors(line)])
assert not out, out
assert not err, err
out, err = proc.communicate()
if proc.returncode != 0:
msg = self._formatMessage(
msg,
"%s and %s differ:\n%s" % (left_path, right_path, err)
)
raise self.failureException(msg)

def test_order_of_generators(self):
# StaticGenerator must run last, so it can identify files that
Expand Down Expand Up @@ -104,7 +96,8 @@ def test_basic_generation_works(self):
pelican = Pelican(settings=settings)
mute(True)(pelican.run)()
self.assertDirsEqual(
self.temp_path, os.path.join(OUTPUT_PATH, 'basic'))
self.temp_path, os.path.join(OUTPUT_PATH, 'basic')
)
self.assertLogCountEqual(
count=1,
msg="Unable to find.*skipping url replacement",
Expand All @@ -121,7 +114,8 @@ def test_custom_generation_works(self):
pelican = Pelican(settings=settings)
mute(True)(pelican.run)()
self.assertDirsEqual(
self.temp_path, os.path.join(OUTPUT_PATH, 'custom'))
self.temp_path, os.path.join(OUTPUT_PATH, 'custom')
)

@unittest.skipUnless(locale_available('fr_FR.UTF-8') or
locale_available('French'), 'French locale needed')
Expand All @@ -141,7 +135,8 @@ def test_custom_locale_generation_works(self):
pelican = Pelican(settings=settings)
mute(True)(pelican.run)()
self.assertDirsEqual(
self.temp_path, os.path.join(OUTPUT_PATH, 'custom_locale'))
self.temp_path, os.path.join(OUTPUT_PATH, 'custom_locale')
)

def test_theme_static_paths_copy(self):
# the same thing with a specified set of settings should work
Expand Down

0 comments on commit c31503f

Please sign in to comment.