Skip to content
Myr

Merge branch '8.0.x'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parents 0a81393, 41f5b7a · Browse files · 8 files+69 added−73 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+69 added−73 removed

  • tests:+45 added−57 removed
  • other:+24 added−16 removed

8 files+69 added−73 removed

modified:src/click/core.py+3 added-1 removed
737 unmodified lines
738 ctx = __self._make_sub_context(other_cmd)739740 for param in other_cmd.params:741 if param.name not in kwargs and param.expose_value:742 kwargs[param.name] = param.get_default(ctx) # type: ignore742 kwargs[param.name] = param.type_cast_value( # type: ignore743 ctx, param.get_default(ctx)744 )745746 # Track all kwargs as params, so that forward() will pass747 # them on in subsequent calls.748 ctx.params.update(kwargs)
2212 unmodified lines
modified:src/click/termui.py+4 added-2 removed
229 unmodified lines
230 while True:231 try:232 # Write the prompt separately so that we get nice233 # coloring through colorama on Windows234 echo(prompt, nl=False, err=err)235 value = visible_prompt_func("").lower().strip()234 echo(prompt.rstrip(" "), nl=False, err=err)235 # Echo a space to stdout to work around an issue where236 # readline causes backspace to clear the whole line.237 value = visible_prompt_func(" ").lower().strip()238 except (KeyboardInterrupt, EOFError):239 raise Abort() from None240 if value in ("y", "yes"):241 rv = True
568 unmodified lines
modified:src/click/types.py+4 added-13 removed
834 unmodified lines
835 is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")836837 if not is_dash:838 if self.resolve_path:839 # Get the absolute directory containing the path.840 dir_ = os.path.dirname(os.path.abspath(rv))839 # os.path.realpath doesn't resolve symlinks on Windows840 # until Python 3.8. Use pathlib for now.841 import pathlib842842 # Resolve a symlink. realpath on Windows Python < 3.9843 # doesn't resolve symlinks. This might return a relative844 # path even if the path to the link is absolute.845 if os.path.islink(rv):846 rv = os.readlink(rv)843 rv = os.fsdecode(pathlib.Path(rv).resolve())844848 # Join dir_ with the resolved symlink if the resolved849 # path is relative. This will make it relative to the850 # original containing directory.851 if not os.path.isabs(rv):852 rv = os.path.join(dir_, rv)853845 try:846 st = os.stat(rv)847 except OSError:848 if not self.exists:
204 unmodified lines
modified:tests/conftest.py+14 added-18 removed
1import os2import shutil2import tempfile34import pytest5
4 unmodified lines
10def runner(request):11 return CliRunner()121315def check_symlink_impl():16 """This function checks if using symlinks is allowed17 on the host machine"""18 tempdir = tempfile.mkdtemp(prefix="click-")19 test_pth = os.path.join(tempdir, "check_sym_impl")20 sym_pth = os.path.join(tempdir, "link")21 open(test_pth, "w").close()22 rv = True23 try:24 os.symlink(test_pth, sym_pth)25 except (NotImplementedError, OSError):26 # Creating symlinks on Windows require elevated access.27 # OSError is thrown if the function is called without it.28 rv = False29 finally:30 shutil.rmtree(tempdir, ignore_errors=True)31 return rv14def _check_symlinks_supported():15 with tempfile.TemporaryDirectory(prefix="click-pytest-") as tempdir:16 target = os.path.join(tempdir, "target")17 open(target, "w").close()18 link = os.path.join(tempdir, "link")1920 try:21 os.symlink(target, link)22 return True23 except OSError:24 return False252627symlinks_supported = _check_symlinks_supported()
modified:tests/test_commands.py+9 added-7 removed
244 unmodified lines
245 def cli(ctx):246 return ctx.invoke(other_cmd)247248 @click.command()249 @click.option("--foo", type=click.INT, default=42)249 @click.option("-a", type=click.INT, default=42)250 @click.option("-b", type=click.INT, default="15")251 @click.option("-c", multiple=True)252 @click.pass_context251 def other_cmd(ctx, foo):252 assert ctx.info_name == "other-cmd"253 click.echo(foo)253 def other_cmd(ctx, a, b, c):254 return ctx.info_name, a, b, c255255 result = runner.invoke(cli, [])256 assert not result.exception257 assert result.output == "42\n"256 result = runner.invoke(cli, standalone_mode=False)257 # invoke should type cast default values, str becomes int, empty258 # multiple should be empty tuple instead of None259 assert result.return_value == ("other-cmd", 42, 15, ())260261262def test_invoked_subcommand(runner):263 @click.group(invoke_without_command=True)
91 unmodified lines
modified:tests/test_types.py+20 added-30 removed
1import os.path2import pathlib34import pytest5from conftest import check_symlink_impl5from conftest import symlinks_supported67import click89
93 unmodified lines
103 assert result.exception is None104 assert result.return_value == expect105106107@pytest.mark.skipif(not check_symlink_impl(), reason="symlink not allowed on device")108@pytest.mark.parametrize(109 ("sym_file", "abs_fun"),110 [111 (("relative_symlink",), os.path.basename),112 (("test", "absolute_symlink"), lambda x: x),113 ],107@pytest.mark.skipif(108 not symlinks_supported, reason="The current OS or FS doesn't support symlinks."109)115def test_symlink_resolution(tmpdir, sym_file, abs_fun):116 """This test ensures symlinks are properly resolved by click"""117 tempdir = str(tmpdir)118 real_path = os.path.join(tempdir, "test_file")119 sym_path = os.path.join(tempdir, *sym_file)110def test_path_resolve_symlink(tmp_path, runner):111 test_file = tmp_path / "file"112 test_file_str = os.fsdecode(test_file)113 test_file.write_text("")114121 # create dirs and files122 os.makedirs(os.path.join(tempdir, "test"), exist_ok=True)123 open(real_path, "w").close()124 os.symlink(abs_fun(real_path), sym_path)115 path_type = click.Path(resolve_path=True)116 param = click.Argument(["a"], type=path_type)117 ctx = click.Context(click.Command("cli", params=[param]))118126 # test127 ctx = click.Context(click.Command("do_stuff"))128 rv = click.Path(resolve_path=True).convert(sym_path, None, ctx)119 test_dir = tmp_path / "dir"120 test_dir.mkdir()121130 # os.readlink prepends path prefixes to absolute131 # links in windows.132 # https://docs.microsoft.com/en-us/windows/win32/133 # ... fileio/naming-a-file#win32-file-namespaces134 #135 # Here we strip win32 path prefix from the resolved path136 rv_drive, rv_path = os.path.splitdrive(rv)137 stripped_rv_drive = rv_drive.split(os.path.sep)[-1]138 rv = os.path.join(stripped_rv_drive, rv_path)122 abs_link = test_dir / "abs"123 abs_link.symlink_to(test_file)124 abs_rv = path_type.convert(os.fsdecode(abs_link), param, ctx)125 assert abs_rv == test_file_str126140 assert rv == real_path127 rel_link = test_dir / "rel"128 rel_link.symlink_to(pathlib.Path("..") / "file")129 rel_rv = path_type.convert(os.fsdecode(rel_link), param, ctx)130 assert rel_rv == test_file_str
modified:tests/test_utils.py+2 added-2 removed
273 unmodified lines
274275 emulate_input("y\n")276 click.confirm("Prompt to stderr", err=True)277 out, err = capfd.readouterr()278 assert out == ""279 assert err == "Prompt to stderr [y/N]: "278 assert out == " "279 assert err == "Prompt to stderr [y/N]:"280281 monkeypatch.setattr(click.termui, "isatty", lambda x: True)282 monkeypatch.setattr(click.termui, "getchar", lambda: " ")283
207 unmodified lines
modified:CHANGES.rst+13 added
7 unmodified lines
8- Single options boolean flags with ``show_default=True`` only show9 the default if it is ``True``. :issue:`1971`:101112Version 8.0.313-------------1415Released 2021-10-101617- Fix issue with ``Path(resolve_path=True)`` type creating invalid18 paths. :issue:`2088`19- Importing ``readline`` does not cause the ``confirm()`` prompt to20 disappear when pressing backspace. :issue:`2092`21- Any default values injected by ``invoke()`` are cast to the22 corresponding parameter's type. :issue:`2089, 2090`232425Version 8.0.226-------------2728Released 2021-10-08
915 unmodified lines