test_utils.py
This file
Path
tests/test_utils.py
431 lines · 12.8 KiB · Python
Last changed by
Merge pull request #1402 from yurihs/add-repeat-behavior-to-promptDavid Lordbefore adoption719690a
History of this fileTake it
The walk stopped after 200 commits: lines older than 123dd71 point at it.
Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 1why, one step back1import osMerge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
line 2why, one step back2import pathlibMerge branch '7.x'David Lordbefore adoption2269786
line 3why, one step back3import statMerge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 4why, one step back4import sysMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 5why, one step back5from io import StringIOMerge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 6–9why, one step back67import pytest89import click._termui_implMerge branch '7.x'David Lordbefore adoption7239794
lines 10–11why, one step back10import click.utils11from click._compat import WINMerge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 12–15why, one step back121314def test_echo(runner):15 with runner.isolation() as outstreams:Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 16why, one step back16 click.echo("\N{SNOWMAN}")Merge branch '7.x'David Lordbefore adoption7239794
line 17why, one step back17 click.echo(b"\x44\x44")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 18why, one step back18 click.echo(42, nl=False)Merge branch '7.x'David Lordbefore adoption7239794
lines 19–22why, one step back19 click.echo(b"a", nl=False)20 click.echo("\x1b[31mx\x1b[39m", nl=False)21 bytes = outstreams[0].getvalue().replace(b"\r\n", b"\n")22 assert bytes == b"\xe2\x98\x83\nDD\n42ax"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 23why, one step back23Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 24why, one step back24 # if wrapped, we expect bytes to survive.Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 25–26why, one step back25 @click.command()26 def cli():Merge branch '7.x'David Lordbefore adoption7239794
lines 27–28why, one step back27 click.echo(b"\xf6")28Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 29why, one step back29 result = runner.invoke(cli, [])Merge branch '7.x'David Lordbefore adoption7239794
line 30why, one step back30 assert result.stdout_bytes == b"\xf6\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 31–33why, one step back3132 # Ensure we do not strip for bytes.33 with runner.isolation() as outstreams:Merge branch '7.x'David Lordbefore adoption7239794
lines 34–35why, one step back34 click.echo(bytearray(b"\x1b[31mx\x1b[39m"), nl=False)35 assert outstreams[0].getvalue() == b"\x1b[31mx\x1b[39m"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 36–39why, one step back363738def test_echo_custom_file():39 import ioMerge branch '7.x'David Lordbefore adoption7239794
line 40why, one step back40Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 41why, one step back41 f = io.StringIO()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 42–43why, one step back42 click.echo("hello", file=f)43 assert f.getvalue() == "hello\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 44–45why, one step back4445Merge branch '7.x'David Lordbefore adoption2269786
lines 46–64why, one step back46@pytest.mark.parametrize(47 ("styles", "ref"),48 [49 ({"fg": "black"}, "\x1b[30mx y\x1b[0m"),50 ({"fg": "red"}, "\x1b[31mx y\x1b[0m"),51 ({"fg": "green"}, "\x1b[32mx y\x1b[0m"),52 ({"fg": "yellow"}, "\x1b[33mx y\x1b[0m"),53 ({"fg": "blue"}, "\x1b[34mx y\x1b[0m"),54 ({"fg": "magenta"}, "\x1b[35mx y\x1b[0m"),55 ({"fg": "cyan"}, "\x1b[36mx y\x1b[0m"),56 ({"fg": "white"}, "\x1b[37mx y\x1b[0m"),57 ({"bg": "black"}, "\x1b[40mx y\x1b[0m"),58 ({"bg": "red"}, "\x1b[41mx y\x1b[0m"),59 ({"bg": "green"}, "\x1b[42mx y\x1b[0m"),60 ({"bg": "yellow"}, "\x1b[43mx y\x1b[0m"),61 ({"bg": "blue"}, "\x1b[44mx y\x1b[0m"),62 ({"bg": "magenta"}, "\x1b[45mx y\x1b[0m"),63 ({"bg": "cyan"}, "\x1b[46mx y\x1b[0m"),64 ({"bg": "white"}, "\x1b[47mx y\x1b[0m"),Merge pull request #1429 from lmjohns3/masterDavid Lordbefore adoption0d43812
lines 65–66why, one step back65 ({"bg": 91}, "\x1b[48;5;91mx y\x1b[0m"),66 ({"bg": (135, 0, 175)}, "\x1b[48;2;135;0;175mx y\x1b[0m"),Merge branch '7.x'David Lordbefore adoption2269786
lines 67–77why, one step back67 ({"blink": True}, "\x1b[5mx y\x1b[0m"),68 ({"underline": True}, "\x1b[4mx y\x1b[0m"),69 ({"bold": True}, "\x1b[1mx y\x1b[0m"),70 ({"dim": True}, "\x1b[2mx y\x1b[0m"),71 ],72)73def test_styling(styles, ref):74 assert click.style("x y", **styles) == ref75 assert click.unstyle(ref) == "x y"7677Merge branch '7.x'David Lordbefore adoption7239794
line 78why, one step back78@pytest.mark.parametrize(("text", "expect"), [("\x1b[?25lx y\x1b[?25h", "x y")])Merge branch '7.x'David Lordbefore adoption2269786
lines 79–80why, one step back79def test_unstyle_other_ansi(text, expect):80 assert click.unstyle(text) == expectMerge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 81–83why, one step back818283def test_filename_formatting():Merge branch '7.x'David Lordbefore adoption7239794
lines 84–85why, one step back84 assert click.format_filename(b"foo.txt") == "foo.txt"85 assert click.format_filename(b"/x/foo.txt") == "/x/foo.txt"Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 86–87why, one step back86 assert click.format_filename("/x/foo.txt") == "/x/foo.txt"87 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 88–90why, one step back8889 # filesystem encoding on windows permits this.90 if not WIN:Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 91why, one step back91 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\ufffd.txt"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 92–96why, one step back929394def test_prompts(runner):95 @click.command()96 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 97–98why, one step back97 if click.confirm("Foo"):98 click.echo("yes!")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 99why, one step back99 else:Merge branch '7.x'David Lordbefore adoption7239794
line 100why, one step back100 click.echo("no :(")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 101why, one step back101Merge branch '7.x'David Lordbefore adoption7239794
line 102why, one step back102 result = runner.invoke(test, input="y\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 103why, one step back103 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 104why, one step back104 assert result.output == "Foo [y/N]: y\nyes!\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 105why, one step back105Merge branch '7.x'David Lordbefore adoption7239794
line 106why, one step back106 result = runner.invoke(test, input="\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 107why, one step back107 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 108why, one step back108 assert result.output == "Foo [y/N]: \nno :(\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 109why, one step back109Merge branch '7.x'David Lordbefore adoption7239794
line 110why, one step back110 result = runner.invoke(test, input="n\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 111why, one step back111 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 112why, one step back112 assert result.output == "Foo [y/N]: n\nno :(\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 113–115why, one step back113114 @click.command()115 def test_no():Merge branch '7.x'David Lordbefore adoption7239794
lines 116–117why, one step back116 if click.confirm("Foo", default=True):117 click.echo("yes!")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 118why, one step back118 else:Merge branch '7.x'David Lordbefore adoption7239794
line 119why, one step back119 click.echo("no :(")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 120why, one step back120Merge branch '7.x'David Lordbefore adoption7239794
line 121why, one step back121 result = runner.invoke(test_no, input="y\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 122why, one step back122 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 123why, one step back123 assert result.output == "Foo [Y/n]: y\nyes!\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 124why, one step back124Merge branch '7.x'David Lordbefore adoption7239794
line 125why, one step back125 result = runner.invoke(test_no, input="\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 126why, one step back126 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 127why, one step back127 assert result.output == "Foo [Y/n]: \nyes!\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 128why, one step back128Merge branch '7.x'David Lordbefore adoption7239794
line 129why, one step back129 result = runner.invoke(test_no, input="n\n")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 130why, one step back130 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 131why, one step back131 assert result.output == "Foo [Y/n]: n\nno :(\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 132–133why, one step back132133Merge pull request #1402 from yurihs/add-repeat-behavior-to-promptDavid Lordbefore adoption719690a
lines 134–141why, one step back134def test_confirm_repeat(runner):135 cli = click.Command(136 "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)]137 )138 result = runner.invoke(cli, input="\ny\n")139 assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"140141Merge branch '7.x'David Lordbefore adoption7239794
line 142why, one step back142@pytest.mark.skipif(WIN, reason="Different behavior on windows.")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 143–146why, one step back143def test_prompts_abort(monkeypatch, capsys):144 def f(_):145 raise KeyboardInterrupt()146Merge branch '7.x'David Lordbefore adoption7239794
line 147why, one step back147 monkeypatch.setattr("click.termui.hidden_prompt_func", f)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 148–149why, one step back148149 try:Merge branch '7.x'David Lordbefore adoption7239794
line 150why, one step back150 click.prompt("Password", hide_input=True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 151why, one step back151 except click.Abort:Merge branch '7.x'David Lordbefore adoption7239794
line 152why, one step back152 click.echo("Screw you.")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 153–154why, one step back153154 out, err = capsys.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
line 155why, one step back155 assert out == "Password: \nScrew you.\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 156–158why, one step back156157158def _test_gen_func():Merge branch '7.x'David Lordbefore adoption7239794
lines 159–162why, one step back159 yield "a"160 yield "b"161 yield "c"162 yield "abc"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 163–164why, one step back163164Merge branch '7.x'David Lordbefore adoption7239794
lines 165–179why, one step back165@pytest.mark.skipif(WIN, reason="Different behavior on windows.")166@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])167@pytest.mark.parametrize(168 "test",169 [170 # We need lambda here, because pytest will171 # reuse the parameters, and then the generators172 # are already used and will not yield anymore173 ("just text\n", lambda: "just text"),174 ("iterable\n", lambda: ["itera", "ble"]),175 ("abcabc\n", lambda: _test_gen_func),176 ("abcabc\n", lambda: _test_gen_func()),177 ("012345\n", lambda: (c for c in range(6))),178 ],179)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 180why, one step back180def test_echo_via_pager(monkeypatch, capfd, cat, test):Merge branch '7.x'David Lordbefore adoption7239794
lines 181–182why, one step back181 monkeypatch.setitem(os.environ, "PAGER", cat)182 monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 183–192why, one step back183184 expected_output = test[0]185 test_input = test[1]()186187 click.echo_via_pager(test_input)188189 out, err = capfd.readouterr()190 assert out == expected_output191192Merge branch '7.x'David Lordbefore adoption7239794
line 193why, one step back193@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 194–195why, one step back194def test_echo_color_flag(monkeypatch, capfd):195 isatty = TrueMerge branch '7.x'David Lordbefore adoption7239794
line 196why, one step back196 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 197why, one step back197Merge branch '7.x'David Lordbefore adoption7239794
lines 198–200why, one step back198 text = "foo"199 styled_text = click.style(text, fg="red")200 assert styled_text == "\x1b[31mfoo\x1b[0m"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 201–203why, one step back201202 click.echo(styled_text, color=False)203 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 204why, one step back204 assert out == f"{text}\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 205–207why, one step back205206 click.echo(styled_text, color=True)207 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 208why, one step back208 assert out == f"{styled_text}\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 209–212why, one step back209210 isatty = True211 click.echo(styled_text)212 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 213why, one step back213 assert out == f"{styled_text}\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 214–217why, one step back214215 isatty = False216 click.echo(styled_text)217 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 218why, one step back218 assert out == f"{text}\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 219–220why, one step back219220Merge pull request #1517 from stgraham2000/masterDavid Lordbefore adoption801a29f
lines 221–227why, one step back221def test_prompt_cast_default(capfd, monkeypatch):222 monkeypatch.setattr(sys, "stdin", StringIO("\n"))223 value = click.prompt("value", default="100", type=int)224 capfd.readouterr()225 assert type(value) is int226227Merge branch '7.x'David Lordbefore adoption7239794
line 228why, one step back228@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 229–231why, one step back229def test_echo_writing_to_standard_error(capfd, monkeypatch):230 def emulate_input(text):231 """Emulate keyboard input."""Merge branch '7.x'David Lordbefore adoption7239794
line 232why, one step back232 monkeypatch.setattr(sys, "stdin", StringIO(text))Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 233why, one step back233Merge branch '7.x'David Lordbefore adoption7239794
line 234why, one step back234 click.echo("Echo to standard output")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 235why, one step back235 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 236–237why, one step back236 assert out == "Echo to standard output\n"237 assert err == ""Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 238why, one step back238Merge branch '7.x'David Lordbefore adoption7239794
line 239why, one step back239 click.echo("Echo to standard error", err=True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 240why, one step back240 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 241–242why, one step back241 assert out == ""242 assert err == "Echo to standard error\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 243why, one step back243Merge branch '7.x'David Lordbefore adoption7239794
lines 244–245why, one step back244 emulate_input("asdlkj\n")245 click.prompt("Prompt to stdin")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 246why, one step back246 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 247–248why, one step back247 assert out == "Prompt to stdin: "248 assert err == ""Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 249why, one step back249Merge branch '7.x'David Lordbefore adoption7239794
lines 250–251why, one step back250 emulate_input("asdlkj\n")251 click.prompt("Prompt to stderr", err=True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 252why, one step back252 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 253–254why, one step back253 assert out == ""254 assert err == "Prompt to stderr: "Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 255why, one step back255Merge branch '7.x'David Lordbefore adoption7239794
lines 256–257why, one step back256 emulate_input("y\n")257 click.confirm("Prompt to stdin")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 258why, one step back258 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 259–260why, one step back259 assert out == "Prompt to stdin [y/N]: "260 assert err == ""Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 261why, one step back261Merge branch '7.x'David Lordbefore adoption7239794
lines 262–263why, one step back262 emulate_input("y\n")263 click.confirm("Prompt to stderr", err=True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 264why, one step back264 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 265–266why, one step back265 assert out == ""266 assert err == "Prompt to stderr [y/N]: "Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 267why, one step back267Merge branch '7.x'David Lordbefore adoption7239794
lines 268–269why, one step back268 monkeypatch.setattr(click.termui, "isatty", lambda x: True)269 monkeypatch.setattr(click.termui, "getchar", lambda: " ")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 270why, one step back270Merge branch '7.x'David Lordbefore adoption7239794
line 271why, one step back271 click.pause("Pause to stdout")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 272why, one step back272 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 273–274why, one step back273 assert out == "Pause to stdout\n"274 assert err == ""Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 275why, one step back275Merge branch '7.x'David Lordbefore adoption7239794
line 276why, one step back276 click.pause("Pause to stderr", err=True)Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 277why, one step back277 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 278–279why, one step back278 assert out == ""279 assert err == "Pause to stderr\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 280–281why, one step back280281Merge pull request #1591 from andy-maier/andy/fix-unsupportedoperationDavid Lordbefore adoption090ff26
lines 282–287why, one step back282def test_echo_with_capsys(capsys):283 click.echo("Capture me.")284 out, err = capsys.readouterr()285 assert out == "Capture me.\n"286287Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 288why, one step back288def test_open_file(runner):Merge branch '7.x'David Lordbefore adoption2269786
line 289why, one step back289 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
lines 291–294why, one step back291 def cli(filename):292 with click.open_file(filename) as f:293 click.echo(f.read())294Merge branch '7.x'David Lordbefore adoption7239794
line 295why, one step back295 click.echo("meep")Merge branch '7.x'David Lordbefore adoption2269786
line 296why, one step back296Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 297why, one step back297 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 298–299why, one step back298 with open("hello.txt", "w") as f:299 f.write("Cool stuff")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 300why, one step back300Merge branch '7.x'David Lordbefore adoption7239794
line 301why, one step back301 result = runner.invoke(cli, ["hello.txt"])Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 302why, one step back302 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 303why, one step back303 assert result.output == "Cool stuff\nmeep\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 304why, one step back304Merge branch '7.x'David Lordbefore adoption7239794
line 305why, one step back305 result = runner.invoke(cli, ["-"], input="foobar")Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
line 306why, one step back306 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 307why, one step back307 assert result.output == "foobar\nmeep\n"Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 308–309why, one step back308309Merge branch '7.x'David Lordbefore adoption2269786
lines 310–347why, one step back310def test_open_file_ignore_errors_stdin(runner):311 @click.command()312 @click.argument("filename")313 def cli(filename):314 with click.open_file(filename, errors="ignore") as f:315 click.echo(f.read())316317 result = runner.invoke(cli, ["-"], input=os.urandom(16))318 assert result.exception is None319320321def test_open_file_respects_ignore(runner):322 with runner.isolated_filesystem():323 with open("test.txt", "w") as f:324 f.write("Hello world!")325326 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:327 assert f.errors == "ignore"328329330def test_open_file_ignore_invalid_utf8(runner):331 with runner.isolated_filesystem():332 with open("test.txt", "wb") as f:333 f.write(b"\xe2\x28\xa1")334335 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:336 f.read()337338339def test_open_file_ignore_no_encoding(runner):340 with runner.isolated_filesystem():341 with open("test.bin", "wb") as f:342 f.write(os.urandom(16))343344 with click.open_file("test.bin", errors="ignore") as f:345 f.read()346347Merge branch '7.x'David Lordbefore adoption7239794
lines 348–349why, one step back348@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")349@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Merge branch '7.x'David Lordbefore adoption2269786
lines 350–351why, one step back350def test_open_file_atomic_permissions_existing_file(runner, permissions):351 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 352–354why, one step back352 with open("existing.txt", "w") as f:353 f.write("content")354 os.chmod("existing.txt", permissions)Merge branch '7.x'David Lordbefore adoption2269786
lines 355–356why, one step back355356 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 357why, one step back357 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 358why, one step back358 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 359why, one step back359 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
line 360why, one step back360Merge branch '7.x'David Lordbefore adoption7239794
line 361why, one step back361 result = runner.invoke(cli, ["existing.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 362why, one step back362 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 363why, one step back363 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 364–365why, one step back364365Merge branch '7.x'David Lordbefore adoption7239794
line 366why, one step back366@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")Merge branch '7.x'David Lordbefore adoption2269786
lines 367–368why, one step back367def test_open_file_atomic_permissions_new_file(runner):368 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
line 369why, one step back369Merge branch '7.x'David Lordbefore adoption2269786
line 370why, one step back370 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 371why, one step back371 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 372why, one step back372 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 373why, one step back373 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
lines 374–376why, one step back374375 # Create a test file to get the expected permissions for new files376 # according to the current umask.Merge branch '7.x'David Lordbefore adoption7239794
line 377why, one step back377 with open("test.txt", "w"):Merge branch '7.x'David Lordbefore adoption2269786
line 378why, one step back378 passMerge branch '7.x'David Lordbefore adoption7239794
line 379why, one step back379 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)Merge branch '7.x'David Lordbefore adoption2269786
line 380why, one step back380Merge branch '7.x'David Lordbefore adoption7239794
line 381why, one step back381 result = runner.invoke(cli, ["new.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 382why, one step back382 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 383why, one step back383 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 384–385why, one step back384385Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 386–387why, one step back386def test_iter_keepopenfile(tmpdir):387 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 388why, one step back388 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 389why, one step back389 p.write("\n".join(expected))Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 390–396why, one step back390 with p.open() as f:391 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):392 assert e_line == a_line.strip()393394395def test_iter_lazyfile(tmpdir):396 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 397why, one step back397 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 398why, one step back398 p.write("\n".join(expected))Merge pull request #1103 from jamestwebber/patch-1dougharrisbefore adoption123dd71
lines 399–402why, one step back399 with p.open() as f:400 with click.utils.LazyFile(f.name) as lf:401 for e_line, a_line in zip(expected, lf):402 assert e_line == a_line.strip()Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
lines 403–431why, one step back403404405class MockMain:406 __slots__ = "__package__"407408 def __init__(self, package_name):409 self.__package__ = package_name410411412@pytest.mark.parametrize(413 ("path", "main", "expected"),414 [415 ("example.py", None, "example.py"),416 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),417 ("example", None, "example"),418 (419 str(pathlib.Path("example/__main__.py")),420 MockMain(".example"),421 "python -m example",422 ),423 (424 str(pathlib.Path("example/cli.py")),425 MockMain(".example"),426 "python -m example.cli",427 ),428 ],429)430def test_detect_program_name(path, main, expected):431 assert click.utils._detect_program_name(path, _main=main) == expected