simplecov 0.22.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +71 -916
  4. data/exe/simplecov +11 -0
  5. data/lib/minitest/simplecov_plugin.rb +13 -5
  6. data/lib/simplecov/atomic_file.rb +70 -0
  7. data/lib/simplecov/autostart.rb +11 -0
  8. data/lib/simplecov/cli/clean.rb +87 -0
  9. data/lib/simplecov/cli/command_helpers.rb +55 -0
  10. data/lib/simplecov/cli/coverage.rb +86 -0
  11. data/lib/simplecov/cli/coverage_file.rb +65 -0
  12. data/lib/simplecov/cli/diff.rb +147 -0
  13. data/lib/simplecov/cli/dotfile.rb +111 -0
  14. data/lib/simplecov/cli/merge.rb +120 -0
  15. data/lib/simplecov/cli/open.rb +48 -0
  16. data/lib/simplecov/cli/report.rb +97 -0
  17. data/lib/simplecov/cli/run.rb +36 -0
  18. data/lib/simplecov/cli/serve/report_preparer.rb +29 -0
  19. data/lib/simplecov/cli/serve/static_file_handler.rb +122 -0
  20. data/lib/simplecov/cli/serve.rb +85 -0
  21. data/lib/simplecov/cli/uncovered.rb +103 -0
  22. data/lib/simplecov/cli.rb +163 -0
  23. data/lib/simplecov/color.rb +83 -0
  24. data/lib/simplecov/combine/branches_combiner.rb +54 -7
  25. data/lib/simplecov/combine/coverage_accumulator.rb +268 -0
  26. data/lib/simplecov/combine/identity_interner.rb +30 -0
  27. data/lib/simplecov/combine/interned_counts.rb +30 -0
  28. data/lib/simplecov/combine/lines_combiner.rb +53 -23
  29. data/lib/simplecov/combine/methods_combiner.rb +74 -0
  30. data/lib/simplecov/combine/results_combiner.rb +13 -36
  31. data/lib/simplecov/combine.rb +5 -23
  32. data/lib/simplecov/command_guesser.rb +104 -33
  33. data/lib/simplecov/configuration/coverage.rb +172 -0
  34. data/lib/simplecov/configuration/coverage_criteria.rb +154 -0
  35. data/lib/simplecov/configuration/eval_coverage.rb +41 -0
  36. data/lib/simplecov/configuration/filters.rb +164 -0
  37. data/lib/simplecov/configuration/formatting.rb +138 -0
  38. data/lib/simplecov/configuration/groups.rb +42 -0
  39. data/lib/simplecov/configuration/ignored_entries.rb +63 -0
  40. data/lib/simplecov/configuration/merging.rb +165 -0
  41. data/lib/simplecov/configuration/thresholds.rb +179 -0
  42. data/lib/simplecov/configuration.rb +83 -420
  43. data/lib/simplecov/coverage_json.rb +24 -0
  44. data/lib/simplecov/coverage_statistics.rb +16 -10
  45. data/lib/simplecov/coverage_violations.rb +159 -0
  46. data/lib/simplecov/defaults.rb +34 -23
  47. data/lib/simplecov/deprecation.rb +45 -0
  48. data/lib/simplecov/directive.rb +167 -0
  49. data/lib/simplecov/exit_codes/check.rb +33 -0
  50. data/lib/simplecov/exit_codes/exit_code_handling.rb +8 -2
  51. data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +18 -65
  52. data/lib/simplecov/exit_codes/maximum_overall_coverage_check.rb +32 -0
  53. data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +17 -36
  54. data/lib/simplecov/exit_codes/minimum_coverage_by_group_check.rb +30 -0
  55. data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +37 -33
  56. data/lib/simplecov/exit_codes.rb +20 -0
  57. data/lib/simplecov/exit_handling.rb +128 -0
  58. data/lib/simplecov/file_list.rb +70 -24
  59. data/lib/simplecov/filter.rb +105 -26
  60. data/lib/simplecov/formatter/base.rb +118 -0
  61. data/lib/simplecov/formatter/coverage_json_writer.rb +97 -0
  62. data/lib/simplecov/formatter/html_formatter/public/index.html +87 -0
  63. data/lib/simplecov/formatter/html_formatter/viewer_data_validator.rb +96 -0
  64. data/lib/simplecov/formatter/html_formatter.rb +105 -0
  65. data/lib/simplecov/formatter/json_formatter/errors_formatter.rb +88 -0
  66. data/lib/simplecov/formatter/json_formatter/result_hash_formatter.rb +118 -0
  67. data/lib/simplecov/formatter/json_formatter/source_file_formatter.rb +93 -0
  68. data/lib/simplecov/formatter/json_formatter.rb +46 -0
  69. data/lib/simplecov/formatter/multi_formatter.rb +12 -8
  70. data/lib/simplecov/formatter/simple_formatter.rb +14 -11
  71. data/lib/simplecov/formatter.rb +16 -0
  72. data/lib/simplecov/group_names.rb +32 -0
  73. data/lib/simplecov/last_run.rb +18 -4
  74. data/lib/simplecov/lines_classifier.rb +48 -15
  75. data/lib/simplecov/load_global_config.rb +12 -4
  76. data/lib/simplecov/parallel_adapters/base.rb +78 -0
  77. data/lib/simplecov/parallel_adapters/generic.rb +45 -0
  78. data/lib/simplecov/parallel_adapters/parallel_tests.rb +93 -0
  79. data/lib/simplecov/parallel_adapters.rb +85 -0
  80. data/lib/simplecov/parallel_coordination.rb +134 -0
  81. data/lib/simplecov/parallel_result_merger.rb +230 -0
  82. data/lib/simplecov/process.rb +52 -12
  83. data/lib/simplecov/profiles/bundler_filter.rb +1 -1
  84. data/lib/simplecov/profiles/hidden_filter.rb +1 -1
  85. data/lib/simplecov/profiles/rails.rb +24 -10
  86. data/lib/simplecov/profiles/root_filter.rb +6 -5
  87. data/lib/simplecov/profiles/strict.rb +32 -0
  88. data/lib/simplecov/profiles/test_frameworks.rb +1 -4
  89. data/lib/simplecov/profiles.rb +32 -3
  90. data/lib/simplecov/report_deferral.rb +49 -0
  91. data/lib/simplecov/report_stamp.rb +28 -0
  92. data/lib/simplecov/result/missing_source_files_reporter.rb +49 -0
  93. data/lib/simplecov/result/source_file_builder.rb +51 -0
  94. data/lib/simplecov/result.rb +145 -26
  95. data/lib/simplecov/result_adapter.rb +139 -8
  96. data/lib/simplecov/result_merger/legacy_format_adapter.rb +28 -0
  97. data/lib/simplecov/result_merger/resultset_file.rb +74 -0
  98. data/lib/simplecov/result_merger/resultset_run_identity.rb +67 -0
  99. data/lib/simplecov/result_merger/resultset_store.rb +53 -0
  100. data/lib/simplecov/result_merger/unloaded_files.rb +103 -0
  101. data/lib/simplecov/result_merger.rb +109 -114
  102. data/lib/simplecov/result_processing.rb +218 -0
  103. data/lib/simplecov/run_identity.rb +77 -0
  104. data/lib/simplecov/simulate_coverage.rb +83 -12
  105. data/lib/simplecov/source_file/branch.rb +1 -3
  106. data/lib/simplecov/source_file/branch_builder.rb +114 -0
  107. data/lib/simplecov/source_file/builder_context.rb +28 -0
  108. data/lib/simplecov/source_file/line.rb +9 -4
  109. data/lib/simplecov/source_file/line_builder.rb +43 -0
  110. data/lib/simplecov/source_file/method.rb +58 -0
  111. data/lib/simplecov/source_file/method_builder.rb +60 -0
  112. data/lib/simplecov/source_file/ruby_data_parser.rb +110 -0
  113. data/lib/simplecov/source_file/skip_chunks.rb +72 -0
  114. data/lib/simplecov/source_file/source_loader.rb +79 -0
  115. data/lib/simplecov/source_file/statistics.rb +65 -0
  116. data/lib/simplecov/source_file.rb +66 -232
  117. data/lib/simplecov/static_coverage_extractor/condition_folding.rb +239 -0
  118. data/lib/simplecov/static_coverage_extractor/location_conventions.rb +236 -0
  119. data/lib/simplecov/static_coverage_extractor/method_collector.rb +62 -0
  120. data/lib/simplecov/static_coverage_extractor/prism_compat.rb +55 -0
  121. data/lib/simplecov/static_coverage_extractor/value_position.rb +74 -0
  122. data/lib/simplecov/static_coverage_extractor/visitor.rb +183 -0
  123. data/lib/simplecov/static_coverage_extractor.rb +110 -0
  124. data/lib/simplecov/unloaded_file_injector.rb +75 -0
  125. data/lib/simplecov/useless_results_remover.rb +16 -7
  126. data/lib/simplecov/version.rb +1 -1
  127. data/lib/simplecov-html.rb +4 -0
  128. data/lib/simplecov.rb +185 -380
  129. data/lib/simplecov_json_formatter.rb +4 -0
  130. data/sig/simplecov.rbs +1777 -0
  131. metadata +111 -62
  132. data/CHANGELOG.md +0 -191
  133. data/doc/alternate-formatters.md +0 -71
  134. data/doc/commercial-services.md +0 -25
  135. data/doc/editor-integration.md +0 -18
  136. data/lib/simplecov/combine/files_combiner.rb +0 -24
  137. data/lib/simplecov/default_formatter.rb +0 -20
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ class SourceFile
5
+ # Computes the set of line ranges that should be excluded from a
6
+ # SourceFile's coverage for each criterion. Two sources contribute:
7
+ #
8
+ # * The deprecated `# :nocov:` block toggle (lines wrapped between
9
+ # even-numbered pairs of nocov markers are excluded from line and
10
+ # branch coverage).
11
+ # * `# simplecov:disable` / `# simplecov:enable` block directives,
12
+ # which can be scoped per-criterion (`# simplecov:disable branch`,
13
+ # etc.) — see `SimpleCov::Directive`.
14
+ class SkipChunks
15
+ @nocov_warned = Set.new
16
+ class << self
17
+ attr_reader :nocov_warned
18
+ end
19
+
20
+ def initialize(filename, src)
21
+ @filename = filename
22
+ @src = src
23
+ end
24
+
25
+ # Every criterion honors the deprecated all-criteria `# :nocov:`
26
+ # chunks plus its own per-criterion directive ranges. Methods
27
+ # included: they carry a source range, and nocov has always meant
28
+ # "exclude everything here".
29
+ def for(criterion)
30
+ nocov_chunks + directive_chunks.fetch(criterion)
31
+ end
32
+
33
+ # Ranges of 1-based line numbers (see `with_index(1)` below);
34
+ # consumers subtract 1 to index into the zero-based lines array.
35
+ def nocov_chunks
36
+ @nocov_chunks ||= build_nocov_chunks
37
+ end
38
+
39
+ def directive_chunks
40
+ @directive_chunks ||= Directive.disabled_ranges(@src)
41
+ end
42
+
43
+ private
44
+
45
+ def build_nocov_chunks
46
+ no_cov_lines = @src.map.with_index(1).select { |line_src, _index| LinesClassifier.no_cov_line?(line_src) }
47
+
48
+ warn_nocov_deprecation(no_cov_lines.first.last) if no_cov_lines.any?
49
+
50
+ # If we have an uneven number of nocovs we assume they go to the
51
+ # end of the file, the source doesn't really matter. Can't deal
52
+ # with this within the each_slice due to differing behavior in
53
+ # JRuby: jruby/jruby#6048
54
+ no_cov_lines << ["", @src.size] if no_cov_lines.size.odd?
55
+
56
+ no_cov_lines.each_slice(2).map do |(_line_src_start, index_start), (_line_src_end, index_end)|
57
+ index_start..index_end
58
+ end
59
+ end
60
+
61
+ # Emit a one-time-per-file deprecation warning pointing the user
62
+ # at the `# simplecov:disable` / `# simplecov:enable` replacement.
63
+ def warn_nocov_deprecation(first_line_number)
64
+ return unless self.class.nocov_warned.add?(@filename)
65
+
66
+ token = SimpleCov.current_nocov_token
67
+ warn "#{@filename}:#{first_line_number}: [DEPRECATION] `# :#{token}:` is deprecated and will be removed " \
68
+ "in a future release. Replace with `# simplecov:disable` / `# simplecov:enable` block comments."
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ class SourceFile
5
+ # Reads a source file into an array of lines, honoring the source's
6
+ # shebang and `coding:` magic comment when present. Always
7
+ # transcodes to UTF-8 with invalid/undef bytes replaced — both for
8
+ # JRuby compatibility and to keep encoding shenanigans in one place
9
+ # (see #866).
10
+ module SourceLoader
11
+ SHEBANG_REGEX = /\A#!/
12
+ RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/
13
+
14
+ module_function
15
+
16
+ def call(filename)
17
+ lines = [] #: Array[String]
18
+ # The default encoding is UTF-8
19
+ File.open(filename, "rb:UTF-8") do |file|
20
+ current_line = scrub_invalid(file.gets)
21
+
22
+ if current_line && shebang?(current_line)
23
+ lines << current_line
24
+ current_line = scrub_invalid(file.gets)
25
+ end
26
+
27
+ read_lines(file, lines, current_line)
28
+ end
29
+ end
30
+
31
+ # A line read as UTF-8 can still carry invalid bytes (a Latin-1
32
+ # source file without a magic comment, say). Replace them before
33
+ # any regex sees the line: the shebang and magic-comment checks
34
+ # would otherwise raise ArgumentError and take the report down.
35
+ def scrub_invalid(line)
36
+ return line if line.nil? || line.valid_encoding?
37
+
38
+ line.scrub
39
+ end
40
+
41
+ def shebang?(line)
42
+ SHEBANG_REGEX.match?(line)
43
+ end
44
+
45
+ def read_lines(file, lines, current_line)
46
+ return lines unless current_line
47
+
48
+ set_encoding_based_on_magic_comment(file, current_line)
49
+ lines.concat([current_line], ensure_remove_undefs(file.readlines))
50
+ end
51
+
52
+ # Encoding magic comment must be placed at first line except for
53
+ # shebang.
54
+ def set_encoding_based_on_magic_comment(file, line)
55
+ if (match = RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX.match(line))
56
+ file.set_encoding(match[1], "UTF-8")
57
+ end
58
+ end
59
+
60
+ # Guarantee every line leaves the loader as valid UTF-8, replacing
61
+ # what can't be represented: transcode non-UTF-8 lines (setting
62
+ # invalid/undef options on `file.set_encoding` doesn't work
63
+ # properly, and this also works around a JRuby incompatibility)
64
+ # and scrub UTF-8-tagged lines that carry invalid bytes, which
65
+ # would otherwise raise from every regex the classifier runs.
66
+ def ensure_remove_undefs(file_lines)
67
+ file_lines.each do |line|
68
+ # simplecov:disable — defensive: only fires for non-UTF-8 source files
69
+ if line.encoding == Encoding::UTF_8
70
+ line.scrub! unless line.valid_encoding?
71
+ else
72
+ line.encode!("UTF-8", invalid: :replace, undef: :replace)
73
+ end
74
+ # simplecov:enable
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ class SourceFile
5
+ # Builds the `CoverageStatistics` triple (`:line`, `:branch`, `:method`)
6
+ # for a SourceFile, regardless of which criteria were actually enabled
7
+ # during the run — disabled or empty criteria collapse to 0/0/0 so
8
+ # downstream consumers don't have to special-case enable-state.
9
+ class Statistics
10
+ def initialize(source_file)
11
+ @source_file = source_file
12
+ end
13
+
14
+ def call
15
+ {
16
+ **line_statistics,
17
+ **branch_statistics,
18
+ **method_statistics
19
+ }
20
+ end
21
+
22
+ private
23
+
24
+ def line_statistics
25
+ {
26
+ line: coverage_statistics(
27
+ @source_file.covered_lines,
28
+ @source_file.missed_lines,
29
+ omitted: @source_file.never_lines.size
30
+ )
31
+ }
32
+ end
33
+
34
+ # Files added via track_files but never loaded have no branch/method
35
+ # data. Report 0% instead of the empty-set default of 100% (see #902).
36
+ def branch_statistics
37
+ sf = @source_file
38
+ covered = sf.covered_branches
39
+ missed = sf.missed_branches
40
+ percent = 0.0 if sf.not_loaded? && covered.empty? && missed.empty?
41
+
42
+ {branch: coverage_statistics(covered, missed, percent: percent)}
43
+ end
44
+
45
+ def method_statistics
46
+ sf = @source_file
47
+ covered = sf.covered_methods
48
+ missed = sf.missed_methods
49
+ percent = 0.0 if sf.not_loaded? && covered.empty? && missed.empty?
50
+
51
+ {method: coverage_statistics(covered, missed, percent: percent)}
52
+ end
53
+
54
+ def coverage_statistics(covered, missed, omitted: 0, percent: nil)
55
+ CoverageStatistics.new(
56
+ total_strength: covered.sum { |item| item.coverage.to_i },
57
+ covered: covered.size,
58
+ missed: missed.size,
59
+ omitted: omitted,
60
+ percent: percent
61
+ )
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,46 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "directive"
4
+ require_relative "static_coverage_extractor"
5
+ require_relative "source_file/ruby_data_parser"
6
+ require_relative "source_file/source_loader"
7
+ require_relative "source_file/skip_chunks"
8
+ require_relative "source_file/builder_context"
9
+ require_relative "source_file/line_builder"
10
+ require_relative "source_file/branch_builder"
11
+ require_relative "source_file/method_builder"
12
+ require_relative "source_file/statistics"
13
+
3
14
  module SimpleCov
