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 fileTake it
The walk stopped after 200 commits: lines older than 950463b point at it.
Fix typo (#817)scottbeldenbefore adoption950463b
line 1why, one step back1import osMerge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 2why, one step back2import statFix typo (#817)scottbeldenbefore adoption950463b
lines 3–7why, one step back3import sys45import pytest67import click._termui_implapply reorder-python-importsDavid Lordbefore adoption488739d
lines 8–9why, one step back8import click.utils9from click._compat import WINFix typo (#817)scottbeldenbefore adoption950463b
lines 10–12why, one step back101112def test_echo(runner):CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 13why, one step back13 with runner.isolation() as outstreams:apply blackDavid Lordbefore adoption93ba3ba
lines 14–15why, one step back14 click.echo(u"\N{SNOWMAN}")15 click.echo(b"\x44\x44")Fix typo (#817)scottbeldenbefore adoption950463b
line 16why, one step back16 click.echo(42, nl=False)apply blackDavid Lordbefore adoption93ba3ba
lines 17–20why, one step back17 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 back2122 # 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 StringIOapply blackDavid Lordbefore adoption93ba3ba
line 26why, one step back26Fix typo (#817)scottbeldenbefore adoption950463b
lines 27–28why, one step back27 sys.stdout = x = StringIO.StringIO()28 try:apply blackDavid Lordbefore adoption93ba3ba
line 29why, one step back29 click.echo("\xf6")Fix typo (#817)scottbeldenbefore adoption950463b
lines 30–31why, one step back30 finally:31 sys.stdout = sys.__stdout__apply blackDavid Lordbefore adoption93ba3ba
line 32why, one step back32 assert x.getvalue() == "\xf6\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 33–36why, one step back3334 # 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 back37 click.echo(b"\xf6")38Fix typo (#817)scottbeldenbefore adoption950463b
line 39why, one step back39 result = runner.invoke(cli, [])apply blackDavid Lordbefore adoption93ba3ba
line 40why, one step back40 assert result.stdout_bytes == b"\xf6\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 41–42why, one step back4142 # Ensure we do not strip for bytes.CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 43why, one step back43 with runner.isolation() as outstreams:apply blackDavid Lordbefore adoption93ba3ba
lines 44–45why, one step back44 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 back464748def test_echo_custom_file():49 import ioapply blackDavid Lordbefore adoption93ba3ba
line 50why, one step back50Fix typo (#817)scottbeldenbefore adoption950463b
line 51why, one step back51 f = io.StringIO()apply blackDavid Lordbefore adoption93ba3ba
lines 52–53why, one step back52 click.echo(u"hello", file=f)53 assert f.getvalue() == u"hello\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 54–55why, one step back5455Merge pull request #1299 from sekenned/7.xDavid Lordbefore adoption278a054
lines 56–85why, one step back56@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"8485apply flake8David Lordbefore adoption5f75f9e
line 86why, one step back86@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 back87def test_unstyle_other_ansi(text, expect):88 assert click.unstyle(text) == expect89Fix typo (#817)scottbeldenbefore adoption950463b
lines 90–91why, one step back9091def test_filename_formatting():apply blackDavid Lordbefore adoption93ba3ba
lines 92–95why, one step back92 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 back9697 # filesystem encoding on windows permits this.98 if not WIN:apply blackDavid Lordbefore adoption93ba3ba
lines 99–101why, one step back99 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 back102103104def test_prompts(runner):105 @click.command()106 def test():apply blackDavid Lordbefore adoption93ba3ba
lines 107–108why, one step back107 if click.confirm("Foo"):108 click.echo("yes!")Fix typo (#817)scottbeldenbefore adoption950463b
line 109why, one step back109 else:apply blackDavid Lordbefore adoption93ba3ba
line 110why, one step back110 click.echo("no :(")Fix typo (#817)scottbeldenbefore adoption950463b
line 111why, one step back111apply blackDavid Lordbefore adoption93ba3ba
line 112why, one step back112 result = runner.invoke(test, input="y\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 113why, one step back113 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 114why, one step back114 assert result.output == "Foo [y/N]: y\nyes!\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 115why, one step back115apply blackDavid Lordbefore adoption93ba3ba
line 116why, one step back116 result = runner.invoke(test, input="\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 117why, one step back117 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 118why, one step back118 assert result.output == "Foo [y/N]: \nno :(\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 119why, one step back119apply blackDavid Lordbefore adoption93ba3ba
line 120why, one step back120 result = runner.invoke(test, input="n\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 121why, one step back121 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 122why, one step back122 assert result.output == "Foo [y/N]: n\nno :(\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 123–125why, one step back123124 @click.command()125 def test_no():apply blackDavid Lordbefore adoption93ba3ba
lines 126–127why, one step back126 if click.confirm("Foo", default=True):127 click.echo("yes!")Fix typo (#817)scottbeldenbefore adoption950463b
line 128why, one step back128 else:apply blackDavid Lordbefore adoption93ba3ba
line 129why, one step back129 click.echo("no :(")Fix typo (#817)scottbeldenbefore adoption950463b
line 130why, one step back130apply blackDavid Lordbefore adoption93ba3ba
line 131why, one step back131 result = runner.invoke(test_no, input="y\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 132why, one step back132 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 133why, one step back133 assert result.output == "Foo [Y/n]: y\nyes!\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 134why, one step back134apply blackDavid Lordbefore adoption93ba3ba
line 135why, one step back135 result = runner.invoke(test_no, input="\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 136why, one step back136 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 137why, one step back137 assert result.output == "Foo [Y/n]: \nyes!\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 138why, one step back138apply blackDavid Lordbefore adoption93ba3ba
line 139why, one step back139 result = runner.invoke(test_no, input="n\n")Fix typo (#817)scottbeldenbefore adoption950463b
line 140why, one step back140 assert not result.exceptionapply blackDavid Lordbefore adoption93ba3ba
line 141why, one step back141 assert result.output == "Foo [Y/n]: n\nno :(\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 142–143why, one step back142143apply blackDavid Lordbefore adoption93ba3ba
line 144why, one step back144@pytest.mark.skipif(WIN, reason="Different behavior on windows.")Fix typo (#817)scottbeldenbefore adoption950463b
lines 145–148why, one step back145def test_prompts_abort(monkeypatch, capsys):146 def f(_):147 raise KeyboardInterrupt()148apply blackDavid Lordbefore adoption93ba3ba
line 149why, one step back149 monkeypatch.setattr("click.termui.hidden_prompt_func", f)Fix typo (#817)scottbeldenbefore adoption950463b
lines 150–151why, one step back150151 try:apply blackDavid Lordbefore adoption93ba3ba
line 152why, one step back152 click.prompt("Password", hide_input=True)Fix typo (#817)scottbeldenbefore adoption950463b
line 153why, one step back153 except click.Abort:apply blackDavid Lordbefore adoption93ba3ba
line 154why, one step back154 click.echo("Screw you.")Fix typo (#817)scottbeldenbefore adoption950463b
lines 155–156why, one step back155156 out, err = capsys.readouterr()apply blackDavid Lordbefore adoption93ba3ba
line 157why, one step back157 assert out == "Password: \nScrew you.\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 158–159why, one step back158159Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 160why, one step back160def _test_gen_func():apply blackDavid Lordbefore adoption93ba3ba
lines 161–164why, one step back161 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 back165166apply blackDavid Lordbefore adoption93ba3ba
lines 167–181why, one step back167@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 back182def test_echo_via_pager(monkeypatch, capfd, cat, test):apply blackDavid Lordbefore adoption93ba3ba
lines 183–184why, one step back183 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 back185186 expected_output = test[0]187 test_input = test[1]()188189 click.echo_via_pager(test_input)190Fix typo (#817)scottbeldenbefore adoption950463b
line 191why, one step back191 out, err = capfd.readouterr()Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 192why, one step back192 assert out == expected_outputFix typo (#817)scottbeldenbefore adoption950463b
lines 193–194why, one step back193194apply blackDavid Lordbefore adoption93ba3ba
line 195why, one step back195@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Fix typo (#817)scottbeldenbefore adoption950463b
lines 196–197why, one step back196def test_echo_color_flag(monkeypatch, capfd):197 isatty = Trueapply blackDavid Lordbefore adoption93ba3ba
line 198why, one step back198 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)Fix typo (#817)scottbeldenbefore adoption950463b
line 199why, one step back199apply blackDavid Lordbefore adoption93ba3ba
lines 200–202why, one step back200 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 back203204 click.echo(styled_text, color=False)205 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
line 206why, one step back206 assert out == text + "\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 207–209why, one step back207208 click.echo(styled_text, color=True)209 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
line 210why, one step back210 assert out == styled_text + "\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 211–214why, one step back211212 isatty = True213 click.echo(styled_text)214 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
line 215why, one step back215 assert out == styled_text + "\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 216–219why, one step back216217 isatty = False218 click.echo(styled_text)219 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
line 220why, one step back220 assert out == text + "\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 221–222why, one step back221222apply blackDavid Lordbefore adoption93ba3ba
line 223why, one step back223@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")Fix typo (#817)scottbeldenbefore adoption950463b
lines 224–230why, one step back224def 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 StringIOapply blackDavid Lordbefore adoption93ba3ba
line 231why, one step back231 monkeypatch.setattr(sys, "stdin", StringIO(text))Fix typo (#817)scottbeldenbefore adoption950463b
line 232why, one step back232apply blackDavid Lordbefore adoption93ba3ba
line 233why, one step back233 click.echo("Echo to standard output")Fix typo (#817)scottbeldenbefore adoption950463b
line 234why, one step back234 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 235–236why, one step back235 assert out == "Echo to standard output\n"236 assert err == ""Fix typo (#817)scottbeldenbefore adoption950463b
line 237why, one step back237apply blackDavid Lordbefore adoption93ba3ba
line 238why, one step back238 click.echo("Echo to standard error", err=True)Fix typo (#817)scottbeldenbefore adoption950463b
line 239why, one step back239 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 240–241why, one step back240 assert out == ""241 assert err == "Echo to standard error\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 242why, one step back242apply blackDavid Lordbefore adoption93ba3ba
lines 243–244why, one step back243 emulate_input("asdlkj\n")244 click.prompt("Prompt to stdin")Fix typo (#817)scottbeldenbefore adoption950463b
line 245why, one step back245 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 246–247why, one step back246 assert out == "Prompt to stdin: "247 assert err == ""Fix typo (#817)scottbeldenbefore adoption950463b
line 248why, one step back248apply blackDavid Lordbefore adoption93ba3ba
lines 249–250why, one step back249 emulate_input("asdlkj\n")250 click.prompt("Prompt to stderr", err=True)Fix typo (#817)scottbeldenbefore adoption950463b
line 251why, one step back251 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 252–253why, one step back252 assert out == ""253 assert err == "Prompt to stderr: "Fix typo (#817)scottbeldenbefore adoption950463b
line 254why, one step back254apply blackDavid Lordbefore adoption93ba3ba
lines 255–256why, one step back255 emulate_input("y\n")256 click.confirm("Prompt to stdin")Fix typo (#817)scottbeldenbefore adoption950463b
line 257why, one step back257 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 258–259why, one step back258 assert out == "Prompt to stdin [y/N]: "259 assert err == ""Fix typo (#817)scottbeldenbefore adoption950463b
line 260why, one step back260apply blackDavid Lordbefore adoption93ba3ba
lines 261–262why, one step back261 emulate_input("y\n")262 click.confirm("Prompt to stderr", err=True)Fix typo (#817)scottbeldenbefore adoption950463b
line 263why, one step back263 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 264–265why, one step back264 assert out == ""265 assert err == "Prompt to stderr [y/N]: "Fix typo (#817)scottbeldenbefore adoption950463b
line 266why, one step back266apply blackDavid Lordbefore adoption93ba3ba
lines 267–268why, one step back267 monkeypatch.setattr(click.termui, "isatty", lambda x: True)268 monkeypatch.setattr(click.termui, "getchar", lambda: " ")Fix typo (#817)scottbeldenbefore adoption950463b
line 269why, one step back269apply blackDavid Lordbefore adoption93ba3ba
line 270why, one step back270 click.pause("Pause to stdout")Fix typo (#817)scottbeldenbefore adoption950463b
line 271why, one step back271 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 272–273why, one step back272 assert out == "Pause to stdout\n"273 assert err == ""Fix typo (#817)scottbeldenbefore adoption950463b
line 274why, one step back274apply blackDavid Lordbefore adoption93ba3ba
line 275why, one step back275 click.pause("Pause to stderr", err=True)Fix typo (#817)scottbeldenbefore adoption950463b
line 276why, one step back276 out, err = capfd.readouterr()apply blackDavid Lordbefore adoption93ba3ba
lines 277–278why, one step back277 assert out == ""278 assert err == "Pause to stderr\n"Fix typo (#817)scottbeldenbefore adoption950463b
lines 279–281why, one step back279280281def test_open_file(runner):Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
line 282why, one step back282 @click.command()apply blackDavid Lordbefore adoption93ba3ba
line 283why, one step back283 @click.argument("filename")Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
lines 284–287why, one step back284 def cli(filename):285 with click.open_file(filename) as f:286 click.echo(f.read())287apply blackDavid Lordbefore adoption93ba3ba
line 288why, one step back288 click.echo("meep")Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
line 289why, one step back289Fix typo (#817)scottbeldenbefore adoption950463b
line 290why, one step back290 with runner.isolated_filesystem():apply blackDavid Lordbefore adoption93ba3ba
lines 291–292why, one step back291 with open("hello.txt", "w") as f:292 f.write("Cool stuff")Fix typo (#817)scottbeldenbefore adoption950463b
line 293why, one step back293apply blackDavid Lordbefore adoption93ba3ba
line 294why, one step back294 result = runner.invoke(cli, ["hello.txt"])Fix typo (#817)scottbeldenbefore adoption950463b
line 295why, one step back295 assert result.exception is Noneapply blackDavid Lordbefore adoption93ba3ba
line 296why, one step back296 assert result.output == "Cool stuff\nmeep\n"Fix typo (#817)scottbeldenbefore adoption950463b
line 297why, one step back297apply blackDavid Lordbefore adoption93ba3ba
line 298why, one step back298 result = runner.invoke(cli, ["-"], input="foobar")Fix typo (#817)scottbeldenbefore adoption950463b
line 299why, one step back299 assert result.exception is Noneapply blackDavid Lordbefore adoption93ba3ba
line 300why, one step back300 assert result.output == "foobar\nmeep\n"Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
lines 301–302why, one step back301302Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
lines 303–340why, one step back303def 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()339340apply blackDavid Lordbefore adoption93ba3ba
line 341why, one step back341@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")apply flake8David Lordbefore adoption5f75f9e
line 342why, one step back342@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
lines 343–344why, one step back343def test_open_file_atomic_permissions_existing_file(runner, permissions):344 with runner.isolated_filesystem():apply blackDavid Lordbefore adoption93ba3ba
lines 345–347why, one step back345 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 back348349 @click.command()apply blackDavid Lordbefore adoption93ba3ba
line 350why, one step back350 @click.argument("filename")Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 351why, one step back351 def cli(filename):apply blackDavid Lordbefore adoption93ba3ba
line 352why, one step back352 click.open_file(filename, "w", atomic=True).close()Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 353why, one step back353apply blackDavid Lordbefore adoption93ba3ba
line 354why, one step back354 result = runner.invoke(cli, ["existing.txt"])Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 355why, one step back355 assert result.exception is Noneapply blackDavid Lordbefore adoption93ba3ba
line 356why, one step back356 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissionsMerge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
lines 357–358why, one step back357358apply blackDavid Lordbefore adoption93ba3ba
line 359why, one step back359@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 back360def test_open_file_atomic_permissions_new_file(runner):361 with runner.isolated_filesystem():apply blackDavid Lordbefore adoption93ba3ba
line 362why, one step back362Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 363why, one step back363 @click.command()apply blackDavid Lordbefore adoption93ba3ba
line 364why, one step back364 @click.argument("filename")Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 365why, one step back365 def cli(filename):apply blackDavid Lordbefore adoption93ba3ba
line 366why, one step back366 click.open_file(filename, "w", atomic=True).close()Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
lines 367–369why, one step back367368 # 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 back370 with open("test.txt", "w"):Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 371why, one step back371 passapply blackDavid Lordbefore adoption93ba3ba
line 372why, one step back372 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 back373apply blackDavid Lordbefore adoption93ba3ba
line 374why, one step back374 result = runner.invoke(cli, ["new.txt"])Merge pull request #1400 from andersk/atomic-open-permissionsDavid Lordbefore adoptione0ad5a6
line 375why, one step back375 assert result.exception is Noneapply blackDavid Lordbefore adoption93ba3ba
line 376why, one step back376 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsFix typo (#817)scottbeldenbefore adoption950463b
lines 377–380why, one step back377378379def test_iter_keepopenfile(tmpdir):380 expected = list(map(str, range(10)))apply blackDavid Lordbefore adoption93ba3ba
line 381why, one step back381 p = tmpdir.mkdir("testdir").join("testfile")Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
line 382why, one step back382 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 383–385why, one step back383 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 back386387388def test_iter_lazyfile(tmpdir):389 expected = list(map(str, range(10)))apply blackDavid Lordbefore adoption93ba3ba
line 390why, one step back390 p = tmpdir.mkdir("testdir").join("testfile")Merge pull request #1298 from joewledger/fix_958David Lordbefore adoption9ea9666
line 391why, one step back391 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 392–395why, one step back392 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()