-
Notifications
You must be signed in to change notification settings - Fork 425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check for piping into Logger functions #1182
Open
hkrutzer
wants to merge
1
commit into
rrrene:master
Choose a base branch
from
hkrutzer:logger_pipeline_check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule Credo.Check.Warning.PipeToLogger do | ||
@moduledoc false | ||
|
||
use Credo.Check, | ||
id: "EX??", | ||
base_priority: :normal, | ||
category: :warning, | ||
explanations: [ | ||
check: """ | ||
Calls into Logger can be purged when `:compile_time_purge_matching` is enabled in | ||
the Logger configuration. This will remove the entire pipeline. | ||
Use |> tap(&Logger.info(&1)) instead. | ||
""" | ||
] | ||
|
||
@logger_functions [ | ||
:debug, | ||
:info, | ||
:notice, | ||
:warn, | ||
:warning, | ||
:error, | ||
:critical, | ||
:alert, | ||
:emergency | ||
] | ||
|
||
def run(%SourceFile{} = source_file, params) do | ||
issue_meta = IssueMeta.for(source_file, params) | ||
|
||
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta)) | ||
end | ||
|
||
defp traverse( | ||
{:|>, meta, [_, {{:., _, [{:__aliases__, _, [:Logger]}, function]}, _, _}]} = ast, | ||
issues, | ||
issue_meta | ||
) | ||
when function in @logger_functions do | ||
trigger = "|> Logger.#{function}" | ||
|
||
new_issue = | ||
format_issue( | ||
issue_meta, | ||
message: "There should be no `@#{trigger} calls.", | ||
trigger: "#{trigger}", | ||
line_no: meta[:line], | ||
column: meta[:column] | ||
) | ||
|
||
{ast, issues ++ [new_issue]} | ||
end | ||
|
||
defp traverse(ast, issues, _issue_meta) do | ||
{ast, issues} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
defmodule Credo.Check.Warning.PipeToLoggerTest do | ||
use Credo.Test.Case | ||
|
||
@described_check Credo.Check.Warning.PipeToLogger | ||
|
||
# | ||
# cases NOT raising issues | ||
# | ||
|
||
test "it should NOT report when Logger is used without piping" do | ||
""" | ||
defmodule CredoSampleModule do | ||
require Logger | ||
|
||
def some_function(parameter1) do | ||
Logger.warn("This is a direct call") | ||
Logger.info("Another direct call") | ||
|
||
something_else() | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> refute_issues() | ||
end | ||
|
||
test "it should NOT report when piping to other functions" do | ||
""" | ||
defmodule CredoSampleModule do | ||
def some_function(parameter1) do | ||
parameter1 | ||
|> process_data() | ||
|> transform_result() | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> refute_issues() | ||
end | ||
|
||
test "it should NOT report when Logger is in a different position" do | ||
""" | ||
defmodule CredoSampleModule do | ||
require Logger | ||
|
||
def some_function(parameter1) do | ||
Logger.warn(parameter1 |> transform_data()) | ||
|
||
# Other code | ||
result = other_function() |> process_result() | ||
|
||
result | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> refute_issues() | ||
end | ||
|
||
# | ||
# cases raising issues | ||
# | ||
|
||
test "it should report a violation when piping to Logger.warn" do | ||
""" | ||
defmodule CredoSampleModule do | ||
require Logger | ||
|
||
def some_function(parameter1) do | ||
parameter1 | ||
|> transform_data() | ||
|> Logger.warn() | ||
|
||
:ok | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> assert_issue(fn issue -> | ||
assert issue.line_no == 7 | ||
assert issue.column == 5 | ||
assert issue.trigger == "|> Logger.warn" | ||
end) | ||
end | ||
|
||
test "it should report a violation when piping to any Logger function" do | ||
""" | ||
defmodule CredoSampleModule do | ||
require Logger | ||
|
||
def some_function(parameter1) do | ||
"Starting process" | ||
|> Logger.info() | ||
|
||
parameter1 | ||
|> transform_data() | ||
|> Logger.error() | ||
|
||
"Debug information" | ||
|> Logger.debug() | ||
|
||
:ok | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> assert_issues(fn issues -> | ||
assert length(issues) == 3 | ||
|
||
[issue1, issue2, issue3] = issues | ||
|
||
assert issue1.line_no == 6 | ||
assert issue1.column == 5 | ||
assert issue1.trigger == "|> Logger.info" | ||
|
||
assert issue2.line_no == 10 | ||
assert issue2.column == 5 | ||
assert issue2.trigger == "|> Logger.error" | ||
|
||
assert issue3.line_no == 13 | ||
assert issue3.column == 5 | ||
assert issue3.trigger == "|> Logger.debug" | ||
end) | ||
end | ||
|
||
test "it should report a violation when piping to Logger with options" do | ||
""" | ||
defmodule CredoSampleModule do | ||
require Logger | ||
|
||
def some_function(error) do | ||
error | ||
|> inspect() | ||
|> Logger.error(some_metadata: "value") | ||
|
||
:error | ||
end | ||
end | ||
""" | ||
|> to_source_file | ||
|> run_check(@described_check) | ||
|> assert_issue(fn issue -> | ||
assert issue.line_no == 7 | ||
assert issue.column == 5 | ||
assert issue.trigger == "|> Logger.error" | ||
end) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / Credo
Modules should have a @moduledoc tag.