test_open_file.py
This file
Path
tests/test_utils/test_open_file.py
117 lines · 3.3 KiB · Python
Last changed by
Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
History of this fileTake it
Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 1–10why, one step back1import os2import pathlib3import stat45import pytest67import click8from click._compat import WIN910Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
line 11why, one step back11def test_open_file(runner, tmp_path):Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 12–19why, one step back12 @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")19Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 20–21why, one step back20 hello = tmp_path / "hello.txt"21 hello.write_text("Cool stuff")Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 22why, one step back22Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 23–25why, one step back23 result = runner.invoke(cli, [str(hello)])24 assert result.exception is None25 assert result.output == "Cool stuff\nmeep\n"Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 26why, one step back26Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 27–29why, one step back27 result = runner.invoke(cli, ["-"], input="foobar")28 assert result.exception is None29 assert result.output == "foobar\nmeep\n"Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 30–58why, one step back303132def test_open_file_pathlib_dash(runner):33 @click.command()34 @click.argument(35 "filename", type=click.Path(allow_dash=True, path_type=pathlib.Path)36 )37 def cli(filename):38 click.echo(str(type(filename)))3940 with click.open_file(filename) as f:41 click.echo(f.read())4243 result = runner.invoke(cli, ["-"], input="value")44 assert result.exception is None45 assert result.output == "pathlib.Path\nvalue\n"464748def test_open_file_ignore_errors_stdin(runner):49 @click.command()50 @click.argument("filename")51 def cli(filename):52 with click.open_file(filename, errors="ignore") as f:53 click.echo(f.read())5455 result = runner.invoke(cli, ["-"], input=os.urandom(16))56 assert result.exception is None5758Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 59–61why, one step back59def test_open_file_respects_ignore(tmp_path):60 path = tmp_path / "test.txt"61 path.write_text("Hello world!")Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 62why, one step back62Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 63–64why, one step back63 with click.open_file(str(path), encoding="utf8", errors="ignore") as f:64 assert f.errors == "ignore"Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 65–66why, one step back6566Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 67–69why, one step back67def test_open_file_ignore_invalid_utf8(tmp_path):68 path = tmp_path / "test.txt"69 path.write_bytes(b"\xe2\x28\xa1")Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 70why, one step back70Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 71–72why, one step back71 with click.open_file(str(path), encoding="utf8", errors="ignore") as f:72 f.read()Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 73–74why, one step back7374Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 75–77why, one step back75def test_open_file_ignore_no_encoding(tmp_path):76 path = tmp_path / "test.bin"77 path.write_bytes(os.urandom(16))Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 78why, one step back78Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 79–80why, one step back79 with click.open_file(str(path), errors="ignore") as f:80 f.read()Merge stable into main. (#3674)Rowlando13before adoption7925a34
lines 81–84why, one step back818283@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")84@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 85–88why, one step back85def test_open_file_atomic_permissions_existing_file(runner, tmp_path, permissions):86 existing = tmp_path / "existing.txt"87 existing.write_text("content")88 os.chmod(existing, permissions)Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 89why, one step back89Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 90–93why, one step back90 @click.command()91 @click.argument("filename")92 def cli(filename):93 click.open_file(filename, "w", atomic=True).close()Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 94why, one step back94Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 95–97why, one step back95 result = runner.invoke(cli, [str(existing)])96 assert result.exception is None97 assert stat.S_IMODE(os.stat(existing).st_mode) == permissionsMerge stable into main. (#3674)Rowlando13before adoption7925a34
lines 98–100why, one step back9899100@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 101–105why, one step back101def test_open_file_atomic_permissions_new_file(runner, tmp_path):102 @click.command()103 @click.argument("filename")104 def cli(filename):105 click.open_file(filename, "w", atomic=True).close()Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 106why, one step back106Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 107–112why, one step back107 # Create a test file to get the expected permissions for new files108 # according to the current umask.109 probe = tmp_path / "test.txt"110 with open(probe, "w"):111 pass112 permissions = stat.S_IMODE(os.stat(probe).st_mode)Merge stable into main. (#3674)Rowlando13before adoption7925a34
line 113why, one step back113Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee
lines 114–117why, one step back114 new = tmp_path / "new.txt"115 result = runner.invoke(cli, [str(new)])116 assert result.exception is None117 assert stat.S_IMODE(os.stat(new).st_mode) == permissions