Skip to content

Commit

Permalink
Make hints optional (#52)
Browse files Browse the repository at this point in the history

Co-authored-by: Johannes Maron <[email protected]>
  • Loading branch information
El-Virus and codingjoe committed Jun 10, 2023
1 parent 4a4e4ba commit 1663426
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
63 changes: 36 additions & 27 deletions relint/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,36 @@ def print_culprits(matches, args):
if args.summarize:
match_groups[test].append(f"{filename}:{start_line_no}")
else:
hint = Panel(
Markdown(test.hint, justify="left"),
title="Hint:",
title_align="left",
padding=(0, 2),
)
message_bits = []

if args.code_padding == -1:
message = hint
else:
if args.code_padding != -1:
lexer = Syntax.guess_lexer(filename)
syntax = Syntax(
match.string,
lexer=lexer,
line_numbers=True,
line_range=(
start_line_no - args.code_padding,
end_line_no + args.code_padding,
),
highlight_lines=range(start_line_no, end_line_no + 1),
message_bits.append(
Syntax(
match.string,
lexer=lexer,
line_numbers=True,
line_range=(
start_line_no - args.code_padding,
end_line_no + args.code_padding,
),
highlight_lines=range(start_line_no, end_line_no + 1),
)
)

if test.hint:
message_bits.append(
Panel(
Markdown(test.hint, justify="left"),
title="Hint:",
title_align="left",
padding=(0, 2),
)
)
message = Group(syntax, hint)

messages.append(
Panel(
message,
Group(*message_bits),
title=f"{'Error' if test.error else 'Warning'}: {test.name}",
title_align="left",
subtitle=f"{filename}:{start_line_no}",
Expand All @@ -140,13 +144,18 @@ def print_culprits(matches, args):

if args.summarize:
for test, filenames in match_groups.items():
hint = Panel(
Markdown(test.hint, justify="left"),
title="Hint:",
title_align="left",
padding=(0, 2),
)
group = Group(Group(*filenames), hint)
group = Group(*filenames)
if test.hint:
group = Group(
group,
Panel(
Markdown(test.hint, justify="left"),
title="Hint:",
title_align="left",
padding=(0, 2),
),
)

messages.append(
Panel(
group,
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/.relint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
filePattern: ^(?!.*test_).*\.(py|js)$
error: true

- name: no hint
pattern: '(?i)hint'
filePattern: ^(?!.*test_).*\.(py|js)$
14 changes: 14 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def test_main_execution_with_error(self, capsys, tmpdir, fixture_dir):
assert "❱ 1 # FIXME do something" in out
assert exc_info.value.code == 1

@pytest.mark.parametrize("args", [tuple(), ("--summarize")])
def test_main_execution_without_hint(self, args, 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("# hint: 🤐")
with tmpdir.as_cwd():
with pytest.raises(SystemExit):
main(["relint.py", "dummy.py", *args])

out, _ = capsys.readouterr()
assert "dummy.py:1" in out
assert "Error: no hint" in out

def test_raise_for_warnings(self, tmpdir, fixture_dir):
with (fixture_dir / ".relint.yml").open() as fs:
config = fs.read()
Expand Down

0 comments on commit 1663426

Please sign in to comment.