Skip to content
Myr

Merge pull request #2006 from MLH-Fellowship/1921-fix-symlinks

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

fix resolution of relative symlinks

authored by David Lord · committed by GitHub · parents 76cc18d, 986f322 · Browse files · 4 files+65 added−4 removed
This commit

State

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

Diff stats

4 files+65 added−4 removed

  • tests:+50 added−0 removed
  • other:+15 added−4 removed

4 files+65 added−4 removed

modified:src/click/types.py+13 added-4 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 # realpath on Windows Python < 3.8 doesn't resolve symlinks839 # Get the absolute directory containing the path.840 dir_ = os.path.dirname(os.path.abspath(rv))841842 # 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)847848 # Join dir_ with the resolved symlink. If the resolved849 # path is relative, this will make it relative to the850 # original containing directory. If it is absolute, this851 # has no effect.843 rv = os.path.realpath(rv)852 rv = os.path.join(dir_, rv)853854 try:855 st = os.stat(rv)856 except OSError:
22 unmodified lines
879 ),880 param,881 ctx,882 )874 if self.writable and not os.access(value, os.W_OK):883 if self.writable and not os.access(rv, os.W_OK):884 self.fail(885 _("{name} {filename!r} is not writable.").format(886 name=self.name.title(), filename=os.fsdecode(value)887 ),888 param,889 ctx,890 )882 if self.readable and not os.access(value, os.R_OK):891 if self.readable and not os.access(rv, os.R_OK):892 self.fail(893 _("{name} {filename!r} is not readable.").format(894 name=self.name.title(), filename=os.fsdecode(value)895 ),
166 unmodified lines
modified:tests/conftest.py+23 added
1import os2import shutil3import tempfile45import pytest67from click.testing import CliRunner8910@pytest.fixture(scope="function")11def runner(request):12 return CliRunner()131415def 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 rv
modified:tests/test_types.py+27 added
1import os.path2import pathlib34import pytest5from conftest import check_symlink_impl67import click89
91 unmodified lines
101 )102 result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)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 ],114)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)120121 # 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)125126 # test127 ctx = click.Context(click.Command("do_stuff"))128 rv = click.Path(resolve_path=True).convert(sym_path, None, ctx)129 assert rv == real_path
modified:CHANGES.rst+2 added
10 unmodified lines
11- Empty ``default`` value is not shown for ``multiple=True``.12 :issue:`1969`13- Fix shell completion for arguments that start with a forward slash14 such as absolute file paths. :issue:`1929`15- ``Path`` type with ``resolve_path=True`` resolves relative symlinks16 to be relative to the containing directory. :issue:`1921`171819Version 8.0.120-------------
889 unmodified lines