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
1import os2import pathlib3import stat45import pytest67import click8from click._compat import WIN91011def test_open_file(runner, tmp_path):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 hello = tmp_path / "hello.txt"21 hello.write_text("Cool stuff")2223 result = runner.invoke(cli, [str(hello)])24 assert result.exception is None25 assert result.output == "Cool stuff\nmeep\n"2627 result = runner.invoke(cli, ["-"], input="foobar")28 assert result.exception is None29 assert result.output == "foobar\nmeep\n"303132def 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 None575859def test_open_file_respects_ignore(tmp_path):60 path = tmp_path / "test.txt"61 path.write_text("Hello world!")6263 with click.open_file(str(path), encoding="utf8", errors="ignore") as f:64 assert f.errors == "ignore"656667def test_open_file_ignore_invalid_utf8(tmp_path):68 path = tmp_path / "test.txt"69 path.write_bytes(b"\xe2\x28\xa1")7071 with click.open_file(str(path), encoding="utf8", errors="ignore") as f:72 f.read()737475def test_open_file_ignore_no_encoding(tmp_path):76 path = tmp_path / "test.bin"77 path.write_bytes(os.urandom(16))7879 with click.open_file(str(path), errors="ignore") as f:80 f.read()818283@pytest.mark.skipif(WIN, reason="os.chmod() is not fully supported on Windows.")84@pytest.mark.parametrize("permissions", [0o400, 0o444, 0o600, 0o644])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)8990 @click.command()91 @click.argument("filename")92 def cli(filename):93 click.open_file(filename, "w", atomic=True).close()9495 result = runner.invoke(cli, [str(existing)])96 assert result.exception is None97 assert stat.S_IMODE(os.stat(existing).st_mode) == permissions9899100@pytest.mark.skipif(WIN, reason="os.stat() is not fully supported on Windows.")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()106107 # 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)113114 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