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
Provenance
- Commit
7007982- Tree
d8d35db- Author
Edward G <edward.g2013@gmail.com>- Committer
Edward G <edward.g2013@gmail.com>- Parent
18400b2
4 files changed+143 added−135 removed
added:tests/test_utils_2/test_KeepOpenFile.py+10 added
1+import click2+3+4+def 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
1+import click2+3+4+def 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
1+import os2+import pathlib3+import stat4+5+import pytest6+7+import click8+from click._compat import WIN9+10+11+def 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())17+18+ click.echo("meep")19+20+ with runner.isolated_filesystem():21+ with open("hello.txt", "w") as f:22+ f.write("Cool stuff")23+24+ result = runner.invoke(cli, ["hello.txt"])25+ assert result.exception is None26+ assert result.output == "Cool stuff\nmeep\n"27+28+ result = runner.invoke(cli, ["-"], input="foobar")29+ assert result.exception is None30+ assert result.output == "foobar\nmeep\n"31+32+33+def 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)))40+41+ with click.open_file(filename) as f:42+ click.echo(f.read())43+44+ result = runner.invoke(cli, ["-"], input="value")45+ assert result.exception is None46+ assert result.output == "pathlib.Path\nvalue\n"47+48+49+def 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())55+56+ result = runner.invoke(cli, ["-"], input=os.urandom(16))57+ assert result.exception is None58+59+60+def test_open_file_respects_ignore(runner):61+ with runner.isolated_filesystem():62+ with open("test.txt", "w") as f:63+ f.write("Hello world!")64+65+ with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:66+ assert f.errors == "ignore"67+68+69+def 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")73+74+ with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:75+ f.read()76+77+78+def 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))82+83+ with click.open_file("test.bin", errors="ignore") as f:84+ f.read()85+86+87+@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")88+@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])89+def 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)94+95+ @click.command()96+ @click.argument("filename")97+ def cli(filename):98+ click.open_file(filename, "w", atomic=True).close()99+100+ result = runner.invoke(cli, ["existing.txt"])101+ assert result.exception is None102+ assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissions103+104+105+@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")106+def test_open_file_atomic_permissions_new_file(runner):107+ with runner.isolated_filesystem():108+109+ @click.command()110+ @click.argument("filename")111+ def cli(filename):112+ click.open_file(filename, "w", atomic=True).close()113+114+ # 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)119+120+ result = runner.invoke(cli, ["new.txt"])121+ assert result.exception is None122+ assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissionsmodified:tests/test_utils.py-135 removed
1 import os2 import pathlib3-import stat3 4 import pytest5 6 import click._termui_impl7 import click.utils9-from click._compat import WIN8 9 12-def 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())18-19- click.echo("meep")20-21- with runner.isolated_filesystem():22- with open("hello.txt", "w") as f:23- f.write("Cool stuff")24-25- result = runner.invoke(cli, ["hello.txt"])26- assert result.exception is None27- assert result.output == "Cool stuff\nmeep\n"28-29- result = runner.invoke(cli, ["-"], input="foobar")30- assert result.exception is None31- assert result.output == "foobar\nmeep\n"32-33-34-def 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)))41-42- with click.open_file(filename) as f:43- click.echo(f.read())44-45- result = runner.invoke(cli, ["-"], input="value")46- assert result.exception is None47- assert result.output == "pathlib.Path\nvalue\n"48-49-50-def 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())56-57- result = runner.invoke(cli, ["-"], input=os.urandom(16))58- assert result.exception is None59-60-61-def test_open_file_respects_ignore(runner):62- with runner.isolated_filesystem():63- with open("test.txt", "w") as f:64- f.write("Hello world!")65-66- with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:67- assert f.errors == "ignore"68-69-70-def 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")74-75- with click.open_file("test.txt", encoding="utf8", errors="ignore") as f:76- f.read()77-78-79-def 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))83-84- with click.open_file("test.bin", errors="ignore") as f:85- f.read()86-87-88-@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")89-@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])90-def 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)95-96- @click.command()97- @click.argument("filename")98- def cli(filename):99- click.open_file(filename, "w", atomic=True).close()100-101- result = runner.invoke(cli, ["existing.txt"])102- assert result.exception is None103- assert stat.S_IMODE(os.stat("existing.txt").st_mode) == permissions104-105-106-@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")107-def test_open_file_atomic_permissions_new_file(runner):108- with runner.isolated_filesystem():109-110- @click.command()111- @click.argument("filename")112- def cli(filename):113- click.open_file(filename, "w", atomic=True).close()114-115- # 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)120-121- result = runner.invoke(cli, ["new.txt"])122- assert result.exception is None123- assert stat.S_IMODE(os.stat("new.txt").st_mode) == permissions124-125-126-def 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()133-134-135-def 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()143-144-10 class MockMain:11 __slots__ = "__package__"12 13 def __init__(self, package_name):66 unmodified lines