From f48e6fa4b2b37f79994613ed60f7edee1f3b2761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Sun, 4 Feb 2024 11:19:06 +0100 Subject: [PATCH] Fix more triggers; add some housekeeping checks --- .../check/refactor/cyclomatic_complexity.ex | 2 +- lib/credo/check/warning/mix_env.ex | 7 +- .../consistency/exception_names_test.exs | 83 ++++++++++--------- .../check/consistency/line_endings_test.exs | 8 +- .../multi_alias_import_require_use_test.exs | 8 +- .../parameter_pattern_matching_test.exs | 22 ++--- .../space_around_operators_test.exs | 26 +++--- .../consistency/space_in_parentheses_test.exs | 12 +-- test/credo/check/design/tag_fixme_test.exs | 5 +- test/credo/check/design/tag_todo_test.exs | 5 +- .../check/housekeeping_heredocs_in_tests.exs | 43 ++++++++++ test/credo/check/housekeeping_params.exs | 83 +++++++++++++++++++ test/credo/check/housekeeping_trigger.exs | 57 +++++++++++++ ...est.exs => prefer_unquoted_atoms_test.exs} | 0 .../refactor/cyclomatic_complexity_test.exs | 15 ++-- test/credo/check/warning/iex_pry_test.exs | 5 +- test/credo/check/warning/io_inspect_test.exs | 5 +- test/credo/check/warning/mix_env_test.exs | 5 +- test/test_helper.exs | 2 +- 19 files changed, 298 insertions(+), 95 deletions(-) create mode 100644 test/credo/check/housekeeping_heredocs_in_tests.exs create mode 100644 test/credo/check/housekeeping_params.exs create mode 100644 test/credo/check/housekeeping_trigger.exs rename test/credo/check/readability/{prefer_unquotes_atoms_test.exs => prefer_unquoted_atoms_test.exs} (100%) diff --git a/lib/credo/check/refactor/cyclomatic_complexity.ex b/lib/credo/check/refactor/cyclomatic_complexity.ex index 5809422fc..7d57d006f 100644 --- a/lib/credo/check/refactor/cyclomatic_complexity.ex +++ b/lib/credo/check/refactor/cyclomatic_complexity.ex @@ -169,7 +169,7 @@ defmodule Credo.Check.Refactor.CyclomaticComplexity do issue_meta, message: "Function is too complex (cyclomatic complexity is #{actual_value}, max is #{max_value}).", - trigger: trigger, + trigger: to_string(trigger), line_no: line_no, severity: Severity.compute(actual_value, max_value) ) diff --git a/lib/credo/check/warning/mix_env.ex b/lib/credo/check/warning/mix_env.ex index a0e943ad6..eb2f542a3 100644 --- a/lib/credo/check/warning/mix_env.ex +++ b/lib/credo/check/warning/mix_env.ex @@ -18,7 +18,6 @@ defmodule Credo.Check.Warning.MixEnv do alias Credo.SourceFile - @call_string "Mix.env" @def_ops [:def, :defp, :defmacro] @doc false @@ -79,14 +78,14 @@ defmodule Credo.Check.Warning.MixEnv do end defp issues_for_call(meta, issues, issue_meta) do - [issue_for(issue_meta, meta[:line], @call_string) | issues] + [issue_for(issue_meta, meta[:line]) | issues] end - defp issue_for(issue_meta, line_no, trigger) do + defp issue_for(issue_meta, line_no) do format_issue( issue_meta, message: "There should be no calls to Mix.env in application code.", - trigger: trigger, + trigger: "Mix.env", line_no: line_no ) end diff --git a/test/credo/check/consistency/exception_names_test.exs b/test/credo/check/consistency/exception_names_test.exs index e3d3f04a9..5703b677e 100644 --- a/test/credo/check/consistency/exception_names_test.exs +++ b/test/credo/check/consistency/exception_names_test.exs @@ -9,10 +9,10 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do test "it should NOT report modules without defexception" do [ - """ + ~S""" defmodule UriParserError """, - """ + ~S""" defmodule SomeOtherException """ ] @@ -23,14 +23,14 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do test "it should NOT report correct behaviour (same suffix)" do [ - """ + ~S""" defmodule Credo.Sample do defmodule UriParserError do defexception [:message] end end """, - """ + ~S""" defmodule SomeOtherError do defexception [:message] end @@ -43,14 +43,14 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do test "it should NOT report correct behaviour (same prefix)" do [ - """ + ~S""" defmodule Credo.Sample do defmodule InvalidSomething do defexception [:message] end end """, - """ + ~S""" defmodule InvalidResponse do defexception [:message] end @@ -63,12 +63,12 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do test "it should NOT report correct behaviour (only one exception class)" do [ - """ + ~S""" defmodule Credo.SampleError do defexception [:message] end """, - """ + ~S""" defmodule SomeModule do end @@ -81,14 +81,14 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do test "it should NOT report a violation for different naming schemes for 1:1 situations" do [ - """ + ~S""" defmodule Credo.Sample do defmodule SomeError do defexception [:message] end end """, - """ + ~S""" defmodule UndefinedResponse do defexception [:message] end @@ -99,13 +99,35 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do |> refute_issues() end + test "it should not report (prefixes)" do + [ + ~S""" + defmodule FactoryUndefined do + defexception [:message] + + def exception(factory_name) do + message = "No factory defined for this." + %UndefinedFactory{message: message} + end + end + + defmodule SaveUndefined do + defexception [:message] + end + """ + ] + |> to_source_files + |> run_check(@described_check) + |> refute_issues() + end + # # cases raising issues # test "it should report a violation for different naming schemes (suffixes)" do [ - """ + ~S""" defmodule Credo.Sample do defmodule SomeException do defexception [:message] @@ -115,7 +137,7 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do end end """, - """ + ~S""" defmodule InputValidationException do defexception [:message] end @@ -123,24 +145,27 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do ] |> to_source_files |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 5 + assert issue.trigger == "UndefinedResponse" + end) end test "it should report a violation for different naming schemes (prefixes)" do [ - """ + ~S""" defmodule Credo.Sample do defmodule InvalidDataRequest do defexception [:message] end end """, - """ + ~S""" defmodule InvalidReponseFromServer do defexception [:message] end """, - """ + ~S""" defmodule UndefinedDataFormat do # <--- does not have the prefix "Invalid" defexception [:message] end @@ -148,28 +173,8 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do ] |> to_source_files |> run_check(@described_check) - |> assert_issue() - end - - test "it should not report (prefixes)" do - [ - """ - defmodule FactoryUndefined do - defexception [:message] - - def exception(factory_name) do - message = "No factory defined for this." - %UndefinedFactory{message: message} - end - end - - defmodule SaveUndefined do - defexception [:message] - end - """ - ] - |> to_source_files - |> run_check(@described_check) - |> refute_issues() + |> assert_issue(fn issue -> + assert issue.trigger == "UndefinedDataFormat" + end) end end diff --git a/test/credo/check/consistency/line_endings_test.exs b/test/credo/check/consistency/line_endings_test.exs index 665097ec2..46b239b98 100644 --- a/test/credo/check/consistency/line_endings_test.exs +++ b/test/credo/check/consistency/line_endings_test.exs @@ -1,9 +1,9 @@ -defmodule Credo.Check.Readability.LineEndingsTest do +defmodule Credo.Check.Consistency.LineEndingsTest do use Credo.Test.Case @described_check Credo.Check.Consistency.LineEndings - @unix_line_endings """ + @unix_line_endings ~S""" defmodule Credo.Sample do defmodule InlineModule do def foobar do @@ -12,7 +12,7 @@ defmodule Credo.Check.Readability.LineEndingsTest do end end """ - @unix_line_endings2 """ + @unix_line_endings2 ~S""" defmodule OtherModule do defmacro foo do {:ok} = File.read @@ -23,7 +23,7 @@ defmodule Credo.Check.Readability.LineEndingsTest do end end """ - @windows_line_endings """ + @windows_line_endings ~s""" defmodule Credo.Sample do\r\n@test_attribute :foo\r\nend """ diff --git a/test/credo/check/consistency/multi_alias_import_require_use_test.exs b/test/credo/check/consistency/multi_alias_import_require_use_test.exs index 27f55f06c..84897df0b 100644 --- a/test/credo/check/consistency/multi_alias_import_require_use_test.exs +++ b/test/credo/check/consistency/multi_alias_import_require_use_test.exs @@ -3,20 +3,20 @@ defmodule Credo.Check.Consistency.MultiAliasImportRequireUseTest do @described_check Credo.Check.Consistency.MultiAliasImportRequireUse - @single """ + @single ~S""" defmodule Credo.Sample2 do alias Foo.Bar alias Foo.Quux require Foo.Bar end """ - @single2 """ + @single2 ~S""" defmodule Credo.Sample2 do import Assertions import MyApp.Factory end """ - @multi """ + @multi ~S""" defmodule Credo.Sample3 do alias Foo.{Bar, Quux} alias Bar.{Baz, Bang} @@ -24,7 +24,7 @@ defmodule Credo.Check.Consistency.MultiAliasImportRequireUseTest do require Foo.Quux end """ - @multi_module_same_file """ + @multi_module_same_file ~S""" defmodule CredoMultiAliasExample.SetMultiAliasToSingles do @moduledoc "This modules does many aliases to set the consistency to multi-alias" diff --git a/test/credo/check/consistency/parameter_pattern_matching_test.exs b/test/credo/check/consistency/parameter_pattern_matching_test.exs index c85999572..6ecf06829 100644 --- a/test/credo/check/consistency/parameter_pattern_matching_test.exs +++ b/test/credo/check/consistency/parameter_pattern_matching_test.exs @@ -1,8 +1,8 @@ -defmodule Credo.Check.Readability.ParameterPatternMatchingTest do +defmodule Credo.Check.Consistency.ParameterPatternMatchingTest do use Credo.Test.Case @described_check Credo.Check.Consistency.ParameterPatternMatching - @left_and_right_mix """ + @left_and_right_mix ~S""" defmodule Credo.Sample do defmodule InlineModule do def list_after([bar, baz] = foo), do: :ok @@ -15,28 +15,28 @@ defmodule Credo.Check.Readability.ParameterPatternMatchingTest do end end """ - @var_left_list """ + @var_left_list ~S""" defmodule Test do def test(foo = [x, y, x]) do nil end end """ - @var_left_tuple """ + @var_left_tuple ~S""" defmodule Test do def test(foo = {x, y, x}) do nil end end """ - @var_left_struct """ + @var_left_struct ~S""" defmodule Test do def test(foo = %Foo{hello: "world"}) do nil end end """ - @var_left_map """ + @var_left_map ~S""" defmodule Test do def test(foo = %{abc: def}) do nil @@ -44,28 +44,28 @@ defmodule Credo.Check.Readability.ParameterPatternMatchingTest do end """ - @var_right_list """ + @var_right_list ~S""" defmodule Test do def test([x, y, x] = foo) do nil end end """ - @var_right_tuple """ + @var_right_tuple ~S""" defmodule Test do def test({x, y, x} = foo) do nil end end """ - @var_right_struct """ + @var_right_struct ~S""" defmodule Test do def test(%Foo{hello: "world"} = foo) do nil end end """ - @var_right_map """ + @var_right_map ~S""" defmodule Test do def test(%{abc: def} = foo) do nil @@ -92,7 +92,7 @@ defmodule Credo.Check.Readability.ParameterPatternMatchingTest do end test "it should NOT break when input has a function without bindings or private funs" do - module_with_fun_without_bindings = """ + module_with_fun_without_bindings = ~S""" defmodule SurviveThisIfYouCan do def start do GenServer.start(__MODULE__, []) diff --git a/test/credo/check/consistency/space_around_operators_test.exs b/test/credo/check/consistency/space_around_operators_test.exs index 45708f3ae..c43d82ce5 100644 --- a/test/credo/check/consistency/space_around_operators_test.exs +++ b/test/credo/check/consistency/space_around_operators_test.exs @@ -3,7 +3,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do @described_check Credo.Check.Consistency.SpaceAroundOperators - @without_spaces """ + @without_spaces ~S""" defmodule Credo.Sample1 do @spec f(<<_::16, _::_*8>>) :: binary defmodule InlineModule do @@ -22,7 +22,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end """ - @without_spaces2 """ + @without_spaces2 ~S""" defmodule OtherModule3 do defmacro foo do 3+4 @@ -33,7 +33,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @without_spaces3 """ + @without_spaces3 ~S""" defmodule OtherModule4 do defp dictionary_changeset() do fields = ~W( @@ -43,7 +43,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces """ + @with_spaces ~S""" defmodule Credo.Sample2 do defmodule F do def f(), do: 1 + 2 @@ -151,7 +151,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces2 """ + @with_spaces2 ~S""" defmodule OtherModule3 do defmacro foo do 1 && 2 @@ -162,7 +162,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces3 """ + @with_spaces3 ~S""" defmodule OtherModule3 do defmacro foo do case foo do @@ -173,7 +173,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces4 """ + @with_spaces4 ~S""" defmodule OtherModule3 do @min -1 @max 2 + 1 @@ -187,7 +187,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces5 """ + @with_spaces5 ~S""" defmodule CredoTest do @moduledoc "" @@ -203,7 +203,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_spaces6 """ + @with_spaces6 ~S""" assert -24 == MyModule.fun assert MyModule.fun != -24 ExUnit.assert -12 == MyApp.fun_that_should_return_a_negative @@ -215,7 +215,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do def foo, do: 1 + 1 end """ - @with_spaces8 """ + @with_spaces8 ~S""" defmodule AlwaysNoSpacesInBinaryTypespecTest do def seed_collection(entity, collection, opts) when is_map(collection) do @@ -234,7 +234,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end """ - @with_and_without_spaces """ + @with_and_without_spaces ~S""" defmodule OtherModule3 do defmacro foo do 3+4 @@ -245,7 +245,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do end end """ - @with_and_without_spaces2 """ + @with_and_without_spaces2 ~S""" defmodule CredoTests do def bar do 2+3 @@ -297,7 +297,7 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do @with_spaces2, @with_spaces3, @with_spaces4, - """ + ~S""" defmodule OtherModule3_0 do for prio < 1..10//2 do # something diff --git a/test/credo/check/consistency/space_in_parentheses_test.exs b/test/credo/check/consistency/space_in_parentheses_test.exs index eb99a1642..d2786e9a6 100644 --- a/test/credo/check/consistency/space_in_parentheses_test.exs +++ b/test/credo/check/consistency/space_in_parentheses_test.exs @@ -1,4 +1,4 @@ -defmodule Credo.Check.Readability.SpaceInParenthesesTest do +defmodule Credo.Check.Consistency.SpaceInParenthesesTest do use Credo.Test.Case @described_check Credo.Check.Consistency.SpaceInParentheses @@ -45,7 +45,7 @@ defmodule Credo.Check.Readability.SpaceInParenthesesTest do end end """ - @with_spaces """ + @with_spaces ~S""" defmodule Credo.Sample2 do defmodule InlineModule do def foobar do @@ -54,7 +54,7 @@ defmodule Credo.Check.Readability.SpaceInParenthesesTest do end end """ - @with_spaces2 """ + @with_spaces2 ~S""" defmodule OtherModule3 do defmacro foo do { :ok } = File.read( filename ) @@ -65,7 +65,7 @@ defmodule Credo.Check.Readability.SpaceInParenthesesTest do end end """ - @with_spaces_empty_params1 """ + @with_spaces_empty_params1 ~S""" defmodule Credo.Sample2 do defmodule InlineModule do def foobar do @@ -74,7 +74,7 @@ defmodule Credo.Check.Readability.SpaceInParenthesesTest do end end """ - @with_spaces_empty_params2 """ + @with_spaces_empty_params2 ~S""" defmodule Credo.Sample2 do defmodule InlineModule do def foobar do @@ -83,7 +83,7 @@ defmodule Credo.Check.Readability.SpaceInParenthesesTest do end end """ - @with_and_without_spaces """ + @with_and_without_spaces ~S""" defmodule OtherModule3 do defmacro foo do { :ok } = File.read( filename ) diff --git a/test/credo/check/design/tag_fixme_test.exs b/test/credo/check/design/tag_fixme_test.exs index b790a530a..bc2bdb504 100644 --- a/test/credo/check/design/tag_fixme_test.exs +++ b/test/credo/check/design/tag_fixme_test.exs @@ -54,7 +54,10 @@ defmodule Credo.Check.Design.TagFIXMETest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 3 + assert issue.trigger =~ "# fixme" + end) end test "it should report a couple of issues" do diff --git a/test/credo/check/design/tag_todo_test.exs b/test/credo/check/design/tag_todo_test.exs index dcdf8f4a5..7cd0b6b09 100644 --- a/test/credo/check/design/tag_todo_test.exs +++ b/test/credo/check/design/tag_todo_test.exs @@ -221,7 +221,10 @@ defmodule Credo.Check.Design.TagTODOTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 3 + assert issue.trigger =~ "# todo" + end) end test "it should report a couple of issues" do diff --git a/test/credo/check/housekeeping_heredocs_in_tests.exs b/test/credo/check/housekeeping_heredocs_in_tests.exs new file mode 100644 index 000000000..d7acfbbd2 --- /dev/null +++ b/test/credo/check/housekeeping_heredocs_in_tests.exs @@ -0,0 +1,43 @@ +defmodule Credo.Check.HousekeepingHeredocsInTestsTest do + use Credo.Test.Case + + @tag housekeeping: :heredocs + test "find triple-double-quote heredocs in check tests" do + errors = + Path.join(__DIR__, "*/**/*_test.exs") + |> Path.wildcard() + |> Enum.reject(&String.match?(&1, ~r/(collector|helper)/)) + |> Enum.map(&{&1, File.read!(&1)}) + |> Enum.flat_map(fn {filename, source} -> + ast = + source + |> Code.string_to_quoted!( + literal_encoder: &{:ok, {:__literal__, &2, [&1]}}, + token_metadata: true + ) + |> Macro.prewalk(fn + {op, params, args} -> {op, Map.new(params), args} + val -> val + end) + + {_ast, acc} = + Macro.prewalk(ast, [], fn + {:__literal__, %{line: line, delimiter: "\"\"\""}, [val]} = ast, acc + when is_binary(val) -> + {ast, ["#{filename}:#{line}"] ++ acc} + + ast, acc -> + {ast, acc} + end) + + acc + end) + + if errors != [] do + flunk( + "Expected to use ~s or ~S heredocs:\n" <> + Enum.join(errors, "\n") + ) + end + end +end diff --git a/test/credo/check/housekeeping_params.exs b/test/credo/check/housekeeping_params.exs new file mode 100644 index 000000000..e0d7acf84 --- /dev/null +++ b/test/credo/check/housekeeping_params.exs @@ -0,0 +1,83 @@ +defmodule Ast do + defmacro collect(ast, pattern1, blocks \\ []) do + dbg(pattern1) + do_block1 = blocks[:do] + + quote do + Macro.prewalk(unquote(ast), [], fn + m_i_ast = unquote(pattern1), m_i_acc -> + m_i_result = unquote(do_block1) + + {m_i_ast, List.wrap(m_i_result) ++ m_i_acc} + + ast, acc -> + {ast, acc} + end) + |> then(fn {_ast, acc} -> acc end) + end + end +end + +defmodule Credo.Check.HousekeepingHeredocsInTestsTest do + use Credo.Test.Case + + require Ast + import Ast + + @tag housekeeping: :params + test "find untested params in check tests" do + errors = + Path.join(__DIR__, "*/**/*_test.exs") + |> Path.wildcard() + |> Enum.reject(&String.match?(&1, ~r/(collector|helper)/)) + |> Enum.map(&{&1, File.read!(&1)}) + |> Enum.map(fn {filename, test_source} -> + check_filename = + filename + |> String.replace("/test/", "/lib/") + |> String.replace("_test.exs", ".ex") + + check_source = + if File.exists?(check_filename) do + File.read!(check_filename) + else + "" + end + + check_ast = Code.string_to_quoted!(check_source) + + all_param_names = + collect check_ast, {:use, _, [{:__aliases__, _, [:Credo, :Check]}, opts]} do + defaults = List.wrap(opts[:param_defaults]) + explanations = List.wrap(opts[:explanations][:params]) + + Enum.uniq(Keyword.keys(defaults) ++ Keyword.keys(explanations)) + end + |> dbg + + test_ast = Code.string_to_quoted!(test_source) + + all_param_names = + collect test_ast, {:test, _, args} do + collect args, {:run_check, _, [_check, params]} do + Keyword.keys(params) + end + end + |> dbg + + untested_params = all_param_names -- acc + + if check_source != "" && untested_params != [] do + "- #{Credo.Code.Module.name(check_ast)} - untested params: #{inspect(untested_params)}" + end + end) + |> Enum.reject(&is_nil/1) + + if errors != [] do + flunk( + "Expected to find at least one test for each param to make sure it works:\n" <> + Enum.join(errors, "\n") + ) + end + end +end diff --git a/test/credo/check/housekeeping_trigger.exs b/test/credo/check/housekeeping_trigger.exs new file mode 100644 index 000000000..e0c94f6e1 --- /dev/null +++ b/test/credo/check/housekeeping_trigger.exs @@ -0,0 +1,57 @@ +defmodule Credo.Check.HousekeepingHeredocsInTestsTest do + use Credo.Test.Case + + @tag housekeeping: :trigger + test "find trigger assertions" do + errors = + Path.join(__DIR__, "*/**/*_test.exs") + |> Path.wildcard() + |> Enum.reject(&String.match?(&1, ~r/(collector|helper)/)) + |> Enum.map(&{&1, File.read!(&1)}) + |> Enum.flat_map(fn {filename, source} -> + ast = Code.string_to_quoted!(source) + + {ast, acc} = + Macro.prewalk(ast, [], fn + {:test, _, [_ | _] = args}, acc -> + Macro.prewalk(args, acc, fn + {op, _, [_ | _] = args2}, acc when op in [:assert_issue, :assert_issues] -> + Macro.prewalk(args2, acc, fn + {:fn, _, args3}, acc -> + Macro.prewalk(args3, acc, fn + {{:., _, [{_issue, _, nil}, :trigger]}, _, []} = ast, acc -> + {ast, [{:ok, filename}] ++ acc} + + ast, acc -> + {ast, acc} + end) + + ast, acc -> + {ast, acc} + end) + + ast, acc -> + {ast, acc} + end) + + ast, acc -> + {ast, acc} + end) + + if acc == [] do + [ + "- #{Credo.Code.Module.name(ast) |> String.replace(~r/Test$/, "")}" + ] + else + [] + end + end) + + if errors != [] do + flunk( + "Expected to find at least one assertion for `:trigger` to make sure it's the right one:\n" <> + Enum.join(errors, "\n") + ) + end + end +end diff --git a/test/credo/check/readability/prefer_unquotes_atoms_test.exs b/test/credo/check/readability/prefer_unquoted_atoms_test.exs similarity index 100% rename from test/credo/check/readability/prefer_unquotes_atoms_test.exs rename to test/credo/check/readability/prefer_unquoted_atoms_test.exs diff --git a/test/credo/check/refactor/cyclomatic_complexity_test.exs b/test/credo/check/refactor/cyclomatic_complexity_test.exs index 5737735fb..982e2d526 100644 --- a/test/credo/check/refactor/cyclomatic_complexity_test.exs +++ b/test/credo/check/refactor/cyclomatic_complexity_test.exs @@ -136,18 +136,19 @@ defmodule Credo.Check.Refactor.CyclomaticComplexityTest do test "it should report a violation on def rather than when" do """ defmodule CredoTest do - defp foobar(v) when is_atom(v) do - if first_condition do - if second_condition && third_condition, do: call_something - if fourth_condition || fifth_condition, do: call_something_else + defp foobar(v) when is_atom(v) do + if first_condition do + if second_condition && third_condition, do: call_something + if fourth_condition || fifth_condition, do: call_something_else + end end end - end """ |> to_source_file |> run_check(@described_check, max_complexity: 4) - |> assert_issue() - |> assert_trigger(:foobar) + |> assert_issue(fn issue -> + assert issue.trigger == "foobar" + end) end # diff --git a/test/credo/check/warning/iex_pry_test.exs b/test/credo/check/warning/iex_pry_test.exs index 821967646..d9f0f1c07 100644 --- a/test/credo/check/warning/iex_pry_test.exs +++ b/test/credo/check/warning/iex_pry_test.exs @@ -35,6 +35,9 @@ defmodule Credo.Check.Warning.IExPryTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 4 + assert issue.trigger == "IEx.pry" + end) end end diff --git a/test/credo/check/warning/io_inspect_test.exs b/test/credo/check/warning/io_inspect_test.exs index 28a7b4bb9..258c0b1f6 100644 --- a/test/credo/check/warning/io_inspect_test.exs +++ b/test/credo/check/warning/io_inspect_test.exs @@ -48,7 +48,10 @@ defmodule Credo.Check.Warning.IoInspectTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 4 + assert issue.trigger == "IO.inspect" + end) end test "it should report a violation /3" do diff --git a/test/credo/check/warning/mix_env_test.exs b/test/credo/check/warning/mix_env_test.exs index 5a4af49b6..f0f6d3d16 100644 --- a/test/credo/check/warning/mix_env_test.exs +++ b/test/credo/check/warning/mix_env_test.exs @@ -122,7 +122,10 @@ defmodule Credo.Check.Warning.MixEnvTest do """ |> to_source_file |> run_check(@described_check) - |> assert_issue() + |> assert_issue(fn issue -> + assert issue.line_no == 3 + assert issue.trigger == "Mix.env" + end) end test "it should report violations from variables named like def operations" do diff --git a/test/test_helper.exs b/test/test_helper.exs index 366c62d1e..71a96ac83 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -11,7 +11,7 @@ check_version = end end) -exclude = Keyword.merge([to_be_implemented: true], check_version) +exclude = Keyword.merge([to_be_implemented: true, housekeeping: true], check_version) ExUnit.configure(exclude: exclude)