Skip to content

Commit

Permalink
Fix more triggers; add some housekeeping checks
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Feb 4, 2024
1 parent 313e7cc commit f48e6fa
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 95 deletions.
2 changes: 1 addition & 1 deletion lib/credo/check/refactor/cyclomatic_complexity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
7 changes: 3 additions & 4 deletions lib/credo/check/warning/mix_env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule Credo.Check.Warning.MixEnv do

alias Credo.SourceFile

@call_string "Mix.env"
@def_ops [:def, :defp, :defmacro]

@doc false
Expand Down Expand Up @@ -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
Expand Down
83 changes: 44 additions & 39 deletions test/credo/check/consistency/exception_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -115,61 +137,44 @@ defmodule Credo.Check.Consistency.ExceptionNamesTest do
end
end
""",
"""
~S"""
defmodule InputValidationException do
defexception [:message]
end
"""
]
|> 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
"""
]
|> 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
8 changes: 4 additions & 4 deletions test/credo/check/consistency/line_endings_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ 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}
alias Foo.Bar
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"
Expand Down
22 changes: 11 additions & 11 deletions test/credo/check/consistency/parameter_pattern_matching_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,57 +15,57 @@ 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
end
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
Expand All @@ -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__, [])
Expand Down
Loading

0 comments on commit f48e6fa

Please sign in to comment.