diff --git a/lib/credo/check.ex b/lib/credo/check.ex index 292fadf41..3272ad6e1 100644 --- a/lib/credo/check.ex +++ b/lib/credo/check.ex @@ -705,6 +705,7 @@ defmodule Credo.Check do column = opts[:column] severity = opts[:severity] || Severity.default_value() trigger = opts[:trigger] + message = to_string(opts[:message]) trigger = if trigger == Issue.no_trigger() do @@ -713,12 +714,23 @@ defmodule Credo.Check do to_string(trigger) end + message = + if String.valid?(message) do + message + else + IO.warn( + "#{check_name(check)} creates an Issue with a `:message` containing invalid bytes: #{inspect(message)}" + ) + + "(see warning) #{inspect(message)}" + end + %Issue{ check: check, category: issue_category, priority: priority, filename: source_file.filename, - message: opts[:message], + message: message, trigger: trigger, line_no: line_no, column: column, @@ -764,6 +776,8 @@ defmodule Credo.Check do end end + defp check_name(module), do: Credo.Code.Name.full(module) + # Returns the scope for the given line as a tuple consisting of the call to # define the scope (`:defmodule`, `:def`, `:defp` or `:defmacro`) and the # name of the scope. diff --git a/lib/credo/plugin.ex b/lib/credo/plugin.ex index a17e8ead4..1c5bfaf3d 100644 --- a/lib/credo/plugin.ex +++ b/lib/credo/plugin.ex @@ -344,7 +344,9 @@ defmodule Credo.Plugin do import Credo.Plugin def init(exec) do - register_cli_switch(exec, :kastle, :string, :X, fn(switch_value) -> + exec + |> register_command("demo", CredoDemoPlugin.DemoCommand) + |> register_cli_switch(:kastle, :string, :X, fn(switch_value) -> {:castle, String.upcase(switch_value)} end) end diff --git a/test/credo/check/readability/string_sigils_test.exs b/test/credo/check/readability/string_sigils_test.exs index 49c424870..aaab5141c 100644 --- a/test/credo/check/readability/string_sigils_test.exs +++ b/test/credo/check/readability/string_sigils_test.exs @@ -170,7 +170,7 @@ defmodule Credo.Check.Readability.StringSigilsTest do end test "doesn't crash on #729" do - log = + stderr_output = capture_io(:stderr, fn -> ~S""" defmodule CredoInterpolationError do @@ -187,6 +187,6 @@ defmodule Credo.Check.Readability.StringSigilsTest do |> refute_issues() end) - assert log == "" + assert stderr_output == "" end end diff --git a/test/credo/check_test.exs b/test/credo/check_test.exs index 6d82ca688..b92f4de10 100644 --- a/test/credo/check_test.exs +++ b/test/credo/check_test.exs @@ -1,6 +1,8 @@ defmodule Credo.CheckTest do use Credo.Test.Case + import ExUnit.CaptureIO + alias Credo.Check @generated_lines 1000 @@ -91,4 +93,27 @@ defmodule Credo.CheckTest do assert issue.severity == 11 end) end + + defmodule IssueInvalidMessageTestCheck do + use Credo.Check + + @message <<70, 111, 117, 110, 100, 32, 109, 105, 115, 115, 112, 101, 108, 108, 101, 100, 32, + 119, 111, 114, 100, 32, 96, 103, 97, 114, 114, 121, 226, 96, 46>> + + def run(%SourceFile{} = source_file, params \\ []) do + IssueMeta.for(source_file, params) |> format_issue(message: @message) |> List.wrap() + end + end + + test "it should handle an invalid message" do + stderr_output = + capture_io(:stderr, fn -> + "# we do not need code, as the check is creating an issue in any case" + |> to_source_file + |> run_check(IssueInvalidMessageTestCheck) + end) + + assert stderr_output != "" + assert stderr_output =~ "containing invalid bytes" + end end