Class: TestBench::DetectCoverage::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/detect_coverage/trace.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actuator, probe, local_directory) ⇒ Trace

Returns a new instance of Trace.



8
9
10
11
12
# File 'lib/test_bench/detect_coverage/trace.rb', line 8

def initialize(actuator, probe, local_directory)
  @actuator = actuator
  @probe = probe
  @local_directory = local_directory
end

Instance Attribute Details

#actuatorObject (readonly)

Returns the value of attribute actuator.



4
5
6
# File 'lib/test_bench/detect_coverage/trace.rb', line 4

def actuator
  @actuator
end

#local_directoryObject (readonly)

Returns the value of attribute local_directory.



6
7
8
# File 'lib/test_bench/detect_coverage/trace.rb', line 6

def local_directory
  @local_directory
end

#probeObject (readonly)

Returns the value of attribute probe.



5
6
7
# File 'lib/test_bench/detect_coverage/trace.rb', line 5

def probe
  @probe
end

Class Method Details

.build(actuator, local_directory: nil, &probe) ⇒ Object



14
15
16
17
18
# File 'lib/test_bench/detect_coverage/trace.rb', line 14

def self.build(actuator, local_directory: nil, &probe)
  local_directory ||= Dir.pwd

  new(actuator, probe, local_directory)
end

.callObject



20
21
22
23
# File 'lib/test_bench/detect_coverage/trace.rb', line 20

def self.call(...)
  instance = build(...)
  instance.()
end

.gem_path_patternObject



99
100
101
102
103
# File 'lib/test_bench/detect_coverage/trace.rb', line 99

def self.gem_path_pattern
  ruby_version = RbConfig::CONFIG['ruby_version']

  File.join('**', 'gems', 'ruby', ruby_version, 'gems', '**')
end

.internal_path_patternObject



79
80
81
# File 'lib/test_bench/detect_coverage/trace.rb', line 79

def self.internal_path_pattern
  %r{\A<internal:[[:graph:]]+>\z}
end

Instance Method Details

#callObject



25
26
27
28
29
30
31
32
33
# File 'lib/test_bench/detect_coverage/trace.rb', line 25

def call
  trace = TracePoint.trace(:call) do |trace_point|
    trace_point(trace_point)
  end

  actuator.()
ensure
  trace.disable
end

#external?(trace_point) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/test_bench/detect_coverage/trace.rb', line 83

def external?(trace_point)
  path = trace_point.path

  gem_path = File.fnmatch?(self.class.gem_path_pattern, path)
  if gem_path
    return true
  end

  external_path = !path.start_with?(local_directory)
  if external_path
    return true
  end

  false
end

#internal?(trace_point) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/test_bench/detect_coverage/trace.rb', line 73

def internal?(trace_point)
  path = trace_point.path

  self.class.internal_path_pattern.match?(path)
end

#trace_point(trace_point) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/test_bench/detect_coverage/trace.rb', line 35

def trace_point(trace_point)
  if internal?(trace_point)
    return
  elsif external?(trace_point)
    return
  end

  path = trace_point.path

  cls = trace_point.defined_class

  if cls.singleton_class?
    receiver = trace_point.self

    if receiver.is_a?(Module)
      cls = trace_point.self

      scope_symbol = '.'
    else
      cls = trace_point.self.class

      scope_symbol = '#'
    end
  else
    scope_symbol = '#'
  end

  constant_name = cls.name

  method_specifier = [
    constant_name,
    scope_symbol,
    trace_point.method_id
  ].join

  probe.(method_specifier)
end