• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

collective / zpretty / 19173411475

07 Nov 2025 03:45PM UTC coverage: 95.794% (-1.1%) from 96.907%
19173411475

push

github

1116 of 1165 relevant lines covered (95.79%)

4.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.89
/zpretty/tests/test_cli.py
1
from importlib.resources import files
5✔
2
from tempfile import TemporaryDirectory
5✔
3
from unittest import TestCase
5✔
4
from zpretty.prettifier import ZPrettifier
5✔
5
from zpretty.tests.mock import MockCLIRunner
5✔
6
from zpretty.xml import XMLPrettifier
5✔
7
from zpretty.zcml import ZCMLPrettifier
8

5✔
9
import os
10

11

5✔
12
class TestCli(TestCase):
5✔
13
    """Test the cli options"""
14

5✔
15
    sample_folder_path = files("zpretty.tests") / "original"
16

5✔
17
    def test_defaults(self):
18
        config = MockCLIRunner().config
×
19
        self.assertEqual(config.paths, "-")
20
        self.assertFalse(config.inplace)
×
21
        self.assertFalse(config.xml)
22
        self.assertFalse(config.zcml)
23
        self.assertEqual(config.encoding, "utf8")
5✔
24
        self.assertFalse(config.check)
25

26
    def test_short_options(self):
5✔
27
        config = MockCLIRunner("-i", "-x", "-z").config
5✔
28
        self.assertTrue(all((config.inplace, config.xml, config.zcml)))
5✔
29

5✔
30
    def test_long_options(self):
5✔
31
        config = MockCLIRunner("--inplace", "--xml", "--zcml").config
5✔
32
        self.assertTrue(all((config.inplace, config.xml, config.zcml)))
5✔
33

5✔
34
    def test_file(self):
35
        html = str(self.sample_folder_path / "sample_html.html")
5✔
36
        xml = str(self.sample_folder_path / "sample_xml.xml")
5✔
37
        config = MockCLIRunner(html, xml).config
5✔
38
        self.assertEqual(config.paths, [html, xml])
39

5✔
40
    def test_stdin(self):
5✔
41
        clirunner = MockCLIRunner()
5✔
42
        self.assertListEqual(clirunner.good_paths, ["-"])
43

5✔
44
    def test_broken_file_path(self):
5✔
45
        with TemporaryDirectory() as tmpdir:
5✔
46
            bad_path = os.path.join(tmpdir, "bad path")
5✔
47
            good_path = os.path.join(tmpdir, "good path")
5✔
48

49
            with open(good_path, "w") as f:
5✔
50
                f.write("I do exist")
5✔
51

5✔
52
            clirunner = MockCLIRunner(bad_path, good_path)
53
            self.assertListEqual(clirunner.good_paths, [good_path])
5✔
54

5✔
55
    def test_choose_prettifier(self):
5✔
56
        """Check the for the given options and file the best choice is made"""
5✔
57
        clirunner = MockCLIRunner("--xml", "--zcml")
58
        self.assertEqual(clirunner.choose_prettifier(""), ZCMLPrettifier)
5✔
59

5✔
60
        clirunner = MockCLIRunner("--xml")
61
        self.assertEqual(clirunner.choose_prettifier(""), XMLPrettifier)
5✔
62

5✔
63
        # Without options the extension rules
64
        clirunner = MockCLIRunner()
5✔
65
        self.assertEqual(clirunner.choose_prettifier("a.zcml"), ZCMLPrettifier)
66
        self.assertEqual(clirunner.choose_prettifier("a.xml"), XMLPrettifier)
5✔
67
        # The default one is returned if the extension is not recognized
5✔
68
        self.assertEqual(clirunner.choose_prettifier("a.txt"), ZPrettifier)
69

5✔
70
    def test_check(self):
5✔
71
        config = MockCLIRunner("--check").config
72
        self.assertTrue(config.check)
73

5✔
74
    def test_run_check(self):
5✔
75
        # XXX increase coverage by improving the mock
5✔
76
        from unittest import mock
77

5✔
78
        clirunner = MockCLIRunner("--check", "zpretty/tests/original/sample_xml.xml")
79
        with mock.patch("builtins.exit", return_value=None) as mocked:
5✔
80
            clirunner.run()
5✔
81
            mocked.assert_not_called()
5✔
82

83
        clirunner = MockCLIRunner("--check", "broken/broken.xml")
5✔
84

85
        with mock.patch("builtins.exit", return_value=None) as mocked:
5✔
86
            clirunner.run()
87
            mocked.assert_called_once_with(1)
5✔
88

5✔
89
    def test_good_paths(self):
5✔
90
        """Test the good_paths property"""
5✔
91
        clirunner = MockCLIRunner()
92
        self.assertListEqual(clirunner.good_paths, ["-"])
5✔
93

