yak(mutation-testing-with-muex-blogex-pure-app-code-1341): mutation testing with muex (blogex + pure app code)

This commit is contained in:
Willem van den Ende
2026-10-29 10:07:55 +00:00
parent 09da15c1df
commit 3ef38bc8fc
14 changed files with 572 additions and 3 deletions
@@ -245,6 +245,24 @@ defmodule Blogex.LinkValidatorTest do
end
describe "validate_body/2" do
# The guard is a conjunction, not a disjunction: both arguments must have
# the documented types. Mutating `and` to `or` would let a string blog id
# (or a non-binary body) through the guard to crash somewhere else, so pin
# the contract at the door.
test "rejects a non-atom blog id at the function clause guard" do
assert_raise FunctionClauseError, fn ->
# apply/3 keeps the compiler's type checker quiet: the point is the
# runtime guard, and the test deliberately passes an invalid type.
apply(LinkValidator, :validate_body, ["Just text, no links.", "engineering"])
end
end
test "rejects a non-binary body at the function clause guard" do
assert_raise FunctionClauseError, fn ->
apply(LinkValidator, :validate_body, [[], :engineering])
end
end
test "returns :ok when body has no internal blog links" do
body = "Just text, no links."
+82
View File
@@ -0,0 +1,82 @@
defmodule Blogex.MutationConfigTest do
# The mutation sensor is opt-in: a dependency and two aliases, deliberately
# absent from `make verify`'s VERIFY_SENSORS. These pins keep the wiring
# honest (dev/test only, never runtime) and keep the aliases reading the
# scope files rather than duplicating a list in mix.exs.
use ExUnit.Case
test "muex is a dev/test-only, non-runtime dependency" do
deps = Mix.Project.config()[:deps]
assert {:muex, requirement, opts} = dep!(deps, :muex)
assert requirement =~ "0.12"
assert opts[:only] == [:dev, :test]
assert opts[:runtime] == false
end
test "mutate and mutate.fast aliases are defined" do
aliases = Mix.Project.config()[:aliases]
assert is_function(aliases[:mutate], 1)
assert is_function(aliases[:"mutate.fast"], 1)
end
test "mutation args always include the scope, the curated mutators and --fail-at 0" do
args = Blogex.MixProject.mutation_args("mutation_scope_fast.txt", [])
scope = Blogex.MixProject.read_mutation_scope("mutation_scope_fast.txt")
assert "--fail-at" in args
assert Enum.at(args, Enum.find_index(args, &(&1 == "--fail-at")) + 1) == "0"
assert files_arg(args) == Enum.join(scope, ",")
mutators = Enum.at(args, Enum.find_index(args, &(&1 == "--mutators")) + 1)
assert mutators ==
"arithmetic,boolean,comparison,conditional,function_call,literal,return_value"
end
test "extra CLI args pass through so a single file can be targeted" do
args =
Blogex.MixProject.mutation_args("mutation_scope_fast.txt", ["--files", "lib/blogex/seo.ex"])
assert args
|> Enum.chunk_every(2, 1, :discard)
|> Enum.any?(fn
["--files", "lib/blogex/seo.ex"] -> true
_ -> false
end)
end
test "an empty scope fails loudly instead of running over nothing" do
path =
Path.join(System.tmp_dir!(), "muex-empty-scope-#{System.unique_integer([:positive])}.txt")
File.write!(path, "\n \n")
on_exit(fn -> File.rm(path) end)
assert_raise Mix.Error, ~r/empty/, fn ->
Blogex.MixProject.read_mutation_scope(path)
end
end
defp dep!(deps, name) do
case Enum.find(deps, fn
{^name, _requirement} -> true
{^name, _requirement, _opts} -> true
_ -> false
end) do
{^name, requirement} -> {name, requirement, []}
{^name, requirement, opts} -> {name, requirement, opts}
nil -> flunk("expected #{inspect(name)} in deps/0")
end
end
defp files_arg(args) do
args
|> Enum.chunk_every(2, 1, :discard)
|> Enum.find_value(fn
["--files", files] -> files
_ -> nil
end)
end
end
+64
View File
@@ -0,0 +1,64 @@
defmodule Blogex.MutationScopeTest do
# The mutation scope files are the single source of truth for what `mix
# mutate` / `mix mutate.fast` are allowed to touch. These pins keep the
# sensor honest: a scope that quietly shrinks to an empty (or stale) set
# would report a score for a check it never ran.
use ExUnit.Case
@root Path.expand("..", __DIR__)
@full "mutation_scope.txt"
@fast "mutation_scope_fast.txt"
# Pure modules only. These are excluded deliberately:
# blogex.ex facade + moduledoc, nothing to mutate
# blogex/blog.ex the `__using__` macro (NimblePublisher expansion)
# router.ex Phoenix router DSL
# components.ex Phoenix component DSL
# layout.ex Phoenix/LiveView layout DSL
@shell_modules [
"lib/blogex.ex",
"lib/blogex/blog.ex",
"lib/blogex/router.ex",
"lib/blogex/components.ex",
"lib/blogex/layout.ex"
]
test "full scope lists only existing files" do
missing = Enum.reject(scope(@full), &File.exists?(Path.join(@root, &1)))
assert missing == [],
"mutation scope names files that do not exist: #{inspect(missing)}"
end
test "full scope is not empty" do
assert scope(@full) != []
end
test "fast scope is not empty" do
assert scope(@fast) != []
end
test "fast scope is a subset of full scope" do
out_of_scope = MapSet.difference(MapSet.new(scope(@fast)), MapSet.new(scope(@full)))
assert MapSet.size(out_of_scope) == 0,
"mutate.fast names files outside the audited scope: #{inspect(MapSet.to_list(out_of_scope))}"
end
test "scope excludes the macro and shell modules" do
scoped = MapSet.new(scope(@full) ++ scope(@fast))
present = Enum.filter(@shell_modules, &MapSet.member?(scoped, &1))
assert present == [],
"shell/macro modules belong outside the mutation scope: #{inspect(present)}"
end
defp scope(file) do
@root
|> Path.join(file)
|> File.read!()
|> String.split("\n", trim: true)
|> Enum.map(&String.trim/1)
|> Enum.reject(&(&1 == ""))
end
end