Class: FastCov::CoverageMap

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_cov/coverage_map.rb

Defined Under Namespace

Classes: ConfigurationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCoverageMap

Returns a new instance of CoverageMap.



12
13
14
15
16
17
18
19
20
# File 'lib/fast_cov/coverage_map.rb', line 12

def initialize
  @root = Dir.pwd
  @threads = true
  @ignored_paths = []
  @connected_dependencies = ConnectedDependencies.new
  @trackers = []
  @native_coverage = nil
  @started = false
end

Instance Attribute Details

#connected_dependenciesObject (readonly)

Returns the value of attribute connected_dependencies.



10
11
12
# File 'lib/fast_cov/coverage_map.rb', line 10

def connected_dependencies
  @connected_dependencies
end

#ignored_pathsObject

Returns the value of attribute ignored_paths.



8
9
10
# File 'lib/fast_cov/coverage_map.rb', line 8

def ignored_paths
  @ignored_paths
end

#rootObject

Returns the value of attribute root.



9
10
11
# File 'lib/fast_cov/coverage_map.rb', line 9

def root
  @root
end

#threadsObject

Returns the value of attribute threads.



7
8
9
# File 'lib/fast_cov/coverage_map.rb', line 7

def threads
  @threads
end

Instance Method Details

#buildObject

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fast_cov/coverage_map.rb', line 55

def build
  raise ArgumentError, "build requires a block" unless block_given?
  raise "CoverageMap is already started" if @started

  start
  begin
    yield
  ensure
    result = stop
  end
  result
end

#connect(from:, to:) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/fast_cov/coverage_map.rb', line 108

def connect(from:, to:)
  source = normalize_path(from)
  target = normalize_path(to)
  return unless include_path?(source)
  return unless include_path?(target)
  return if source == target

  @connected_dependencies.connect(from: source, to: target)
end

#include_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/fast_cov/coverage_map.rb', line 100

def include_path?(path)
  return false unless path
  return false unless Utils.path_within?(path, normalized_root)
  return true if @ignored_paths.empty?

  normalized_ignored_paths.none? { |ignored_path| Utils.path_within?(path, ignored_path) }
end

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fast_cov/coverage_map.rb', line 68

def start
  return self if @started

  begin
    @native_coverage = Coverage.new(
      root: normalized_root,
      ignored_paths: normalized_ignored_paths,
      threads: @threads != false
    )
    @native_coverage.start
    @trackers.each(&:start)
    @started = true
  rescue StandardError
    cleanup_failed_start
    raise
  end

  self
end

#stopObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fast_cov/coverage_map.rb', line 88

def stop
  return Set.new unless @started

  result = Set.new(@native_coverage.stop.each_key)
  @trackers.reverse_each { |tracker| result.merge(tracker.stop) }
  @connected_dependencies.expand(result)
  Utils.relativize_paths(result, normalized_root)
ensure
  @native_coverage = nil
  @started = false
end

#use(tracker_class, **options) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
# File 'lib/fast_cov/coverage_map.rb', line 45

def use(tracker_class, **options)
  raise "CoverageMap is already started" if @started
  raise ArgumentError, "#{tracker_class} is already registered" if @trackers.any? { |tracker| tracker.is_a?(tracker_class) }

  tracker = tracker_class.new(self, **options)
  tracker.install if tracker.respond_to?(:install)
  @trackers << tracker
  self
end