94
        clirunner = MockCLIRunner("zpretty/tests/original/sample_xml.xml")
5✔
95
        self.assertListEqual(
5✔
96
            clirunner.good_paths, ["zpretty/tests/original/sample_xml.xml"]
5✔
97
        )
98

5✔
99
        clirunner = MockCLIRunner("broken/broken.xml")
100
        self.assertListEqual(clirunner.good_paths, [])
5✔
101

5✔
102
        clirunner = MockCLIRunner(
103
            "broken/broken.xml", "zpretty/tests/original/sample_xml.xml"
5✔
104
        )
5✔
105
        self.assertListEqual(
106
            clirunner.good_paths, ["zpretty/tests/original/sample_xml.xml"]
107
        )
108

5✔
109
        clirunner = MockCLIRunner("zpretty/tests", "--include", "broken")
5✔
110
        self.assertListEqual(
111
            clirunner.good_paths,
5✔
112
            [
113
                "zpretty/tests/broken/broken.xml",
114
            ],
5✔
115
        )
116

117
        clirunner = MockCLIRunner(
118
            "zpretty/tests", "--include", "broken", "--exclude", "broken"
5✔
119
        )
5✔
120
        self.assertListEqual(clirunner.good_paths, [])
121

122
        clirunner = MockCLIRunner(
123
            "zpretty/tests/include-exclude",
124
        )
125
        self.assertListEqual(
126
            clirunner.good_paths,
5✔
127
            [
128
                "zpretty/tests/include-exclude/foo/bar/bar.html",
129
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
5✔
130
                "zpretty/tests/include-exclude/foo/bar/bar.xml",
131
                "zpretty/tests/include-exclude/foo/bar/bar.zcml",
5✔
132
                "zpretty/tests/include-exclude/foo/bar/foo.html",
133
                "zpretty/tests/include-exclude/foo/bar/foo.pt",
134
                "zpretty/tests/include-exclude/foo/bar/foo.xml",
5✔
135
                "zpretty/tests/include-exclude/foo/bar/foo.zcml",
136
            ],
137
        )
138

139
        clirunner = MockCLIRunner(
140
            "zpretty/tests/include-exclude", "--extend-exclude", "bar/foo"
141
        )
142
        self.assertListEqual(
143
            clirunner.good_paths,
144
            [
145
                "zpretty/tests/include-exclude/foo/bar/bar.html",
146
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
147
                "zpretty/tests/include-exclude/foo/bar/bar.xml",
148
                "zpretty/tests/include-exclude/foo/bar/bar.zcml",
5✔
149
            ],
150
        )
151

5✔
152
        clirunner = MockCLIRunner(
153
            "zpretty/tests/include-exclude",
154
            "--include",
155
            "pt$",
156
            "--extend-exclude",
157
            "bar/foo",
158
        )
159
        self.assertListEqual(
160
            clirunner.good_paths,
161
            [
5✔
162
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
163
            ],
164
        )
165

166
        clirunner = MockCLIRunner("zpretty/tests/include-exclude", "--exclude", "foo")
167
        self.assertListEqual(
168
            clirunner.good_paths,
5✔
169
            [
170
                "zpretty/tests/include-exclude/.venv/bar.html",
171
                "zpretty/tests/include-exclude/.venv/bar.pt",
172
                "zpretty/tests/include-exclude/.venv/bar.xml",
173
                "zpretty/tests/include-exclude/.venv/bar.zcml",
174
                "zpretty/tests/include-exclude/venv/bar.html",
175
                "zpretty/tests/include-exclude/venv/bar.pt",
5✔
176
                "zpretty/tests/include-exclude/venv/bar.xml",
5✔
177
                "zpretty/tests/include-exclude/venv/bar.zcml",
178
            ],
179
        )
180

181
        clirunner = MockCLIRunner(
182
            "zpretty/tests/include-exclude", "--exclude", "(foo|bar)"
183
        )
184
        self.assertListEqual(
185
            clirunner.good_paths,
186
            [],
187
        )
188

189
        with TemporaryDirectory() as tempdir:
190
            with open(os.path.join(tempdir, "foo.pt"), "w"):
5✔
191
                pass
192
            clirunner = MockCLIRunner(
193
                tempdir,
5✔
194
                "--include",
195
                "*",
196
                "--exclude",
197
                "+",
198
                "--extend-exclude",
5✔
199
                "[a-0]",
5✔
200
            )
5✔
201
            self.assertListEqual(
5✔
202
                clirunner.good_paths,
203
                [f"{tempdir}/foo.pt"],
204
            )
205
            self.assertListEqual(
206
                clirunner.errors,
207
                [
208
                    "Invalid regular expression for --exclude: '+'",
209
                    "Invalid regular expression for --extend-exclude: '[a-0]'",
210
                    "Invalid regular expression for --include: '*'",
5✔
211
                ],
212
            )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc