File: test_buildhtml.py

package info (click to toggle)
python-docutils 0.19%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,668 kB
  • sloc: python: 45,630; lisp: 14,475; xml: 1,789; javascript: 1,032; sh: 130; makefile: 104
file content (110 lines) | stat: -rw-r--r-- 3,540 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3

# $Id: test_buildhtml.py 9072 2022-06-15 11:31:09Z milde $
# Author: engelbert gruber <grubert@users.sourceforge.net>
# Copyright: This module has been placed in the public domain.

"""
test buildhtml options, because ``--local`` is broken.

Build-HTML Options
------------------
--recurse               Recursively scan subdirectories for files to process.
                        This is the default.
--local                 Do not scan subdirectories for files to process.
--prune=<directory>     Do not process files in <directory>.  This option may
                        be used more than once to specify multiple
                        directories.
--ignore=<patterns>     Recursively ignore files or directories matching any
                        of the given wildcard (shell globbing) patterns
                        (separated by colons).  Default: ".svn:CVS"
--silent                Work silently (no progress messages).  Independent of
                        "--quiet".
"""

import unittest
import os
from subprocess import Popen, PIPE, STDOUT
import sys
import tempfile


buildhtml_path = os.path.abspath(os.path.join(
                    os.path.dirname(__file__) or os.curdir,
                    '..', 'buildhtml.py'))


def process_and_return_filelist(options):
    dirs = []
    files = []
    p = Popen([sys.executable, buildhtml_path] + options,
              stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    (cin, cout) = (p.stdin, p.stdout)
    while True:
        line = cout.readline()
        if not line:
            break
        # in Py 3x, cout.readline() returns `bytes` and the processing fails
        line = line.decode('ascii', 'replace')
        # BUG no colon in filename/path allowed
        item = line.split(": ")[-1].strip()
        if line.startswith(" "):
            files.append(item)
        else:
            dirs.append(item)
    cin.close()
    cout.close()
    p.wait()
    return dirs, files


class BuildHtmlTests(unittest.TestCase):
    tree = ("_tmp_test_tree",
            "_tmp_test_tree/one.txt",
            "_tmp_test_tree/two.txt",
            "_tmp_test_tree/dir1",
            "_tmp_test_tree/dir1/one.txt",
            "_tmp_test_tree/dir1/two.txt",
            "_tmp_test_tree/dir2",
            "_tmp_test_tree/dir2/one.txt",
            "_tmp_test_tree/dir2/two.txt",
            "_tmp_test_tree/dir2/sub",
            "_tmp_test_tree/dir2/sub/one.txt",
            "_tmp_test_tree/dir2/sub/two.txt",
            )

    def setUp(self):
        self.root = tempfile.mkdtemp()

        for s in self.tree:
            s = os.path.join(self.root, s)
            if "." not in s:
                os.mkdir(s)
            else:
                fd_s = open(s, "w", encoding='utf-8')
                fd_s.write("dummy")
                fd_s.close()

    def tearDown(self):
        for i in range(len(self.tree) - 1, -1, -1):
            s = os.path.join(self.root, self.tree[i])
            if "." not in s:
                os.rmdir(s)
            else:
                os.remove(s)
        os.rmdir(self.root)

    def test_1(self):
        opts = ["--dry-run", self.root]
        dirs, files = process_and_return_filelist(opts)
        self.assertEqual(files.count("one.txt"), 4)

    def test_local(self):
        opts = ["--dry-run", "--local", self.root]
        dirs, files = process_and_return_filelist(opts)
        self.assertEqual(len(dirs), 1)
        self.assertEqual(files, [])


if __name__ == '__main__':
    unittest.main()