diff options
-rw-r--r-- | CHANGES.rst | 9 | ||||
-rw-r--r-- | src/click/_compat.py | 6 | ||||
-rw-r--r-- | tests/test_arguments.py | 4 | ||||
-rw-r--r-- | tests/test_utils.py | 16 |
4 files changed, 30 insertions, 5 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index b65e91d..f46d446 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,5 +1,14 @@ .. currentmodule:: click +Version 8.0.4 +------------- + +Unreleased + +- ``open_file`` recognizes ``Path("-")`` as a standard stream, the + same as the string ``"-"``. :issue:`2106` + + Version 8.0.3 ------------- diff --git a/src/click/_compat.py b/src/click/_compat.py index b9e1f0d..7154dfd 100644 --- a/src/click/_compat.py +++ b/src/click/_compat.py @@ -388,9 +388,9 @@ def open_stream( ) -> t.Tuple[t.IO, bool]: binary = "b" in mode - # Standard streams first. These are simple because they don't need - # special handling for the atomic flag. It's entirely ignored. - if filename == "-": + # Standard streams first. These are simple because they ignore the + # atomic flag. Use fsdecode to handle Path("-"). + if os.fsdecode(filename) == "-": if any(m in mode for m in ["w", "a", "x"]): if binary: return get_binary_stdout(), False diff --git a/tests/test_arguments.py b/tests/test_arguments.py index f4d7afd..b4719d6 100644 --- a/tests/test_arguments.py +++ b/tests/test_arguments.py @@ -120,9 +120,9 @@ def test_file_args(runner): assert result.exit_code == 0 -def test_path_args(runner): +def test_path_allow_dash(runner): @click.command() - @click.argument("input", type=click.Path(dir_okay=False, allow_dash=True)) + @click.argument("input", type=click.Path(allow_dash=True)) def foo(input): click.echo(input) diff --git a/tests/test_utils.py b/tests/test_utils.py index 271177d..519b1a6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -320,6 +320,22 @@ def test_open_file(runner): assert result.output == "foobar\nmeep\n" +def test_open_file_pathlib_dash(runner): + @click.command() + @click.argument( + "filename", type=click.Path(allow_dash=True, path_type=pathlib.Path) + ) + def cli(filename): + click.echo(str(type(filename))) + + with click.open_file(filename) as f: + click.echo(f.read()) + + result = runner.invoke(cli, ["-"], input="value") + assert result.exception is None + assert result.output == "pathlib.Path\nvalue\n" + + def test_open_file_ignore_errors_stdin(runner): @click.command() @click.argument("filename") |