diff --git a/lib/credo/check/consistency/space_around_operators.ex b/lib/credo/check/consistency/space_around_operators.ex index 1824c61b2..66ec78d18 100644 --- a/lib/credo/check/consistency/space_around_operators.ex +++ b/lib/credo/check/consistency/space_around_operators.ex @@ -29,11 +29,6 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators do @collector Credo.Check.Consistency.SpaceAroundOperators.Collector - # TODO: add *ignored* operators, so you can add "|" and still write - # [head|tail] while enforcing 2 + 3 / 1 ... - # FIXME: this seems to be already implemented, but there don't seem to be - # any related test cases around. - @doc false @impl true def run_on_all_source_files(exec, source_files, params) do diff --git a/test/credo/check/consistency/line_endings_test.exs b/test/credo/check/consistency/line_endings_test.exs index 357fca799..4299c65bd 100644 --- a/test/credo/check/consistency/line_endings_test.exs +++ b/test/credo/check/consistency/line_endings_test.exs @@ -57,4 +57,13 @@ defmodule Credo.Check.Consistency.LineEndingsTest do assert issue.trigger == "\r\n" end) end + + test "it should report an issue here when there is no problem, but the :force param specifies the other kind of line ending" do + [@windows_line_endings] + |> to_source_files + |> run_check(@described_check, force: :unix) + |> assert_issue(fn issue -> + assert issue.trigger == "\r\n" + end) + end end diff --git a/test/credo/check/consistency/parameter_pattern_matching_test.exs b/test/credo/check/consistency/parameter_pattern_matching_test.exs index b601e4277..ec7aa60c5 100644 --- a/test/credo/check/consistency/parameter_pattern_matching_test.exs +++ b/test/credo/check/consistency/parameter_pattern_matching_test.exs @@ -166,4 +166,19 @@ defmodule Credo.Check.Consistency.ParameterPatternMatchingTest do assert 3 == Enum.count(issues) end + + test "it should report issues when variable declarations are inconsistent throughout sourcefiles (forcing left side)" do + issues = + [ + @var_right_map, + @var_right_struct, + @var_right_list, + @var_left_map + ] + |> to_source_files + |> run_check(@described_check, force: :before) + |> assert_issues() + + assert 3 == Enum.count(issues) + end end diff --git a/test/credo/check/consistency/space_around_operators_test.exs b/test/credo/check/consistency/space_around_operators_test.exs index 39a7190c7..ce457044b 100644 --- a/test/credo/check/consistency/space_around_operators_test.exs +++ b/test/credo/check/consistency/space_around_operators_test.exs @@ -436,4 +436,29 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do |> run_check(@described_check) |> refute_issues() end + + test "it should allow no spaces if specified in :ignore" do + source_files = + [ + ~S""" + defmodule TestTest do + def test(a, b, c) do + a = fn b, c -> b+c end + + a.(-30, 10) + a.(-3.0, 1.0) + end + end + """ + ] + |> to_source_files() + + source_files + |> run_check(@described_check) + |> assert_issue() + + source_files + |> run_check(@described_check, ignore: [:+]) + |> refute_issues() + end end diff --git a/test/credo/check/consistency/unused_variable_names_test.exs b/test/credo/check/consistency/unused_variable_names_test.exs index 09721957e..6c7e6e890 100644 --- a/test/credo/check/consistency/unused_variable_names_test.exs +++ b/test/credo/check/consistency/unused_variable_names_test.exs @@ -220,4 +220,44 @@ defmodule Credo.Check.Consistency.UnusedVariableNamesTest do assert 3 == issue.line_no end) end + + test "it should report a violation for naming schemes other than the forced one" do + [ + """ + defmodule Credo.SampleOne do + defmodule Foo do + def bar(name, _) when is_binary(name) do + case name do + "foo" <> _name -> "FOO" + "bar" <> _name -> "BAR" + _name -> "DEFAULT" + end + end + end + end + """, + """ + defmodule Credo.SampleTwo do + defmodule Foo do + def bar(list) do + Enum.map(list, fn _item -> 1 end) + end + end + end + """ + ] + |> to_source_files + |> run_check(@described_check, force: :anonymous) + |> assert_issues(fn issues -> + assert Enum.count(issues) == 4 + + assert Enum.any?(issues, fn issue -> + issue.trigger == "_name" + end) + + assert Enum.any?(issues, fn issue -> + issue.trigger == "_item" + end) + end) + end end diff --git a/test/credo/check/design/alias_usage_test.exs b/test/credo/check/design/alias_usage_test.exs index a35fb4d5d..806ecf112 100644 --- a/test/credo/check/design/alias_usage_test.exs +++ b/test/credo/check/design/alias_usage_test.exs @@ -183,6 +183,32 @@ defmodule Credo.Check.Design.AliasUsageTest do |> refute_issues() end + test "it should NOT report violation when module is included in only parameter but is also in excluded_lastnames" do + """ + defmodule Test do + def just_an_example do + Credo.Foo.Bar.call + end + end + """ + |> to_source_file + |> run_check(@described_check, only: ~r/^Credo.+$/, excluded_lastnames: ["Bar"]) + |> refute_issues() + end + + test "it should NOT report violation when module is covered in excluded_lastnames" do + """ + defmodule Test do + def just_an_example do + Credo.Foo.Bar.call + end + end + """ + |> to_source_file + |> run_check(@described_check, excluded_lastnames: ["Bar"]) + |> refute_issues() + end + test "it should NOT report with __MODULE__" do """ defmodule Test do diff --git a/test/credo/check/design/tag_fixme_test.exs b/test/credo/check/design/tag_fixme_test.exs index bc2bdb504..b38298fa8 100644 --- a/test/credo/check/design/tag_fixme_test.exs +++ b/test/credo/check/design/tag_fixme_test.exs @@ -61,12 +61,12 @@ defmodule Credo.Check.Design.TagFIXMETest do end test "it should report a couple of issues" do - """ + ~S''' defmodule CredoSampleModule do use ExUnit.Case # FIXME: this is the first - @moduledoc \"\"\" + @moduledoc """ this is an example # FIXME: and this is no actual comment - \"\"\" + """ def some_fun do # FIXME this is the second x = ~s{also: # FIXME: no comment here} @@ -76,11 +76,35 @@ defmodule Credo.Check.Design.TagFIXMETest do "also: # FIXME: no comment here as well" end end - """ + ''' |> to_source_file |> run_check(@described_check) |> assert_issues(fn issues -> - assert 3 == Enum.count(issues) + assert Enum.count(issues) == 3 + end) + end + + test "it should report a couple of issues when including docstrings" do + ~S''' + defmodule CredoSampleModule do + use ExUnit.Case # FIXME: this is the first + + @doc """ + FIXME: and this is no actual comment + """ + def some_fun do # FIXME this is the second + x = ~s{also: # FIXME: no comment here} + assert 2 == x + ?" # FIXME: this is the third + + "also: # FIXME: no comment here as well" + end + end + ''' + |> to_source_file + |> run_check(@described_check, include_doc: true) + |> assert_issues(fn issues -> + assert Enum.count(issues) == 4 end) end end diff --git a/test/credo/check/design/tag_todo_test.exs b/test/credo/check/design/tag_todo_test.exs index 7cd0b6b09..0a215d0a9 100644 --- a/test/credo/check/design/tag_todo_test.exs +++ b/test/credo/check/design/tag_todo_test.exs @@ -247,7 +247,31 @@ defmodule Credo.Check.Design.TagTODOTest do |> to_source_file |> run_check(@described_check) |> assert_issues(fn issues -> - assert 3 == Enum.count(issues) + assert Enum.count(issues) == 3 + end) + end + + test "it should report a couple of issues when including docs" do + ~S''' + defmodule CredoSampleModule do + use ExUnit.Case # TODO: this is the first + + @doc """ + TODO: and this is an actual TODO + """ + def some_fun do # TODO this is the second + x = ~s{also: # TODO: no comment here} + assert 2 == x + ?" # TODO: this is the third + + "also: # TODO: no comment here as well" + end + end + ''' + |> to_source_file + |> run_check(@described_check, include_doc: true) + |> assert_issues(fn issues -> + assert Enum.count(issues) == 4 end) end end diff --git a/test/credo/check/housekeeping_params.exs b/test/credo/check/housekeeping_params.exs index a8ac6c8a3..bf95addcc 100644 --- a/test/credo/check/housekeeping_params.exs +++ b/test/credo/check/housekeeping_params.exs @@ -23,7 +23,7 @@ defmodule Ast do end end -defmodule Credo.Check.HousekeepingHeredocsInTestsTest do +defmodule Credo.Check.HousekeepingParamsTest do use Credo.Test.Case require Ast @@ -34,7 +34,9 @@ defmodule Credo.Check.HousekeepingHeredocsInTestsTest do errors = Path.join(__DIR__, "*/**/*_test.exs") |> Path.wildcard() - |> Enum.reject(&String.match?(&1, ~r/(collector|helper)/)) + |> Enum.reject( + &String.match?(&1, ~r/(collector|helper|duplicated_code|perceived_complexity)/) + ) |> Enum.map(&{&1, File.read!(&1)}) |> Enum.map(fn {filename, test_source} -> check_filename = @@ -68,10 +70,11 @@ defmodule Credo.Check.HousekeepingHeredocsInTestsTest do end end - untested_params = all_param_names -- tested_params + untested_params = + (all_param_names -- tested_params) -- Credo.Check.Params.builtin_param_names() if check_source != "" && untested_params != [] do - "- #{Credo.Code.Module.name(check_ast)} - untested params: #{inspect(untested_params)}" + "- #{Path.relative_to_cwd(filename)}:1 - untested params: #{inspect(untested_params)}" end end) |> Enum.reject(&is_nil/1) diff --git a/test/credo/check/readability/max_line_length_test.exs b/test/credo/check/readability/max_line_length_test.exs index 08ca51c66..911b21eab 100644 --- a/test/credo/check/readability/max_line_length_test.exs +++ b/test/credo/check/readability/max_line_length_test.exs @@ -102,6 +102,42 @@ defmodule Credo.Check.Readability.MaxLineLengthTest do |> refute_issues() end + test "it should NOT report a violation if heredocs are excluded" do + ~S''' + defmodule CredoSampleModule do + use ExUnit.Case + + def some_fun do + IO.puts 1 + """ + long string, right? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 == 2 + """ + IO.puts 2 + end + end + ''' + |> to_source_file + |> run_check(@described_check, max_length: 80, ignore_heredocs: true) + |> refute_issues() + end + + test "it should NOT report a violation if sigils are excluded" do + ~S''' + defmodule CredoSampleModule do + use ExUnit.Case + + def some_fun do + IO.puts 1 + ~s(long string, right? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 == 2) + IO.puts 2 + end + end + ''' + |> to_source_file + |> run_check(@described_check, max_length: 80, ignore_sigils: true) + |> refute_issues() + end + test "it should NOT report a violation if strings are excluded for heredocs" do """ defmodule CredoSampleModule do diff --git a/test/credo/check/readability/module_doc_test.exs b/test/credo/check/readability/module_doc_test.exs index 47b138fd1..a8c08a1ee 100644 --- a/test/credo/check/readability/module_doc_test.exs +++ b/test/credo/check/readability/module_doc_test.exs @@ -108,4 +108,16 @@ defmodule Credo.Check.Readability.ModuleDocTest do assert issue.trigger == "Person" end) end + + test "it should report controller submodules when the :ignore_names param says so" do + """ + defmodule MyApp.SomePhoenixController do + defmodule SubModule do + end + end + """ + |> to_source_file + |> run_check(@described_check, ignore_names: []) + |> assert_issues() + end end diff --git a/test/credo/check/refactor/abc_size_test.exs b/test/credo/check/refactor/abc_size_test.exs index a6f14a64f..f5cf46f81 100644 --- a/test/credo/check/refactor/abc_size_test.exs +++ b/test/credo/check/refactor/abc_size_test.exs @@ -177,6 +177,31 @@ defmodule Credo.Check.Refactor.ABCSizeTest do end) end + test "it should NOT report count ecto functions when ecto functions are excluded via :excluded_functions" do + """ + defmodule CredoEctoQueryModule do + + def foobar() do + Favorite + |> where(user_id: ^user.id) + |> join(:left, [f], t in Template, f.entity_id == t.id and f.entity_type == "template") + |> join(:left, [f, t], d in Document, f.entity_id == d.id and f.entity_type == "document") + |> join(:left, [f, t, d], dt in Template, dt.id == d.template_id) + |> join(:left, [f, t, d, dt], c in Category, c.id == t.category_id or c.id == dt.category_id) + |> select([f, t, d, dt, c], c) + |> distinct(true) + |> Repo.all() + end + end + """ + |> to_source_file + |> run_check(@described_check, + max_size: 3, + excluded_functions: ["where", "from", "select", "join"] + ) + |> refute_issues() + end + # # ABC size unit tests # diff --git a/test/credo/check/refactor/nesting_test.exs b/test/credo/check/refactor/nesting_test.exs index e69703871..8d43a76b5 100644 --- a/test/credo/check/refactor/nesting_test.exs +++ b/test/credo/check/refactor/nesting_test.exs @@ -63,6 +63,29 @@ defmodule Credo.Check.Refactor.NestingTest do |> refute_issues() end + test "it should NOT report when :max_nesting is used properly" do + """ + defmodule CredoSampleModule do + defp foo() do + if true do + "flat" + else + if true do + if true do + if true do + "nested" + end + end + end + end + end + end + """ + |> to_source_file + |> run_check(@described_check, max_nesting: 4) + |> refute_issues() + end + # # cases raising issues #