Skip to content

Commit 1663426

Browse files
El-Viruscodingjoe
andauthored
Make hints optional (#52)
Co-authored-by: Johannes Maron <[email protected]>
1 parent 4a4e4ba commit 1663426

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

Diff for: relint/parse.py

+36-27
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,36 @@ def print_culprits(matches, args):
103103
if args.summarize:
104104
match_groups[test].append(f"{filename}:{start_line_no}")
105105
else:
106-
hint = Panel(
107-
Markdown(test.hint, justify="left"),
108-
title="Hint:",
109-
title_align="left",
110-
padding=(0, 2),
111-
)
106+
message_bits = []
112107

113-
if args.code_padding == -1:
114-
message = hint
115-
else:
108+
if args.code_padding != -1:
116109
lexer = Syntax.guess_lexer(filename)
117-
syntax = Syntax(
118-
match.string,
119-
lexer=lexer,
120-
line_numbers=True,
121-
line_range=(
122-
start_line_no - args.code_padding,
123-
end_line_no + args.code_padding,
124-
),
125-
highlight_lines=range(start_line_no, end_line_no + 1),
110+
message_bits.append(
111+
Syntax(
112+
match.string,
113+
lexer=lexer,
114+
line_numbers=True,
115+
line_range=(
116+
start_line_no - args.code_padding,
117+
end_line_no + args.code_padding,
118+
),
119+
highlight_lines=range(start_line_no, end_line_no + 1),
120+
)
121+
)
122+
123+
if test.hint:
124+
message_bits.append(
125+
Panel(
126+
Markdown(test.hint, justify="left"),
127+
title="Hint:",
128+
title_align="left",
129+
padding=(0, 2),
130+
)
126131
)
127-
message = Group(syntax, hint)
128132

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

141145
if args.summarize:
142146
for test, filenames in match_groups.items():
143-
hint = Panel(
144-
Markdown(test.hint, justify="left"),
145-
title="Hint:",
146-
title_align="left",
147-
padding=(0, 2),
148-
)
149-
group = Group(Group(*filenames), hint)
147+
group = Group(*filenames)
148+
if test.hint:
149+
group = Group(
150+
group,
151+
Panel(
152+
Markdown(test.hint, justify="left"),
153+
title="Hint:",
154+
title_align="left",
155+
padding=(0, 2),
156+
),
157+
)
158+
150159
messages.append(
151160
Panel(
152161
group,

Diff for: tests/fixtures/.relint.yml

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@
3131
3232
filePattern: ^(?!.*test_).*\.(py|js)$
3333
error: true
34+
35+
- name: no hint
36+
pattern: '(?i)hint'
37+
filePattern: ^(?!.*test_).*\.(py|js)$

Diff for: tests/test_main.py

+14
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def test_main_execution_with_error(self, capsys, tmpdir, fixture_dir):
4444
assert "❱ 1 # FIXME do something" in out
4545
assert exc_info.value.code == 1
4646

47+
@pytest.mark.parametrize("args", [tuple(), ("--summarize")])
48+
def test_main_execution_without_hint(self, args, capsys, tmpdir, fixture_dir):
49+
with (fixture_dir / ".relint.yml").open() as fs:
50+
config = fs.read()
51+
tmpdir.join(".relint.yml").write(config)
52+
tmpdir.join("dummy.py").write("# hint: 🤐")
53+
with tmpdir.as_cwd():
54+
with pytest.raises(SystemExit):
55+
main(["relint.py", "dummy.py", *args])
56+
57+
out, _ = capsys.readouterr()
58+
assert "dummy.py:1" in out
59+
assert "Error: no hint" in out
60+
4761
def test_raise_for_warnings(self, tmpdir, fixture_dir):
4862
with (fixture_dir / ".relint.yml").open() as fs:
4963
config = fs.read()

0 commit comments

Comments
 (0)