Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

384 lines · 11.5 KiB · Python

Last changed by

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

History of this file

Take it

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 1why, one step back
1import os

Merge branch '7.x'David Lordbefore adoption2269786

line 2why, one step back
2import stat

Fix typo (#817)scottbeldenbefore adoption950463b

line 3why, one step back
3import sys

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 5–8why, one step back
5
6import pytest78import click._termui_impl

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 11–13why, one step back
11
1213def test_echo(runner):

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

line 14why, one step back
14 with runner.isolation() as outstreams:

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

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 22why, one step back
22

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

line 32why, one step back
32 with runner.isolation() as outstreams:

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 35–38why, one step back
35
3637def test_echo_custom_file():38 import io

Merge branch '7.x'David Lordbefore adoption7239794

line 39why, one step back
39

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 43–44why, one step back
43
44

Merge branch '7.x'David Lordbefore adoption2269786

lines 45–74why, one step back
45@pytest.mark.parametrize(
46 ("styles", "ref"),47 [48 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),49 ({"fg": "red"}, "\x1b[31mx y\x1b[0m"),50 ({"fg": "green"}, "\x1b[32mx y\x1b[0m"),51 ({"fg": "yellow"}, "\x1b[33mx y\x1b[0m"),52 ({"fg": "blue"}, "\x1b[34mx y\x1b[0m"),53 ({"fg": "magenta"}, "\x1b[35mx y\x1b[0m"),54 ({"fg": "cyan"}, "\x1b[36mx y\x1b[0m"),55 ({"fg": "white"}, "\x1b[37mx y\x1b[0m"),56 ({"bg": "black"}, "\x1b[40mx y\x1b[0m"),57 ({"bg": "red"}, "\x1b[41mx y\x1b[0m"),58 ({"bg": "green"}, "\x1b[42mx y\x1b[0m"),59 ({"bg": "yellow"}, "\x1b[43mx y\x1b[0m"),60 ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),61 ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),62 ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),63 ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),64 ({"blink": True}, "\x1b[5mx y\x1b[0m"),65 ({"underline": True}, "\x1b[4mx y\x1b[0m"),66 ({"bold": True}, "\x1b[1mx y\x1b[0m"),67 ({"dim": True}, "\x1b[2mx y\x1b[0m"),68 ],69)70def test_styling(styles, ref):71 assert click.style("x y", **styles) == ref72 assert click.unstyle(ref) == "x y"7374

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 78–80why, one step back
78
7980def test_filename_formatting():

Merge branch '7.x'David Lordbefore adoption7239794

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 89–93why, one step back
89
9091def test_prompts(runner):92 @click.command()93 def test():

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 96why, one step back
96 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 98why, one step back
98

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 102why, one step back
102

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 106why, one step back
106

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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]: n\nno :(\n"

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 115why, one step back
115 else:

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 117why, one step back
117

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 121why, one step back
121

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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="n\n")

Fix typo (#817)scottbeldenbefore adoption950463b

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]: n\nno :(\n"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 129–130why, one step back
129
130

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 145–146why, one step back
145
146

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 152–153why, one step back
152
153

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 172–177why, one step back
172
173 expected_output = test[0]174 test_input = test[1]()175176 click.echo_via_pager(test_input)177

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 180–181why, one step back
180
181

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 186why, one step back
186

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 198–201why, one step back
198
199 isatty = True200 click.echo(styled_text)201 out, err = capfd.readouterr()

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 203–206why, one step back
203
204 isatty = False205 click.echo(styled_text)206 out, err = capfd.readouterr()

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 208–209why, one step back
208
209

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 215why, one step back
215

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 220why, one step back
220

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 225why, one step back
225

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 231why, one step back
231

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 237why, one step back
237

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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 == "Prompt to stdin [y/N]: "
242 assert err == ""

Fix typo (#817)scottbeldenbefore adoption950463b

line 243why, one step back
243

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 249why, one step back
249

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 252why, one step back
252

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 257why, one step back
257

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 262–263why, one step back
262
263

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 278why, one step back
278

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 282why, one step back
282

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 286why, one step back
286

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 290–291why, one step back
290
291

Merge branch '7.x'David Lordbefore adoption2269786

lines 292–329why, one step back
292def test_open_file_ignore_errors_stdin(runner):
293 @click.command()294 @click.argument("filename")295 def cli(filename):296 with click.open_file(filename, errors="ignore") as f:297 click.echo(f.read())298299 result = runner.invoke(cli, ["-"], input=os.urandom(16))300 assert result.exception is None301302303def test_open_file_respects_ignore(runner):304 with runner.isolated_filesystem():305 with open("test.txt", "w") as f:306 f.write("Hello world!")307308 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:309 assert f.errors == "ignore"310311312def test_open_file_ignore_invalid_utf8(runner):313 with runner.isolated_filesystem():314 with open("test.txt", "wb") as f:315 f.write(b"\xe2\x28\xa1")316317 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:318 f.read()319320321def test_open_file_ignore_no_encoding(runner):322 with runner.isolated_filesystem():323 with open("test.bin", "wb") as f:324 f.write(os.urandom(16))325326 with click.open_file("test.bin", errors="ignore") as f:327 f.read()328329

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 342why, one step back
342

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 346–347why, one step back
346
347

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

line 351why, one step back
351

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 360why, one step back
360 pass

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

line 362why, one step back
362

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 366–367why, one step back
366
367

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge branch '7.x'David Lordbefore adoption7239794

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

Merge branch '7.x'David Lordbefore adoption2269786

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

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

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