Skip to content

Instantly share code, notes, and snippets.

@vnegrisolo
Created August 4, 2020 00:53
Show Gist options
  • Save vnegrisolo/47cf4f97785a8ac2fe83e8feb2646af1 to your computer and use it in GitHub Desktop.
Save vnegrisolo/47cf4f97785a8ac2fe83e8feb2646af1 to your computer and use it in GitHub Desktop.
defmodule Finder do
def run do
"find . | grep 'lib/' | grep -v 'deps/'"
|> cmd()
|> String.split("\n")
|> Enum.filter(&String.ends_with?(&1, ".ex"))
|> Enum.map(&find_test_file/1)
|> Enum.map(&find_tests/1)
|> Enum.filter(&has_missing_tests/1)
|> Map.new()
end
defp find_test_file(lib_file) do
test = lib_file
|> String.replace("lib/", "test/")
|> String.replace(~r/\.ex$/, "_test.exs")
{lib_file, test}
end
defp find_tests({lib_file, test_file}) do
dir = test_file |> Path.basename()
cmd("mkdir #{dir}")
cmd("touch #{test_file}")
functions = lib_file
|> File.read!()
|> find_public_funcs()
|> Enum.uniq()
tested_functions = case File.read(test_file) do
{:ok, test_content} -> find_describes(test_content)
_ -> []
end |> Enum.uniq()
missing_tests = Enum.sort(functions -- tested_functions)
{lib_file, missing_tests}
end
defp has_missing_tests({_lib_file, []}), do: false
defp has_missing_tests({_lib_file, _missing_tests}), do: true
defp find_public_funcs(file_content) do
~r/\sdef\s(\w+)/
|> Regex.scan(file_content)
|> Enum.map(&List.last/1)
end
defp find_describes(file_content) do
~r/\sdescribe\s"(\w+)/
|> Regex.scan(file_content)
|> Enum.map(&List.last/1)
end
defp cmd(command) do
command
|> String.to_char_list
|> :os.cmd()
|> to_string()
|> String.trim()
end
end
Finder.run() |> IO.inspect(limit: :infinity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment