Created
July 9, 2026 19:25
-
-
Save sb8244/4d0e38bf9fe53a0811961803e331505b to your computer and use it in GitHub Desktop.
Helper to call `mix test lib/my_file.ex` and have it run `mix test test/my_file_test.exs`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # lib/mix/tasks/test_with_lib_paths.ex | |
| defmodule Mix.Tasks.TestWithLibPaths do | |
| @moduledoc """ | |
| Wrapper around `mix test` that accepts the exact same arguments, but | |
| rewrites any `lib/` path to its corresponding `test/` file before | |
| delegating. | |
| """ | |
| @shortdoc "Runs `mix test`, rewriting lib/ paths to their test files" | |
| use Mix.Task | |
| @impl Mix.Task | |
| def run(args) do | |
| replaced_args = | |
| Enum.map(args, fn arg -> | |
| cond do | |
| String.starts_with?(arg, "lib/") -> | |
| new_path = | |
| arg | |
| |> String.replace("lib", "test", global: false) | |
| |> String.replace(~r/\.ex$/, "_test.exs") | |
| if !File.exists?(new_path) do | |
| raise "Test does not exist #{new_path}" | |
| end | |
| Mix.shell().info("Replaced #{arg} → #{new_path}") | |
| new_path | |
| true -> | |
| arg | |
| end | |
| end) | |
| Mix.Task.run("test", replaced_args) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment