test_utils.py
This file
Path
tests/test_utils.py
416 lines · 12.3 KiB · Python
Last changed by
Merge pull request #1429 from lmjohns3/masterDavid Lordbefore adoption0d43812
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a10fe1 point at it.
Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 1why, one step 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 statAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 4why, one step back4import sysMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 5why, one step back5from io import StringIOAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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 WINAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 12–14why, one step back121314def test_echo(runner):CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 15why, one step back15 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")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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.Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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")28Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 31–32why, one step back3132 # Ensure we do not strip for bytes.CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 33why, one step back33 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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 36–39why, one step back363738def test_echo_custom_file():39 import ioMerge branch '7.x'David Lordbefore adoption7239794
line 40why, one step back40Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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) == expectAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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!")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 99why, one step back99 else:Merge branch '7.x'David Lordbefore adoption7239794
line 100why, one step back100 click.echo("no :(")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 101why, one step back101Merge branch '7.x'David Lordbefore adoption7239794
line 102why, one step back102 result = runner.invoke(test, input="y\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 105why, one step back105Merge branch '7.x'David Lordbefore adoption7239794
line 106why, one step back106 result = runner.invoke(test, input="\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 109why, one step back109Merge branch '7.x'David Lordbefore adoption7239794
line 110why, one step back110 result = runner.invoke(test, input="n\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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!")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 118why, one step back118 else:Merge branch '7.x'David Lordbefore adoption7239794
line 119why, one step back119 click.echo("no :(")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 120why, one step back120Merge branch '7.x'David Lordbefore adoption7239794
line 121why, one step back121 result = runner.invoke(test_no, input="y\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 124why, one step back124Merge branch '7.x'David Lordbefore adoption7239794
line 125why, one step back125 result = runner.invoke(test_no, input="\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 128why, one step back128Merge branch '7.x'David Lordbefore adoption7239794
line 129why, one step back129 result = runner.invoke(test_no, input="n\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
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"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 132–133why, one step back132133Merge branch '7.x'David Lordbefore adoption7239794
line 134why, one step back134@pytest.mark.skipif(WIN, reason="Different behavior on windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 135–138why, one step back135def test_prompts_abort(monkeypatch, capsys):136 def f(_):137 raise KeyboardInterrupt()138Merge branch '7.x'David Lordbefore adoption7239794
line 139why, one step back139 monkeypatch.setattr("click.termui.hidden_prompt_func", f)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 140–141why, one step back140141 try:Merge branch '7.x'David Lordbefore adoption7239794
line 142why, one step back142 click.prompt("Password", hide_input=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 143why, one step back143 except click.Abort:Merge branch '7.x'David Lordbefore adoption7239794
line 144why, one step back144 click.echo("Screw you.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 145–146why, one step back145146 out, err = capsys.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
line 147why, one step back147 assert out == "Password: \nScrew you.\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 148–149why, one step back148149Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 150why, one step back150def _test_gen_func():Merge branch '7.x'David Lordbefore adoption7239794
lines 151–154why, one step back151 yield "a"152 yield "b"153 yield "c"154 yield "abc"Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
lines 155–156why, one step back155156Merge branch '7.x'David Lordbefore adoption7239794
lines 157–171why, one step back157@pytest.mark.skipif(WIN, reason="Different behavior on windows.")158@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])159@pytest.mark.parametrize(160 "test",161 [162 # We need lambda here, because pytest will163 # reuse the parameters, and then the generators164 # are already used and will not yield anymore165 ("just text\n", lambda: "just text"),166 ("iterable\n", lambda: ["itera", "ble"]),167 ("abcabc\n", lambda: _test_gen_func),168 ("abcabc\n", lambda: _test_gen_func()),169 ("012345\n", lambda: (c for c in range(6))),170 ],171)Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 172why, one step back172def test_echo_via_pager(monkeypatch, capfd, cat, test):Merge branch '7.x'David Lordbefore adoption7239794
lines 173–174why, one step back173 monkeypatch.setitem(os.environ, "PAGER", cat)174 monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
lines 175–180why, one step back175176 expected_output = test[0]177 test_input = test[1]()178179 click.echo_via_pager(test_input)180Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 181why, one step back181 out, err = capfd.readouterr()Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 182why, one step back182 assert out == expected_outputAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 183–184why, one step back183184Merge branch '7.x'David Lordbefore adoption7239794
line 185why, one step back185@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 186–187why, one step back186def test_echo_color_flag(monkeypatch, capfd):187 isatty = TrueMerge branch '7.x'David Lordbefore adoption7239794
line 188why, one step back188 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 189why, one step back189Merge branch '7.x'David Lordbefore adoption7239794
lines 190–192why, one step back190 text = "foo"191 styled_text = click.style(text, fg="red")192 assert styled_text == "\x1b[31mfoo\x1b[0m"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 193–195why, one step back193194 click.echo(styled_text, color=False)195 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 196why, one step back196 assert out == f"{text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 197–199why, one step back197198 click.echo(styled_text, color=True)199 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 200why, one step back200 assert out == f"{styled_text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 201–204why, one step back201202 isatty = True203 click.echo(styled_text)204 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 205why, one step back205 assert out == f"{styled_text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 206–209why, one step back206207 isatty = False208 click.echo(styled_text)209 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 210why, one step back210 assert out == f"{text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 211–212why, one step back211212Merge branch '7.x'David Lordbefore adoption7239794
line 213why, one step back213@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 214–216why, one step back214def test_echo_writing_to_standard_error(capfd, monkeypatch):215 def emulate_input(text):216 """Emulate keyboard input."""Merge branch '7.x'David Lordbefore adoption7239794
line 217why, one step back217 monkeypatch.setattr(sys, "stdin", StringIO(text))Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 218why, one step back218Merge branch '7.x'David Lordbefore adoption7239794
line 219why, one step back219 click.echo("Echo to standard output")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 220why, one step back220 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 221–222why, one step back221 assert out == "Echo to standard output\n"222 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 223why, one step back223Merge branch '7.x'David Lordbefore adoption7239794
line 224why, one step back224 click.echo("Echo to standard error", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 225why, one step back225 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 226–227why, one step back226 assert out == ""227 assert err == "Echo to standard error\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 228why, one step back228Merge branch '7.x'David Lordbefore adoption7239794
lines 229–230why, one step back229 emulate_input("asdlkj\n")230 click.prompt("Prompt to stdin")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 231why, one step back231 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 232–233why, one step back232 assert out == "Prompt to stdin: "233 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 234why, one step back234Merge branch '7.x'David Lordbefore adoption7239794
lines 235–236why, one step back235 emulate_input("asdlkj\n")236 click.prompt("Prompt to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 237why, one step back237 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 238–239why, one step back238 assert out == ""239 assert err == "Prompt to stderr: "Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 240why, one step back240Merge branch '7.x'David Lordbefore adoption7239794
lines 241–242why, one step back241 emulate_input("y\n")242 click.confirm("Prompt to stdin")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 243why, one step back243 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 244–245why, one step back244 assert out == "Prompt to stdin [y/N]: "245 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 246why, one step back246Merge branch '7.x'David Lordbefore adoption7239794
lines 247–248why, one step back247 emulate_input("y\n")248 click.confirm("Prompt to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 249why, one step back249 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 250–251why, one step back250 assert out == ""251 assert err == "Prompt to stderr [y/N]: "Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 252why, one step back252Merge branch '7.x'David Lordbefore adoption7239794
lines 253–254why, one step back253 monkeypatch.setattr(click.termui, "isatty", lambda x: True)254 monkeypatch.setattr(click.termui, "getchar", lambda: " ")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 255why, one step back255Merge branch '7.x'David Lordbefore adoption7239794
line 256why, one step back256 click.pause("Pause to stdout")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 257why, one step back257 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 258–259why, one step back258 assert out == "Pause to stdout\n"259 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 260why, one step back260Merge branch '7.x'David Lordbefore adoption7239794
line 261why, one step back261 click.pause("Pause to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 262why, one step back262 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 263–264why, one step back263 assert out == ""264 assert err == "Pause to stderr\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 265–266why, one step back265266Merge pull request #1591 from andy-maier/andy/fix-unsupportedoperationDavid Lordbefore adoption090ff26
lines 267–272why, one step back267def test_echo_with_capsys(capsys):268 click.echo("Capture me.")269 out, err = capsys.readouterr()270 assert out == "Capture me.\n"271272Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 273why, one step back273def test_open_file(runner):Merge branch '7.x'David Lordbefore adoption2269786
line 274why, one step back274 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 275why, one step back275 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
lines 276–279why, one step back276 def cli(filename):277 with click.open_file(filename) as f:278 click.echo(f.read())279Merge branch '7.x'David Lordbefore adoption7239794
line 280why, one step back280 click.echo("meep")Merge branch '7.x'David Lordbefore adoption2269786
line 281why, one step back281Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 282why, one step back282 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 283–284why, one step back283 with open("hello.txt", "w") as f:284 f.write("Cool stuff")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 285why, one step back285Merge branch '7.x'David Lordbefore adoption7239794
line 286why, one step back286 result = runner.invoke(cli, ["hello.txt"])Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 287why, one step back287 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 288why, one step back288 assert result.output == "Cool stuff\nmeep\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 289why, one step back289Merge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 result = runner.invoke(cli, ["-"], input="foobar")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 291why, one step back291 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 292why, one step back292 assert result.output == "foobar\nmeep\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 293–294why, one step back293294Merge branch '7.x'David Lordbefore adoption2269786
lines 295–332why, one step back295def test_open_file_ignore_errors_stdin(runner):296 @click.command()297 @click.argument("filename")298 def cli(filename):299 with click.open_file(filename, errors="ignore") as f:300 click.echo(f.read())301302 result = runner.invoke(cli, ["-"], input=os.urandom(16))303 assert result.exception is None304305306def test_open_file_respects_ignore(runner):307 with runner.isolated_filesystem():308 with open("test.txt", "w") as f:309 f.write("Hello world!")310311 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:312 assert f.errors == "ignore"313314315def test_open_file_ignore_invalid_utf8(runner):316 with runner.isolated_filesystem():317 with open("test.txt", "wb") as f:318 f.write(b"\xe2\x28\xa1")319320 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:321 f.read()322323324def test_open_file_ignore_no_encoding(runner):325 with runner.isolated_filesystem():326 with open("test.bin", "wb") as f:327 f.write(os.urandom(16))328329 with click.open_file("test.bin", errors="ignore") as f:330 f.read()331332Merge branch '7.x'David Lordbefore adoption7239794
lines 333–334why, one step back333@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")334@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Merge branch '7.x'David Lordbefore adoption2269786
lines 335–336why, one step back335def test_open_file_atomic_permissions_existing_file(runner, permissions):336 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 337–339why, one step back337 with open("existing.txt", "w") as f:338 f.write("content")339 os.chmod("existing.txt", permissions)Merge branch '7.x'David Lordbefore adoption2269786
lines 340–341why, one step back340341 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 342why, one step back342 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 343why, one step back343 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 344why, one step back344 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
line 345why, one step back345Merge branch '7.x'David Lordbefore adoption7239794
line 346why, one step back346 result = runner.invoke(cli, ["existing.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 347why, one step back347 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 348why, one step back348 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 349–350why, one step back349350Merge branch '7.x'David Lordbefore adoption7239794
line 351why, one step back351@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")Merge branch '7.x'David Lordbefore adoption2269786
lines 352–353why, one step back352def test_open_file_atomic_permissions_new_file(runner):353 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
line 354why, one step back354Merge branch '7.x'David Lordbefore adoption2269786
line 355why, one step back355 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 356why, one step back356 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 357why, one step back357 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 358why, one step back358 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
lines 359–361why, one step back359360 # Create a test file to get the expected permissions for new files361 # according to the current umask.Merge branch '7.x'David Lordbefore adoption7239794
line 362why, one step back362 with open("test.txt", "w"):Merge branch '7.x'David Lordbefore adoption2269786
line 363why, one step back363 passMerge branch '7.x'David Lordbefore adoption7239794
line 364why, one step back364 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)Merge branch '7.x'David Lordbefore adoption2269786
line 365why, one step back365Merge branch '7.x'David Lordbefore adoption7239794
line 366why, one step back366 result = runner.invoke(cli, ["new.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 367why, one step back367 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 368why, one step back368 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 369–370why, one step back369370Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 371–372why, one step back371def test_iter_keepopenfile(tmpdir):372 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 373why, one step back373 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 374why, one step back374 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 375–377why, one step back375 with p.open() as f:376 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):377 assert e_line == a_line.strip()Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 378–381why, one step back378379380def test_iter_lazyfile(tmpdir):381 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 382why, one step back382 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 383why, one step back383 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 384–387why, one step back384 with p.open() as f:385 with click.utils.LazyFile(f.name) as lf:386 for e_line, a_line in zip(expected, lf):387 assert e_line == a_line.strip()Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
lines 388–416why, one step back388389390class MockMain:391 __slots__ = "__package__"392393 def __init__(self, package_name):394 self.__package__ = package_name395396397@pytest.mark.parametrize(398 ("path", "main", "expected"),399 [400 ("example.py", None, "example.py"),401 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),402 ("example", None, "example"),403 (404 str(pathlib.Path("example/__main__.py")),405 MockMain(".example"),406 "python -m example",407 ),408 (409 str(pathlib.Path("example/cli.py")),410 MockMain(".example"),411 "python -m example.cli",412 ),413 ],414)415def test_detect_program_name(path, main, expected):416 assert click.utils._detect_program_name(path, _main=main) == expected