Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 11 additions & 37 deletions lib/credo/check/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,17 @@ defmodule Credo.Check.Runner do
files_excluded = Params.files_excluded(params, check)

found_relevant_files =
cond do
files_included == known_files and files_excluded == [] ->
[]

exec.read_from_stdin ->
# TODO: I am unhappy with how convoluted this gets
# but it is necessary to avoid hitting the filesystem when reading from STDIN
[%Credo.SourceFile{filename: filename}] = Execution.get_source_files(exec)

file_included? =
if files_included != known_files do
Credo.Sources.filename_matches?(filename, files_included)
else
true
end

file_excluded? =
if files_excluded != [] do
Credo.Sources.filename_matches?(filename, files_excluded)
else
false
end

if !file_included? || file_excluded? do
:skip_run
else
[]
end

true ->
exec
|> Execution.working_dir()
|> Credo.Sources.find_in_dir(files_included, files_excluded)
|> case do
[] -> :skip_run
files -> files
end
if files_included == known_files and files_excluded == [] do
[]
else
known_files
|> Enum.map(&Path.expand/1)
|> Enum.filter(&Credo.Sources.filename_matches?(&1, files_included))
|> Enum.reject(&Credo.Sources.filename_matches?(&1, files_excluded))
|> case do
[] -> :skip_run
files -> files
end
end

source_files =
Expand Down
157 changes: 157 additions & 0 deletions test/credo/check/runner_test.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,159 @@
defmodule Credo.Check.RunnerTest do
use ExUnit.Case, async: true

alias Credo.Check.Runner
alias Credo.Execution
alias Credo.Execution.ExecutionSourceFiles
alias Credo.SourceFile

defmodule CollectFilenamesCheck do
def param_defaults do
[
files: %{
included: nil,
excluded: []
}
]
end

def run_on_all_source_files(_exec, source_files, params) do
filenames = Enum.map(source_files, & &1.filename)
send(params[:test_pid], {:checked_files, filenames})
:ok
end
end

describe "read_from_stdin behaviour" do
test "runs the check on the stdin file when it matches files.included", %{test_pid: test_pid} do

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 25.3

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 25.3

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 25.3

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 26.2

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 27.3

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 26.2

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)

Check failure on line 27 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 26.2

test read_from_stdin behaviour runs the check on the stdin file when it matches files.included (Credo.Check.RunnerTest)
filename = Path.expand("lib/foo.ex")
source_file = %SourceFile{filename: filename}

exec =
ExecutionSourceFiles.start_server(%Execution{
read_from_stdin: true,
checks: %{
enabled: [
{CollectFilenamesCheck,
[files: %{included: ["lib/**/*.ex"], excluded: []}, test_pid: test_pid]}
],
disabled: []
}
})

ExecutionSourceFiles.put(exec, [source_file])

:ok = Runner.run([source_file], exec)

assert_receive {:checked_files, [^filename]}
end

test "runs the check with no files when the stdin file does not match files.included",

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 25.3

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 25.3

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 25.3

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 26.2

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 27.3

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 26.2

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)

Check failure on line 50 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 26.2

test read_from_stdin behaviour runs the check with no files when the stdin file does not match files.included (Credo.Check.RunnerTest)
%{test_pid: test_pid} do
filename = Path.expand("test/foo_test.exs")
source_file = %SourceFile{filename: filename}

exec =
ExecutionSourceFiles.start_server(%Execution{
read_from_stdin: true,
checks: %{
enabled: [
{CollectFilenamesCheck,
[files: %{included: ["lib/**/*.ex"], excluded: []}, test_pid: test_pid]}
],
disabled: []
}
})

ExecutionSourceFiles.put(exec, [source_file])

:ok = Runner.run([source_file], exec)

assert_receive {:checked_files, []}
end
end

describe "watch / rerun behaviour" do
test "runs the check only on the changed file", %{test_pid: test_pid} do

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 25.3

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 25.3

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 25.3

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 26.2

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 27.3

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 26.2

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)

Check failure on line 76 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 26.2

test watch / rerun behaviour runs the check only on the changed file (Credo.Check.RunnerTest)
foo = Path.expand("lib/foo.ex")
bar = Path.expand("lib/bar.ex")

source_files = [
%SourceFile{filename: foo},
%SourceFile{filename: bar}
]

exec =
ExecutionSourceFiles.start_server(%Execution{
read_from_stdin: false,
checks: %{
enabled: [
{CollectFilenamesCheck, [__rerun_files_that_changed__: [foo], test_pid: test_pid]}
],
disabled: []
}
})

ExecutionSourceFiles.put(exec, source_files)

:ok = Runner.run(source_files, exec)

assert_receive {:checked_files, [^foo]}
end

test "runs the check with no files when the changed file is not a known source file",

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 25.3

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 25.3

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 25.3

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 26.2

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 27.3

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 26.2

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)

Check failure on line 103 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 26.2

test watch / rerun behaviour runs the check with no files when the changed file is not a known source file (Credo.Check.RunnerTest)
%{test_pid: test_pid} do
foo = Path.expand("lib/foo.ex")
bar = Path.expand("lib/bar.ex")
baz = Path.expand("lib/baz.ex")

source_files = [
%SourceFile{filename: foo},
%SourceFile{filename: bar}
]

exec =
ExecutionSourceFiles.start_server(%Execution{
read_from_stdin: false,
checks: %{
enabled: [
{CollectFilenamesCheck, [__rerun_files_that_changed__: [baz], test_pid: test_pid]}
],
disabled: []
}
})

ExecutionSourceFiles.put(exec, source_files)

:ok = Runner.run(source_files, exec)

assert_receive {:checked_files, []}
end
end

describe "files pattern filtering" do
test "filters known files using files patterns from params", %{test_pid: test_pid} do

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 25.3

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 25.3

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 25.3

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.15.7 OTP 26.2

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 27.3

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.17.3 OTP 26.2

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)

Check failure on line 134 in test/credo/check/runner_test.exs

View workflow job for this annotation

GitHub Actions / Elixir 1.16.2 OTP 26.2

test files pattern filtering filters known files using files patterns from params (Credo.Check.RunnerTest)
dir = "test/fixtures/integration_test_config"
filename = Path.expand(Path.join(dir, "lib/clean.ex"))
source_file = %SourceFile{filename: filename}

exec =
ExecutionSourceFiles.start_server(%Execution{
cli_options: %{path: dir},
read_from_stdin: false,
checks: %{
enabled: [
{CollectFilenamesCheck,
[__files__: %{included: ["lib/*.ex"], excluded: []}, test_pid: test_pid]}
],
disabled: []
}
})

ExecutionSourceFiles.put(exec, [source_file])

:ok = Runner.run([source_file], exec)

assert_receive {:checked_files, [^filename]}
end
end
end
Loading