Listing all modules from an Elixir/Erlang application

Given an application called :my_new_app. The mix.exs file would be something like this:

  def project do
    [
      app: :my_new_app,
      version: "1.1.0",
      elixir: "~> 1.10",
      ...
    ]
  end

Now to list all modules that are part of this application one can run :application.get_key/2 like this:

iex> :application.get_key(:my_new_app, :modules)
{:ok, [MyNewApp, MyNewApp.MyNewModule, MyNewApp.Supervisor]}

How could this be useful?

Let's say you want to ensure that all modules have a corresponding test file:

test "test files exist" do
  {:ok, modules} = :application.get_key(:my_new_app, :modules)

  for module <- modules do
    assert File.exists?("test/#{Macro.underscore(module)}_test.exs")
  end
end

Application specification

This is not the only key defined as part of the application specification. To list everything :application.get_all_key/1 can be used.

More info about the application specification: https://erlang.org/doc/man/app.html#file-syntax

Posted on July 5, 2020