• 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 tempfile import TemporaryDirectory
5✔
2
from unittest import TestCase
5✔
3
from zpretty.prettifier import ZPrettifier
5✔
4
from zpretty.tests.mock import MockCLIRunner
5✔
5
from zpretty.xml import XMLPrettifier
5✔
6
from zpretty.zcml import ZCMLPrettifier
5✔
7

8
import os
5✔
9

10

11
try:
5✔
12
    from importlib.resources import files
5✔
13

14
    def resource_filename(package, resource):
5✔
15
        """Get the resource filename for a package and resource."""
16
        return str(files(package).joinpath(resource))
5✔
17

18
except ImportError:  # Python < 3.9
×
19
    # Python < 3.9
20
    from pkg_resources import resource_filename
×
21

22

23
class TestCli(TestCase):
5✔
24
    """Test the cli options"""
25

26
    def test_defaults(self):
5✔
27
        config = MockCLIRunner().config
5✔
28
        self.assertEqual(config.paths, "-")
5✔
29
        self.assertFalse(config.inplace)
5✔
30
        self.assertFalse(config.xml)
5✔
31
        self.assertFalse(config.zcml)
5✔
32
        self.assertEqual(config.encoding, "utf8")
5✔
33
        self.assertFalse(config.check)
5✔
34

35
    def test_short_options(self):
5✔
36
        config = MockCLIRunner("-i", "-x", "-z").config
5✔
37
        self.assertTrue(all((config.inplace, config.xml, config.zcml)))
5✔
38

39
    def test_long_options(self):
5✔
40
        config = MockCLIRunner("--inplace", "--xml", "--zcml").config
5✔
41
        self.assertTrue(all((config.inplace, config.xml, config.zcml)))
5✔
42

43
    def test_file(self):
5✔
44
        html = resource_filename("zpretty.tests", "original/sample_html.html")
5✔
45
        xml = resource_filename("zpretty.tests", "original/sample_xml.xml")
5✔
46
        config = MockCLIRunner(html, xml).config
5✔
47
        self.assertEqual(config.paths, [html, xml])
5✔
48

49
    def test_stdin(self):
5✔
50
        clirunner = MockCLIRunner()
5✔
51
        self.assertListEqual(clirunner.good_paths, ["-"])
5✔
52

53
    def test_broken_file_path(self):
5✔
54
        with TemporaryDirectory() as tmpdir:
5✔
55
            bad_path = os.path.join(tmpdir, "bad path")
5✔
56
            good_path = os.path.join(tmpdir, "good path")
5✔
57

58
            with open(good_path, "w") as f:
5✔
59
                f.write("I do exist")
5✔
60

61
            clirunner = MockCLIRunner(bad_path, good_path)
5✔
62
            self.assertListEqual(clirunner.good_paths, [good_path])
5✔
63

64
    def test_choose_prettifier(self):
5✔
65
        """Check the for the given options and file the best choice is made"""
66
        clirunner = MockCLIRunner("--xml", "--zcml")
5✔
67
        self.assertEqual(clirunner.choose_prettifier(""), ZCMLPrettifier)
5✔
68

69
        clirunner = MockCLIRunner("--xml")
5✔
70
        self.assertEqual(clirunner.choose_prettifier(""), XMLPrettifier)
5✔
71

72
        # Without options the extension rules
73
        clirunner = MockCLIRunner()
5✔
74
        self.assertEqual(clirunner.choose_prettifier("a.zcml"), ZCMLPrettifier)
5✔
75
        self.assertEqual(clirunner.choose_prettifier("a.xml"), XMLPrettifier)
5✔
76
        # The default one is returned if the extension is not recognized
77
        self.assertEqual(clirunner.choose_prettifier("a.txt"), ZPrettifier)
5✔
78

79
    def test_check(self):
5✔
80
        config = MockCLIRunner("--check").config
5✔
81
        self.assertTrue(config.check)
5✔
82

83
    def test_run_check(self):
5✔
84
        # XXX increase coverage by improving the mock
85
        from unittest import mock
5✔
86

87
        clirunner = MockCLIRunner("--check", "zpretty/tests/original/sample_xml.xml")
5✔
88
        with mock.patch("builtins.exit", return_value=None) as mocked:
5✔
89
            clirunner.run()
5✔
90
            mocked.assert_not_called()
5✔
91

92
        clirunner = MockCLIRunner("--check", "broken/broken.xml")
5✔
93

94
        with mock.patch("builtins.exit", return_value=None) as mocked:
5✔
95
            clirunner.run()
5✔
96
            mocked.assert_called_once_with(1)
5✔
97

98
    def test_good_paths(self):
5✔
99
        """Test the good_paths property"""
100
        clirunner = MockCLIRunner()
