test_utils.py
This file
Path
tests/test_utils.py
414 lines · 12.2 KiB · Python
Last changed by
Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
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–75why, 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"),65 ({"blink": True}, "\x1b[5mx y\x1b[0m"),66 ({"underline": True}, "\x1b[4mx y\x1b[0m"),67 ({"bold": True}, "\x1b[1mx y\x1b[0m"),68 ({"dim": True}, "\x1b[2mx y\x1b[0m"),69 ],70)71def test_styling(styles, ref):72 assert click.style("x y", **styles) == ref73 assert click.unstyle(ref) == "x y"7475Merge branch '7.x'David Lordbefore adoption7239794
line 76why, one step back76@pytest.mark.parametrize(("text", "expect"), [("\x1b[?25lx y\x1b[?25h", "x y")])Merge branch '7.x'David Lordbefore adoption2269786
lines 77–78why, one step back77def test_unstyle_other_ansi(text, expect):78 assert click.unstyle(text) == expectAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 79–81why, one step back798081def test_filename_formatting():Merge branch '7.x'David Lordbefore adoption7239794
lines 82–83why, one step back82 assert click.format_filename(b"foo.txt") == "foo.txt"83 assert click.format_filename(b"/x/foo.txt") == "/x/foo.txt"Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 84–85why, one step back84 assert click.format_filename("/x/foo.txt") == "/x/foo.txt"85 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 86–88why, one step back8687 # filesystem encoding on windows permits this.88 if not WIN:Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 89why, one step back89 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\ufffd.txt"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 90–94why, one step back909192def test_prompts(runner):93 @click.command()94 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 95–96why, one step back95 if click.confirm("Foo"):96 click.echo("yes!")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 97why, one step back97 else:Merge branch '7.x'David Lordbefore adoption7239794
line 98why, one step back98 click.echo("no :(")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 99why, one step back99Merge branch '7.x'David Lordbefore adoption7239794
line 100why, one step back100 result = runner.invoke(test, input="y\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 101why, one step back101 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 102why, one step back102 assert result.output == "Foo [y/N]: y\nyes!\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 103why, one step back103Merge branch '7.x'David Lordbefore adoption7239794
line 104why, one step back104 result = runner.invoke(test, input="\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 105why, one step back105 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 106why, one step back106 assert result.output == "Foo [y/N]: \nno :(\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 107why, one step back107Merge branch '7.x'David Lordbefore adoption7239794
line 108why, one step back108 result = runner.invoke(test, input="n\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 109why, one step back109 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 110why, one step back110 assert result.output == "Foo [y/N]: n\nno :(\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 111–113why, one step back111112 @click.command()113 def test_no():Merge branch '7.x'David Lordbefore adoption7239794
lines 114–115why, one step back114 if click.confirm("Foo", default=True):115 click.echo("yes!")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 116why, one step back116 else:Merge branch '7.x'David Lordbefore adoption7239794
line 117why, one step back117 click.echo("no :(")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 118why, one step back118Merge branch '7.x'David Lordbefore adoption7239794
line 119why, one step back119 result = runner.invoke(test_no, input="y\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 120why, one step back120 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 121why, one step back121 assert result.output == "Foo [Y/n]: y\nyes!\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 122why, one step back122Merge branch '7.x'David Lordbefore adoption7239794
line 123why, one step back123 result = runner.invoke(test_no, input="\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 124why, one step back124 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 125why, one step back125 assert result.output == "Foo [Y/n]: \nyes!\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 126why, one step back126Merge branch '7.x'David Lordbefore adoption7239794
line 127why, one step back127 result = runner.invoke(test_no, input="n\n")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 128why, one step back128 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 129why, one step back129 assert result.output == "Foo [Y/n]: n\nno :(\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 130–131why, one step back130131Merge branch '7.x'David Lordbefore adoption7239794
line 132why, one step back132@pytest.mark.skipif(WIN, reason="Different behavior on windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 133–136why, one step back133def test_prompts_abort(monkeypatch, capsys):134 def f(_):135 raise KeyboardInterrupt()136Merge branch '7.x'David Lordbefore adoption7239794
line 137why, one step back137 monkeypatch.setattr("click.termui.hidden_prompt_func", f)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 138–139why, one step back138139 try:Merge branch '7.x'David Lordbefore adoption7239794
line 140why, one step back140 click.prompt("Password", hide_input=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 141why, one step back141 except click.Abort:Merge branch '7.x'David Lordbefore adoption7239794
line 142why, one step back142 click.echo("Screw you.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 143–144why, one step back143144 out, err = capsys.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
line 145why, one step back145 assert out == "Password: \nScrew you.\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 146–147why, one step back146147Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 148why, one step back148def _test_gen_func():Merge branch '7.x'David Lordbefore adoption7239794
lines 149–152why, one step back149 yield "a"150 yield "b"151 yield "c"152 yield "abc"Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
lines 153–154why, one step back153154Merge branch '7.x'David Lordbefore adoption7239794
lines 155–169why, one step back155@pytest.mark.skipif(WIN, reason="Different behavior on windows.")156@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])157@pytest.mark.parametrize(158 "test",159 [160 # We need lambda here, because pytest will161 # reuse the parameters, and then the generators162 # are already used and will not yield anymore163 ("just text\n", lambda: "just text"),164 ("iterable\n", lambda: ["itera", "ble"]),165 ("abcabc\n", lambda: _test_gen_func),166 ("abcabc\n", lambda: _test_gen_func()),167 ("012345\n", lambda: (c for c in range(6))),168 ],169)Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 170why, one step back170def test_echo_via_pager(monkeypatch, capfd, cat, test):Merge branch '7.x'David Lordbefore adoption7239794
lines 171–172why, one step back171 monkeypatch.setitem(os.environ, "PAGER", cat)172 monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
lines 173–178why, one step back173174 expected_output = test[0]175 test_input = test[1]()176177 click.echo_via_pager(test_input)178Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 179why, one step back179 out, err = capfd.readouterr()Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449
line 180why, one step back180 assert out == expected_outputAdded pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 181–182why, one step back181182Merge branch '7.x'David Lordbefore adoption7239794
line 183why, one step back183@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 184–185why, one step back184def test_echo_color_flag(monkeypatch, capfd):185 isatty = TrueMerge branch '7.x'David Lordbefore adoption7239794
line 186why, one step back186 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 187why, one step back187Merge branch '7.x'David Lordbefore adoption7239794
lines 188–190why, one step back188 text = "foo"189 styled_text = click.style(text, fg="red")190 assert styled_text == "\x1b[31mfoo\x1b[0m"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 191–193why, one step back191192 click.echo(styled_text, color=False)193 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 194why, one step back194 assert out == f"{text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 195–197why, one step back195196 click.echo(styled_text, color=True)197 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 198why, one step back198 assert out == f"{styled_text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 199–202why, one step back199200 isatty = True201 click.echo(styled_text)202 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 203why, one step back203 assert out == f"{styled_text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 204–207why, one step back204205 isatty = False206 click.echo(styled_text)207 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 208why, one step back208 assert out == f"{text}\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 209–210why, one step back209210Merge branch '7.x'David Lordbefore adoption7239794
line 211why, one step back211@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 212–214why, one step back212def test_echo_writing_to_standard_error(capfd, monkeypatch):213 def emulate_input(text):214 """Emulate keyboard input."""Merge branch '7.x'David Lordbefore adoption7239794
line 215why, one step back215 monkeypatch.setattr(sys, "stdin", StringIO(text))Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 216why, one step back216Merge branch '7.x'David Lordbefore adoption7239794
line 217why, one step back217 click.echo("Echo to standard output")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 218why, one step back218 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 219–220why, one step back219 assert out == "Echo to standard output\n"220 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 221why, one step back221Merge branch '7.x'David Lordbefore adoption7239794
line 222why, one step back222 click.echo("Echo to standard error", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 223why, one step back223 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 224–225why, one step back224 assert out == ""225 assert err == "Echo to standard error\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 226why, one step back226Merge branch '7.x'David Lordbefore adoption7239794
lines 227–228why, one step back227 emulate_input("asdlkj\n")228 click.prompt("Prompt to stdin")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 229why, one step back229 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 230–231why, one step back230 assert out == "Prompt to stdin: "231 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 232why, one step back232Merge branch '7.x'David Lordbefore adoption7239794
lines 233–234why, one step back233 emulate_input("asdlkj\n")234 click.prompt("Prompt to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 235why, one step back235 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 236–237why, one step back236 assert out == ""237 assert err == "Prompt to stderr: "Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 238why, one step back238Merge branch '7.x'David Lordbefore adoption7239794
lines 239–240why, one step back239 emulate_input("y\n")240 click.confirm("Prompt to stdin")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 241why, one step back241 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 242–243why, one step back242 assert out == "Prompt to stdin [y/N]: "243 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 244why, one step back244Merge branch '7.x'David Lordbefore adoption7239794
lines 245–246why, one step back245 emulate_input("y\n")246 click.confirm("Prompt to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 247why, one step back247 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 248–249why, one step back248 assert out == ""249 assert err == "Prompt to stderr [y/N]: "Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 250why, one step back250Merge branch '7.x'David Lordbefore adoption7239794
lines 251–252why, one step back251 monkeypatch.setattr(click.termui, "isatty", lambda x: True)252 monkeypatch.setattr(click.termui, "getchar", lambda: " ")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 253why, one step back253Merge branch '7.x'David Lordbefore adoption7239794
line 254why, one step back254 click.pause("Pause to stdout")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 255why, one step back255 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 256–257why, one step back256 assert out == "Pause to stdout\n"257 assert err == ""Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 258why, one step back258Merge branch '7.x'David Lordbefore adoption7239794
line 259why, one step back259 click.pause("Pause to stderr", err=True)Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 260why, one step back260 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 261–262why, one step back261 assert out == ""262 assert err == "Pause to stderr\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 263–264why, one step back263264Merge pull request #1591 from andy-maier/andy/fix-unsupportedoperationDavid Lordbefore adoption090ff26
lines 265–270why, one step back265def test_echo_with_capsys(capsys):266 click.echo("Capture me.")267 out, err = capsys.readouterr()268 assert out == "Capture me.\n"269270Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 271why, one step back271def test_open_file(runner):Merge branch '7.x'David Lordbefore adoption2269786
line 272why, one step back272 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 273why, one step back273 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
lines 274–277why, one step back274 def cli(filename):275 with click.open_file(filename) as f:276 click.echo(f.read())277Merge branch '7.x'David Lordbefore adoption7239794
line 278why, one step back278 click.echo("meep")Merge branch '7.x'David Lordbefore adoption2269786
line 279why, one step back279Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 280why, one step back280 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 281–282why, one step back281 with open("hello.txt", "w") as f:282 f.write("Cool stuff")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 283why, one step back283Merge branch '7.x'David Lordbefore adoption7239794
line 284why, one step back284 result = runner.invoke(cli, ["hello.txt"])Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 285why, one step back285 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 286why, one step back286 assert result.output == "Cool stuff\nmeep\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 287why, one step back287Merge branch '7.x'David Lordbefore adoption7239794
line 288why, one step back288 result = runner.invoke(cli, ["-"], input="foobar")Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
line 289why, one step back289 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 290why, one step back290 assert result.output == "foobar\nmeep\n"Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 291–292why, one step back291292Merge branch '7.x'David Lordbefore adoption2269786
lines 293–330why, one step back293def test_open_file_ignore_errors_stdin(runner):294 @click.command()295 @click.argument("filename")296 def cli(filename):297 with click.open_file(filename, errors="ignore") as f:298 click.echo(f.read())299300 result = runner.invoke(cli, ["-"], input=os.urandom(16))301 assert result.exception is None302303304def test_open_file_respects_ignore(runner):305 with runner.isolated_filesystem():306 with open("test.txt", "w") as f:307 f.write("Hello world!")308309 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:310 assert f.errors == "ignore"311312313def test_open_file_ignore_invalid_utf8(runner):314 with runner.isolated_filesystem():315 with open("test.txt", "wb") as f:316 f.write(b"\xe2\x28\xa1")317318 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:319 f.read()320321322def test_open_file_ignore_no_encoding(runner):323 with runner.isolated_filesystem():324 with open("test.bin", "wb") as f:325 f.write(os.urandom(16))326327 with click.open_file("test.bin", errors="ignore") as f:328 f.read()329330Merge branch '7.x'David Lordbefore adoption7239794
lines 331–332why, one step back331@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")332@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Merge branch '7.x'David Lordbefore adoption2269786
lines 333–334why, one step back333def test_open_file_atomic_permissions_existing_file(runner, permissions):334 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 335–337why, one step back335 with open("existing.txt", "w") as f:336 f.write("content")337 os.chmod("existing.txt", permissions)Merge branch '7.x'David Lordbefore adoption2269786
lines 338–339why, one step back338339 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 340why, one step back340 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 341why, one step back341 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 342why, one step back342 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
line 343why, one step back343Merge branch '7.x'David Lordbefore adoption7239794
line 344why, one step back344 result = runner.invoke(cli, ["existing.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 345why, one step back345 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 346why, one step back346 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 347–348why, one step back347348Merge branch '7.x'David Lordbefore adoption7239794
line 349why, one step back349@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")Merge branch '7.x'David Lordbefore adoption2269786
lines 350–351why, one step back350def test_open_file_atomic_permissions_new_file(runner):351 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
line 352why, one step back352Merge branch '7.x'David Lordbefore adoption2269786
line 353why, one step back353 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 354why, one step back354 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 355why, one step back355 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 356why, one step back356 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
lines 357–359why, one step back357358 # Create a test file to get the expected permissions for new files359 # according to the current umask.Merge branch '7.x'David Lordbefore adoption7239794
line 360why, one step back360 with open("test.txt", "w"):Merge branch '7.x'David Lordbefore adoption2269786
line 361why, one step back361 passMerge branch '7.x'David Lordbefore adoption7239794
line 362why, one step back362 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)Merge branch '7.x'David Lordbefore adoption2269786
line 363why, one step back363Merge branch '7.x'David Lordbefore adoption7239794
line 364why, one step back364 result = runner.invoke(cli, ["new.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 365why, one step back365 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 366why, one step back366 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 367–368why, one step back367368Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 369–370why, one step back369def test_iter_keepopenfile(tmpdir):370 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 371why, one step back371 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 372why, one step back372 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 373–375why, one step back373 with p.open() as f:374 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):375 assert e_line == a_line.strip()Added pytest cache to gitignoreArmin Ronacherbefore adoption7a10fe1
lines 376–379why, one step back376377378def test_iter_lazyfile(tmpdir):379 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 380why, one step back380 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 381why, one step back381 p.write("\n".join(expected))Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498
lines 382–385why, one step back382 with p.open() as f:383 with click.utils.LazyFile(f.name) as lf:384 for e_line, a_line in zip(expected, lf):385 assert e_line == a_line.strip()Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
lines 386–414why, one step back386387388class MockMain:389 __slots__ = "__package__"390391 def __init__(self, package_name):392 self.__package__ = package_name393394395@pytest.mark.parametrize(396 ("path", "main", "expected"),397 [398 ("example.py", None, "example.py"),399 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),400 ("example", None, "example"),401 (402 str(pathlib.Path("example/__main__.py")),403 MockMain(".example"),404 "python -m example",405 ),406 (407 str(pathlib.Path("example/cli.py")),408 MockMain(".example"),409 "python -m example.cli",410 ),411 ],412)413def test_detect_program_name(path, main, expected):414 assert click.utils._detect_program_name(path, _main=main) == expected