resolve relative symlinks to the containing directory
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by cAtaman · committed by David Lord · parent 76cc18d · 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
Provenance
- Commit
986f322- Tree
f8cb477- Author
cAtaman <chimaataman@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
76cc18d
4 files changed+65 added−4 removed
modified:src/click/types.py+13 added-4 removed
834 unmodified lines835 is_dash = self.file_okay and self.allow_dash and rv in (b"-", "-")836 837 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))841+842+ # 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)847 848+ # 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)853 854 try:855 st = os.stat(rv)856 except OSError:22 unmodified lines879 ),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 linesmodified:tests/conftest.py+23 added
1+import os2+import shutil3+import tempfile4+5 import pytest6 7 from click.testing import CliRunner8 9 10 @pytest.fixture(scope="function")11 def runner(request):12 return CliRunner()13+14+15+def 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 rvmodified:tests/test_types.py+27 added
1+import os.path2 import pathlib3 4 import pytest5+from conftest import check_symlink_impl6 7 import click8 9 91 unmodified lines101 )102 result = runner.invoke(cli, ["a/b/c.txt"], standalone_mode=False)103 assert result.exception is None104 assert result.return_value == expect105+106+107+@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+)115+def 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)120+121+ # 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)125+126+ # test127+ ctx = click.Context(click.Command("do_stuff"))128+ rv = click.Path(resolve_path=True).convert(sym_path, None, ctx)129+ assert rv == real_pathmodified:CHANGES.rst+2 added
10 unmodified lines11 - 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`17 18 19 Version 8.0.120 -------------889 unmodified lines