5✔
101
        self.assertListEqual(clirunner.good_paths, ["-"])
5✔
102

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

108
        clirunner = MockCLIRunner("broken/broken.xml")
5✔
109
        self.assertListEqual(clirunner.good_paths, [])
5✔
110

111
        clirunner = MockCLIRunner(
5✔
112
            "broken/broken.xml", "zpretty/tests/original/sample_xml.xml"
113
        )
114
        self.assertListEqual(
5✔
115
            clirunner.good_paths, ["zpretty/tests/original/sample_xml.xml"]
116
        )
117

118
        clirunner = MockCLIRunner("zpretty/tests", "--include", "broken")
5✔
119
        self.assertListEqual(
5✔
120
            clirunner.good_paths,
121
            [
122
                "zpretty/tests/broken/broken.xml",
123
            ],
124
        )
125

126
        clirunner = MockCLIRunner(
5✔
127
            "zpretty/tests", "--include", "broken", "--exclude", "broken"
128
        )
129
        self.assertListEqual(clirunner.good_paths, [])
5✔
130

131
        clirunner = MockCLIRunner(
5✔
132
            "zpretty/tests/include-exclude",
133
        )
134
        self.assertListEqual(
5✔
135
            clirunner.good_paths,
136
            [
137
                "zpretty/tests/include-exclude/foo/bar/bar.html",
138
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
139
                "zpretty/tests/include-exclude/foo/bar/bar.xml",
140
                "zpretty/tests/include-exclude/foo/bar/bar.zcml",
141
                "zpretty/tests/include-exclude/foo/bar/foo.html",
142
                "zpretty/tests/include-exclude/foo/bar/foo.pt",
143
                "zpretty/tests/include-exclude/foo/bar/foo.xml",
144
                "zpretty/tests/include-exclude/foo/bar/foo.zcml",
145
            ],
146
        )
147

148
        clirunner = MockCLIRunner(
5✔
149
            "zpretty/tests/include-exclude", "--extend-exclude", "bar/foo"
150
        )
151
        self.assertListEqual(
5✔
152
            clirunner.good_paths,
153
            [
154
                "zpretty/tests/include-exclude/foo/bar/bar.html",
155
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
156
                "zpretty/tests/include-exclude/foo/bar/bar.xml",
157
                "zpretty/tests/include-exclude/foo/bar/bar.zcml",
158
            ],
159
        )
160

161
        clirunner = MockCLIRunner(
5✔
162
            "zpretty/tests/include-exclude",
163
            "--include",
164
            "pt$",
165
            "--extend-exclude",
166
            "bar/foo",
167
        )
168
        self.assertListEqual(
5✔
169
            clirunner.good_paths,
170
            [
171
                "zpretty/tests/include-exclude/foo/bar/bar.pt",
172
            ],
173
        )
174

175
        clirunner = MockCLIRunner("zpretty/tests/include-exclude", "--exclude", "foo")
5✔
176
        self.assertListEqual(
5✔
177
            clirunner.good_paths,
178
            [
179
                "zpretty/tests/include-exclude/.venv/bar.html",
180
                "zpretty/tests/include-exclude/.venv/bar.pt",
181
                "zpretty/tests/include-exclude/.venv/bar.xml",
182
                "zpretty/tests/include-exclude/.venv/bar.zcml",
183
                "zpretty/tests/include-exclude/venv/bar.html",
184
                "zpretty/tests/include-exclude/venv/bar.pt",
185
                "zpretty/tests/include-exclude/venv/bar.xml",
186
                "zpretty/tests/include-exclude/venv/bar.zcml",
187
            ],
188
        )
189

190
        clirunner = MockCLIRunner(
5✔
191
            "zpretty/tests/include-exclude", "--exclude", "(foo|bar)"
192
        )
193
        self.assertListEqual(
5✔
194
            clirunner.good_paths,
195
            [],
196
        )
197

198
        with TemporaryDirectory() as tempdir:
5✔
199
            with open(os.path.join(tempdir, "foo.pt"), "w"):
5✔
200
                pass
5✔
201
            clirunner = MockCLIRunner(
5✔
202
                tempdir,
203
                "--include",
204
                "*",
205
                "--exclude",
206
                "+",
207
                "--extend-exclude",
208
                "[a-0]",
209
            )
210
            self.assertListEqual(
5✔
211
                clirunner.good_paths,
212
                [f"{tempdir}/foo.pt"],
213
            )
214
            self.assertListEqual(
5✔
215
                clirunner.errors,
216
                [
217
                    "Invalid regular expression for --exclude: '+'",
218
                    "Invalid regular expression for --extend-exclude: '[a-0]'",
219
                    "Invalid regular expression for --include: '*'",
220
                ],
221
            )
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