test_utils.py
This file
Path
tests/test_utils.py
446 lines · 13.5 KiB · Python
Last changed by
Merge pull request #1822 from rbanffy/overline-italicDavid Lordbefore adoptionc0f9c06
History of this fileTake it
The walk stopped after 200 commits: lines older than 3ce663c point at it.
Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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 statClarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 4why, one step back4import sysMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 5why, one step back5from io import StringIOClarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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 WINClarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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.Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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")28Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 36–39why, one step back363738def test_echo_custom_file():39 import ioMerge branch '7.x'David Lordbefore adoption7239794
line 40why, one step back40Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
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–68why, one step back67 ({"bold": True}, "\x1b[1mx y\x1b[0m"),68 ({"dim": True}, "\x1b[2mx y\x1b[0m"),Merge pull request #1822 from rbanffy/overline-italicDavid Lordbefore adoptionc0f9c06
lines 69–75why, one step back69 ({"underline": True}, "\x1b[4mx y\x1b[0m"),70 ({"overline": True}, "\x1b[55mx y\x1b[0m"),71 ({"italic": True}, "\x1b[23mx y\x1b[0m"),72 ({"blink": True}, "\x1b[5mx y\x1b[0m"),73 ({"reverse": True}, "\x1b[7mx y\x1b[0m"),74 ({"strikethrough": True}, "\x1b[9mx y\x1b[0m"),75 ({"fg": "black", "reset": False}, "\x1b[30mx y"),Merge branch '7.x'David Lordbefore adoption2269786
lines 76–82why, one step back76 ],77)78def test_styling(styles, ref):79 assert click.style("x y", **styles) == ref80 assert click.unstyle(ref) == "x y"8182Merge branch '7.x'David Lordbefore adoption7239794
line 83why, one step back83@pytest.mark.parametrize(("text", "expect"), [("\x1b[?25lx y\x1b[?25h", "x y")])Merge branch '7.x'David Lordbefore adoption2269786
lines 84–85why, one step back84def test_unstyle_other_ansi(text, expect):85 assert click.unstyle(text) == expectClarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 86–88why, one step back868788def test_filename_formatting():Merge branch '7.x'David Lordbefore adoption7239794
lines 89–90why, one step back89 assert click.format_filename(b"foo.txt") == "foo.txt"90 assert click.format_filename(b"/x/foo.txt") == "/x/foo.txt"Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
lines 91–92why, one step back91 assert click.format_filename("/x/foo.txt") == "/x/foo.txt"92 assert click.format_filename("/x/foo.txt", shorten=True) == "foo.txt"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 93–95why, one step back9394 # filesystem encoding on windows permits this.95 if not WIN:Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 96why, one step back96 assert click.format_filename(b"/x/foo\xff.txt", shorten=True) == "foo\ufffd.txt"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 97–101why, one step back979899def test_prompts(runner):100 @click.command()101 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 102–103why, one step back102 if click.confirm("Foo"):103 click.echo("yes!")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 104why, one step back104 else:Merge branch '7.x'David Lordbefore adoption7239794
line 105why, one step back105 click.echo("no :(")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 106why, one step back106Merge branch '7.x'David Lordbefore adoption7239794
line 107why, one step back107 result = runner.invoke(test, input="y\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 108why, one step back108 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 109why, one step back109 assert result.output == "Foo [y/N]: y\nyes!\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 110why, one step back110Merge branch '7.x'David Lordbefore adoption7239794
line 111why, one step back111 result = runner.invoke(test, input="\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 112why, one step back112 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 113why, one step back113 assert result.output == "Foo [y/N]: \nno :(\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 114why, one step back114Merge branch '7.x'David Lordbefore adoption7239794
line 115why, one step back115 result = runner.invoke(test, input="n\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 116why, one step back116 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 117why, one step back117 assert result.output == "Foo [y/N]: n\nno :(\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 118–120why, one step back118119 @click.command()120 def test_no():Merge branch '7.x'David Lordbefore adoption7239794
lines 121–122why, one step back121 if click.confirm("Foo", default=True):122 click.echo("yes!")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 123why, one step back123 else:Merge branch '7.x'David Lordbefore adoption7239794
line 124why, one step back124 click.echo("no :(")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 125why, one step back125Merge branch '7.x'David Lordbefore adoption7239794
line 126why, one step back126 result = runner.invoke(test_no, input="y\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 127why, one step back127 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 128why, one step back128 assert result.output == "Foo [Y/n]: y\nyes!\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 129why, one step back129Merge branch '7.x'David Lordbefore adoption7239794
line 130why, one step back130 result = runner.invoke(test_no, input="\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 131why, one step back131 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 132why, one step back132 assert result.output == "Foo [Y/n]: \nyes!\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 133why, one step back133Merge branch '7.x'David Lordbefore adoption7239794
line 134why, one step back134 result = runner.invoke(test_no, input="n\n")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 135why, one step back135 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 136why, one step back136 assert result.output == "Foo [Y/n]: n\nno :(\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 137–138why, one step back137138Merge pull request #1402 from yurihs/add-repeat-behavior-to-promptDavid Lordbefore adoption719690a
lines 139–146why, one step back139def test_confirm_repeat(runner):140 cli = click.Command(141 "cli", params=[click.Option(["--a/--no-a"], default=None, prompt=True)]142 )143 result = runner.invoke(cli, input="\ny\n")144 assert result.output == "A [y/n]: \nError: invalid input\nA [y/n]: y\n"145146Merge branch '7.x'David Lordbefore adoption7239794
line 147why, one step back147@pytest.mark.skipif(WIN, reason="Different behavior on windows.")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 148–151why, one step back148def test_prompts_abort(monkeypatch, capsys):149 def f(_):150 raise KeyboardInterrupt()151Merge branch '7.x'David Lordbefore adoption7239794
line 152why, one step back152 monkeypatch.setattr("click.termui.hidden_prompt_func", f)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 153–154why, one step back153154 try:Merge branch '7.x'David Lordbefore adoption7239794
line 155why, one step back155 click.prompt("Password", hide_input=True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 156why, one step back156 except click.Abort:Merge branch '7.x'David Lordbefore adoption7239794
line 157why, one step back157 click.echo("Screw you.")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 158–159why, one step back158159 out, err = capsys.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
line 160why, one step back160 assert out == "Password: \nScrew you.\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 161–163why, one step back161162163def _test_gen_func():Merge branch '7.x'David Lordbefore adoption7239794
lines 164–167why, one step back164 yield "a"165 yield "b"166 yield "c"167 yield "abc"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 168–169why, one step back168169Merge branch '7.x'David Lordbefore adoption7239794
lines 170–184why, one step back170@pytest.mark.skipif(WIN, reason="Different behavior on windows.")171@pytest.mark.parametrize("cat", ["cat", "cat ", "cat "])172@pytest.mark.parametrize(173 "test",174 [175 # We need lambda here, because pytest will176 # reuse the parameters, and then the generators177 # are already used and will not yield anymore178 ("just text\n", lambda: "just text"),179 ("iterable\n", lambda: ["itera", "ble"]),180 ("abcabc\n", lambda: _test_gen_func),181 ("abcabc\n", lambda: _test_gen_func()),182 ("012345\n", lambda: (c for c in range(6))),183 ],184)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 185why, one step back185def test_echo_via_pager(monkeypatch, capfd, cat, test):Merge branch '7.x'David Lordbefore adoption7239794
lines 186–187why, one step back186 monkeypatch.setitem(os.environ, "PAGER", cat)187 monkeypatch.setattr(click._termui_impl, "isatty", lambda x: True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 188–197why, one step back188189 expected_output = test[0]190 test_input = test[1]()191192 click.echo_via_pager(test_input)193194 out, err = capfd.readouterr()195 assert out == expected_output196197Merge branch '7.x'David Lordbefore adoption7239794
line 198why, one step back198@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 199–200why, one step back199def test_echo_color_flag(monkeypatch, capfd):200 isatty = TrueMerge branch '7.x'David Lordbefore adoption7239794
line 201why, one step back201 monkeypatch.setattr(click._compat, "isatty", lambda x: isatty)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 202why, one step back202Merge branch '7.x'David Lordbefore adoption7239794
lines 203–205why, one step back203 text = "foo"204 styled_text = click.style(text, fg="red")205 assert styled_text == "\x1b[31mfoo\x1b[0m"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 206–208why, one step back206207 click.echo(styled_text, color=False)208 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 209why, one step back209 assert out == f"{text}\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 210–212why, one step back210211 click.echo(styled_text, color=True)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"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 214–217why, one step back214215 isatty = True216 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"{styled_text}\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 219–222why, one step back219220 isatty = False221 click.echo(styled_text)222 out, err = capfd.readouterr()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 223why, one step back223 assert out == f"{text}\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 224–225why, one step back224225Merge pull request #1517 from stgraham2000/masterDavid Lordbefore adoption801a29f
lines 226–232why, one step back226def test_prompt_cast_default(capfd, monkeypatch):227 monkeypatch.setattr(sys, "stdin", StringIO("\n"))228 value = click.prompt("value", default="100", type=int)229 capfd.readouterr()230 assert type(value) is int231232Merge branch '7.x'David Lordbefore adoption7239794
line 233why, one step back233@pytest.mark.skipif(WIN, reason="Test too complex to make work windows.")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 234–236why, one step back234def test_echo_writing_to_standard_error(capfd, monkeypatch):235 def emulate_input(text):236 """Emulate keyboard input."""Merge branch '7.x'David Lordbefore adoption7239794
line 237why, one step back237 monkeypatch.setattr(sys, "stdin", StringIO(text))Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 238why, one step back238Merge branch '7.x'David Lordbefore adoption7239794
line 239why, one step back239 click.echo("Echo to standard output")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 240why, one step back240 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 241–242why, one step back241 assert out == "Echo to standard output\n"242 assert err == ""Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 243why, one step back243Merge branch '7.x'David Lordbefore adoption7239794
line 244why, one step back244 click.echo("Echo to standard error", err=True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 245why, one step back245 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 246–247why, one step back246 assert out == ""247 assert err == "Echo to standard error\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 248why, one step back248Merge branch '7.x'David Lordbefore adoption7239794
lines 249–250why, one step back249 emulate_input("asdlkj\n")250 click.prompt("Prompt to stdin")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 251why, one step back251 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 252–253why, one step back252 assert out == "Prompt to stdin: "253 assert err == ""Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 254why, one step back254Merge branch '7.x'David Lordbefore adoption7239794
lines 255–256why, one step back255 emulate_input("asdlkj\n")256 click.prompt("Prompt to stderr", err=True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 257why, one step back257 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 258–259why, one step back258 assert out == ""259 assert err == "Prompt to stderr: "Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 260why, one step back260Merge branch '7.x'David Lordbefore adoption7239794
lines 261–262why, one step back261 emulate_input("y\n")262 click.confirm("Prompt to stdin")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 263why, one step back263 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 264–265why, one step back264 assert out == "Prompt to stdin [y/N]: "265 assert err == ""Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 266why, one step back266Merge branch '7.x'David Lordbefore adoption7239794
lines 267–268why, one step back267 emulate_input("y\n")268 click.confirm("Prompt to stderr", err=True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 269why, one step back269 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 270–271why, one step back270 assert out == ""271 assert err == "Prompt to stderr [y/N]: "Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 272why, one step back272Merge branch '7.x'David Lordbefore adoption7239794
lines 273–274why, one step back273 monkeypatch.setattr(click.termui, "isatty", lambda x: True)274 monkeypatch.setattr(click.termui, "getchar", lambda: " ")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 275why, one step back275Merge branch '7.x'David Lordbefore adoption7239794
line 276why, one step back276 click.pause("Pause to stdout")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 277why, one step back277 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 278–279why, one step back278 assert out == "Pause to stdout\n"279 assert err == ""Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 280why, one step back280Merge branch '7.x'David Lordbefore adoption7239794
line 281why, one step back281 click.pause("Pause to stderr", err=True)Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 282why, one step back282 out, err = capfd.readouterr()Merge branch '7.x'David Lordbefore adoption7239794
lines 283–284why, one step back283 assert out == ""284 assert err == "Pause to stderr\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 285–286why, one step back285286Merge pull request #1591 from andy-maier/andy/fix-unsupportedoperationDavid Lordbefore adoption090ff26
lines 287–292why, one step back287def test_echo_with_capsys(capsys):288 click.echo("Capture me.")289 out, err = capsys.readouterr()290 assert out == "Capture me.\n"291292Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 293why, one step back293def test_open_file(runner):Merge branch '7.x'David Lordbefore adoption2269786
line 294why, one step back294 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 295why, one step back295 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
lines 296–299why, one step back296 def cli(filename):297 with click.open_file(filename) as f:298 click.echo(f.read())299Merge branch '7.x'David Lordbefore adoption7239794
line 300why, one step back300 click.echo("meep")Merge branch '7.x'David Lordbefore adoption2269786
line 301why, one step back301Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 302why, one step back302 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 303–304why, one step back303 with open("hello.txt", "w") as f:304 f.write("Cool stuff")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 305why, one step back305Merge branch '7.x'David Lordbefore adoption7239794
line 306why, one step back306 result = runner.invoke(cli, ["hello.txt"])Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 307why, one step back307 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 308why, one step back308 assert result.output == "Cool stuff\nmeep\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 309why, one step back309Merge branch '7.x'David Lordbefore adoption7239794
line 310why, one step back310 result = runner.invoke(cli, ["-"], input="foobar")Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
line 311why, one step back311 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 312why, one step back312 assert result.output == "foobar\nmeep\n"Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 313–314why, one step back313314Merge branch '7.x'David Lordbefore adoption2269786
lines 315–352why, one step back315def test_open_file_ignore_errors_stdin(runner):316 @click.command()317 @click.argument("filename")318 def cli(filename):319 with click.open_file(filename, errors="ignore") as f:320 click.echo(f.read())321322 result = runner.invoke(cli, ["-"], input=os.urandom(16))323 assert result.exception is None324325326def test_open_file_respects_ignore(runner):327 with runner.isolated_filesystem():328 with open("test.txt", "w") as f:329 f.write("Hello world!")330331 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:332 assert f.errors == "ignore"333334335def test_open_file_ignore_invalid_utf8(runner):336 with runner.isolated_filesystem():337 with open("test.txt", "wb") as f:338 f.write(b"\xe2\x28\xa1")339340 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:341 f.read()342343344def test_open_file_ignore_no_encoding(runner):345 with runner.isolated_filesystem():346 with open("test.bin", "wb") as f:347 f.write(os.urandom(16))348349 with click.open_file("test.bin", errors="ignore") as f:350 f.read()351352Merge branch '7.x'David Lordbefore adoption7239794
lines 353–354why, one step back353@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")354@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Merge branch '7.x'David Lordbefore adoption2269786
lines 355–356why, one step back355def test_open_file_atomic_permissions_existing_file(runner, permissions):356 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
lines 357–359why, one step back357 with open("existing.txt", "w") as f:358 f.write("content")359 os.chmod("existing.txt", permissions)Merge branch '7.x'David Lordbefore adoption2269786
lines 360–361why, one step back360361 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 362why, one step back362 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 363why, one step back363 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 364why, one step back364 click.open_file(filename, "w", atomic=True).close()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, ["existing.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("existing.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 369–370why, one step back369370Merge branch '7.x'David Lordbefore adoption7239794
line 371why, one step back371@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")Merge branch '7.x'David Lordbefore adoption2269786
lines 372–373why, one step back372def test_open_file_atomic_permissions_new_file(runner):373 with runner.isolated_filesystem():Merge branch '7.x'David Lordbefore adoption7239794
line 374why, one step back374Merge branch '7.x'David Lordbefore adoption2269786
line 375why, one step back375 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 376why, one step back376 @click.argument("filename")Merge branch '7.x'David Lordbefore adoption2269786
line 377why, one step back377 def cli(filename):Merge branch '7.x'David Lordbefore adoption7239794
line 378why, one step back378 click.open_file(filename, "w", atomic=True).close()Merge branch '7.x'David Lordbefore adoption2269786
lines 379–381why, one step back379380 # Create a test file to get the expected permissions for new files381 # according to the current umask.Merge branch '7.x'David Lordbefore adoption7239794
line 382why, one step back382 with open("test.txt", "w"):Merge branch '7.x'David Lordbefore adoption2269786
line 383why, one step back383 passMerge branch '7.x'David Lordbefore adoption7239794
line 384why, one step back384 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)Merge branch '7.x'David Lordbefore adoption2269786
line 385why, one step back385Merge branch '7.x'David Lordbefore adoption7239794
line 386why, one step back386 result = runner.invoke(cli, ["new.txt"])Merge branch '7.x'David Lordbefore adoption2269786
line 387why, one step back387 assert result.exception is NoneMerge branch '7.x'David Lordbefore adoption7239794
line 388why, one step back388 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsMerge branch '7.x'David Lordbefore adoption2269786
lines 389–390why, one step back389390Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 391–392why, one step back391def test_iter_keepopenfile(tmpdir):392 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 393why, one step back393 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 394why, one step back394 p.write("\n".join(expected))Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 395–401why, one step back395 with p.open() as f:396 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):397 assert e_line == a_line.strip()398399400def test_iter_lazyfile(tmpdir):401 expected = list(map(str, range(10)))Merge branch '7.x'David Lordbefore adoption7239794
line 402why, one step back402 p = tmpdir.mkdir("testdir").join("testfile")Merge branch '7.x'David Lordbefore adoption2269786
line 403why, one step back403 p.write("\n".join(expected))Clarify optparse relationshipArmin Ronacherbefore adoption3ce663c
lines 404–407why, one step back404 with p.open() as f:405 with click.utils.LazyFile(f.name) as lf:406 for e_line, a_line in zip(expected, lf):407 assert e_line == a_line.strip()Merge pull request #1609 from MLH-Fellowship/program-name-detectionDavid Lordbefore adoption6c1eaba
lines 408–436why, one step back408409410class MockMain:411 __slots__ = "__package__"412413 def __init__(self, package_name):414 self.__package__ = package_name415416417@pytest.mark.parametrize(418 ("path", "main", "expected"),419 [420 ("example.py", None, "example.py"),421 (str(pathlib.Path("/foo/bar/example.py")), None, "example.py"),422 ("example", None, "example"),423 (424 str(pathlib.Path("example/__main__.py")),425 MockMain(".example"),426 "python -m example",427 ),428 (429 str(pathlib.Path("example/cli.py")),430 MockMain(".example"),431 "python -m example.cli",432 ),433 ],434)435def test_detect_program_name(path, main, expected):436 assert click.utils._detect_program_name(path, _main=main) == expectedMerge pull request #1830 from pallets/windows-globDavid Lordbefore adoption9330dba
lines 437–446why, one step back437438439def test_expand_args(monkeypatch):440 user = os.path.expanduser("~")441 assert user in click.utils._expand_args(["~"])442 monkeypatch.setenv("CLICK_TEST", "hello")443 assert "hello" in click.utils._expand_args(["$CLICK_TEST"])444 assert "setup.cfg" in click.utils._expand_args(["*.cfg"])445 assert os.path.join("docs", "conf.py") in click.utils._expand_args(["**/conf.py"])446 assert "*.not-found" in click.utils._expand_args(["*.not-found"])