From 04e255d8da6b30b602f90bb65fe1361d47b7ce22 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 19 Jan 2021 11:48:06 +0100 Subject: [PATCH 1/2] Fix formatting of relative paths Prior to vulture 2.2, it was possible to pass an absolute path below the current directory, and the report showed a relative path instead: With vulture 2.1, inside `~/proj/qutebrowser/git/`: $ vulture ~/proj/qutebrowser/git/qutebrowser/qutebrowser.py qutebrowser/qutebrowser.py:138: unused function 'directory' (60% confidence) qutebrowser/qutebrowser.py:186: unused function 'main' (60% confidence) With vulture 2.3: $ vulture ~/proj/qutebrowser/git/qutebrowser/qutebrowser.py /home/florian/proj/qutebrowser/git/qutebrowser/qutebrowser.py:138: unused function 'directory' (60% confidence) /home/florian/proj/qutebrowser/git/qutebrowser/qutebrowser.py:186: unused function 'main' (60% confidence) This is due to #226, notably the `path.relative_to(os.curdir)` in `utils.format_path` - this won't work properly if the input is absolute: >>> import os, pathlib >>> os.getcwd() '/tmp' >>> pathlib.Path('/tmp/somefile').relative_to('.') Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.9/pathlib.py", line 928, in relative_to raise ValueError("{!r} is not in the subpath of {!r}" ValueError: '/tmp/somefile' is not in the subpath of '' OR one path is relative and the other is absolute. >>> pathlib.Path('/tmp/somefile').relative_to(pathlib.Path.cwd()) PosixPath('somefile') --- CHANGELOG.md | 5 +++++ tests/test_utils.py | 37 +++++++++++++++++++++++++++++++++++++ vulture/utils.py | 4 ++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a7d06dc..08200913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.4 (unreleased) + +* Print absolute filepaths as relative again (as with 2.1 and before) if they + are below the current directory. (The-Compiler, #246) + # 2.3 (2021-01-16) * Add [pre-commit](https://pre-commit.com) hook (Clément Robert, #244). diff --git a/tests/test_utils.py b/tests/test_utils.py index 176dc0c7..0dddda70 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,8 +1,45 @@ import ast +import os +import pathlib + +import pytest from vulture import utils +class TestFormatPath: + @pytest.fixture + def tmp_cwd(self, tmp_path, monkeypatch): + cwd = tmp_path / "workingdir" + cwd.mkdir() + monkeypatch.chdir(cwd) + return cwd + + def test_relative_inside(self): + filepath = pathlib.Path("testfile.py") + formatted = utils.format_path(filepath) + assert formatted == filepath + assert not formatted.is_absolute() + + def test_relative_outside(self, tmp_cwd): + filepath = pathlib.Path(os.pardir) / "testfile.py" + formatted = utils.format_path(filepath) + assert formatted == filepath + assert not formatted.is_absolute() + + def test_absolute_inside(self, tmp_cwd): + filepath = tmp_cwd / "testfile.py" + formatted = utils.format_path(filepath) + assert formatted == pathlib.Path("testfile.py") + assert not formatted.is_absolute() + + def test_absolute_outside(self, tmp_cwd): + filepath = (tmp_cwd / os.pardir / "testfile.py").resolve() + formatted = utils.format_path(filepath) + assert formatted == filepath + assert formatted.is_absolute() + + def check_decorator_names(code, expected_names): decorator_names = [] diff --git a/vulture/utils.py b/vulture/utils.py index 1e7bfa01..2e020128 100644 --- a/vulture/utils.py +++ b/vulture/utils.py @@ -1,6 +1,6 @@ import ast -import os import sys +import pathlib import tokenize @@ -47,7 +47,7 @@ def condition_is_always_true(condition): def format_path(path): try: - return path.relative_to(os.curdir) + return path.relative_to(pathlib.Path.cwd()) except ValueError: # Path is not below the current directory. return path From 5fb9d44b9d837bb4fc8209a915505036933893ad Mon Sep 17 00:00:00 2001 From: Jendrik Seipp Date: Thu, 21 Jan 2021 16:35:16 +0100 Subject: [PATCH 2/2] Sort imports. --- vulture/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vulture/utils.py b/vulture/utils.py index 2e020128..5799a837 100644 --- a/vulture/utils.py +++ b/vulture/utils.py @@ -1,6 +1,6 @@ import ast -import sys import pathlib +import sys import tokenize