Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

414 lines · 12.2 KiB · Python

Last changed by

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

History of this file

Take it

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 12–14why, one step back
12
1314def test_echo(runner):

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 15why, one step back
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")

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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"

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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.

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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"

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 31–32why, one step back
31
32 # Ensure we do not strip for bytes.

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 33why, one step back
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"

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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"

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 44–45why, one step back
44
45

Merge branch '7.x'David Lordbefore adoption2269786

lines 46–75why, 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"),65 ({"blink": True}, "\x1b[5mx y\x1b[0m"),66 ({"underline": True}, "\x1b[4mx y\x1b[0m"),67 ({"bold": True}, "\x1b[1mx y\x1b[0m"),68 ({"dim": True}, "\x1b[2mx y\x1b[0m"),69 ],70)71def test_styling(styles, ref):72 assert click.style("x y", **styles) == ref73 assert click.unstyle(ref) == "x y"7475

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 79–81why, one step back
79
8081def test_filename_formatting():

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 90–94why, one step back
90
9192def test_prompts(runner):93 @click.command()94 def test():

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 97why, one step back
97 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 99why, one step back
99

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 103why, one step back
103

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 107why, one step back
107

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 116why, one step back
116 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 118why, one step back
118

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 122why, one step back
122

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 126why, one step back
126

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 130–131why, one step back
130
131

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 138–139why, one step back
138
139 try:

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 143–144why, one step back
143
144 out, err = capsys.readouterr()

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 146–147why, one step back
146
147

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

line 148why, one step back
148def _test_gen_func():

Merge branch '7.x'David Lordbefore adoption7239794

lines 149–152why, one step back
149 yield "a"
150 yield "b"151 yield "c"152 yield "abc"

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 153–154why, one step back
153
154

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 173–178why, one step back
173
174 expected_output = test[0]175 test_input = test[1]()176177 click.echo_via_pager(test_input)178

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

line 180why, one step back
180 assert out == expected_output

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 181–182why, one step back
181
182

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 187why, one step back
187

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 199–202why, one step back
199
200 isatty = True201 click.echo(styled_text)202 out, err = capfd.readouterr()

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 204–207why, one step back
204
205 isatty = False206 click.echo(styled_text)207 out, err = capfd.readouterr()

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

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 209–210why, one step back
209
210

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 212–214why, one step back
212def test_echo_writing_to_standard_error(capfd, monkeypatch):
213 def emulate_input(text):214 """Emulate keyboard input."""

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 216why, one step back
216

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 221why, one step back
221

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 226why, one step back
226

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 232why, one step back
232

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 238why, one step back
238

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 244why, one step back
244

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 250why, one step back
250

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 253why, one step back
253

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 258why, one step back
258

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 263–264why, one step back
263
264

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

lines 265–270why, one step back
265def test_echo_with_capsys(capsys):
266 click.echo("Capture me.")267 out, err = capsys.readouterr()268 assert out == "Capture me.\n"269270

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 274–277why, one step back
274 def cli(filename):
275 with click.open_file(filename) as f:276 click.echo(f.read())277

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 279why, one step back
279

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 283why, one step back
283

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

line 287why, one step back
287

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 291–292why, one step back
291
292

Merge branch '7.x'David Lordbefore adoption2269786

lines 293–330why, one step back
293def test_open_file_ignore_errors_stdin(runner):
294 @click.command()295 @click.argument("filename")296 def cli(filename):297 with click.open_file(filename, errors="ignore") as f:298 click.echo(f.read())299300 result = runner.invoke(cli, ["-"], input=os.urandom(16))301 assert result.exception is None302303304def test_open_file_respects_ignore(runner):305 with runner.isolated_filesystem():306 with open("test.txt", "w") as f:307 f.write("Hello world!")308309 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:310 assert f.errors == "ignore"311312313def test_open_file_ignore_invalid_utf8(runner):314 with runner.isolated_filesystem():315 with open("test.txt", "wb") as f:316 f.write(b"\xe2\x28\xa1")317318 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:319 f.read()320321322def test_open_file_ignore_no_encoding(runner):323 with runner.isolated_filesystem():324 with open("test.bin", "wb") as f:325 f.write(os.urandom(16))326327 with click.open_file("test.bin", errors="ignore") as f:328 f.read()329330

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 338–339why, one step back
338
339 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 343why, one step back
343

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 347–348why, one step back
347
348

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

line 352why, one step back
352

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 357–359why, one step back
357
358 # Create a test file to get the expected permissions for new files359 # according to the current umask.

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 361why, one step back
361 pass

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 363why, one step back
363

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 367–368why, one step back
367
368

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498

lines 373–375why, one step back
373 with p.open() as f:
374 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):375 assert e_line == a_line.strip()

Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1

lines 376–379why, one step back
376
377378def test_iter_lazyfile(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 #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498

lines 382–385why, one step back
382 with p.open() as f:
383 with click.utils.LazyFile(f.name) as lf:384 for e_line, a_line in zip(expected, lf):385 assert e_line == a_line.strip()

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

lines 386–414why, one step back
386
387388class MockMain:389 __slots__ = "__package__"390391 def __init__(self, package_name):392 self.__package__ = package_name393394395@pytest.mark.parametrize(396 ("path", "main", "expected"),397 [398 ("example.py", None, "example.py"),399 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),400 ("example", None, "example"),401 (402 str(pathlib.Path("example/__main__.py")),403 MockMain(".example"),404 "python -m example",405 ),406 (407 str(pathlib.Path("example/cli.py")),408 MockMain(".example"),409 "python -m example.cli",410 ),411 ],412)413def test_detect_program_name(path, main, expected):414 assert click.utils._detect_program_name(path, _main=main) == expected