4
15
  #
5
16
  # Representation of a source file including it's coverage data, source code,
6
17
  # source lines and featuring helpers to interpret that data.
7
18
  #
8
19
  class SourceFile
20
+ include BuilderContext
21
+
9
22
  # The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
10
23
  attr_reader :filename
11
24
  # The array of coverage data received from the Coverage.result
12
25
  attr_reader :coverage_data
13
26
 
14
- def initialize(filename, coverage_data)
27
+ def initialize(filename, coverage_data, loaded: true)
15
28
  @filename = filename
16
29
  @coverage_data = coverage_data
30
+ @loaded = loaded
17
31
  end
18
32
 
19
33
  # The path to this source file relative to the projects directory
20
34
  def project_filename
21
- @filename.delete_prefix(SimpleCov.root)
35
+ @filename.delete_prefix(SimpleCov.root).sub(%r{\A[/\\]}, "")
22
36
  end
23
37
 
24
- # The source code for this file. Aliased as :source
38
+ # The source code for this file. Aliased as :source.
39
+ # Intentionally read lazily to suppress reading unused source code.
25
40
  def src
26
- # We intentionally read source code lazily to
27
- # suppress reading unused source code.
28
- @src ||= load_source
41
+ @src ||= SourceLoader.call(filename)
29
42
  end
