Class: DeepCover::Coverage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/deep_cover/coverage.rb,
lib/deep_cover/coverage/analysis.rb,
lib/deep_cover/coverage/persistence.rb

Overview

A collection of CoveredCode

Defined Under Namespace

Classes: Analysis, Persistence

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Coverage

Returns a new instance of Coverage.



14
15
16
17
18
# File 'lib/deep_cover/coverage.rb', line 14

def initialize(**options)
  @covered_code_index = {}
  @options = options
  @tracker_storage_per_path = TrackerStoragePerPath.new(TrackerBucket[tracker_global])
end

Instance Attribute Details

#tracker_storage_per_pathObject (readonly)

Returns the value of attribute tracker_storage_per_path.



12
13
14
# File 'lib/deep_cover/coverage.rb', line 12

def tracker_storage_per_path
  @tracker_storage_per_path
end

Class Method Details

.load(dest_path, dirname = 'deep_cover', with_trackers: true) ⇒ Object



88
89
90
# File 'lib/deep_cover/coverage.rb', line 88

def self.load(dest_path, dirname = 'deep_cover', with_trackers: true)
  Persistence.new(dest_path, dirname).load(with_trackers: with_trackers)
end

.saved?(dest_path, dirname = 'deep_cover') ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/deep_cover/coverage.rb', line 92

def self.saved?(dest_path, dirname = 'deep_cover')
  Persistence.new(dest_path, dirname).saved?
end

Instance Method Details

#analysis(**options) ⇒ Object



110
111
112
# File 'lib/deep_cover/coverage.rb', line 110

def analysis(**options)
  Analysis.new(covered_codes, options)
end

#covered_code(path, **options) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/deep_cover/coverage.rb', line 32

def covered_code(path, **options)
  raise 'path must be an absolute path' unless Pathname.new(path).absolute?
  @covered_code_index[path] ||= CoveredCode.new(path: path,
                                                tracker_storage: @tracker_storage_per_path[path],
                                                **options,
                                                **@options)
end

#covered_code?(path) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/deep_cover/coverage.rb', line 28

def covered_code?(path)
  @covered_code_index.include?(path)
end

#covered_code_or_warn(path, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/deep_cover/coverage.rb', line 40

def covered_code_or_warn(path, **options)
  covered_code(path, **options)
rescue Parser::SyntaxError => e
  if e.message =~ /contains escape sequences incompatible with UTF-8/
    warn "Can't cover #{path} because of incompatible encoding (see issue #9)"
  else
    warn "The file #{path} can't be instrumented"
  end
  nil
end

#covered_codesObject



20
21
22
# File 'lib/deep_cover/coverage.rb', line 20

def covered_codes
  each.to_a
end

#eachObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/deep_cover/coverage.rb', line 52

def each
  return to_enum unless block_given?
  @tracker_storage_per_path.each_key do |path|
    begin
      cov_code = covered_code(path)
    rescue Parser::SyntaxError
      next
    end
    yield cov_code
  end
  self
end

#line_coverage(filename, **options) ⇒ Object



24
25
26
# File 'lib/deep_cover/coverage.rb', line 24

def line_coverage(filename, **options)
  covered_code(filename).line_coverage(**options)
end

#report(**options) ⇒ Object



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

def report(**options)
  case (reporter = options.fetch(:reporter, DEFAULTS[:reporter]).to_sym)
  when :html
    msg = if (output = options.fetch(:output, DEFAULTS[:output]))
            Reporter::HTML.report(self, **options)
            "HTML generated: open #{output}/index.html"
          else
            'No HTML generated'
          end
    Reporter::Text.report(self, **options) + "\n\n" + msg
  when :istanbul
    if Reporter::Istanbul.available?
      Reporter::Istanbul.report(self, **options)
    else
      warn 'nyc not available. Please install `nyc` using `yarn global add nyc` or `npm i nyc -g`'
    end
  when :text
    Reporter::Text.report(self, **options)
  else
    raise ArgumentError, "Unknown reporter: #{reporter}"
  end
end

#save(dest_path, dirname = 'deep_cover') ⇒ Object



96
97
98
99
# File 'lib/deep_cover/coverage.rb', line 96

def save(dest_path, dirname = 'deep_cover')
  Persistence.new(dest_path, dirname).save(self)
  self
end

#save_trackers(dest_path, dirname = 'deep_cover') ⇒ Object



101
102
103
104
# File 'lib/deep_cover/coverage.rb', line 101

def save_trackers(dest_path, dirname = 'deep_cover')
  Persistence.new(dest_path, dirname).save_trackers(@tracker_storage_per_path.tracker_hits_per_path)
  self
end

#tracker_globalObject



106
107
108
# File 'lib/deep_cover/coverage.rb', line 106

def tracker_global
  @options.fetch(:tracker_global, DEFAULTS[:tracker_global])
end