| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" |
| 7 |
Perform tests with the data in the functional/ directory. |
| 8 |
|
| 9 |
Please see the documentation on `functional testing`__ for details. |
| 10 |
|
| 11 |
__ ../../docs/dev/testing.html#functional |
| 12 |
""" |
| 13 |
|
| 14 |
import difflib |
| 15 |
from pathlib import Path |
| 16 |
import shutil |
| 17 |
import sys |
| 18 |
import unittest |
| 19 |
|
| 20 |
if __name__ == '__main__': |
| 21 |
|
| 22 |
|
| 23 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 24 |
|
| 25 |
from docutils import core, SettingsSpec |
| 26 |
|
| 27 |
FUNCTIONAL = Path('functional') |
| 28 |
EXPECTED = FUNCTIONAL / 'expected' |
| 29 |
INPUT = FUNCTIONAL / 'input' |
| 30 |
OUTPUT = FUNCTIONAL / 'output' |
| 31 |
TESTS = FUNCTIONAL / 'tests' |
| 32 |
|
| 33 |
NO_EXPECTED_TEMPLATE = """\ |
| 34 |
Cannot find expected output at {exp} |
| 35 |
If the output in {out} |
| 36 |
is correct, move it to the expected/ dir and check it in: |
| 37 |
|
| 38 |
mv {out} {exp} |
| 39 |
svn add {exp} |
| 40 |
svn commit -m "<comment>" {exp} |
| 41 |
""" |
| 42 |
|
| 43 |
EXPECTED_OUTPUT_DIFFERS_TEMPLATE = """\ |
| 44 |
Expected and actual output differ. |
| 45 |
|
| 46 |
{diff} |
| 47 |
|
| 48 |
If the actual output is correct, please replace the |
| 49 |
expected output and check it in: |
| 50 |
|
| 51 |
mv {out} {exp} |
| 52 |
svn add {exp} |
| 53 |
svn commit -m "<comment>" {exp} |
| 54 |
""" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
functional_tests_settings_spec = SettingsSpec() |
| 61 |
functional_tests_settings_spec.settings_default_overrides = { |
| 62 |
'_disable_config': True, |
| 63 |
'halt_level': 5, |
| 64 |
'warning_stream': '', |
| 65 |
'input_encoding': 'utf-8', |
| 66 |
'embed_stylesheet': False, |
| 67 |
'syntax_highlight': 'none' |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
def compare_output(output, destination_path, expected_path): |
| 72 |
|
| 73 |
try: |
| 74 |
expected = expected_path.read_text(encoding='utf-8') |
| 75 |
except OSError as err: |
| 76 |
raise OSError(NO_EXPECTED_TEMPLATE.format(exp=expected_path, |
| 77 |
out=destination_path) |
| 78 |
) from err |
| 79 |
if output != expected: |
| 80 |
diff = ''.join(difflib.unified_diff(expected.splitlines(True), |
| 81 |
output.splitlines(True), |
| 82 |
expected_path.as_posix(), |
| 83 |
destination_path.as_posix())) |
| 84 |
raise AssertionError( |
| 85 |
EXPECTED_OUTPUT_DIFFERS_TEMPLATE.format( |
| 86 |
diff=diff, exp=expected_path, out=destination_path) |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
class FunctionalTests(unittest.TestCase): |
| 91 |
|
| 92 |
"""Test case for one config file.""" |
| 93 |
|
| 94 |
def setUp(self): |
| 95 |
"""Clear output directory.""" |
| 96 |
for entry in OUTPUT.rglob('*'): |
| 97 |
if entry.is_dir(): |
| 98 |
shutil.rmtree(entry) |
| 99 |
elif entry.name != 'README.rst': |
| 100 |
entry.unlink() |
| 101 |
|
| 102 |
def test_functional(self): |
| 103 |
"""Process test file.""" |
| 104 |
for test_file in TESTS.glob("*.py"): |
| 105 |
with self.subTest(test_file=test_file.as_posix()): |
| 106 |
namespace = {} |
| 107 |
|
| 108 |
exec(test_file.read_text(encoding='utf-8'), namespace) |
| 109 |
del namespace['__builtins__'] |
| 110 |
|
| 111 |
|
| 112 |
source_path = INPUT / namespace.pop('test_source') |
| 113 |
destination_path = OUTPUT / namespace['test_destination'] |
| 114 |
expected_path = EXPECTED / namespace.pop('test_destination') |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
output = core.publish_file( |
| 119 |
**namespace, |
| 120 |
source_path=source_path.as_posix(), |
| 121 |
destination_path=destination_path.as_posix(), |
| 122 |
settings_spec=functional_tests_settings_spec, |
| 123 |
) |
| 124 |
|
| 125 |
compare_output(output, destination_path, expected_path) |
| 126 |
|
| 127 |
|
| 128 |
if __name__ == '__main__': |
| 129 |
unittest.main() |