Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

395 lines · 11.8 KiB · Python

Last changed by

apply flake8David Lordbefore adoption5f75f9e

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 pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

line 2why, one step back
2import stat

Fix typo (#817)scottbeldenbefore adoption950463b

lines 3–7why, one step back
3import sys
45import pytest67import click._termui_impl

apply reorder-python-importsDavid Lordbefore adoption488739d

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 10–12why, one step back
10
1112def test_echo(runner):

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

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

apply blackDavid Lordbefore adoption93ba3ba

lines 14–15why, one step back
14 click.echo(u"\N{SNOWMAN}")
15 click.echo(b"\x44\x44")

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 21–25why, one step back
21
22 # If we are in Python 2, we expect that writing bytes into a string io23 # does not do anything crazy. In Python 324 if sys.version_info[0] == 2:25 import StringIO

apply blackDavid Lordbefore adoption93ba3ba

line 26why, one step back
26

Fix typo (#817)scottbeldenbefore adoption950463b

lines 27–28why, one step back
27 sys.stdout = x = StringIO.StringIO()
28 try:

apply blackDavid Lordbefore adoption93ba3ba

line 29why, one step back
29 click.echo("\xf6")

Fix typo (#817)scottbeldenbefore adoption950463b

lines 30–31why, one step back
30 finally:
31 sys.stdout = sys.__stdout__

apply blackDavid Lordbefore adoption93ba3ba

line 32why, one step back
32 assert x.getvalue() == "\xf6\n"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 33–36why, one step back
33
34 # And in any case, if wrapped, we expect bytes to survive.35 @click.command()36 def cli():

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

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

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 46–49why, one step back
46
4748def test_echo_custom_file():49 import io

apply blackDavid Lordbefore adoption93ba3ba

line 50why, one step back
50

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 54–55why, one step back
54
55

Merge pull request #1299 from sekenned/7.xDavid Lordbefore adoption278a054

lines 56–85why, one step back
56@pytest.mark.parametrize(
57 ("styles", "ref"),58 [59 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),60 ({"fg": "red"}, "\x1b[31mx y\x1b[0m"),61 ({"fg": "green"}, "\x1b[32mx y\x1b[0m"),62 ({"fg": "yellow"}, "\x1b[33mx y\x1b[0m"),63 ({"fg": "blue"}, "\x1b[34mx y\x1b[0m"),64 ({"fg": "magenta"}, "\x1b[35mx y\x1b[0m"),65 ({"fg": "cyan"}, "\x1b[36mx y\x1b[0m"),66 ({"fg": "white"}, "\x1b[37mx y\x1b[0m"),67 ({"bg": "black"}, "\x1b[40mx y\x1b[0m"),68 ({"bg": "red"}, "\x1b[41mx y\x1b[0m"),69 ({"bg": "green"}, "\x1b[42mx y\x1b[0m"),70 ({"bg": "yellow"}, "\x1b[43mx y\x1b[0m"),71 ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),72 ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),73 ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),74 ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),75 ({"blink": True}, "\x1b[5mx y\x1b[0m"),76 ({"underline": True}, "\x1b[4mx y\x1b[0m"),77 ({"bold": True}, "\x1b[1mx y\x1b[0m"),78 ({"dim": True}, "\x1b[2mx y\x1b[0m"),79 ],80)81def test_styling(styles, ref):82 assert click.style("x y", **styles) == ref83 assert click.unstyle(ref) == "x y"8485

apply flake8David Lordbefore adoption5f75f9e

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

Merge pull request #1299 from sekenned/7.xDavid Lordbefore adoption278a054

lines 87–89why, one step back
87def test_unstyle_other_ansi(text, expect):
88 assert click.unstyle(text) == expect89