30
43
  alias source src
31
44
 
32
- def coverage_statistics
33
- @coverage_statistics ||=
34
- {
35
- **line_coverage_statistics,
36
- **branch_coverage_statistics
37
- }
45
+ # Returns a hash keyed by every supported coverage criterion. Each
46
+ # value is a CoverageStatistics, even for criteria that weren't
47
+ # enabled during the run — those collapse to 0/0/0. Consumers
48
+ # (FileList, formatters) decide which keys to surface based on
49
+ # `SimpleCov.coverage_criterion_enabled?`.
50
+ # The per-criterion coverage statistics for this file. With no argument
51
+ # returns the `{line:, branch:, method:}` Hash; pass a criterion symbol
52
+ # (`:line` / `:branch` / `:method`) to get that one CoverageStatistics.
53
+ def coverage_statistics(criterion = nil)
54
+ stats = (@coverage_statistics ||= Statistics.new(self).call)
55
+ criterion ? stats[criterion] : stats
38
56
  end
39
57
 
40
58
  # Returns all source lines for this file as instances of SimpleCov::SourceFile::Line,
41
59
  # and thus including coverage data. Aliased as :source_lines
42
60
  def lines
43
- @lines ||= build_lines
61
+ @lines ||= LineBuilder.new(self).call
44
62
  end
