Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

423 lines · 12.5 KiB · Python

Last changed by

Merge pull request #1517 from stgraham2000/masterDavid Lordbefore adoption801a29f

History of this file

Take it

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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.

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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–77why, one step back
67 ({"blink": True}, "\x1b[5mx y\x1b[0m"),
68 ({"underline": True}, "\x1b[4mx y\x1b[0m"),69 ({"bold": True}, "\x1b[1mx y\x1b[0m"),70 ({"dim": True}, "\x1b[2mx y\x1b[0m"),71 ],72)73def test_styling(styles, ref):74 assert click.style("x y", **styles) == ref75 assert click.unstyle(ref) == "x y"7677

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 81–83why, one step back
81
8283def test_filename_formatting():

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 88–90why, one step back
88
89 # filesystem encoding on windows permits this.90 if not WIN:

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 92–96why, one step back
92
9394def test_prompts(runner):95 @click.command()96 def test():

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 99why, one step back
99 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 101why, one step back
101

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 105why, one step back
105

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 109why, one step back
109

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 113–115why, one step back
113
114 @click.command()115 def test_no():

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 118why, one step back
118 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 120why, one step back
120

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 124why, one step back
124

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 128why, one step back
128

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 132–133why, one step back
132
133

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 135–138why, one step back
135def test_prompts_abort(monkeypatch, capsys):
136 def f(_):137 raise KeyboardInterrupt()138

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 140–141why, one step back
140
141 try:

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

line 144why, one step back
144 click.echo("Screw you.")

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 145–146why, one step back
145
146 out, err = capsys.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

line 147why, one step back
147 assert out == "Password: \nScrew you.\n"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 148–150why, one step back
148
149150def _test_gen_func():

Merge branch '7.x'David Lordbefore adoption7239794

lines 151–154why, one step back
151 yield "a"
152 yield "b"153 yield "c"154 yield "abc"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 155–156why, one step back
155
156

Merge branch '7.x'David Lordbefore adoption7239794

lines 157–171why, one step back
157@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
158@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])159@pytest.mark.parametrize(160 "test",161 [162 # We need lambda here, because pytest will163 # reuse the parameters, and then the generators164 # are already used and will not yield anymore165 ("just text\n", lambda: "just text"),166 ("iterable\n", lambda: ["itera", "ble"]),167 ("abcabc\n", lambda: _test_gen_func),168 ("abcabc\n", lambda: _test_gen_func()),169 ("012345\n", lambda: (c for c in range(6))),170 ],171)

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 175–184why, one step back
175
176 expected_output = test[0]177 test_input = test[1]()178179 click.echo_via_pager(test_input)180181 out, err = capfd.readouterr()182 assert out == expected_output183184

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 189why, one step back
189

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 193–195why, one step back
193
194 click.echo(styled_text, color=False)195 out, err = capfd.readouterr()

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 197–199why, one step back
197
198 click.echo(styled_text, color=True)199 out, err = capfd.readouterr()

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 201–204why, one step back
201
202 isatty = True203 click.echo(styled_text)204 out, err = capfd.readouterr()

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 206–209why, one step back
206
207 isatty = False208 click.echo(styled_text)209 out, err = capfd.readouterr()

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

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 211–212why, one step back
211
212

Merge pull request #1517 from stgraham2000/masterDavid Lordbefore adoption801a29f

lines 213–219why, one step back
213def test_prompt_cast_default(capfd, monkeypatch):
214 monkeypatch.setattr(sys, "stdin", StringIO("\n"))215 value = click.prompt("value", default="100", type=int)216 capfd.readouterr()217 assert type(value) is int218219

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 221–223why, one step back
221def test_echo_writing_to_standard_error(capfd, monkeypatch):
222 def emulate_input(text):223 """Emulate keyboard input."""

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 225why, one step back
225

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 230why, one step back
230

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 235why, one step back
235

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 241why, one step back
241

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 247why, one step back
247

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 253why, one step back
253

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 259why, one step back
259

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 262why, one step back
262

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 267why, one step back
267

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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 == "Pause to stderr\n"

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 272–273why, one step back
272
273

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

lines 274–279why, one step back
274def test_echo_with_capsys(capsys):
275 click.echo("Capture me.")276 out, err = capsys.readouterr()277 assert out == "Capture me.\n"278279

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 283–286why, one step back
283 def cli(filename):
284 with click.open_file(filename) as f:285 click.echo(f.read())286

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 288why, one step back
288

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 292why, one step back
292

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

line 296why, one step back
296

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 300–301why, one step back
300
301

Merge branch '7.x'David Lordbefore adoption2269786

lines 302–339why, one step back
302def test_open_file_ignore_errors_stdin(runner):
303 @click.command()304 @click.argument("filename")305 def cli(filename):306 with click.open_file(filename, errors="ignore") as f:307 click.echo(f.read())308309 result = runner.invoke(cli, ["-"], input=os.urandom(16))310 assert result.exception is None311312313def test_open_file_respects_ignore(runner):314 with runner.isolated_filesystem():315 with open("test.txt", "w") as f:316 f.write("Hello world!")317318 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:319 assert f.errors == "ignore"320321322def test_open_file_ignore_invalid_utf8(runner):323 with runner.isolated_filesystem():324 with open("test.txt", "wb") as f:325 f.write(b"\xe2\x28\xa1")326327 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:328 f.read()329330331def test_open_file_ignore_no_encoding(runner):332 with runner.isolated_filesystem():333 with open("test.bin", "wb") as f:334 f.write(os.urandom(16))335336 with click.open_file("test.bin", errors="ignore") as f:337 f.read()338339

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 347–348why, one step back
347
348 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 352why, one step back
352

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 356–357why, one step back
356
357

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

line 361why, one step back
361

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 366–368why, one step back
366
367 # Create a test file to get the expected permissions for new files368 # according to the current umask.

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 370why, one step back
370 pass

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 372why, one step back
372

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 376–377why, one step back
376
377

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 382–388why, one step back
382 with p.open() as f:
383 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):384 assert e_line == a_line.strip()385386387def test_iter_lazyfile(tmpdir):388 expected = list(map(str, range(10)))

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge pull request #1040 from jcrotts/masterZachery Birbefore adoptioncd90e83

lines 391–394why, one step back
391 with p.open() as f:
392 with click.utils.LazyFile(f.name) as lf:393 for e_line, a_line in zip(expected, lf):394 assert e_line == a_line.strip()

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

lines 395–423why, one step back
395
396397class MockMain:398 __slots__ = "__package__"399400 def __init__(self, package_name):401 self.__package__ = package_name402403404@pytest.mark.parametrize(405 ("path", "main", "expected"),406 [407 ("example.py", None, "example.py"),408 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),409 ("example", None, "example"),410 (411 str(pathlib.Path("example/__main__.py")),412 MockMain(".example"),413 "python -m example",414 ),415 (416 str(pathlib.Path("example/cli.py")),417 MockMain(".example"),418 "python -m example.cli",419 ),420 ],421)422def test_detect_program_name(path, main, expected):423 assert click.utils._detect_program_name(path, _main=main) == expected