Fix typo (#817)scottbeldenbefore adoption950463b

lines 90–91why, one step back
90
91def test_filename_formatting():

apply blackDavid Lordbefore adoption93ba3ba

lines 92–95why, one step back
92 assert click.format_filename(b"foo.txt") == "foo.txt"
93 assert click.format_filename(b"/x/foo.txt") == "/x/foo.txt"94 assert click.format_filename(u"/x/foo.txt") == "/x/foo.txt"95 assert click.format_filename(u"/x/foo.txt", shorten=True) == "foo.txt"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 96–98why, one step back
96
97 # filesystem encoding on windows permits this.98 if not WIN:

apply blackDavid Lordbefore adoption93ba3ba

lines 99–101why, one step back
99 assert (
100 click.format_filename(b"/x/foo\xff.txt", shorten=True) == u"foo\ufffd.txt"101 )

Fix typo (#817)scottbeldenbefore adoption950463b

lines 102–106why, one step back
102
103104def test_prompts(runner):105 @click.command()106 def test():

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 109why, one step back
109 else:

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 111why, one step back
111

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 115why, one step back
115

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 119why, one step back
119

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 123–125why, one step back
123
124 @click.command()125 def test_no():

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 128why, one step back
128 else:

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 130why, one step back
130

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 134why, one step back
134

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 138why, one step back
138

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 142–143why, one step back
142
143

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 145–148why, one step back
145def test_prompts_abort(monkeypatch, capsys):
146 def f(_):147 raise KeyboardInterrupt()148

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 150–151why, one step back
150
151 try:

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 155–156why, one step back
155
156 out, err = capsys.readouterr()

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 158–159why, one step back
158
159

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

apply blackDavid Lordbefore adoption93ba3ba

lines 161–164why, one step back
161 yield "a"
162 yield "b"163 yield "c"164 yield "abc"

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 165–166why, one step back
165
166

apply blackDavid Lordbefore adoption93ba3ba

lines 167–181why, one step back
167@pytest.mark.skipif(WIN, reason="Different behavior on windows.")
168@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])169@pytest.mark.parametrize(170 "test",171 [172 # We need lambda here, because pytest will173 # reuse the parameters, and then the generators174 # are already used and will not yield anymore175 ("just text\n", lambda: "just text"),176 ("iterable\n", lambda: ["itera", "ble"]),177 ("abcabc\n", lambda: _test_gen_func),178 ("abcabc\n", lambda: _test_gen_func()),179 ("012345\n", lambda: (c for c in range(6))),180 ],181)

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 185–190why, one step back
185
186 expected_output = test[0]187 test_input = test[1]()188189 click.echo_via_pager(test_input)190

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 193–194why, one step back
193
194

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 199why, one step back
199

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 203–205why, one step back
203
204 click.echo(styled_text, color=False)205 out, err = capfd.readouterr()

apply blackDavid Lordbefore adoption93ba3ba

line 206why, one step back
206 assert out == text + "\n"

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

line 210why, one step back
210 assert out == styled_text + "\n"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 211–214why, one step back
211
212 isatty = True213 click.echo(styled_text)214 out, err = capfd.readouterr()

apply blackDavid Lordbefore adoption93ba3ba

line 215why, one step back
215 assert out == styled_text + "\n"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 216–219why, one step back
216
217 isatty = False218 click.echo(styled_text)219 out, err = capfd.readouterr()

apply blackDavid Lordbefore adoption93ba3ba

line 220why, one step back
220 assert out == text + "\n"

Fix typo (#817)scottbeldenbefore adoption950463b

lines 221–222why, one step back
221
222

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 224–230why, one step back
224def test_echo_writing_to_standard_error(capfd, monkeypatch):
225 def emulate_input(text):226 """Emulate keyboard input."""227 if sys.version_info[0] == 2:228 from StringIO import StringIO229 else:230 from io import StringIO

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 232why, one step back
232

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 237why, one step back
237

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 242why, one step back
242

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 248why, one step back
248

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 254why, one step back
254

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 260why, one step back
260

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 266why, one step back
266

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 269why, one step back
269

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 274why, one step back
274

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 279–281why, one step back
279
280281def test_open_file(runner):

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

line 289why, one step back
289

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 293why, one step back
293

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 297why, one step back
297

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

lines 301–302why, one step back
301
302

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

lines 303–340why, one step back
303def test_open_file_ignore_errors_stdin(runner):
304 @click.command()305 @click.argument("filename")306 def cli(filename):307 with click.open_file(filename, errors="ignore") as f:308 click.echo(f.read())309310 result = runner.invoke(cli, ["-"], input=os.urandom(16))311 assert result.exception is None312313314def test_open_file_respects_ignore(runner):315 with runner.isolated_filesystem():316 with open("test.txt", "w") as f:317 f.write("Hello world!")318319 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:320 assert f.errors == "ignore"321322323def test_open_file_ignore_invalid_utf8(runner):324 with runner.isolated_filesystem():325 with open("test.txt", "wb") as f:326 f.write(b"\xe2\x28\xa1")327328 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:329 f.read()330331332def test_open_file_ignore_no_encoding(runner):333 with runner.isolated_filesystem():334 with open("test.bin", "wb") as f:335 f.write(os.urandom(16))336337 with click.open_file("test.bin", errors="ignore") as f:338 f.read()339340

apply blackDavid Lordbefore adoption93ba3ba

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

apply flake8David Lordbefore adoption5f75f9e

line 342why, one step back
342@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

line 353why, one step back
353

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

lines 357–358why, one step back
357
358

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

line 362why, one step back
362

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

line 371why, one step back
371 pass

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

line 373why, one step back
373

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6

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

apply blackDavid Lordbefore adoption93ba3ba

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

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

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

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 386–389why, one step back
386
387388def test_iter_lazyfile(tmpdir):389 expected = list(map(str, range(10)))

apply blackDavid Lordbefore adoption93ba3ba

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

Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666

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

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

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