45
63
  alias source_lines lines
46
64
 
@@ -68,7 +86,7 @@ module SimpleCov
68
86
 
69
87
  # Returns the number of relevant lines (covered + missed)
70
88
  def lines_of_code
71
- coverage_statistics[:line]&.total
89
+ coverage_statistics[:line]&.total || 0
72
90
  end
73
91
 
74
92
  # Access SimpleCov::SourceFile::Line source lines by line number
@@ -76,65 +94,60 @@ module SimpleCov
76
94
  lines[number - 1]
77
95
  end
78
96
 
79
- # The coverage for this file in percent. 0 if the file has no coverage lines
80
- def covered_percent
81
- coverage_statistics[:line]&.percent
97
+ # The coverage for this file in percent, for the given criterion (line by
98
+ # default). Returns nil if the criterion was not measured.
99
+ def covered_percent(criterion = :line)
100
+ coverage_statistics(criterion)&.percent
82
101
  end
83
102
 
84
- def covered_strength
85
- coverage_statistics[:line]&.strength
103
+ def covered_strength(criterion = :line)
104
+ coverage_statistics(criterion)&.strength
86
105
  end
87
106
 
88
107
  def no_lines?
89
- lines.length.zero? || (lines.length == never_lines.size)
108
+ lines.empty? || (lines.length == never_lines.size)
90
109
  end
91
110
 
92
111
  def relevant_lines
93
112
  lines.size - never_lines.size - skipped_lines.size
94
113
  end
95
114
 
96
- #
97
115
  # Return all the branches inside current source file
98
116
  def branches
99
- @branches ||= build_branches
117
+ @branches ||= BranchBuilder.new(self).call
100
118
  end
101
119
 
102
120
  def no_branches?
103
121
  total_branches.empty?
104
122
  end
105
123
 
124
+ # DEPRECATED: use `covered_percent(:branch)`.
106
125
  def branches_coverage_percent
107
- coverage_statistics[:branch]&.percent
126
+ SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#branches_coverage_percent` is deprecated. " \
127
+ "Use `covered_percent(:branch)`.")
128
+ covered_percent(:branch)
108
129
  end
109
130
 
110
- #
111
131
  # Return the relevant branches to source file
112
132
  def total_branches
113
133
  @total_branches ||= covered_branches + missed_branches
114
134
  end
115
135
 
116
- #
117
136
  # Return hash with key of line number and branch coverage count as value
118
137
  def branches_report
119
- @branches_report ||= build_branches_report
138
+ @branches_report ||=
139
+ branches.reject(&:skipped?).group_by(&:report_line).transform_values { |bs| bs.map(&:report) }
120
140
  end
