From b9a76f1e01ebc758ef17e5f689a5ff8a9622ffb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Wed, 20 Nov 2024 21:20:41 +0100 Subject: [PATCH] Add test for Check.format_issue/2 --- lib/credo/check.ex | 15 ++++++++++----- test/credo/check_test.exs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/credo/check.ex b/lib/credo/check.ex index 6eda6b9ae..d09f88240 100644 --- a/lib/credo/check.ex +++ b/lib/credo/check.ex @@ -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] @@ -723,6 +725,8 @@ defmodule Credo.Check do end %Issue{ + check: check, + category: issue_category, priority: priority, filename: source_file.filename, message: opts[:message], @@ -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 diff --git a/test/credo/check_test.exs b/test/credo/check_test.exs index 53a106f86..6d82ca688 100644 --- a/test/credo/check_test.exs +++ b/test/credo/check_test.exs @@ -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