Skip to content
Myr

Add KeepOpenFile, LazyFile and open_file files.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Edward G · committed by Edward G · parent 18400b2 · Browse files · 4 files+143 added−135 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+143 added−135 removed

  • tests:+143 added−135 removed

4 files+143 added−135 removed

added:tests/test_utils_2/test_KeepOpenFile.py+10 added
1import click234def test_iter_keepopenfile(tmpdir):5 expected = list(map(str, range(10)))6 p = tmpdir.mkdir("testdir").join("testfile")7 p.write("\n".join(expected))8 with p.open() as f:9 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f), strict=False):10 assert e_line == a_line.strip()
added:tests/test_utils_2/test_LazyFile.py+11 added
1import click234def test_iter_lazyfile(tmpdir):5 expected = list(map(str, range(10)))6 p = tmpdir.mkdir("testdir").join("testfile")7 p.write("\n".join(expected))8 with p.open() as f:9 with click.utils.LazyFile(f.name) as lf:10 for e_line, a_line in zip(expected, lf, strict=False):11 assert e_line == a_line.strip()
added:tests/test_utils_2/test_open_file.py+122 added
1import os2import pathlib3import stat45import pytest67import click8from click._compat import WIN91011def test_open_file(runner):12 @click.command()13 @click.argument("filename")14 def cli(filename):15 with click.open_file(filename) as f:16 click.echo(f.read())1718 click.echo("meep")1920 with runner.isolated_filesystem():21 with open("hello.txt", "w") as f:22 f.write("Cool stuff")2324 result = runner.invoke(cli, ["hello.txt"])25 assert result.exception is None26 assert result.output == "Cool stuff\nmeep\n"2728 result = runner.invoke(cli, ["-"], input="foobar")29 assert result.exception is None30 assert result.output == "foobar\nmeep\n"313233def test_open_file_pathlib_dash(runner):34 @click.command()35 @click.argument(36 "filename", type=click.Path(allow_dash=True, path_type=pathlib.Path)37 )38 def cli(filename):39 click.echo(str(type(filename)))4041 with click.open_file(filename) as f:42 click.echo(f.read())4344 result = runner.invoke(cli, ["-"], input="value")45 assert result.exception is None46 assert result.output == "pathlib.Path\nvalue\n"474849def test_open_file_ignore_errors_stdin(runner):50 @click.command()51 @click.argument("filename")52 def cli(filename):53 with click.open_file(filename, errors="ignore") as f:54 click.echo(f.read())5556 result = runner.invoke(cli, ["-"], input=os.urandom(16))57 assert result.exception is None585960def test_open_file_respects_ignore(runner):61 with runner.isolated_filesystem():62 with open("test.txt", "w") as f:63 f.write("Hello world!")6465 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:66 assert f.errors == "ignore"676869def test_open_file_ignore_invalid_utf8(runner):70 with runner.isolated_filesystem():71 with open("test.txt", "wb") as f:72 f.write(b"\xe2\x28\xa1")7374 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:75 f.read()767778def test_open_file_ignore_no_encoding(runner):79 with runner.isolated_filesystem():80 with open("test.bin", "wb") as f:81 f.write(os.urandom(16))8283 with click.open_file("test.bin", errors="ignore") as f:84 f.read()858687@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")88@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])89def test_open_file_atomic_permissions_existing_file(runner, permissions):90 with runner.isolated_filesystem():91 with open("existing.txt", "w") as f:92 f.write("content")93 os.chmod("existing.txt", permissions)9495 @click.command()96 @click.argument("filename")97 def cli(filename):98 click.open_file(filename, "w", atomic=True).close()99100 result = runner.invoke(cli, ["existing.txt"])101 assert result.exception is None102 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissions103104105@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")106def test_open_file_atomic_permissions_new_file(runner):107 with runner.isolated_filesystem():108109 @click.command()110 @click.argument("filename")111 def cli(filename):112 click.open_file(filename, "w", atomic=True).close()113114 # Create a test file to get the expected permissions for new files115 # according to the current umask.116 with open("test.txt", "w"):117 pass118 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)119120 result = runner.invoke(cli, ["new.txt"])121 assert result.exception is None122 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissions
modified:tests/test_utils.py-135 removed
1import os2import pathlib3import stat34import pytest56import click._termui_impl7import click.utils9from click._compat import WIN8912def test_open_file(runner):13 @click.command()14 @click.argument("filename")15 def cli(filename):16 with click.open_file(filename) as f:17 click.echo(f.read())1819 click.echo("meep")2021 with runner.isolated_filesystem():22 with open("hello.txt", "w") as f:23 f.write("Cool stuff")2425 result = runner.invoke(cli, ["hello.txt"])26 assert result.exception is None27 assert result.output == "Cool stuff\nmeep\n"2829 result = runner.invoke(cli, ["-"], input="foobar")30 assert result.exception is None31 assert result.output == "foobar\nmeep\n"323334def test_open_file_pathlib_dash(runner):35 @click.command()36 @click.argument(37 "filename", type=click.Path(allow_dash=True, path_type=pathlib.Path)38 )39 def cli(filename):40 click.echo(str(type(filename)))4142 with click.open_file(filename) as f:43 click.echo(f.read())4445 result = runner.invoke(cli, ["-"], input="value")46 assert result.exception is None47 assert result.output == "pathlib.Path\nvalue\n"484950def test_open_file_ignore_errors_stdin(runner):51 @click.command()52 @click.argument("filename")53 def cli(filename):54 with click.open_file(filename, errors="ignore") as f:55 click.echo(f.read())5657 result = runner.invoke(cli, ["-"], input=os.urandom(16))58 assert result.exception is None596061def test_open_file_respects_ignore(runner):62 with runner.isolated_filesystem():63 with open("test.txt", "w") as f:64 f.write("Hello world!")6566 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:67 assert f.errors == "ignore"686970def test_open_file_ignore_invalid_utf8(runner):71 with runner.isolated_filesystem():72 with open("test.txt", "wb") as f:73 f.write(b"\xe2\x28\xa1")7475 with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:76 f.read()777879def test_open_file_ignore_no_encoding(runner):80 with runner.isolated_filesystem():81 with open("test.bin", "wb") as f:82 f.write(os.urandom(16))8384 with click.open_file("test.bin", errors="ignore") as f:85 f.read()868788@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")89@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])90def test_open_file_atomic_permissions_existing_file(runner, permissions):91 with runner.isolated_filesystem():92 with open("existing.txt", "w") as f:93 f.write("content")94 os.chmod("existing.txt", permissions)9596 @click.command()97 @click.argument("filename")98 def cli(filename):99 click.open_file(filename, "w", atomic=True).close()100101 result = runner.invoke(cli, ["existing.txt"])102 assert result.exception is None103 assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissions104105106@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")107def test_open_file_atomic_permissions_new_file(runner):108 with runner.isolated_filesystem():109110 @click.command()111 @click.argument("filename")112 def cli(filename):113 click.open_file(filename, "w", atomic=True).close()114115 # Create a test file to get the expected permissions for new files116 # according to the current umask.117 with open("test.txt", "w"):118 pass119 permissions = stat.S_IMODE(os.stat("test.txt").st_mode)120121 result = runner.invoke(cli, ["new.txt"])122 assert result.exception is None123 assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissions124125126def test_iter_keepopenfile(tmpdir):127 expected = list(map(str, range(10)))128 p = tmpdir.mkdir("testdir").join("testfile")129 p.write("\n".join(expected))130 with p.open() as f:131 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f), strict=False):132 assert e_line == a_line.strip()133134135def test_iter_lazyfile(tmpdir):136 expected = list(map(str, range(10)))137 p = tmpdir.mkdir("testdir").join("testfile")138 p.write("\n".join(expected))139 with p.open() as f:140 with click.utils.LazyFile(f.name) as lf:141 for e_line, a_line in zip(expected, lf, strict=False):142 assert e_line == a_line.strip()14314410class MockMain:11 __slots__ = "__package__"1213 def __init__(self, package_name):
66 unmodified lines