121
141
 
122
- #
123
- # Select the covered branches
124
- # Here we user tree schema because some conditions like case may have additional
125
- # else that is not in declared inside the code but given by default by coverage report
126
- #
127
- # @return [Array]
128
- #
142
+ # Select the covered branches. We use a tree schema here because
143
+ # some conditions like `case` may have an additional `else` that
144
+ # isn't declared in code but is given by default by the coverage
145
+ # report.
129
146
  def covered_branches
130
147
  @covered_branches ||= branches.select(&:covered?)
131
148
  end
132
149
 
133
- #
134
150
  # Select the missed branches with coverage equal to zero
135
- #
136
- # @return [Array]
137
- #
138
151
  def missed_branches
139
152
  @missed_branches ||= branches.select(&:missed?)
140
153
  end
@@ -143,213 +156,34 @@ module SimpleCov
143
156
  branches_report.fetch(line_number, [])
144
157
  end
145
158
 
146
- #
147
159
  # Check if any branches missing on given line number
148
- #
149
- # @param [Integer] line_number
150
- #
151
- # @return [Boolean]
152
- #
153
160
  def line_with_missed_branch?(line_number)
154
- branches_for_line(line_number).select { |_type, count| count.zero? }.any?
155
- end
156
-
157
- private
158
-
159
- # no_cov_chunks is zero indexed to work directly with the array holding the lines
160
- def no_cov_chunks
161
- @no_cov_chunks ||= build_no_cov_chunks
162
- end
163
-
164
- def build_no_cov_chunks
165
- no_cov_lines = src.map.with_index(1).select { |line_src, _index| LinesClassifier.no_cov_line?(line_src) }
166
-
167
- # if we have an uneven number of nocovs we assume they go to the
168
- # end of the file, the source doesn't really matter
169
- # Can't deal with this within the each_slice due to differing
170
- # behavior in JRuby: jruby/jruby#6048
171
- no_cov_lines << ["", src.size] if no_cov_lines.size.odd?
172
-
173
- no_cov_lines.each_slice(2).map do |(_line_src_start, index_start), (_line_src_end, index_end)|
174
- index_start..index_end
175
- end
176
- end
177
-
178
- def load_source
179
- lines = []
180
- # The default encoding is UTF-8
181
- File.open(filename, "rb:UTF-8") do |file|
182
- current_line = file.gets
183
-
184
- if shebang?(current_line)
185
- lines << current_line
186
- current_line = file.gets
187
- end
188
-
189
- read_lines(file, lines, current_line)
190
- end
191
- end
192
-
193
- SHEBANG_REGEX = /\A#!/.freeze
194
- def shebang?(line)
195
- SHEBANG_REGEX.match?(line)
196
- end
197
-
198
- def read_lines(file, lines, current_line)
199
- return lines unless current_line
200
-
201
- set_encoding_based_on_magic_comment(file, current_line)
202
- lines.concat([current_line], ensure_remove_undefs(file.readlines))
203
- end
204
-
205
- RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX = /\A#\s*(?:-\*-)?\s*(?:en)?coding:\s*(\S+)\s*(?:-\*-)?\s*\z/.freeze
206
- def set_encoding_based_on_magic_comment(file, line)
207
- # Check for encoding magic comment
208
- # Encoding magic comment must be placed at first line except for shebang
209
- if (match = RUBY_FILE_ENCODING_MAGIC_COMMENT_REGEX.match(line))
210
- file.set_encoding(match[1], "UTF-8")
211
- end
161
+ branches_for_line(line_number).any? { |_type, count| count.zero? }
212
162
  end
