Skip to content

Commit

Permalink
Fix #23 -- implement output message template format (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki and codingjoe committed Oct 4, 2022
1 parent 3c5a78c commit 0dc9dc3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ option `--diff [-d]` or `--git-diff [-g]`:
git diff --unified=0 | relint my_file.py --diff
```

### Custom message format

Customize the output message format with the `--msg-template=<format string>` option.
[Python format syntax](https://docs.python.org/3/library/string.html#formatstrings)
is suported for the message template and the following fields are available:

* filename

The name of the file being linted.

* line_no

The line number of the match.

* match

The matched text.

* test.*

Any attribute of the test rule, e.g. `test.name` or `test.hint`.


### pre-commit

You can automate the linting process by adding a
Expand Down
10 changes: 9 additions & 1 deletion relint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def parse_args(args):
parser.add_argument(
"-W", "--fail-warnings", action="store_true", help="Fail for warnings."
)
parser.add_argument(
"--msg-template",
metavar="MSG_TEMPLATE",
type=str,
default="{filename}:{line_no} {test.name}\nHint: {test.hint}\n{match}",
help="Template used to display messages. "
r"Default: {filename}:{line_no} {test.name}\nHint: {test.hint}\n{match}",
)
return parser.parse_args(args=args)


Expand Down Expand Up @@ -73,7 +81,7 @@ def main(args=sys.argv[1:]):
changed_content = parse_diff(output)
matches = match_with_diff_changes(changed_content, matches)

exit_code = print_culprits(matches)
exit_code = print_culprits(matches, args.msg_template)
exit(exit_code)


Expand Down
25 changes: 12 additions & 13 deletions relint/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def split_diff_content_by_filename(output: str) -> {str: str}:
return content_by_filename


def print_culprits(matches):
def print_culprits(matches, msg_template):
exit_code = 0
_filename = ""
lines = []
Expand All @@ -96,24 +96,23 @@ def print_culprits(matches):

start_line_no = match.string[: match.start()].count("\n")
end_line_no = match.string[: match.end()].count("\n")
output_format = "{filename}:{line_no} {test.name}"
print(
output_format.format(
filename=filename,
line_no=start_line_no + 1,
test=test,
)
)
if test.hint:
print("Hint:", test.hint)
match_lines = (
"{line_no}> {code_line}".format(
line_no=no + start_line_no + 1,
code_line=line,
code_line=line.lstrip(),
)
for no, line in enumerate(lines[start_line_no : end_line_no + 1])
)
print(*match_lines, sep="\n")
# special characters from shell are escaped
msg_template = msg_template.replace("\\n", "\n")
print(
msg_template.format(
filename=filename,
line_no=start_line_no + 1,
test=test,
match=chr(10).join(match_lines),
)
)

return exit_code

Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/.relint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@
hint: Get it done right away!
filePattern: ^(?!.*test_).*\.(py|js)$
error: false

- name: Not Unicode
pattern: '[tT][oO][dD][oO]'
filePattern: .*
error: false
17 changes: 17 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ def test_main_execution(self, filename, tmpdir, fixture_dir):

assert exc_info.value.code == 0

def test_main_execution_with_custom_template(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")

with tmpdir.as_cwd():
with pytest.raises(SystemExit) as exc_info:
template = r"😵{filename}:{line_no} | {test.name} \n {match}"
main(["relint.py", "dummy.py", "--msg-template", template])

expected_message = "😵dummy.py:1 | No ToDo \n" " 1> # TODO do something\n"

out, _ = capsys.readouterr()
assert expected_message == out
assert exc_info.value.code == 0

@pytest.mark.parametrize("filename", ["test_parse.py", "[a-b].py", "[b-a].py"])
def test_raise_for_warnings(self, filename, tmpdir, fixture_dir):
with (fixture_dir / ".relint.yml").open() as fs:
Expand Down

0 comments on commit 0dc9dc3

Please sign in to comment.