Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

482 lines · 14.6 KiB · Python

Last changed by

Merge pull request #1850 from janLuke/fix/make-default-short-helpDavid Lordbefore adoption82a83fd

History of this file

Take it

The walk stopped after 200 commits: lines older than 3ce663c point at it.

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 1why, one step back
1import os

Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba

line 2why, one step back
2import pathlib

Merge branch '7.x'David Lordbefore adoption2269786

line 3why, one step back
3import stat

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 4why, one step back
4import sys

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 5why, one step back
5from io import StringIO

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 6–9why, one step back
6
7import pytest89import click._termui_impl

Merge branch '7.x'David Lordbefore adoption7239794

lines 10–11why, one step back
10import click.utils
11from click._compat import WIN

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 12–15why, one step back
12
1314def test_echo(runner):15 with runner.isolation() as outstreams:

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 16why, one step back
16 click.echo("\N{SNOWMAN}")

Merge branch '7.x'David Lordbefore adoption7239794

line 17why, one step back
17 click.echo(b"\x44\x44")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 18why, one step back
18 click.echo(42, nl=False)

Merge branch '7.x'David Lordbefore adoption7239794

lines 19–22why, one step back
19 click.echo(b"a", nl=False)
20 click.echo("\x1b[31mx\x1b[39m", nl=False)21 bytes = outstreams[0].getvalue().replace(b"\r\n", b"\n")22 assert bytes == b"\xe2\x98\x83\nDD\n42ax"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 23why, one step back
23

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 24why, one step back
24 # if wrapped, we expect bytes to survive.

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 25–26why, one step back
25 @click.command()
26 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

lines 27–28why, one step back
27 click.echo(b"\xf6")
28

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 29why, one step back
29 result = runner.invoke(cli, [])

Merge branch '7.x'David Lordbefore adoption7239794

line 30why, one step back
30 assert result.stdout_bytes == b"\xf6\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 31–33why, one step back
31
32 # Ensure we do not strip for bytes.33 with runner.isolation() as outstreams:

Merge branch '7.x'David Lordbefore adoption7239794

lines 34–35why, one step back
34 click.echo(bytearray(b"\x1b[31mx\x1b[39m"), nl=False)
35 assert outstreams[0].getvalue() == b"\x1b[31mx\x1b[39m"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 36–39why, one step back
36
3738def test_echo_custom_file():39 import io

Merge branch '7.x'David Lordbefore adoption7239794

line 40why, one step back
40

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 41why, one step back
41 f = io.StringIO()

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

lines 42–43why, one step back
42 click.echo("hello", file=f)
43 assert f.getvalue() == "hello\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 44–45why, one step back
44
45

Merge branch '7.x'David Lordbefore adoption2269786

lines 46–64why, one step back
46@pytest.mark.parametrize(
47 ("styles", "ref"),48 [49 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),50 ({"fg": "red"}, "\x1b[31mx y\x1b[0m"),51 ({"fg": "green"}, "\x1b[32mx y\x1b[0m"),52 ({"fg": "yellow"}, "\x1b[33mx y\x1b[0m"),53 ({"fg": "blue"}, "\x1b[34mx y\x1b[0m"),54 ({"fg": "magenta"}, "\x1b[35mx y\x1b[0m"),55 ({"fg": "cyan"}, "\x1b[36mx y\x1b[0m"),56 ({"fg": "white"}, "\x1b[37mx y\x1b[0m"),57 ({"bg": "black"}, "\x1b[40mx y\x1b[0m"),58 ({"bg": "red"}, "\x1b[41mx y\x1b[0m"),59 ({"bg": "green"}, "\x1b[42mx y\x1b[0m"),60 ({"bg": "yellow"}, "\x1b[43mx y\x1b[0m"),61 ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),62 ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),63 ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),64 ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),

Merge pull request #1429 from lmjohns3/masterDavid Lordbefore adoption0d43812

lines 65–66why, one step back
65 ({"bg": 91}, "\x1b[48;5;91mx y\x1b[0m"),
66 ({"bg": (135, 0, 175)}, "\x1b[48;2;135;0;175mx y\x1b[0m"),

Merge branch '7.x'David Lordbefore adoption2269786

lines 67–68why, one step back
67 ({"bold": True}, "\x1b[1mx y\x1b[0m"),
68 ({"dim": True}, "\x1b[2mx y\x1b[0m"),

Merge pull request #1822 from rbanffy/overline-italicDavid Lordbefore adoptionc0f9c06

lines 69–75why, one step back
69 ({"underline": True}, "\x1b[4mx y\x1b[0m"),
70 ({"overline": True}, "\x1b[55mx y\x1b[0m"),71 ({"italic": True}, "\x1b[23mx y\x1b[0m"),72 ({"blink": True}, "\x1b[5mx y\x1b[0m"),73 ({"reverse": True}, "\x1b[7mx y\x1b[0m"),74 ({"strikethrough": True}, "\x1b[9mx y\x1b[0m"),75 ({"fg": "black", "reset": False}, "\x1b[30mx y"),

Merge branch '7.x'David Lordbefore adoption2269786

lines 76–82why, one step back
76 ],
77)78def test_styling(styles, ref):79 assert click.style("x y", **styles) == ref80 assert click.unstyle(ref) == "x y"8182

Merge branch '7.x'David Lordbefore adoption7239794

line 83why, one step back
83@pytest.mark.parametrize(("text", "expect"), [("\x1b[?25lx y\x1b[?25h", "x y")])

Merge branch '7.x'David Lordbefore adoption2269786

lines 84–85why, one step back
84def test_unstyle_other_ansi(text, expect):
85 assert click.unstyle(text) == expect

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 86–88why, one step back
86
8788def test_filename_formatting():

Merge branch '7.x'David Lordbefore adoption7239794

lines 89–90why, one step back
89 assert click.format_filename(b"foo.txt") == "foo.txt"
90 assert click.format_filename(b"/x/foo.txt") == "/x/foo.txt"

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

lines 91–92why, one step back
91 assert click.format_filename("/x/foo.txt") == "/x/foo.txt"
92 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 93–95why, one step back
93
94 # filesystem encoding on windows permits this.95 if not WIN:

Merge pull request #1846 from pallets/fsdecodeDavid Lordbefore adoption269a4ae

line 96why, one step back
96 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\udcff.txt"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 97–101why, one step back
97
9899def test_prompts(runner):100 @click.command()101 def test():

Merge branch '7.x'David Lordbefore adoption7239794

lines 102–103why, one step back
102 if click.confirm("Foo"):
103 click.echo("yes!")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 104why, one step back
104 else:

Merge branch '7.x'David Lordbefore adoption7239794

line 105why, one step back
105 click.echo("no :(")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 106why, one step back
106

Merge branch '7.x'David Lordbefore adoption7239794

