Skip to content
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
9 changes: 7 additions & 2 deletions relint/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
from rich.syntax import Syntax

GIT_DIFF_LINE_NUMBERS_PATTERN = re.compile(r"@ -\d+(,\d+)? \+(\d+)(,)?(\d+)? @")
GIT_DIFF_FILENAME_PATTERN = re.compile(r"(?:\n|^)diff --git a\/.* b\/(.*)(?:\n|$)")
GIT_DIFF_SPLIT_PATTERN = re.compile(r"(?:\n|^)diff --git a\/.* b\/.*(?:\n|$)")
GIT_DIFF_PREFIX_PATTERN = r"[abciow]/"
GIT_DIFF_FILENAME_PATTERN = re.compile(
rf"(?:\n|^)diff --git {GIT_DIFF_PREFIX_PATTERN}.* {GIT_DIFF_PREFIX_PATTERN}(.*)(?:\n|$)"
)
GIT_DIFF_SPLIT_PATTERN = re.compile(
rf"(?:\n|^)diff --git {GIT_DIFF_PREFIX_PATTERN}.* {GIT_DIFF_PREFIX_PATTERN}.*(?:\n|$)"
)


def lint_file(filename, tests):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def test_line_numbers_when_only_one_line_was_changed(self):
"diff --git a/pardal/authserver/app.py b/pardal/authserver/app.py",
"pardal/authserver/app.py",
),
(
"diff --git c/sql/very-important-sql.sql i/sql/very-important-sql.sql",
"sql/very-important-sql.sql",
),
],
)
def test_parse_filenames(self, output, expected_filename):
Expand Down Expand Up @@ -118,6 +122,21 @@ def test_parse_complete_diff(self):

assert parsed_content == expected

def test_parse_diff_with_mnemonic_prefixes(self):
output = (
"diff --git c/test_parse.py i/test_parse.py\n"
"index 9c7f392..9bde2ad 100644\n"
"--- c/test_parse.py\n"
"+++ i/test_parse.py\n"
"@@ -1,0 +2 @@\n"
"+# TODO: I'll do it later, promise\n"
)

parsed_content = parse_diff(output)
expected = {"test_parse.py": [2]}

assert parsed_content == expected

def test_empty_config_file(self, tmpdir):
tmpdir.join(".relint.yml").write("")
tmpdir.join("dummy.py").write("")
Expand Down Expand Up @@ -166,6 +185,26 @@ def test_git_diff(self, capsys, tmpdir, fixture_dir):

assert "0" in str(exc_info.value)

def test_git_diff_with_mnemonic_prefixes(self, capsys, tmpdir, fixture_dir):
with (fixture_dir / ".relint.yml").open() as fs:
config = fs.read()
tmpdir.join(".relint.yml").write(config)
tmpdir.join("dummy.py").write("# TODO do something")
subprocess.check_call(["git", "init"], cwd=tmpdir.strpath) # noqa: S607
subprocess.check_call(
["git", "config", "diff.mnemonicPrefix", "true"], # noqa: S607
cwd=tmpdir.strpath,
)
subprocess.check_call(["git", "add", "dummy.py"], cwd=tmpdir.strpath) # noqa: S607

with tmpdir.as_cwd():
with pytest.raises(SystemExit) as exc_info:
main(["dummy.py", "--git-diff"])

out, _ = capsys.readouterr()
assert "Get it done right away!" in out
assert exc_info.value.code == 0


def test_no_unicode(capsys, tmpdir, fixture_dir):
with (fixture_dir / ".relint.yml").open() as fs:
Expand Down