diff --git a/relint/parse.py b/relint/parse.py index c14c9bc..bb54e09 100644 --- a/relint/parse.py +++ b/relint/parse.py @@ -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): diff --git a/tests/test_parse.py b/tests/test_parse.py index 941afbe..aaf1a76 100644 --- a/tests/test_parse.py +++ b/tests/test_parse.py @@ -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): @@ -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("") @@ -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: