diff options
| author | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-11-24 15:09:24 +0000 |
|---|---|---|
| committer | milde <milde@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2022-11-24 15:09:24 +0000 |
| commit | b8f17d13eef35fb496ac13033c3f31521a0841e9 (patch) | |
| tree | ff72af0c8a8041b9cf0af3f1f7a7bc3f4b3ecfd6 /docutils | |
| parent | 51a197af917f2b988a806eda6181b6129ff64aa1 (diff) | |
| download | docutils-b8f17d13eef35fb496ac13033c3f31521a0841e9.tar.gz | |
Make "record_dependecies" test independent of the working dir.
In rST, the location of images and figures is specified as URI
and used literally. As a consequence, finding a corresponding
image file in the local filesystem depends on the current working
directory. The tests are designed to work if the cwd is the test root.
As only files that are actually found are written to the "dependencies",
we disable the tests for image file registry if cwd != test root.
Also, use "pathlib.Path" for simpler path manipulations.
Signed-off-by: milde <milde@users.sf.net>
git-svn-id: https://svn.code.sf.net/p/docutils/code/trunk@9267 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
| -rwxr-xr-x | docutils/test/test_dependencies.py | 117 |
1 files changed, 60 insertions, 57 deletions
diff --git a/docutils/test/test_dependencies.py b/docutils/test/test_dependencies.py index ba68f461e..9123fc507 100755 --- a/docutils/test/test_dependencies.py +++ b/docutils/test/test_dependencies.py @@ -9,8 +9,8 @@ Test module for the --record-dependencies option. """ from io import StringIO -import os.path from pathlib import Path +import os.path import sys import unittest @@ -24,26 +24,24 @@ import docutils.utils import docutils.io from docutils.parsers.rst.directives.images import PIL -# TEST_ROOT is ./test/ from the docutils root -TEST_ROOT = os.path.abspath(os.path.dirname(__file__)) -# DATA_ROOT is ./test/data/ from the docutils root -DATA_ROOT = os.path.join(TEST_ROOT, 'data') +TEST_ROOT = Path(__file__).parent # ./test/ from the docutils root +DATA_ROOT = TEST_ROOT / 'data' +CWD = Path(os.getcwd()) -def relpath(*parts): - return os.path.relpath( - os.path.join(*parts), os.getcwd()).replace('\\', '/') +def relpath(path): + # docutils.utils.DependencyList records POSIX paths, + # i.e. "/" as a path separator even on Windows. + return os.path.relpath(path, CWD).replace('\\', '/') -# docutils.utils.DependencyList records POSIX paths, -# i.e. "/" as a path separator even on Windows (not os.path.join). paths = { - 'include': relpath(DATA_ROOT, 'include.txt'), # included rst file - 'raw': relpath(DATA_ROOT, 'raw.txt'), # included raw "HTML file" - 'scaled-image': relpath(TEST_ROOT, - '../docs/user/rst/images/biohazard.png'), - 'figure-image': relpath(TEST_ROOT, '../docs/user/rst/images/title.png'), - 'stylesheet': relpath(DATA_ROOT, 'stylesheet.txt'), + 'include': relpath(DATA_ROOT / 'include.txt'), # included rst file + 'raw': relpath(DATA_ROOT / 'raw.txt'), # included raw "HTML file" + 'stylesheet': relpath(DATA_ROOT / 'stylesheet.txt'), + # the "image" and "figure" directives expect a URI and use it literally + 'scaled-image': '../docs/user/rst/images/biohazard.png', + 'figure-image': '../docs/user/rst/images/title.png', } # avoid latex writer future warnings: @@ -53,47 +51,47 @@ latex_settings_overwrites = {'legacy_column_widths': False, class RecordDependenciesTests(unittest.TestCase): - def get_record(self, **settings): + maxDiff = None + + def get_record(self, **kwargs): recordfile = 'record.txt' recorder = docutils.utils.DependencyList(recordfile) # (Re) create the record file by running a conversion: - settings.setdefault('source_path', - os.path.join(DATA_ROOT, 'dependencies.txt')) - settings.setdefault('settings_overrides', {}) - settings['settings_overrides'].update(_disable_config=True, - record_dependencies=recorder) - docutils.core.publish_file(destination=StringIO(), # ignored - **settings) + kwargs.setdefault('source_path', str(DATA_ROOT/'dependencies.txt')) + kwargs.setdefault('settings_overrides', {}) + kwargs['settings_overrides'].update(_disable_config=True, + record_dependencies=recorder) + output = docutils.core.publish_file(destination=StringIO(), # ignored + **kwargs) recorder.close() # Read the record file: with open(recordfile, encoding='utf-8') as record: - return record.read().splitlines() + return record.read().splitlines(), output def test_dependencies_xml(self): # Note: currently, raw input files are read (and hence recorded) while # parsing even if not used in the chosen output format. # This should change (see parsers/rst/directives/misc.py). keys = ['include', 'raw'] - if PIL: + if PIL and TEST_ROOT == CWD: keys += ['figure-image'] expected = [paths[key] for key in keys] - record = sorted(self.get_record(writer_name='xml')) + record, output = self.get_record(writer_name='xml') # the order of the files is arbitrary - expected.sort() - self.assertEqual(record, expected) + self.assertEqual(sorted(record), sorted(expected)) def test_dependencies_html(self): keys = ['include', 'raw'] - if PIL: + if PIL and (TEST_ROOT == CWD): keys += ['figure-image', 'scaled-image'] expected = [paths[key] for key in keys] # stylesheets are tested separately in test_stylesheet_dependencies(): - so = {'stylesheet_path': None, 'stylesheet': None} - record = sorted(self.get_record(writer_name='html', - settings_overrides=so)) + settings = {'stylesheet_path': None, 'stylesheet': None} + record, output = self.get_record(writer_name='html5', + settings_overrides=settings) # the order of the files is arbitrary - expected.sort() - self.assertEqual(record, expected) + self.assertEqual(sorted(record), sorted(expected), + msg='output is:\n'+output) def test_dependencies_latex(self): # since 0.9, the latex writer records only really accessed files, too. @@ -101,42 +99,47 @@ class RecordDependenciesTests(unittest.TestCase): # parsing even if not used in the chosen output format. # This should change (see parsers/rst/directives/misc.py). keys = ['include', 'raw'] - if PIL: + if PIL and TEST_ROOT == CWD: keys += ['figure-image'] expected = [paths[key] for key in keys] - record = sorted(self.get_record( + record, output = self.get_record( writer_name='latex', - settings_overrides=latex_settings_overwrites)) + settings_overrides=latex_settings_overwrites) # the order of the files is arbitrary - expected.sort() - self.assertEqual(record, expected) + self.assertEqual(sorted(record), sorted(expected), + msg='output is:\n'+output) def test_csv_dependencies(self): - csvsource = os.path.join(DATA_ROOT, 'csv_dep.txt') - self.assertEqual(self.get_record(source_path=csvsource), - [relpath(DATA_ROOT, 'csv_data.txt')]) + csvsource = str(DATA_ROOT / 'csv_dep.txt') + record, output = self.get_record(source_path=csvsource) + self.assertEqual(record, [relpath(DATA_ROOT / 'csv_data.txt')], + msg='output is:\n'+output) def test_stylesheet_dependencies(self): stylesheet = paths['stylesheet'] - so = {'stylesheet_path': paths['stylesheet'], - 'stylesheet': None} - so.update(latex_settings_overwrites) - so['embed_stylesheet'] = False - record = self.get_record(writer_name='html', settings_overrides=so) + settings = {'stylesheet_path': paths['stylesheet'], + 'stylesheet': None} + settings.update(latex_settings_overwrites) + settings['embed_stylesheet'] = False + record, output = self.get_record(writer_name='html', + settings_overrides=settings) self.assertTrue(stylesheet not in record, - '%r should not be in %r' % (stylesheet, record)) - record = self.get_record(writer_name='latex', settings_overrides=so) + f'{stylesheet!r} should not be in {record!r}') + record, output = self.get_record(writer_name='latex', + settings_overrides=settings) self.assertTrue(stylesheet not in record, - '%r should not be in %r' % (stylesheet, record)) + f'{stylesheet!r} should not be in {record!r}') - so['embed_stylesheet'] = True - record = self.get_record(writer_name='html', settings_overrides=so) + settings['embed_stylesheet'] = True + record, output = self.get_record(writer_name='html', + settings_overrides=settings) self.assertTrue(stylesheet in record, - '%r should be in %r' % (stylesheet, record)) - so['embed_stylesheet'] = True - record = self.get_record(writer_name='latex', settings_overrides=so) + f'{stylesheet!r} should be in {record!r}') + settings['embed_stylesheet'] = True + record, output = self.get_record(writer_name='latex', + settings_overrides=settings) self.assertTrue(stylesheet in record, - '%r should be in %r' % (stylesheet, record)) + f'{stylesheet!r} should be in {record!r}') def tearDown(self) -> None: os.unlink("record.txt") |
