Skip to content

Commit

Permalink
Adjust trigger for PassAsyncInTestCases
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Feb 1, 2024
1 parent 6bd912e commit 06c1443
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
23 changes: 8 additions & 15 deletions lib/credo/check/refactor/pass_async_in_test_cases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,40 @@ defmodule Credo.Check.Refactor.PassAsyncInTestCases do

# `use` with options
defp traverse(
{:use, _, [{_, meta, module_namespace}, [_ | _] = options]} = ast,
{:use, meta, [{_, _meta, module_namespace}, [_ | _] = options]} = ast,
issues,
issue_meta
) do
module_name = module_name_from(module_namespace)
module_name = Credo.Code.Name.last(module_namespace)

Check warning on line 34 in lib/credo/check/refactor/pass_async_in_test_cases.ex

View workflow job for this annotation

GitHub Actions / Compile & Run

Nested modules could be aliased at the top of the invoking module.

if String.ends_with?(module_name, "Case") and !Keyword.has_key?(options, :async) do
{ast, issues ++ [issue_for(module_name, meta[:line], issue_meta)]}
{ast, issues ++ [issue_for(meta[:line], issue_meta)]}
else
{ast, issues}
end
end

# `use` without options
defp traverse({:use, _, [{_, meta, module_namespace}]} = ast, issues, issue_meta) do
module_name = module_name_from(module_namespace)
defp traverse({:use, meta, [{_op, _meta, module_namespace}]} = ast, issues, issue_meta) do
module_name = Credo.Code.Name.last(module_namespace)

Check warning on line 45 in lib/credo/check/refactor/pass_async_in_test_cases.ex

View workflow job for this annotation

GitHub Actions / Compile & Run

Nested modules could be aliased at the top of the invoking module.

if String.ends_with?(module_name, "Case") do
{ast, issues ++ [issue_for(module_name, meta[:line], issue_meta)]}
{ast, issues ++ [issue_for(meta[:line], issue_meta)]}
else
{ast, issues}
end
end

# Ignore all other AST nodes
defp traverse(ast, issues, _issue_meta) do
{ast, issues}
end

defp issue_for(module_name, line_no, issue_meta) do
defp issue_for(line_no, issue_meta) do
format_issue(
issue_meta,
message: "Pass an `:async` boolean option to `use` a test case module",
trigger: module_name,
trigger: "use",
line_no: line_no
)
end

defp module_name_from(module_namespace) do
module_namespace
|> List.last()
|> to_string()
end
end
2 changes: 2 additions & 0 deletions test/credo/check/refactor/pass_async_in_test_cases_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule Credo.Check.Refactor.PassAsyncInTestCasesTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 2
assert issue.trigger == "use"
end)
end

Expand All @@ -75,6 +76,7 @@ defmodule Credo.Check.Refactor.PassAsyncInTestCasesTest do
|> run_check(@described_check)
|> assert_issue(fn issue ->
assert issue.line_no == 3
assert issue.trigger == "use"
end)
end
end
Expand Down

0 comments on commit 06c1443

Please sign in to comment.