Skip to content
Myr

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 file

Take it

Merge stable into main. (#3674)Rowlando13before adoption7925a34

lines 1–10why, one step back
1import os
2import pathlib3import stat45import pytest67import click8from click._compat import WIN910

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

line 11why, one step back
11def test_open_file(runner, tmp_path):

Merge stable into main. (#3674)Rowlando13before adoption7925a34

lines 12–19why, one step back
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")19

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 20–21why, one step back
20 hello = tmp_path / "hello.txt"
21 hello.write_text("Cool stuff")

Merge stable into main. (#3674)Rowlando13before adoption7925a34

line 22why, one step back
22

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 23–25why, one step back
23 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 back
26

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 27–29why, one step back
27 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 back
30
3132def 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 None5758

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 59–61why, one step back
59def 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 back
62

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 63–64why, one step back
63 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 back
65
66

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 67–69why, one step back
67def 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 back
70

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 71–72why, one step back
71 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 back
73
74

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 75–77why, one step back
75def 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 back
78

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 79–80why, one step back
79 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 back
81
8283@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 back
85def 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 back
89

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 90–93why, one step back
90 @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 back
94

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 95–97why, one step back
95 result = runner.invoke(cli, [str(existing)])
96 assert result.exception is None97 assert stat.S_IMODE(os.stat(existing).st_mode) == permissions

Merge stable into main. (#3674)Rowlando13before adoption7925a34

lines 98–100why, one step back
98
99100@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 back
101def 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 back
106

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 107–112why, one step back
107 # Create a test file to get the expected permissions for new files
108 # 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 back
113

Deprecate `isolated_filesystem` and document its limits (#3704)Rowlando13before adoptioncfa01ee

lines 114–117why, one step back
114 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