Skip to content

Commit

Permalink
Add test for Check.format_issue/2
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Nov 20, 2024
1 parent 47cad6b commit b9a76f1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/credo/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,9 @@ defmodule Credo.Check do

priority = Priority.to_integer(issue_priority)

exit_status_or_category = opts[:exit_status] || Params.exit_status(params, check) || issue_category
exit_status_or_category =
opts[:exit_status] || Params.exit_status(params, check) || issue_category

exit_status = Check.to_exit_status(exit_status_or_category)

line_no = opts[:line_no]
Expand All @@ -723,6 +725,8 @@ defmodule Credo.Check do
end

%Issue{
check: check,
category: issue_category,
priority: priority,
filename: source_file.filename,
message: opts[:message],
Expand All @@ -734,14 +738,15 @@ defmodule Credo.Check do
}
|> add_line_no_options(line_no, source_file)
|> add_column_if_missing(trigger, line_no, column, source_file)
|> add_check_and_category(check, issue_category)
|> force_priority_if_given(opts[:priority])
end

defp add_check_and_category(issue, check, issue_category) do
defp force_priority_if_given(issue, nil), do: issue

defp force_priority_if_given(issue, priority) do
%Issue{
issue
| check: check,
category: issue_category
| priority: priority
}
end

Expand Down
39 changes: 39 additions & 0 deletions test/credo/check_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,43 @@ defmodule Credo.CheckTest do

assert Credo.Check.Readability.ModuleDoc.id() == "EX3009"
end

defmodule MyCustomCheck1 do
use Credo.Check, category: :example, base_priority: :high

def run(%SourceFile{} = source_file, params \\ []) do
issue_meta = IssueMeta.for(source_file, params)

[
format_issue(issue_meta,
priority: 113,
trigger: :foobar,
line_no: 3,
column: 15,
exit_status: 23,
severity: 11
)
]
end
end

test "it should use format_issue/2" do
"""
defmodule AliasTest do
def test do
Any.Thing.foobar()
end
end
"""
|> to_source_file
|> run_check(MyCustomCheck1)
|> assert_issue(fn issue ->
assert issue.priority == 113
assert issue.trigger == "foobar"
assert issue.line_no == 3
assert issue.column == 15
assert issue.exit_status == 23
assert issue.severity == 11
end)
end
end

0 comments on commit b9a76f1

Please sign in to comment.