Debian Codegraph @

tools/test/test_buildhtml.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # $Id: test_buildhtml.py 9906 2024-08-15 08:43:38Z grubert $
4 # Author: engelbert gruber <[email protected]>
5 # Copyright: This module has been placed in the public domain.
6
7 """
8 test buildhtml options, because ``--local`` is broken.
9
10 Build-HTML Options
11 ------------------
12 --recurse Recursively scan subdirectories for files to process.
13 This is the default.
14 --local Do not scan subdirectories for files to process.
15 --prune=<directory> Do not process files in <directory>. This option may
16 be used more than once to specify multiple
17 directories.
18 --ignore=<patterns> Recursively ignore files or directories matching any
19 of the given wildcard (shell globbing) patterns
20 (separated by colons). Default: ".svn:CVS"
21 --silent Work silently (no progress messages). Independent of
22 "--quiet".
23 """
24
25 from __future__ import annotations
26
27 import shutil
28 import subprocess
29 import sys
30 import tempfile
31 import unittest
32 from pathlib import Path
33
34 # TOOLS_ROOT is ./tools/ from the docutils root
35 TOOLS_ROOT = Path(__file__).resolve().parent.parent
36 BUILDHTML_PATH = TOOLS_ROOT / 'buildhtml.py'
37
38
39 def process_and_return_filelist(
40 options: list[str],
41 ) -> tuple[list[str], list[str]]:
42 dirs = []
43 files = []
44 ret = subprocess.run(
45 [sys.executable, BUILDHTML_PATH] + options,
46 stdout=subprocess.PIPE,
47 stderr=subprocess.STDOUT,
48 text=True,
49 encoding='utf-8',
50 errors='replace',
51 )
52 for line in ret.stdout.splitlines():
53 # BUG no colon in filename/path allowed
54 item = line.split(": ")[-1].strip()
55 if line.startswith(" "):
56 files.append(item)
57 else:
58 dirs.append(item)
59 return dirs, files
60
61
62 class BuildHtmlTests(unittest.TestCase):
63 tree = (
64 "_tmp_test_tree/one.rst",
65 "_tmp_test_tree/two.rst",
66 "_tmp_test_tree/dir1/one.rst",
67 "_tmp_test_tree/dir1/two.rst",
68 "_tmp_test_tree/dir2/one.rst",
69 "_tmp_test_tree/dir2/two.rst",
70 "_tmp_test_tree/dir2/sub/one.rst",
71 "_tmp_test_tree/dir2/sub/two.rst",
72 )
73
74 def setUp(self) -> None:
75 self.root = Path(tempfile.mkdtemp()).resolve()
76
77 for file in self.tree:
78 path = self.root / file
79 path.parent.mkdir(parents=True, exist_ok=True)
80 path.write_text('dummy', encoding='utf-8')
81
82 def tearDown(self) -> None:
83 shutil.rmtree(self.root)
84
85 def test_1(self) -> None:
86 opts = ["--dry-run", str(self.root)]
87 _dirs, files = process_and_return_filelist(opts)
88 self.assertEqual(files.count("one.rst"), 4)
89
90 def test_local(self) -> None:
91 opts = ["--dry-run", "--local", str(self.root)]
92 dirs, files = process_and_return_filelist(opts)
93 self.assertEqual(len(dirs), 1)
94 self.assertEqual(files, [])
95
96
97 if __name__ == '__main__':
98 unittest.main()