213
163
 
214
- def ensure_remove_undefs(file_lines)
215
- # invalid/undef replace are technically not really necessary but nice to
216
- # have and work around a JRuby incompatibility. Also moved here from
217
- # simplecov-html to have encoding shenaningans in one place. See #866
218
- # also setting these option on `file.set_encoding` doesn't seem to work
219
- # properly so it has to be done here.
220
- file_lines.each do |line|
221
- if line.encoding == Encoding::UTF_8
222
- line
223
- else
224
- line.encode!("UTF-8", invalid: :replace, undef: :replace)
225
- end
226
- end
164
+ # Return all methods detected in this source file
165
+ def methods
166
+ @methods ||= MethodBuilder.new(self).call
227
167
  end
228
168
 
229
- def build_lines
230
- coverage_exceeding_source_warn if coverage_data["lines"].size > src.size
231
- lines = src.map.with_index(1) do |src, i|
232
- SimpleCov::SourceFile::Line.new(src, i, coverage_data["lines"][i - 1])
233
- end
234
- process_skipped_lines(lines)
169
+ def covered_methods
170
+ @covered_methods ||= methods.select(&:covered?)
235
171
  end
236
172
 
237
- def process_skipped_lines(lines)
238
- # the array the lines are kept in is 0-based whereas the line numbers in the nocov
239
- # chunks are 1-based and are expected to be like this in other parts (and it's also
240
- # arguably more understandable)
241
- no_cov_chunks.each { |chunk| lines[(chunk.begin - 1)..(chunk.end - 1)].each(&:skipped!) }
242
-
243
- lines
244
- end
245
-
246
- def lines_strength
247
- lines.sum { |line| line.coverage.to_i }
248
- end
249
-
250
- # Warning to identify condition from Issue #56
251
- def coverage_exceeding_source_warn
252
- warn "Warning: coverage data provided by Coverage [#{coverage_data['lines'].size}] exceeds number of lines in #{filename} [#{src.size}]"
253
- end
254
-
255
- #
256
- # Build full branches report
257
- # Root branches represent the wrapper of all condition state that
258
- # have inside the branches
259
- #
260
- # @return [Hash]
261
- #
262
- def build_branches_report
263
- branches.reject(&:skipped?).each_with_object({}) do |branch, coverage_statistics|
264
- coverage_statistics[branch.report_line] ||= []
265
- coverage_statistics[branch.report_line] << branch.report
266
- end
267
- end
268
-
269
- #
270
- # Call recursive method that transform our static hash to array of objects
271
- # @return [Array]
272
- #
273
- def build_branches
274
- coverage_branch_data = coverage_data.fetch("branches", {})
275
- branches = coverage_branch_data.flat_map do |condition, coverage_branches|
276
- build_branches_from(condition, coverage_branches)
277
- end
278
-
279
- process_skipped_branches(branches)
280
- end
281
-
282
- def process_skipped_branches(branches)
283
- return branches if no_cov_chunks.empty?
284
-
285
- branches.each do |branch|
286
- branch.skipped! if no_cov_chunks.any? { |no_cov_chunk| branch.overlaps_with?(no_cov_chunk) }
287
- end
288
-
289
- branches
290
- end
291
-
292
- # Since we are dumping to and loading from JSON, and we have arrays as keys those
293
- # don't make their way back to us intact e.g. just as a string
294
- #
295
- # We should probably do something different here, but as it stands these are
296
- # our data structures that we write so eval isn't _too_ bad.
297
- #
298
- # See #801
299
- #
300
- def restore_ruby_data_structure(structure)
301
- # Tests use the real data structures (except for integration tests) so no need to
302
- # put them through here.
303
- return structure if structure.is_a?(Array)
304
-
305
- # rubocop:disable Security/Eval
306
- eval structure
307
- # rubocop:enable Security/Eval
308
- end
309
-
310
- def build_branches_from(condition, branches)
311
- # the format handed in from the coverage data is like this:
312
- #
313
- # [:then, 4, 6, 6, 6, 10]
314
- #
315
- # which is [type, id, start_line, start_col, end_line, end_col]
316
- _condition_type, _condition_id, condition_start_line, * = restore_ruby_data_structure(condition)
317
-
318
- branches.map do |branch_data, hit_count|
319
- branch_data = restore_ruby_data_structure(branch_data)
320
- build_branch(branch_data, hit_count, condition_start_line)
321
- end
322
- end
323
-
324
- def build_branch(branch_data, hit_count, condition_start_line)
325
- type, _id, start_line, _start_col, end_line, _end_col = branch_data
326
-
327
- SourceFile::Branch.new(
328
- start_line: start_line,
329
- end_line: end_line,
330
- coverage: hit_count,
331
- inline: start_line == condition_start_line,
332
- type: type
333
- )
173
+ def missed_methods
174
+ @missed_methods ||= methods.select(&:missed?)
334
175
  end
335
176
 
336
- def line_coverage_statistics
337
- {
338
- line: CoverageStatistics.new(
339
- total_strength: lines_strength,
340
- covered: covered_lines.size,
341
- missed: missed_lines.size
342
- )
343
- }
177
+ # DEPRECATED: use `covered_percent(:method)`.
178
+ def methods_coverage_percent
179
+ SimpleCov::Deprecation.warn("`SimpleCov::SourceFile#methods_coverage_percent` is deprecated. " \
180
+ "Use `covered_percent(:method)`.")
181
+ covered_percent(:method)
344
182
  end
345
183
 
346
- def branch_coverage_statistics
347
- {
348
- branch: CoverageStatistics.new(
349
- covered: covered_branches.size,
350
- missed: missed_branches.size
351
- )
352
- }
184
+ # Whether this file was added via track_files but never loaded/required.
185
+ def not_loaded?
186
+ !@loaded
353
187
  end
354
188
  end
355
189
  end