line 107why, one step back
107 result = runner.invoke(test, input="y\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 108why, one step back
108 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 109why, one step back
109 assert result.output == "Foo [y/N]: y\nyes!\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 110why, one step back
110

Merge branch '7.x'David Lordbefore adoption7239794

line 111why, one step back
111 result = runner.invoke(test, input="\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 112why, one step back
112 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 113why, one step back
113 assert result.output == "Foo [y/N]: \nno :(\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 114why, one step back
114

Merge branch '7.x'David Lordbefore adoption7239794

line 115why, one step back
115 result = runner.invoke(test, input="n\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 116why, one step back
116 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 117why, one step back
117 assert result.output == "Foo [y/N]: n\nno :(\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 118–120why, one step back
118
119 @click.command()120 def test_no():

Merge branch '7.x'David Lordbefore adoption7239794

lines 121–122why, one step back
121 if click.confirm("Foo", default=True):
122 click.echo("yes!")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 123why, one step back
123 else:

Merge branch '7.x'David Lordbefore adoption7239794

line 124why, one step back
124 click.echo("no :(")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 125why, one step back
125

Merge branch '7.x'David Lordbefore adoption7239794

line 126why, one step back
126 result = runner.invoke(test_no, input="y\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 127why, one step back
127 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 128why, one step back
128 assert result.output == "Foo [Y/n]: y\nyes!\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 129why, one step back
129

Merge branch '7.x'David Lordbefore adoption7239794

line 130why, one step back
130 result = runner.invoke(test_no, input="\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 131why, one step back
131 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 132why, one step back
132 assert result.output == "Foo [Y/n]: \nyes!\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 133why, one step back
133

Merge branch '7.x'David Lordbefore adoption7239794

line 134why, one step back
134 result = runner.invoke(test_no, input="n\n")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 135why, one step back
135 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 136why, one step back
136 assert result.output == "Foo [Y/n]: n\nno :(\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 137–138why, one step back
137
138

Merge pull request #1402 from yurihs/add-repeat-behavior-to-promptDavid Lordbefore adoption719690a

lines 139–146why, one step back
139def test_confirm_repeat(runner):
140 cli = click.Command(141 "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)]142 )143 result = runner.invoke(cli, input="\ny\n")144 assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"145146

Merge branch '7.x'David Lordbefore adoption7239794

line 147why, one step back
147@pytest.mark.skipif(WIN, reason="Different behavior on windows.")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 148–151why, one step back
148def test_prompts_abort(monkeypatch, capsys):
149 def f(_):150 raise KeyboardInterrupt()151

Merge branch '7.x'David Lordbefore adoption7239794

line 152why, one step back
152 monkeypatch.setattr("click.termui.hidden_prompt_func", f)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 153–154why, one step back
153
154 try:

Merge branch '7.x'David Lordbefore adoption7239794

line 155why, one step back
155 click.prompt("Password", hide_input=True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 156why, one step back
156 except click.Abort:

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 157why, one step back
157 click.echo("interrupted")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 158–159why, one step back
158
159 out, err = capsys.readouterr()

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 160why, one step back
160 assert out == "Password:\ninterrupted\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 161–163why, one step back
161
162163def _test_gen_func():

Merge branch '7.x'David Lordbefore adoption7239794

lines 164–167why, one step back
164 yield "a"
165 yield "b"166 yield "c"167 yield "abc"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 168–169why, one step back
168
169

Merge branch '7.x'David Lordbefore adoption7239794

lines 170–184why, one step back
170@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
171@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])172@pytest.mark.parametrize(173 "test",174 [175 # We need lambda here, because pytest will176 # reuse the parameters, and then the generators177 # are already used and will not yield anymore178 ("just text\n", lambda: "just text"),179 ("iterable\n", lambda: ["itera", "ble"]),180 ("abcabc\n", lambda: _test_gen_func),181 ("abcabc\n", lambda: _test_gen_func()),182 ("012345\n", lambda: (c for c in range(6))),183 ],184)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 185why, one step back
185def test_echo_via_pager(monkeypatch, capfd, cat, test):

Merge branch '7.x'David Lordbefore adoption7239794

lines 186–187why, one step back
186 monkeypatch.setitem(os.environ, "PAGER", cat)
187 monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 188–197why, one step back
188
189 expected_output = test[0]190 test_input = test[1]()191192 click.echo_via_pager(test_input)193194 out, err = capfd.readouterr()195 assert out == expected_output196197

Merge branch '7.x'David Lordbefore adoption7239794

line 198why, one step back
198@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 199–200why, one step back
199def test_echo_color_flag(monkeypatch, capfd):
200 isatty = True

Merge branch '7.x'David Lordbefore adoption7239794

line 201why, one step back
201 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 202why, one step back
202

Merge branch '7.x'David Lordbefore adoption7239794

lines 203–205why, one step back
203 text = "foo"
204 styled_text = click.style(text, fg="red")205 assert styled_text == "\x1b[31mfoo\x1b[0m"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 206–208why, one step back
206
207 click.echo(styled_text, color=False)208 out, err = capfd.readouterr()

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 209why, one step back
209 assert out == f"{text}\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 210–212why, one step back
210
211 click.echo(styled_text, color=True)212 out, err = capfd.readouterr()

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 213why, one step back
213 assert out == f"{styled_text}\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 214–217why, one step back
214
215 isatty = True216 click.echo(styled_text)217 out, err = capfd.readouterr()

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 218why, one step back
218 assert out == f"{styled_text}\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 219–222why, one step back
219
220 isatty = False221 click.echo(styled_text)222 out, err = capfd.readouterr()

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 223why, one step back
223 assert out == f"{text}\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 224–225why, one step back
224
225

Merge pull request #1517 from stgraham2000/masterDavid Lordbefore adoption801a29f

lines 226–232why, one step back
226def test_prompt_cast_default(capfd, monkeypatch):
227 monkeypatch.setattr(sys, "stdin", StringIO("\n"))228 value = click.prompt("value", default="100", type=int)229 capfd.readouterr()230 assert type(value) is int231232

Merge branch '7.x'David Lordbefore adoption7239794

line 233why, one step back
233@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 234–236why, one step back
234def test_echo_writing_to_standard_error(capfd, monkeypatch):
235 def emulate_input(text):236 """Emulate keyboard input."""

Merge branch '7.x'David Lordbefore adoption7239794

line 237why, one step back
237 monkeypatch.setattr(sys, "stdin", StringIO(text))

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 238why, one step back
238

Merge branch '7.x'David Lordbefore adoption7239794

line 239why, one step back
239 click.echo("Echo to standard output")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 240why, one step back
240 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 241–242why, one step back
241 assert out == "Echo to standard output\n"
242 assert err == ""

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 243why, one step back
243

Merge branch '7.x'David Lordbefore adoption7239794

line 244why, one step back
244 click.echo("Echo to standard error", err=True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 245why, one step back
245 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 246–247why, one step back
246 assert out == ""
247 assert err == "Echo to standard error\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 248why, one step back
248

Merge branch '7.x'David Lordbefore adoption7239794

lines 249–250why, one step back
249 emulate_input("asdlkj\n")
250 click.prompt("Prompt to stdin")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 251why, one step back
251 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 252–253why, one step back
252 assert out == "Prompt to stdin: "
253 assert err == ""

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 254why, one step back
254

Merge branch '7.x'David Lordbefore adoption7239794

lines 255–256why, one step back
255 emulate_input("asdlkj\n")
256 click.prompt("Prompt to stderr", err=True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 257why, one step back
257 out, err = capfd.readouterr()

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 258–259why, one step back
258 assert out == " "
259 assert err == "Prompt to stderr:"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 260why, one step back
260

Merge branch '7.x'David Lordbefore adoption7239794

lines 261–262why, one step back
261 emulate_input("y\n")
262 click.confirm("Prompt to stdin")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 263why, one step back
263 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 264–265why, one step back
264 assert out == "Prompt to stdin [y/N]: "
265 assert err == ""

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 266why, one step back
266

Merge branch '7.x'David Lordbefore adoption7239794

lines 267–268why, one step back
267 emulate_input("y\n")
268 click.confirm("Prompt to stderr", err=True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 269why, one step back
269 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 270–271why, one step back
270 assert out == ""
271 assert err == "Prompt to stderr [y/N]: "

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 272why, one step back
272

Merge branch '7.x'David Lordbefore adoption7239794

lines 273–274why, one step back
273 monkeypatch.setattr(click.termui, "isatty", lambda x: True)
274 monkeypatch.setattr(click.termui, "getchar", lambda: " ")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 275why, one step back
275

Merge branch '7.x'David Lordbefore adoption7239794

line 276why, one step back
276 click.pause("Pause to stdout")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 277why, one step back
277 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 278–279why, one step back
278 assert out == "Pause to stdout\n"
279 assert err == ""

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 280why, one step back
280

Merge branch '7.x'David Lordbefore adoption7239794

line 281why, one step back
281 click.pause("Pause to stderr", err=True)

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 282why, one step back
282 out, err = capfd.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

lines 283–284why, one step back
283 assert out == ""
284 assert err == "Pause to stderr\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 285–286why, one step back
285
286

Merge pull request #1591 from andy-maier/andy/fix-unsupportedoperationDavid Lordbefore adoption090ff26

lines 287–292why, one step back
287def test_echo_with_capsys(capsys):
288 click.echo("Capture me.")289 out, err = capsys.readouterr()290 assert out == "Capture me.\n"291292

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 293why, one step back
293def test_open_file(runner):

Merge branch '7.x'David Lordbefore adoption2269786

line 294why, one step back
294 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 295why, one step back
295 @click.argument("filename")

Merge branch '7.x'David Lordbefore adoption2269786

lines 296–299why, one step back
296 def cli(filename):
297 with click.open_file(filename) as f:298 click.echo(f.read())299

Merge branch '7.x'David Lordbefore adoption7239794

line 300why, one step back
300 click.echo("meep")

Merge branch '7.x'David Lordbefore adoption2269786

line 301why, one step back
301

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 302why, one step back
302 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 303–304why, one step back
303 with open("hello.txt", "w") as f:
304 f.write("Cool stuff")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 305why, one step back
305

Merge branch '7.x'David Lordbefore adoption7239794

line 306why, one step back
306 result = runner.invoke(cli, ["hello.txt"])

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 307why, one step back
307 assert result.exception is None

Merge branch '7.x'David Lordbefore adoption7239794

line 308why, one step back
308 assert result.output == "Cool stuff\nmeep\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 309why, one step back
309

Merge branch '7.x'David Lordbefore adoption7239794

line 310why, one step back
310 result = runner.invoke(cli, ["-"], input="foobar")

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

line 311why, one step back
311 assert result.exception is None

Merge branch '7.x'David Lordbefore adoption7239794

line 312why, one step back
312 assert result.output == "foobar\nmeep\n"

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 313–314why, one step back
313
314

Merge branch '7.x'David Lordbefore adoption2269786

lines 315–352why, one step back
315def test_open_file_ignore_errors_stdin(runner):
316 @click.command()317 @click.argument("filename")318 def cli(filename):319 with click.open_file(filename, errors="ignore") as f:320 click.echo(f.read())321322 result = runner.invoke(cli, ["-"], input=os.urandom(16))323 assert result.exception is None324325326def test_open_file_respects_ignore(runner):327 with runner.isolated_filesystem():328 with open("test.txt", "w") as f:329 f.write("Hello world!")330331 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:332 assert f.errors == "ignore"333334335def test_open_file_ignore_invalid_utf8(runner):336 with runner.isolated_filesystem():337 with open("test.txt", "wb") as f:338 f.write(b"\xe2\x28\xa1")339340 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:341 f.read()342343344def test_open_file_ignore_no_encoding(runner):345 with runner.isolated_filesystem():346 with open("test.bin", "wb") as f:347 f.write(os.urandom(16))348349 with click.open_file("test.bin", errors="ignore") as f:350 f.read()351352

Merge branch '7.x'David Lordbefore adoption7239794

lines 353–354why, one step back
353@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")
354@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])

Merge branch '7.x'David Lordbefore adoption2269786

lines 355–356why, one step back
355def test_open_file_atomic_permissions_existing_file(runner, permissions):
356 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

lines 357–359why, one step back
357 with open("existing.txt", "w") as f:
358 f.write("content")359 os.chmod("existing.txt", permissions)

Merge branch '7.x'David Lordbefore adoption2269786

lines 360–361why, one step back
360
361 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 362why, one step back
362 @click.argument("filename")

Merge branch '7.x'David Lordbefore adoption2269786

line 363why, one step back
363 def cli(filename):

Merge branch '7.x'David Lordbefore adoption7239794

line 364why, one step back
364 click.open_file(filename, "w", atomic=True).close()

Merge branch '7.x'David Lordbefore adoption2269786

line 365why, one step back
365

Merge branch '7.x'David Lordbefore adoption7239794

line 366why, one step back
366 result = runner.invoke(cli, ["existing.txt"])

Merge branch '7.x'David Lordbefore adoption2269786

line 367why, one step back
367 assert result.exception is None

Merge branch '7.x'David Lordbefore adoption7239794

line 368why, one step back
368 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissions

Merge branch '7.x'David Lordbefore adoption2269786

lines 369–370why, one step back
369
370

Merge branch '7.x'David Lordbefore adoption7239794

line 371why, one step back
371@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")

Merge branch '7.x'David Lordbefore adoption2269786

lines 372–373why, one step back
372def test_open_file_atomic_permissions_new_file(runner):
373 with runner.isolated_filesystem():

Merge branch '7.x'David Lordbefore adoption7239794

line 374why, one step back
374

Merge branch '7.x'David Lordbefore adoption2269786

line 375why, one step back
375 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 376why, one step back
376 @click.argument("filename")

Merge branch '7.x'David Lordbefore adoption2269786

line 377why, one step back
377 def cli(filename):

Merge branch '7.x'David Lordbefore adoption7239794

line 378why, one step back
378 click.open_file(filename, "w", atomic=True).close()

Merge branch '7.x'David Lordbefore adoption2269786

lines 379–381why, one step back
379
380 # Create a test file to get the expected permissions for new files381 # according to the current umask.

Merge branch '7.x'David Lordbefore adoption7239794

line 382why, one step back
382 with open("test.txt", "w"):

Merge branch '7.x'David Lordbefore adoption2269786

line 383why, one step back
383 pass

Merge branch '7.x'David Lordbefore adoption7239794

line 384why, one step back
384 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)

Merge branch '7.x'David Lordbefore adoption2269786

line 385why, one step back
385

Merge branch '7.x'David Lordbefore adoption7239794

line 386why, one step back
386 result = runner.invoke(cli, ["new.txt"])

Merge branch '7.x'David Lordbefore adoption2269786

line 387why, one step back
387 assert result.exception is None

Merge branch '7.x'David Lordbefore adoption7239794

line 388why, one step back
388 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissions

Merge branch '7.x'David Lordbefore adoption2269786

lines 389–390why, one step back
389
390

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 391–392why, one step back
391def test_iter_keepopenfile(tmpdir):
392 expected = list(map(str, range(10)))

Merge branch '7.x'David Lordbefore adoption7239794

line 393why, one step back
393 p = tmpdir.mkdir("testdir").join("testfile")

Merge branch '7.x'David Lordbefore adoption2269786

line 394why, one step back
394 p.write("\n".join(expected))

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 395–401why, one step back
395 with p.open() as f:
396 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):397 assert e_line == a_line.strip()398399400def test_iter_lazyfile(tmpdir):401 expected = list(map(str, range(10)))

Merge branch '7.x'David Lordbefore adoption7239794

line 402why, one step back
402 p = tmpdir.mkdir("testdir").join("testfile")

Merge branch '7.x'David Lordbefore adoption2269786

line 403why, one step back
403 p.write("\n".join(expected))

Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c

lines 404–407why, one step back
404 with p.open() as f:
405 with click.utils.LazyFile(f.name) as lf:406 for e_line, a_line in zip(expected, lf):407 assert e_line == a_line.strip()

Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba

lines 408–436why, one step back
408
409410class MockMain:411 __slots__ = "__package__"412413 def __init__(self, package_name):414 self.__package__ = package_name415416417@pytest.mark.parametrize(418 ("path", "main", "expected"),419 [420 ("example.py", None, "example.py"),421 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),422 ("example", None, "example"),423 (424 str(pathlib.Path("example/__main__.py")),425 MockMain(".example"),426 "python -m example",427 ),428 (429 str(pathlib.Path("example/cli.py")),430 MockMain(".example"),431 "python -m example.cli",432 ),433 ],434)435def test_detect_program_name(path, main, expected):436 assert click.utils._detect_program_name(path, _main=main) == expected

Merge pull request #1830 from pallets/windows-globDavid Lordbefore adoption9330dba

lines 437–446why, one step back
437
438439def test_expand_args(monkeypatch):440 user = os.path.expanduser("~")441 assert user in click.utils._expand_args(["~"])442 monkeypatch.setenv("CLICK_TEST", "hello")443 assert "hello" in click.utils._expand_args(["$CLICK_TEST"])444 assert "setup.cfg" in click.utils._expand_args(["*.cfg"])445 assert os.path.join("docs", "conf.py") in click.utils._expand_args(["**/conf.py"])446 assert "*.not-found" in click.utils._expand_args(["*.not-found"])

Merge pull request #1850 from janLuke/fix/make-default-short-helpDavid Lordbefore adoption82a83fd

lines 447–482why, one step back
447
448449@pytest.mark.parametrize(450 ("value", "max_length", "expect"),451 [452 pytest.param("", 10, "", id="empty"),453 pytest.param("123 567 90", 10, "123 567 90", id="equal length, no dot"),454 pytest.param("123 567 9. aaaa bbb", 10, "123 567 9.", id="sentence < max"),455 pytest.param("123 567\n\n 9. aaaa bbb", 10, "123 567", id="paragraph < max"),456 pytest.param("123 567 90123.", 10, "123 567...", id="truncate"),457 pytest.param("123 5678 xxxxxx", 10, "123...", id="length includes suffix"),458 pytest.param(459 "token in ~/.netrc ciao ciao",460 20,461 "token in ~/.netrc...",462 id="ignore dot in word",463 ),464 ],465)466@pytest.mark.parametrize(467 "alter",468 [469 pytest.param(None, id=""),470 pytest.param(471 lambda text: "\n\b\n" + " ".join(text.split(" ")) + "\n", id="no-wrap mark"472 ),473 ],474)475def test_make_default_short_help(value, max_length, alter, expect):476 assert len(expect) <= max_length477478 if alter:479 value = alter(value)480481 out = click.utils.make_default_short_help(value, max_length)482 assert out == expect