Skip to content

Commit

Permalink
Minor fixes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomohopia authored Nov 16, 2023
1 parent 7b786ac commit 8207afe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Add `:mutix` to your test dependencies in your `mix.exs` file:
```elixir
def deps do
[
{:mutix, git: "https://github.com/tuomohopia/mutix.git", tag: "v0.1.0", only: :test}
{:mutix, git: "https://github.com/tuomohopia/mutix.git", tag: "v0.1.0", only: [:dev, :test]}
]
end
```
Expand Down
11 changes: 9 additions & 2 deletions lib/mix/mutate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ defmodule Mix.Tasks.Mutate do
if source_file_count == 0,
do:
Mix.raise(
"Please provide path to the source file to mutate, e.g. `mix mutate lib/my_app/transformer.ex`. Other options unsupported at this time."
"Please provide path to the source file to mutate, e.g. `mix mutate lib/my_app/transformer.ex`."
)

if source_file_count > 1,
Expand All @@ -96,7 +96,10 @@ defmodule Mix.Tasks.Mutate do
end

if Mix.Task.recursing?(), do: Mix.raise("Umbrella apps not supported yet.")
unless File.exists?(source_file), do: Mix.raise("Source module file must exist.")

unless File.exists?(source_file),
do: Mix.raise("Source module file #{source_file} does not exist.")

_ = Mix.Project.get!()
project = Mix.Project.config()

Expand Down Expand Up @@ -137,6 +140,10 @@ defmodule Mix.Tasks.Mutate do
# One clean run first to assert all tests pass
ExUnit.CaptureIO.with_io(fn ->
case ExUnit.run() do
%{failures: 0, total: total, excluded: excluded, skipped: skipped}
when excluded + skipped == total ->
Mix.raise("No tests to run detected. Exited without running a mutation test suite.")

%{failures: 0} ->
:ok

Expand Down
5 changes: 3 additions & 2 deletions lib/mutix/report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule Mutix.Report do
def mutation(test_results, source_file_path, operator_mutation) do
{from, to} = operator_mutation
score = mutation_score(test_results)
percentage = score.survived_count / score.mutant_count * 100

percentage = (score.mutant_count - score.survived_count) / score.mutant_count * 100
survived = score.survived

survived_report =
Expand All @@ -32,7 +33,7 @@ defmodule Mutix.Report do
#{score.killed_count} / #{score.mutant_count} mutants killed by the test suite.
Mutation score: #{percentage} %
Mutation score: #{Float.round(percentage, 1)} %
#{survived_report}
"""
end
Expand Down
18 changes: 17 additions & 1 deletion test/mutix/report_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Mutix.ReportTest do
0 / 1 mutants killed by the test suite.
Mutation score: 100.0 %
Mutation score: 0.0 %
Surviving mutants (no test failed with these injections):
Expand All @@ -36,5 +36,21 @@ defmodule Mutix.ReportTest do
result = Report.mutation(test_results, source_file_path, operator_mutation)
assert result == String.replace_trailing(@single_operator_result, "\n\n\n", "\n\n")
end

test "reports mutation score correctly" do
test_results = [
{%{total: 5, failures: 0, excluded: 0, skipped: 0}, [index_on_line: 0, line: 3], ""},
{%{total: 5, failures: 1, excluded: 0, skipped: 0}, [index_on_line: 0, line: 5], ""},
{%{total: 5, failures: 5, excluded: 0, skipped: 0}, [index_on_line: 0, line: 8], ""}
]

source_file_path = "test/support/single_operator_source.ex"
operator_mutation = {:>, :<}

result = Report.mutation(test_results, source_file_path, operator_mutation)
assert result =~ "5 tests were run for each mutant."
assert result =~ "2 / 3 mutants killed by the test suite."
assert result =~ "Mutation score: 66.7 %"
end
end
end

0 comments on commit 8207afe

Please sign in to comment.