Class: DeepCover::Coverage

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

Overview

A collection of CoveredCode

Defined Under Namespace

Classes: Persistence

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Coverage

Returns a new instance of Coverage.



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

def initialize(**options)
  @covered_codes = {}
  @options = options
end

Class Method Details

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



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

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)


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

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

Instance Method Details

#basic_reportObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deep_cover/coverage.rb', line 72

def basic_report
  missing = map do |covered_code|
    if covered_code.has_executed?
      missed = covered_code.line_coverage.each_with_index.map do |line_cov, line_index|
        line_index + 1 if line_cov == 0
      end.compact
    else
      missed = ['all']
    end
    [covered_code.buffer.name, missed] unless missed.empty?
  end.compact.to_h
  missing.map do |path, lines|
    "#{File.basename(path)}: #{lines.join(', ')}"
  end.join("\n")
end

#covered_code(path, **options) ⇒ Object



32
33
34
35
# 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_codes[path] ||= CoveredCode.new(path: path, **options, **@options)
end

#covered_codesObject



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

def covered_codes
  @covered_codes.dup
end

#eachObject



37
38
39
40
41
# File 'lib/deep_cover/coverage.rb', line 37

def each
  return to_enum unless block_given?
  @covered_codes.each_value { |covered_code| yield covered_code }
  self
end

#line_coverage(filename, **options) ⇒ Object



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

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

#output_istanbul(dir: '.', name: '.nyc_output', **options) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/deep_cover/coverage.rb', line 50

def output_istanbul(dir: '.', name: '.nyc_output', **options)
  path = Pathname.new(dir).expand_path.join(name)
  path.mkpath
  path.each_child(&:delete)
  path.join('deep_cover.json').write(JSON.pretty_generate(to_istanbul(**options)))
  path
end

#report(**options) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/deep_cover/coverage.rb', line 88

def report(**options)
  if Reporter::Istanbul.available?
    report_istanbul(**options)
  else
    warn 'nyc not available. Please install `nyc` using `yarn global add nyc` or `npm i nyc -g`'
    basic_report
  end
end

#report_istanbul(output: nil, **options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/deep_cover/coverage.rb', line 58

def report_istanbul(output: nil, **options)
  dir = output_istanbul(**options).dirname
  unless [nil, '', 'false'].include? output
    output = File.expand_path(output)
    html = "--reporter=html --report-dir='#{output}'"
    if options[:open]
      html += " && open '#{output}/index.html'"
    else
      msg = "\nHTML coverage written to: '#{output}/index.html'"
    end
  end
  `cd #{dir} && nyc report --reporter=text #{html}` + msg.to_s
end

#resetObject



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

def reset
  @covered_codes = {}
end

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



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

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

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



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

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

#to_istanbul(**options) ⇒ Object



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

def to_istanbul(**options)
  map do |covered_code|
    next {} unless covered_code.has_executed?
    covered_code.to_istanbul(**options)
  end.inject(:merge)
end

#tracker_globalObject



115
116
117
# File 'lib/deep_cover/coverage.rb', line 115

def tracker_global
  @options.fetch(:tracker_global, CoveredCode::DEFAULT_TRACKER_GLOBAL)
end