Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix formatting of relative paths #246

Merged
merged 2 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
37 changes: 37 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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 = []

Expand Down
4 changes: 2 additions & 2 deletions vulture/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ast
import os
import pathlib
import sys
import tokenize

Expand Down Expand Up @@ -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
Expand Down