MixTester - tiny tool for testing mix tasks and any other project management tools

MixTester

This is a tiny tool I’ve developed during implementation of incremental compilation for my optimizing compiler. MixTester’s primary goal was to provide very simple and handy helpers for mix project initialization and project management in test environment.

Docs
Github

Features

  • 100% documented
  • Real mix project management experience with new flags and other stuff
  • Application env configuration
  • Dependency list and other mix.exs configuration
  • Handy and simple helpers for common commands
  • ExUnit’s async friendly

Example:

defmodule AwesomeTask do
  use ExUnit.Case, async: true

  setup do
    deps = [ {:awesome_task, path: File.cwd!()} ]
    configuration = %{{:awesome_task, :year} => 2007}

    # Creates project with `mix new my_project --sup`
    # Which has `awesome_task` as a dependency
    # And configuration where `config.exs` has
    project =
      MixTester.setup(
        name: "my_project",
        new: "--sup",
        application_env: %{
          "config" => configuration
        },
        project: [
          deps: deps
        ]
      )

    # Creates the test file
    MixTester.write_ast(project, "test/my_project_test.exs", quote do
      defmodule MyProjectTest do
        use ExUnit.Case, async: true

        test "Just works" do
          assert 2007 == AwesomeModule.what_year_is_today()
        end
      end
    end)

    # Cleanup the tmp dir
    on_exit(fn -> MixTester.cleanup(project) end)
    {:ok, project: project}
  end

  test "My awesome task", %{project: project} do
    # Run the task we are testing
    assert {_, 0} = MixTester.mix_cmd(project, "awesome")

    # Run the test written above and check if it's run successfully
    assert {_, 0} = MixTester.mix_cmd(project, "test")
  end
end
2 Likes