| 1 |
|
| 2 |
e. |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
""" |
| 16 |
Test module for the command line interface. |
| 17 |
""" |
| 18 |
|
| 19 |
import difflib |
| 20 |
from io import StringIO |
| 21 |
import locale |
| 22 |
from pathlib import Path |
| 23 |
import os |
| 24 |
import re |
| 25 |
import sys |
| 26 |
import unittest |
| 27 |
|
| 28 |
if __name__ == '__main__': |
| 29 |
|
| 30 |
|
| 31 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 32 |
|
| 33 |
from docutils import __main__, core |
| 34 |
|
| 35 |
|
| 36 |
DATA_ROOT = Path(__file__).parent / 'data' |
| 37 |
|
| 38 |
|
| 39 |
def print_mismatch(expected, output): |
| 40 |
diff = ''.join(difflib.unified_diff( |
| 41 |
expected.splitlines(keepends=True), |
| 42 |
output.splitlines(keepends=True), |
| 43 |
'expected', 'output')) |
| 44 |
raise AssertionError('Unexpected output:\n'+diff) |
| 45 |
|
| 46 |
|
| 47 |
class CliTests(unittest.TestCase): |
| 48 |
|
| 49 |
def setUp(self): |
| 50 |
|
| 51 |
self.orig_argv = sys.argv |
| 52 |
self.orig_stdout = sys.stdout |
| 53 |
os.environ['DOCUTILSCONFIG'] = '' |
| 54 |
sys.stdout = StringIO() |
| 55 |
|
| 56 |
def tearDown(self): |
| 57 |
del os.environ['DOCUTILSCONFIG'] |
| 58 |
sys.stdout = self.orig_stdout |
| 59 |
sys.argv = self.orig_argv |
| 60 |
|
| 61 |
if sys.platform != "win32": |
| 62 |
locale.setlocale(locale.LC_MESSAGES, 'C') |
| 63 |
locale.setlocale(locale.LC_TIME, 'C') |
| 64 |
|
| 65 |
def get_help_text(self, prog, entry_point): |
| 66 |
|
| 67 |
sys.argv = [prog, '--help'] |
| 68 |
try: |
| 69 |
entry_point() |
| 70 |
except SystemExit: |
| 71 |
pass |
| 72 |
output = sys.stdout.getvalue() |
| 73 |
|
| 74 |
output = re.sub(r'default:[^)]*[/\\][^)]*\)', 'default: [...])', |
| 75 |
output, flags=re.DOTALL) |
| 76 |
|
| 77 |
output = output.replace( |
| 78 |
'error output. Default: ' |
| 79 |
f'{core.OptionParser.default_error_encoding}', |
| 80 |
'error output. Default: utf-8') |
| 81 |
return output |
| 82 |
|
| 83 |
def test_main_help(self): |
| 84 |
|
| 85 |
output = self.get_help_text('docutils', __main__.main) |
| 86 |
|
| 87 |
|
| 88 |
docutils_txt = os.path.join(DATA_ROOT, 'help/docutils.rst') |
| 89 |
expected = Path(docutils_txt).read_text(encoding='utf-8') |
| 90 |
if expected != output: |
| 91 |
print_mismatch(expected, output) |
| 92 |
|
| 93 |
def test_rst2html_help(self): |
| 94 |
|
| 95 |
output = self.get_help_text('rst2html', core.rst2html) |
| 96 |
|
| 97 |
rst2html_txt = os.path.join(DATA_ROOT, 'help/rst2html.rst') |
| 98 |
expected = Path(rst2html_txt).read_text(encoding='utf-8') |
| 99 |
if expected != output: |
| 100 |
print_mismatch(expected, output) |
| 101 |
|
| 102 |
def test_rst2latex_help(self): |
| 103 |
|
| 104 |
output = self.get_help_text('rst2latex', core.rst2latex) |
| 105 |
|
| 106 |
rst2latex_txt = os.path.join(DATA_ROOT, 'help/rst2latex.rst') |
| 107 |
expected = Path(rst2latex_txt).read_text(encoding='utf-8') |
| 108 |
if expected != output: |
| 109 |
print_mismatch(expected, output) |
| 110 |
|
| 111 |
|
| 112 |
if __name__ == '__main__': |
| 113 |
unittest.main() |