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

Add tracing #939

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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: 3 additions & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ module Mutant
require 'mutant/integration/null'
require 'mutant/selector'
require 'mutant/selector/expression'
require 'mutant/selector/intersection'
require 'mutant/selector/null'
require 'mutant/selector/trace'
require 'mutant/config'
require 'mutant/cli'
require 'mutant/color'
Expand Down Expand Up @@ -186,6 +188,7 @@ module Mutant
require 'mutant/variable'
require 'mutant/zombifier'
require 'mutant/range'
require 'mutant/line_trace'

module Mutant
WORLD = World.new(
Expand Down
23 changes: 22 additions & 1 deletion lib/mutant/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ def fmap(&block)
def apply(&block)
require_block(&block)
end

# Unwrap just
#
# @return [Object]
#
# rubocop:disable Style/GuardClause
def from_just
if block_given?
yield
else
fail "Expected just value, got #{inspect}"
end
end
# rubocop:enable Style/GuardClause
end # Nothing

class Just < self
Expand All @@ -66,10 +80,17 @@ def fmap

# Evalute applicative block
#
# @return [Maybe]
# @return [Maybe<Object>]
def apply
yield(value)
end

# Unwrap just
#
# @return [Object]
def from_just
value
end
end # Just
end # Maybe

Expand Down
2 changes: 1 addition & 1 deletion lib/mutant/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def kill(mutation)
# @return Hash{Mutation => Enumerable<Test>}
def selections
subjects.map do |subject|
[subject, selector.call(subject)]
[subject, selector.call(subject).from_just { EMPTY_ARRAY }]
end.to_h
end
memoize :selections
Expand Down
6 changes: 3 additions & 3 deletions lib/mutant/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Integration

# Setup integration
#
# @param env [Bootstrap]
# @param env [Env]
#
# @return [Either<String, Integration>]
def self.setup(env)
Expand All @@ -34,7 +34,7 @@ def self.setup(env)

# Attempt to require integration
#
# @param env [Bootstrap]
# @param env [Env]
#
# @return [Either<String, undefined>]
#
Expand All @@ -58,7 +58,7 @@ def self.attempt_require(env)

# Attempt const get
#
# @param env [Boostrap]
# @param env [Env]
#
# @return [Either<String, Class<Integration>>]
#
Expand Down
10 changes: 8 additions & 2 deletions lib/mutant/integration/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,21 @@ def all_tests_index
def parse_example(example, index)
metadata = example.metadata

location = metadata.fetch(:location)

path, lineno = location.split(':', 2)

id = TEST_ID_FORMAT % {
index: index,
location: metadata.fetch(:location),
location: location,
description: metadata.fetch(:full_description)
}

Test.new(
expression: parse_expression(metadata),
id: id
id: id,
lineno: Integer(lineno),
path: Pathname.pwd.join(path).to_s
)
end

Expand Down
17 changes: 17 additions & 0 deletions lib/mutant/line_trace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Mutant
# Line tracer
module LineTrace
# Run line tracer
def self.call(keep, &block)
lines = []

result = TracePoint.new(:b_call, :call, :line) do |trace|
lines << "#{trace.path}:#{trace.lineno}" if keep.call(trace)
end.enable(&block)

[result, lines.freeze].freeze
end
end # LineTrace
end # Mutant
82 changes: 81 additions & 1 deletion lib/mutant/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ module Runner
def self.apply(env)
reporter(env).start(env)

Either::Right.new(run_mutation_analysis(env))
run_tracing(env).fmap do |env|
run_mutation_analysis(env)
end
end

# Run mutation analysis
#
# @param [Env] env
#
# @return [undefined]
def self.run_mutation_analysis(env)
reporter = reporter(env)
Expand All @@ -27,6 +31,82 @@ def self.run_mutation_analysis(env)
end
private_class_method :run_mutation_analysis

# Run tracing
def self.run_tracing(env)
integration = env.integration
tests = integration.all_tests

subject_paths = env.subjects.map(&:source_path).map(&:to_s).to_set
paths = subject_paths + tests.map(&:path)

result = env.config.isolation.call do
LineTrace.call(->(trace) { paths.include?(trace.path) }) do
integration.call(tests)
end
end

if result.success?
test_result, traces = result.value

if test_result.passed
trace_selector(env, traces)
else
Either::Left.new('Trace tests did not pass! %s' % test_result.output)
end
else
Either::Left.new(trace_failure(result))
end
end

# Create trace selector
#
# @param [Env] env
# @param [Array<String>] traces
#
# @return [Either<String, Env>]
def self.trace_selector(env, traces)
all_tests = env.integration.all_tests

trace_tests = {}
test_traces = {}

all_tests.each do |test|
(trace_tests[test.trace_location] ||= Set.new) << test
end

current_traces = Set.new

traces.each do |trace|
tests = trace_tests[trace]

if tests
current_traces = tests.map do |test|
test_traces[test.id] ||= Set.new
end
end

current_traces.each { |test_trace| test_trace << trace }
end

missing = all_tests.count { |test| !test_traces.key?(test.id) }

if missing.zero?
Either::Right.new(
env.with(
selector: Selector::Intersection.new(
[
env.selector,
Selector::Trace.new(all_tests, test_traces)
]
)
)
)
else
Either::Left.new("total: #{all_tests.length} missing: #{missing}, found: #{test_traces.length} tests in traces")
end
end
private_class_method :trace_selector

# Run driver
#
# @param [Reporter] reporter
Expand Down
4 changes: 2 additions & 2 deletions lib/mutant/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Selector

# Tests for subject
#
# @param [Subject] subjecto
# @param [Subject] subject
#
# @return [Enumerable<Test>]
# @return [Maybe<Enumerable<Test>>]
abstract_method :call

end # Selector
Expand Down
7 changes: 4 additions & 3 deletions lib/mutant/selector/expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ class Expression < self
#
# @param [Subject] subject
#
# @return [Enumerable<Test>]
# @return [Maybe<Enumerable<Test>>]
def call(subject)
subject.match_expressions.each do |match_expression|
subject_tests = integration.all_tests.select do |test|
match_expression.prefix?(test.expression)
end
return subject_tests if subject_tests.any?

return Maybe::Just.new(subject_tests) if subject_tests.any?
end

EMPTY_ARRAY
Maybe::Just.new(EMPTY_ARRAY)
end

end # Expression
Expand Down
23 changes: 23 additions & 0 deletions lib/mutant/selector/intersection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Mutant
class Selector
# Selector that returns the intersection of tests returned by downstreadm selectors
class Intersection < self
include Adamantium::Flat, Concord.new(:selectors)

# Test selected for subject
#
# @param [Subject]
#
# @return [Maybe<Enumerable<Test>>]
def call(subject)
selections = selectors.map { |selector| selector.call(subject) }

Maybe::Just.new(
selections.reduce do |left, right|
left.from_just { return Maybe::Nothing.new } & right.from_just { return Maybe::Nothing.new }
end
)
end
end # Intersection
end # Selector
end # Mutant
4 changes: 2 additions & 2 deletions lib/mutant/selector/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class Null < self
#
# @param [Subject] subject
#
# @return [Enumerable<Test>]
# @return [Maybe<Enumerable<Test>>]
def call(_subject)
EMPTY_ARRAY
Maybe::Just.new(EMPTY_ARRAY)
end
end # Null
end # Selector
Expand Down
22 changes: 22 additions & 0 deletions lib/mutant/selector/trace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Mutant
class Selector
class Trace < self
include Concord.new(:tests, :test_traces)

# Tests for subject
#
# @param [Subject] subject
#
# @return [Maybe<Enumerable<Test>>]
def call(subject)
Maybe::Just.new(
tests.select do |test|
test_traces.fetch(test.id).include?(subject.trace_location)
end
)
end
end # Trace
end # Selector
end # Mutant
8 changes: 8 additions & 0 deletions lib/mutant/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def mutations
end
memoize :mutations

# Trace location
#
# @return [String]
def trace_location
"#{source_path}:#{source_line}"
end
memoize :trace_location

# Source path
#
# @return [Pathname]
Expand Down
16 changes: 15 additions & 1 deletion lib/mutant/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ module Mutant
class Test
include Adamantium::Flat, Anima.new(
:expression,
:id
:id,
:lineno,
:path
)

# Identification string
#
# @return [String]
alias_method :identification, :id

# Trace location
#
# @return [String]
def trace_location
"#{path}:#{lineno}"
end
memoize :trace_location

def <=>(other)
id <=> other.id
end

end # Test
end # Mutant
Loading