wrong_test_file_extension.ex (1208B)
1 defmodule Credo.Check.Warning.WrongTestFileExtension do 2 use Credo.Check, 3 base_priority: :high, 4 param_defaults: [included: ["test/**/*_test.ex"]], 5 explanations: [ 6 check: """ 7 Invoking mix test from the command line will run the tests in each file 8 matching the pattern `*_test.exs` found in the test directory of your project. 9 10 (from the `ex_unit` docs) 11 12 This check ensures that test files are not ending with `.ex` (which would cause them to be skipped). 13 """ 14 ] 15 16 @test_files_with_ex_ending_regex ~r/test\/.*\/.*_test.ex$/ 17 18 alias Credo.SourceFile 19 20 @doc false 21 def run(%SourceFile{filename: filename} = source_file, params \\ []) do 22 issue_meta = IssueMeta.for(source_file, params) 23 24 if matches?(filename, @test_files_with_ex_ending_regex) do 25 issue_meta 26 |> issue_for() 27 |> List.wrap() 28 else 29 [] 30 end 31 end 32 33 defp issue_for(issue_meta) do 34 format_issue( 35 issue_meta, 36 message: "Test files should end with .exs" 37 ) 38 end 39 40 defp matches?(directory, path) when is_binary(path), do: String.starts_with?(directory, path) 41 defp matches?(directory, %Regex{} = regex), do: Regex.match?(regex, directory) 42 end