-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add support for "--failed" in ExUnit #7373
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a73d4f
Deal with `:file` tags that were incorrectly overriden by a `setup` b…
myronmarston 77adfb9
Refactor: have mix pass the manifest file instead of dir path
myronmarston 0ba4b2c
Add support for `mix test --failed`
myronmarston a28bb62
Optimize ExUnit manifest to store only failures
myronmarston c50937a
Address code review feedback
myronmarston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
defmodule ExUnit.FailuresManifest do | ||
@moduledoc false | ||
|
||
@type test_id :: {module, name :: atom} | ||
@opaque t :: %{test_id => test_file :: Path.t()} | ||
|
||
@manifest_vsn 1 | ||
|
||
@spec new() :: t | ||
def new, do: %{} | ||
|
||
@spec files_with_failures(t) :: MapSet.t(Path.t()) | ||
def files_with_failures(%{} = manifest) do | ||
manifest | ||
|> Map.values() | ||
|> MapSet.new() | ||
end | ||
|
||
@spec failed_test_ids(t) :: MapSet.t(test_id) | ||
def failed_test_ids(%{} = manifest) do | ||
manifest | ||
|> Map.keys() | ||
|> MapSet.new() | ||
end | ||
|
||
@spec put_test(t, ExUnit.Test.t()) :: t | ||
# TODO: figure out a way to prevent ExUnit from allowing the `file` tag | ||
# to be set incorrectly instead of using this function clause | ||
# (which only ignores values of the wrong type, but not wrong binary values) | ||
def put_test(%{} = manifest, %ExUnit.Test{tags: %{file: file}}) | ||
when not is_binary(file), | ||
do: manifest | ||
|
||
def put_test(%{} = manifest, %ExUnit.Test{state: {ignored_state, _}}) | ||
when ignored_state in [:skipped, :excluded], | ||
do: manifest | ||
|
||
def put_test(%{} = manifest, %ExUnit.Test{state: nil} = test) do | ||
Map.delete(manifest, {test.module, test.name}) | ||
end | ||
|
||
def put_test(%{} = manifest, %ExUnit.Test{state: {failed_state, _}} = test) | ||
when failed_state in [:failed, :invalid] do | ||
Map.put(manifest, {test.module, test.name}, test.tags.file) | ||
end | ||
|
||
@spec write!(t, Path.t()) :: :ok | ||
def write!(manifest, file) when is_binary(file) do | ||
manifest = prune_deleted_tests(manifest) | ||
binary = :erlang.term_to_binary({@manifest_vsn, manifest}) | ||
Path.dirname(file) |> File.mkdir_p!() | ||
File.write!(file, binary) | ||
end | ||
|
||
@spec read(Path.t()) :: t | ||
def read(file) when is_binary(file) do | ||
with {:ok, binary} <- File.read(file), | ||
{:ok, {@manifest_vsn, manifest}} when is_map(manifest) <- safe_binary_to_term(binary) do | ||
manifest | ||
else | ||
_ -> new() | ||
end | ||
end | ||
|
||
defp safe_binary_to_term(binary) do | ||
{:ok, :erlang.binary_to_term(binary)} | ||
rescue | ||
ArgumentError -> | ||
:error | ||
end | ||
|
||
defp prune_deleted_tests(manifest) do | ||
Map.drop(manifest, find_deleted_tests(Enum.to_list(manifest), %{}, [])) | ||
end | ||
|
||
defp find_deleted_tests([], _file_existence, deleted_tests), do: deleted_tests | ||
|
||
defp find_deleted_tests([{{mod, name} = id, file} | rest] = all, file_existence, acc) do | ||
file_exists = Map.fetch(file_existence, file) | ||
|
||
cond do | ||
file_exists == :error -> | ||
# This is the first time we've looked up the existence of the file. | ||
# Cache the result and try again. | ||
file_existence = Map.put(file_existence, file, File.regular?(file)) | ||
find_deleted_tests(all, file_existence, acc) | ||
|
||
file_exists == {:ok, false} -> | ||
# The file does not exist, so the test has been deleted. | ||
find_deleted_tests(rest, file_existence, [id | acc]) | ||
|
||
:code.is_loaded(mod) != false and not function_exported?(mod, name, 1) -> | ||
# The test module has been loaded, but the test no longer exists. | ||
find_deleted_tests(rest, file_existence, [id | acc]) | ||
|
||
true -> | ||
# The file exists and the test module was not loaded (which means the test | ||
# *might* still exist) or the function is exported (which means the test | ||
# *definitely* still exists). Either way, we do not want to prune it. | ||
find_deleted_tests(rest, file_existence, acc) | ||
end | ||
end | ||
end |
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ defmodule ExUnit.Runner do | |
include: opts[:include], | ||
manager: manager, | ||
max_cases: opts[:max_cases], | ||
only_test_ids: opts[:only_test_ids], | ||
seed: opts[:seed], | ||
modules: :async, | ||
timeout: opts[:timeout], | ||
|
@@ -132,8 +133,9 @@ defmodule ExUnit.Runner do | |
tests = shuffle(config, tests) | ||
include = config.include | ||
exclude = config.exclude | ||
test_ids = config.only_test_ids | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to document this and the failure_manifest_file in lib/ex_unit.ex. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
for test <- tests do | ||
for test <- tests, include_test?(test_ids, test) do | ||
tags = Map.merge(test.tags, %{test: test.name, module: test.module}) | ||
|
||
case ExUnit.Filters.eval(include, exclude, tags, tests) do | ||
|
@@ -143,6 +145,12 @@ defmodule ExUnit.Runner do | |
end | ||
end | ||
|
||
defp include_test?(nil, _test), do: true | ||
|
||
defp include_test?(test_ids, test) do | ||
MapSet.member?(test_ids, {test.module, test.name}) | ||
end | ||
|
||
defp spawn_module(config, test_module, tests) do | ||
parent = self() | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a TODO here so we don't forget to check why we need this clause after merging. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.