Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use seed to randomize require order of test files #12442

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ex_unit/lib/ex_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ defmodule ExUnit do
@doc since: "1.12.0"
@spec async_run() :: Task.t()
def async_run() do
options = persist_defaults(configuration())

Comment on lines +398 to +399
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to be outside of Task.async to ensure seed was set when it's fetched in test.ex

Task.async(fn ->
options = persist_defaults(configuration())
ExUnit.Runner.run(options, nil)
end)
end
Expand Down
12 changes: 12 additions & 0 deletions lib/mix/lib/mix/compilers/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Mix.Compilers.Test do
@compile {:no_warn_undefined, ExUnit}
@stale_manifest "compile.test_stale"
@manifest_vsn 1
@rand_algorithm :exs1024

@doc """
Requires and runs test files.
Expand Down Expand Up @@ -65,6 +66,8 @@ defmodule Mix.Compilers.Test do
true ->
task = ExUnit.async_run()
warnings_as_errors? = Keyword.get(opts, :warnings_as_errors, false)
seed = Application.fetch_env!(:ex_unit, :seed)
test_files = shuffle(seed, test_files)

try do
failed? =
Expand Down Expand Up @@ -315,4 +318,13 @@ defmodule Mix.Compilers.Test do
defp get_external_resources(module, cwd) do
for file <- Module.get_attribute(module, :external_resource), do: Path.relative_to(file, cwd)
end

defp shuffle(_seed = 0, list) do
list
end

defp shuffle(seed, list) do
_ = :rand.seed(@rand_algorithm, {seed, seed, seed})
Enum.shuffle(list)
end
end
26 changes: 26 additions & 0 deletions lib/mix/test/mix/tasks/test_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,32 @@ defmodule Mix.Tasks.TestTest do
end
end

describe "--seed" do
test "requires modules alphabetical order if seed is 0" do
in_fixture("test_stale", fn ->
opts = ["--seed", "0", "--trace"]
# Forcing the parallel compiler to operate synchronously
envs = [{"ELIXIR_ERL_OPTIONS", "+S 1:1"}]
output = mix(["test" | opts], envs)

matches = Regex.scan(~r"\wTest\s\[.*\]", output)
assert matches == [["BTest [test/b_test_stale.exs]"], ["ATest [test/a_test_stale.exs]"]]
end)
end

test "requires modules in random order if seed is provided" do
in_fixture("test_stale", fn ->
opts = ["--seed", "5", "--trace"]
# Forcing the parallel compiler to operate synchronously
envs = [{"ELIXIR_ERL_OPTIONS", "+S 1:1"}]
output = mix(["test" | opts], envs)

matches = Regex.scan(~r"\wTest\s\[.*\]", output)
assert matches == [["ATest [test/a_test_stale.exs]"], ["BTest [test/b_test_stale.exs]"]]
end)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those tests are rather expensive and they rely on implementation details-ish. I am thinking we could just skip them altogether? Other than that, the impl is great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me! Let me remove the test.


describe "--stale" do
test "runs all tests for first run, then none on second" do
in_fixture("test_stale", fn ->
Expand Down