simplecov 0.15.1 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +116 -1
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +12 -9
  5. data/ISSUE_TEMPLATE.md +23 -0
  6. data/{MIT-LICENSE → LICENSE} +0 -0
  7. data/README.md +302 -116
  8. data/doc/alternate-formatters.md +20 -0
  9. data/lib/simplecov.rb +321 -45
  10. data/lib/simplecov/combine.rb +30 -0
  11. data/lib/simplecov/combine/branches_combiner.rb +32 -0
  12. data/lib/simplecov/combine/files_combiner.rb +24 -0
  13. data/lib/simplecov/combine/lines_combiner.rb +43 -0
  14. data/lib/simplecov/combine/results_combiner.rb +60 -0
  15. data/lib/simplecov/command_guesser.rb +8 -3
  16. data/lib/simplecov/configuration.rb +112 -9
  17. data/lib/simplecov/coverage_statistics.rb +56 -0
  18. data/lib/simplecov/defaults.rb +13 -80
  19. data/lib/simplecov/exit_codes.rb +2 -0
  20. data/lib/simplecov/file_list.rb +68 -13
  21. data/lib/simplecov/filter.rb +5 -2
  22. data/lib/simplecov/formatter.rb +2 -0
  23. data/lib/simplecov/formatter/multi_formatter.rb +4 -2
  24. data/lib/simplecov/formatter/simple_formatter.rb +6 -4
  25. data/lib/simplecov/last_run.rb +5 -1
  26. data/lib/simplecov/lines_classifier.rb +21 -5
  27. data/lib/simplecov/load_global_config.rb +2 -0
  28. data/lib/simplecov/no_defaults.rb +2 -0
  29. data/lib/simplecov/profiles.rb +11 -7
  30. data/lib/simplecov/profiles/bundler_filter.rb +5 -0
  31. data/lib/simplecov/profiles/hidden_filter.rb +5 -0
  32. data/lib/simplecov/profiles/rails.rb +18 -0
  33. data/lib/simplecov/profiles/root_filter.rb +10 -0
  34. data/lib/simplecov/profiles/test_frameworks.rb +8 -0
  35. data/lib/simplecov/result.rb +41 -6
  36. data/lib/simplecov/result_adapter.rb +30 -0
  37. data/lib/simplecov/result_merger.rb +20 -11
  38. data/lib/simplecov/simulate_coverage.rb +29 -0
  39. data/lib/simplecov/source_file.rb +225 -126
  40. data/lib/simplecov/source_file/branch.rb +84 -0
  41. data/lib/simplecov/source_file/line.rb +72 -0
  42. data/lib/simplecov/useless_results_remover.rb +16 -0
  43. data/lib/simplecov/version.rb +3 -1
  44. metadata +43 -160
  45. data/.gitignore +0 -31
  46. data/.rspec +0 -3
  47. data/.rubocop.yml +0 -88
  48. data/.travis.yml +0 -29
  49. data/.yardopts +0 -1
  50. data/Gemfile +0 -38
  51. data/Rakefile +0 -39
  52. data/cucumber.yml +0 -13
  53. data/features/config_autoload.feature +0 -46
  54. data/features/config_command_name.feature +0 -45
  55. data/features/config_coverage_dir.feature +0 -33
  56. data/features/config_deactivate_merging.feature +0 -42
  57. data/features/config_formatters.feature +0 -77
  58. data/features/config_merge_timeout.feature +0 -39
  59. data/features/config_nocov_token.feature +0 -79
  60. data/features/config_profiles.feature +0 -44
  61. data/features/config_project_name.feature +0 -27
  62. data/features/config_styles.feature +0 -121
  63. data/features/config_tracked_files.feature +0 -29
  64. data/features/config_tracked_files_relevant_lines.feature +0 -31
  65. data/features/cucumber_basic.feature +0 -29
  66. data/features/maximum_coverage_drop.feature +0 -89
  67. data/features/merging_test_unit_and_rspec.feature +0 -44
  68. data/features/minimum_coverage.feature +0 -59
  69. data/features/refuse_coverage_drop.feature +0 -95
  70. data/features/rspec_basic.feature +0 -32
  71. data/features/rspec_fails_on_initialization.feature +0 -14
  72. data/features/rspec_groups_and_filters_basic.feature +0 -29
  73. data/features/rspec_groups_and_filters_complex.feature +0 -37
  74. data/features/rspec_groups_using_filter_class.feature +0 -41
  75. data/features/rspec_without_simplecov.feature +0 -20
  76. data/features/skipping_code_blocks_manually.feature +0 -70
  77. data/features/step_definitions/html_steps.rb +0 -44
  78. data/features/step_definitions/simplecov_steps.rb +0 -68
  79. data/features/step_definitions/transformers.rb +0 -13
  80. data/features/step_definitions/web_steps.rb +0 -64
  81. data/features/support/aruba_freedom_patch.rb +0 -53
  82. data/features/support/env.rb +0 -50
  83. data/features/test_unit_basic.feature +0 -34
  84. data/features/test_unit_groups_and_filters_basic.feature +0 -29
  85. data/features/test_unit_groups_and_filters_complex.feature +0 -35
  86. data/features/test_unit_groups_using_filter_class.feature +0 -40
  87. data/features/test_unit_without_simplecov.feature +0 -20
  88. data/features/unicode_compatiblity.feature +0 -67
  89. data/lib/simplecov/jruby_fix.rb +0 -42
  90. data/lib/simplecov/railtie.rb +0 -7
  91. data/lib/simplecov/railties/tasks.rake +0 -11
  92. data/lib/simplecov/raw_coverage.rb +0 -39
  93. data/simplecov.gemspec +0 -27
  94. data/spec/1_8_fallbacks_spec.rb +0 -31
  95. data/spec/command_guesser_spec.rb +0 -48
  96. data/spec/config_loader_spec.rb +0 -14
  97. data/spec/configuration_spec.rb +0 -35
  98. data/spec/defaults_spec.rb +0 -41
  99. data/spec/deleted_source_spec.rb +0 -12
  100. data/spec/faked_project/Gemfile +0 -6
  101. data/spec/faked_project/Rakefile +0 -8
  102. data/spec/faked_project/cucumber.yml +0 -13
  103. data/spec/faked_project/features/step_definitions/my_steps.rb +0 -22
  104. data/spec/faked_project/features/support/env.rb +0 -12
  105. data/spec/faked_project/features/test_stuff.feature +0 -6
  106. data/spec/faked_project/lib/faked_project.rb +0 -11
  107. data/spec/faked_project/lib/faked_project/framework_specific.rb +0 -18
  108. data/spec/faked_project/lib/faked_project/meta_magic.rb +0 -24
  109. data/spec/faked_project/lib/faked_project/some_class.rb +0 -28
  110. data/spec/faked_project/lib/faked_project/untested_class.rb +0 -11
  111. data/spec/faked_project/spec/faked_spec.rb +0 -11
  112. data/spec/faked_project/spec/forking_spec.rb +0 -8
  113. data/spec/faked_project/spec/meta_magic_spec.rb +0 -15
  114. data/spec/faked_project/spec/some_class_spec.rb +0 -13
  115. data/spec/faked_project/spec/spec_helper.rb +0 -11
  116. data/spec/faked_project/test/faked_test.rb +0 -11
  117. data/spec/faked_project/test/meta_magic_test.rb +0 -13
  118. data/spec/faked_project/test/some_class_test.rb +0 -15
  119. data/spec/faked_project/test/test_helper.rb +0 -12
  120. data/spec/file_list_spec.rb +0 -50
  121. data/spec/filters_spec.rb +0 -202
  122. data/spec/fixtures/app/controllers/sample_controller.rb +0 -10
  123. data/spec/fixtures/app/models/user.rb +0 -10
  124. data/spec/fixtures/deleted_source_sample.rb +0 -15
  125. data/spec/fixtures/frameworks/rspec_bad.rb +0 -9
  126. data/spec/fixtures/frameworks/rspec_good.rb +0 -9
  127. data/spec/fixtures/frameworks/testunit_bad.rb +0 -9
  128. data/spec/fixtures/frameworks/testunit_good.rb +0 -9
  129. data/spec/fixtures/iso-8859.rb +0 -3
  130. data/spec/fixtures/never.rb +0 -2
  131. data/spec/fixtures/resultset1.rb +0 -4
  132. data/spec/fixtures/resultset2.rb +0 -4
  133. data/spec/fixtures/sample.rb +0 -16
  134. data/spec/fixtures/skipped.rb +0 -4
  135. data/spec/fixtures/skipped_and_executed.rb +0 -8
  136. data/spec/fixtures/utf-8.rb +0 -3
  137. data/spec/helper.rb +0 -26
  138. data/spec/last_run_spec.rb +0 -48
  139. data/spec/lines_classifier_spec.rb +0 -103
  140. data/spec/multi_formatter_spec.rb +0 -20
  141. data/spec/raw_coverage_spec.rb +0 -92
  142. data/spec/result_merger_spec.rb +0 -171
  143. data/spec/result_spec.rb +0 -209
  144. data/spec/return_codes_spec.rb +0 -34
  145. data/spec/simplecov_spec.rb +0 -109
  146. data/spec/source_file_line_spec.rb +0 -155
  147. data/spec/source_file_spec.rb +0 -145
  148. data/spec/support/fail_rspec_on_ruby_warning.rb +0 -75
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ SimpleCov.profiles.define "hidden_filter" do
4
+ add_filter %r{^/\..*}
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ SimpleCov.profiles.define "rails" do
4
+ load_profile "test_frameworks"
5
+
6
+ add_filter %r{^/config/}
7
+ add_filter %r{^/db/}
8
+
9
+ add_group "Controllers", "app/controllers"
10
+ add_group "Channels", "app/channels"
11
+ add_group "Models", "app/models"
12
+ add_group "Mailers", "app/mailers"
13
+ add_group "Helpers", "app/helpers"
14
+ add_group "Jobs", %w[app/jobs app/workers]
15
+ add_group "Libraries", "lib/"
16
+
17
+ track_files "{app,lib}/**/*.rb"
18
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ SimpleCov.profiles.define "root_filter" do
4
+ # Exclude all files outside of simplecov root
5
+ root_filter = nil
6
+ add_filter do |src|
7
+ root_filter ||= /\A#{Regexp.escape(SimpleCov.root + File::SEPARATOR)}/io
8
+ src.filename !~ root_filter
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ SimpleCov.profiles.define "test_frameworks" do
4
+ add_filter "/test/"
5
+ add_filter "/features/"
6
+ add_filter "/spec/"
7
+ add_filter "/autotest/"
8
+ end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "digest/sha1"
2
4
  require "forwardable"
3
5
 
4
6
  module SimpleCov
5
7
  #
6
- # A simplecov code coverage result, initialized from the Hash Ruby 1.9's built-in coverage
8
+ # A simplecov code coverage result, initialized from the Hash Ruby's built-in coverage
7
9
  # library generates (Coverage.result).
8
10
  #
9
11
  class Result
@@ -18,15 +20,16 @@ module SimpleCov
18
20
  # Explicitly set the command name that was used for this coverage result. Defaults to SimpleCov.command_name
19
21
  attr_writer :command_name
20
22
 
21
- def_delegators :files, :covered_percent, :covered_percentages, :least_covered_file, :covered_strength, :covered_lines, :missed_lines
23
+ def_delegators :files, :covered_percent, :covered_percentages, :least_covered_file, :covered_strength, :covered_lines, :missed_lines, :total_branches, :covered_branches, :missed_branches, :coverage_statistics
22
24
  def_delegator :files, :lines_of_code, :total_lines
23
25
 
24
26
  # Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of
25
27
  # coverage data)
26
28
  def initialize(original_result)
27
- @original_result = original_result.freeze
28
- @files = SimpleCov::FileList.new(original_result.map do |filename, coverage|
29
- SimpleCov::SourceFile.new(filename, coverage) if File.file?(filename)
29
+ result = adapt_result(original_result)
30
+ @original_result = result.freeze
31
+ @files = SimpleCov::FileList.new(result.map do |filename, coverage|
32
+ SimpleCov::SourceFile.new(filename, JSON.parse(JSON.dump(coverage))) if File.file?(filename)
30
33
  end.compact.sort_by(&:filename))
31
34
  filter!
32
35
  end
@@ -59,13 +62,20 @@ module SimpleCov
59
62
 
60
63
  # Returns a hash representation of this Result that can be used for marshalling it into JSON
61
64
  def to_hash
62
- {command_name => {"coverage" => coverage, "timestamp" => created_at.to_i}}
65
+ {
66
+ command_name => {
67
+ "coverage" => coverage,
68
+ "timestamp" => created_at.to_i
69
+ }
70
+ }
63
71
  end
64
72
 
65
73
  # Loads a SimpleCov::Result#to_hash dump
66
74
  def self.from_hash(hash)
67
75
  command_name, data = hash.first
76
+
68
77
  result = SimpleCov::Result.new(data["coverage"])
78
+
69
79
  result.command_name = command_name
70
80
  result.created_at = Time.at(data["timestamp"])
71
81
  result
@@ -73,6 +83,31 @@ module SimpleCov
73
83
 
74
84
  private
75
85
 
86
+ # We changed the format of the raw result data in simplecov, as people are likely
87
+ # to have "old" resultsets lying around (but not too old so that they're still
88
+ # considered we can adapt them).
89
+ # See https://github.com/colszowka/simplecov/pull/824#issuecomment-576049747
90
+ def adapt_result(result)
91
+ if pre_simplecov_0_18_result?(result)
92
+ adapt_pre_simplecov_0_18_result(result)
93
+ else
94
+ result
95
+ end
96
+ end
97
+
98
+ # pre 0.18 coverage data pointed from file directly to an array of line coverage
99
+ def pre_simplecov_0_18_result?(result)
100
+ _key, data = result.first
101
+
102
+ data.is_a?(Array)
103
+ end
104
+
105
+ def adapt_pre_simplecov_0_18_result(result)
106
+ result.map do |file_path, line_coverage_data|
107
+ [file_path, {"lines" => line_coverage_data}]
108
+ end.to_h
109
+ end
110
+
76
111
  def coverage
77
112
  keys = original_result.keys & filenames
78
113
  Hash[keys.zip(original_result.values_at(*keys))]
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ #
5
+ # Responsible for adapting the format of the coverage result whether it's default or with statistics
6
+ #
7
+ class ResultAdapter
8
+ attr_reader :result
9
+
10
+ def initialize(result)
11
+ @result = result
12
+ end
13
+
14
+ def self.call(*args)
15
+ new(*args).adapt
16
+ end
17
+
18
+ def adapt
19
+ return unless result
20
+
21
+ result.each_with_object({}) do |(file_name, cover_statistic), adapted_result|
22
+ if cover_statistic.is_a?(Array)
23
+ adapted_result.merge!(file_name => {"lines" => cover_statistic})
24
+ else
25
+ adapted_result.merge!(file_name => cover_statistic)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "json"
2
4
 
3
- #
4
- # Singleton that is responsible for caching, loading and merging
5
- # SimpleCov::Results into a single result for coverage analysis based
6
- # upon multiple test suites.
7
- #
8
5
  module SimpleCov
6
+ #
7
+ # Singleton that is responsible for caching, loading and merging
8
+ # SimpleCov::Results into a single result for coverage analysis based
9
+ # upon multiple test suites.
10
+ #
9
11
  module ResultMerger
10
12
  class << self
11
13
  # The path to the .resultset.json cache file
@@ -25,7 +27,7 @@ module SimpleCov
25
27
  if data
26
28
  begin
27
29
  JSON.parse(data) || {}
28
- rescue
30
+ rescue StandardError
29
31
  {}
30
32
  end
31
33
  else
@@ -38,8 +40,10 @@ module SimpleCov
38
40
  def stored_data
39
41
  synchronize_resultset do
40
42
  return unless File.exist?(resultset_path)
43
+
41
44
  data = File.read(resultset_path)
42
45
  return if data.nil? || data.length < 2
46
+
43
47
  data
44
48
  end
45
49
  end
@@ -53,19 +57,24 @@ module SimpleCov
53
57
  resultset.each do |command_name, data|
54
58
  result = SimpleCov::Result.from_hash(command_name => data)
55
59
  # Only add result if the timeout is above the configured threshold
56
- if (Time.now - result.created_at) < SimpleCov.merge_timeout
57
- results << result
58
- end
60
+ results << result if (Time.now - result.created_at) < SimpleCov.merge_timeout
59
61
  end
60
62
  results
61
63
  end
62
64
 
65
+ def merge_and_store(*results)
66
+ result = merge_results(*results)
67
+ store_result(result) if result
68
+ result
69
+ end
70
+
63
71
  # Merge two or more SimpleCov::Results into a new one with merged
64
72
  # coverage data and the command_name for the result consisting of a join
65
73
  # on all source result's names
66
74
  def merge_results(*results)
67
- merged = SimpleCov::RawCoverage.merge_results(*results.map(&:original_result))
68
- result = SimpleCov::Result.new(merged)
75
+ parsed_results = JSON.parse(JSON.dump(results.map(&:original_result)))
76
+ combined_result = SimpleCov::Combine::ResultsCombiner.combine(*parsed_results)
77
+ result = SimpleCov::Result.new(combined_result)
69
78
  # Specify the command name
70
79
  result.command_name = results.map(&:command_name).sort.join(", ")
71
80
  result
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ #
5
+ # Responsible for producing file coverage metrics.
6
+ #
7
+ module SimulateCoverage
8
+ module_function
9
+
10
+ #
11
+ # Simulate normal file coverage report on
12
+ # ruby 2.5 and return similar hash with lines and branches keys
13
+ #
14
+ # Happens when a file wasn't required but still tracked.
15
+ #
16
+ # @return [Hash]
17
+ #
18
+ def call(absolute_path)
19
+ lines = File.foreach(absolute_path)
20
+
21
+ {
22
+ "lines" => LinesClassifier.new.classify(lines),
23
+ # we don't want to parse branches ourselves...
24
+ # requiring files can have side effects and we don't want to trigger that
25
+ "branches" => {}
26
+ }
27
+ end
28
+ end
29
+ end
@@ -1,88 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SimpleCov
2
4
  #
3
5
  # Representation of a source file including it's coverage data, source code,
4
6
  # source lines and featuring helpers to interpret that data.
5
7
  #
6
8
  class SourceFile
7
- # Representation of a single line in a source file including
8
- # this specific line's source code, line_number and code coverage,
9
- # with the coverage being either nil (coverage not applicable, e.g. comment
10
- # line), 0 (line not covered) or >1 (the amount of times the line was
11
- # executed)
12
- class Line
13
- # The source code for this line. Aliased as :source
14
- attr_reader :src
15
- # The line number in the source file. Aliased as :line, :number
16
- attr_reader :line_number
17
- # The coverage data for this line: either nil (never), 0 (missed) or >=1 (times covered)
18
- attr_reader :coverage
19
- # Whether this line was skipped
20
- attr_reader :skipped
21
-
22
- # Lets grab some fancy aliases, shall we?
23
- alias source src
24
- alias line line_number
25
- alias number line_number
26
-
27
- def initialize(src, line_number, coverage)
28
- raise ArgumentError, "Only String accepted for source" unless src.is_a?(String)
29
- raise ArgumentError, "Only Integer accepted for line_number" unless line_number.is_a?(Integer)
30
- raise ArgumentError, "Only Integer and nil accepted for coverage" unless coverage.is_a?(Integer) || coverage.nil?
31
- @src = src
32
- @line_number = line_number
33
- @coverage = coverage
34
- @skipped = false
35
- end
36
-
37
- # Returns true if this is a line that should have been covered, but was not
38
- def missed?
39
- !never? && !skipped? && coverage.zero?
40
- end
41
-
42
- # Returns true if this is a line that has been covered
43
- def covered?
44
- !never? && !skipped? && coverage > 0
45
- end
46
-
47
- # Returns true if this line is not relevant for coverage
48
- def never?
49
- !skipped? && coverage.nil?
50
- end
51
-
52
- # Flags this line as skipped
53
- def skipped!
54
- @skipped = true
55
- end
56
-
57
- # Returns true if this line was skipped, false otherwise. Lines are skipped if they are wrapped with
58
- # # :nocov: comment lines.
59
- def skipped?
60
- !!skipped
61
- end
62
-
63
- # The status of this line - either covered, missed, skipped or never. Useful i.e. for direct use
64
- # as a css class in report generation
65
- def status
66
- return "skipped" if skipped?
67
- return "never" if never?
68
- return "missed" if missed?
69
- return "covered" if covered?
70
- end
71
- end
72
-
73
9
  # The full path to this source file (e.g. /User/colszowka/projects/simplecov/lib/simplecov/source_file.rb)
74
10
  attr_reader :filename
75
11
  # The array of coverage data received from the Coverage.result
76
- attr_reader :coverage
12
+ attr_reader :coverage_data
77
13
 
78
- def initialize(filename, coverage)
14
+ def initialize(filename, coverage_data)
79
15
  @filename = filename
80
- @coverage = coverage
16
+ @coverage_data = coverage_data
81
17
  end
82
18
 
83
19
  # The path to this source file relative to the projects directory
84
20
  def project_filename
85
- @filename.sub(/^#{SimpleCov.root}/, "")
21
+ @filename.sub(Regexp.new("^#{Regexp.escape(SimpleCov.root)}"), "")
86
22
  end
87
23
 
88
24
  # The source code for this file. Aliased as :source
@@ -93,6 +29,14 @@ module SimpleCov
93
29
  end
94
30
  alias source src
95
31
 
32
+ def coverage_statistics
33
+ @coverage_statistics ||=
34
+ {
35
+ **line_coverage_statistics,
36
+ **branch_coverage_statistics
37
+ }
38
+ end
39
+
96
40
  # Returns all source lines for this file as instances of SimpleCov::SourceFile::Line,
97
41
  # and thus including coverage data. Aliased as :source_lines
98
42
  def lines
@@ -100,19 +44,31 @@ module SimpleCov
100
44
  end
101
45
  alias source_lines lines
102
46
 
103
- def build_lines
104
- coverage_exceeding_source_warn if coverage.size > src.size
47
+ # Returns all covered lines as SimpleCov::SourceFile::Line
48
+ def covered_lines
49
+ @covered_lines ||= lines.select(&:covered?)
50
+ end
105
51
 
106
- lines = src.map.with_index(1) do |src, i|
107
- SimpleCov::SourceFile::Line.new(src, i, coverage[i - 1])
108
- end
52
+ # Returns all lines that should have been, but were not covered
53
+ # as instances of SimpleCov::SourceFile::Line
54
+ def missed_lines
55
+ @missed_lines ||= lines.select(&:missed?)
56
+ end
109
57
 
110
- process_skipped_lines(lines)
58
+ # Returns all lines that are not relevant for coverage as
59
+ # SimpleCov::SourceFile::Line instances
60
+ def never_lines
61
+ @never_lines ||= lines.select(&:never?)
111
62
  end
112
63
 
113
- # Warning to identify condition from Issue #56
114
- def coverage_exceeding_source_warn
115
- $stderr.puts "Warning: coverage data provided by Coverage [#{coverage.size}] exceeds number of lines in #{filename} [#{src.size}]"
64
+ # Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
65
+ def skipped_lines
66
+ @skipped_lines ||= lines.select(&:skipped?)
67
+ end
68
+
69
+ # Returns the number of relevant lines (covered + missed)
70
+ def lines_of_code
71
+ coverage_statistics[:line]&.total
116
72
  end
117
73
 
118
74
  # Access SimpleCov::SourceFile::Line source lines by line number
@@ -120,82 +76,225 @@ module SimpleCov
120
76
  lines[number - 1]
121
77
  end
122
78
 
123
- # The coverage for this file in percent. 0 if the file has no relevant lines
79
+ # The coverage for this file in percent. 0 if the file has no coverage lines
124
80
  def covered_percent
125
- return 100.0 if no_lines?
126
-
127
- return 0.0 if relevant_lines.zero?
128
-
129
- Float(covered_lines.size * 100.0 / relevant_lines.to_f)
81
+ coverage_statistics[:line]&.percent
130
82
  end
131
83
 
132
84
  def covered_strength
133
- return 0.0 if relevant_lines.zero?
134
-
135
- round_float(lines_strength / relevant_lines.to_f, 1)
85
+ coverage_statistics[:line]&.strength
136
86
  end
137
87
 
138
88
  def no_lines?
139
89
  lines.length.zero? || (lines.length == never_lines.size)
140
90
  end
141
91
 
142
- def lines_strength
143
- lines.map(&:coverage).compact.reduce(:+)
144
- end
145
-
146
92
  def relevant_lines
147
93
  lines.size - never_lines.size - skipped_lines.size
148
94
  end
149
95
 
150
- # Returns all covered lines as SimpleCov::SourceFile::Line
151
- def covered_lines
152
- @covered_lines ||= lines.select(&:covered?)
96
+ #
97
+ # Return all the branches inside current source file
98
+ def branches
99
+ @branches ||= build_branches
153
100
  end
154
101
 
155
- # Returns all lines that should have been, but were not covered
156
- # as instances of SimpleCov::SourceFile::Line
157
- def missed_lines
158
- @missed_lines ||= lines.select(&:missed?)
102
+ def no_branches?
103
+ total_branches.empty?
159
104
  end
160
105
 
161
- # Returns all lines that are not relevant for coverage as
162
- # SimpleCov::SourceFile::Line instances
163
- def never_lines
164
- @never_lines ||= lines.select(&:never?)
106
+ def branches_coverage_percent
107
+ coverage_statistics[:branch]&.percent
165
108
  end
166
109
 
167
- # Returns all lines that were skipped as SimpleCov::SourceFile::Line instances
168
- def skipped_lines
169
- @skipped_lines ||= lines.select(&:skipped?)
110
+ #
111
+ # Return the relevant branches to source file
112
+ def total_branches
113
+ @total_branches ||= covered_branches + missed_branches
170
114
  end
171
115
 
172
- # Returns the number of relevant lines (covered + missed)
173
- def lines_of_code
174
- covered_lines.size + missed_lines.size
116
+ #
117
+ # Return hash with key of line number and branch coverage count as value
118
+ def branches_report
119
+ @branches_report ||= build_branches_report
120
+ end
121
+
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
+ #
129
+ def covered_branches
130
+ @covered_branches ||= branches.select(&:covered?)
131
+ end
132
+
133
+ #
134
+ # Select the missed branches with coverage equal to zero
135
+ #
136
+ # @return [Array]
137
+ #
138
+ def missed_branches
139
+ @missed_branches ||= branches.select(&:missed?)
140
+ end
141
+
142
+ def branches_for_line(line_number)
143
+ branches_report.fetch(line_number, [])
144
+ end
145
+
146
+ #
147
+ # Check if any branches missing on given line number
148
+ #
149
+ # @param [Integer] line_number
150
+ #
151
+ # @return [Boolean]
152
+ #
153
+ 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, _index| LinesClassifier.no_cov_line?(line) }
166
+
167
+ warn "uneven number of nocov comments detected" if no_cov_lines.size.odd?
168
+
169
+ no_cov_lines.each_slice(2).map do |(_line_start, index_start), (_line_end, index_end)|
170
+ index_start..index_end
171
+ end
172
+ end
173
+
174
+ def build_lines
175
+ coverage_exceeding_source_warn if coverage_data["lines"].size > src.size
176
+ lines = src.map.with_index(1) do |src, i|
177
+ SimpleCov::SourceFile::Line.new(src, i, coverage_data["lines"][i - 1])
178
+ end
179
+ process_skipped_lines(lines)
175
180
  end
176
181
 
177
- # Will go through all source files and mark lines that are wrapped within # :nocov: comment blocks
178
- # as skipped.
179
182
  def process_skipped_lines(lines)
180
- skipping = false
181
-
182
- lines.each do |line|
183
- if line.src =~ SimpleCov::LinesClassifier.no_cov_line
184
- skipping = !skipping
185
- line.skipped!
186
- elsif skipping
187
- line.skipped!
188
- end
183
+ # the array the lines are kept in is 0-based whereas the line numbers in the nocov
184
+ # chunks are 1-based and are expected to be like this in other parts (and it's also
185
+ # arguably more understandable)
186
+ no_cov_chunks.each { |chunk| lines[(chunk.begin - 1)..(chunk.end - 1)].each(&:skipped!) }
187
+
188
+ lines
189
+ end
190
+
191
+ def lines_strength
192
+ lines.map(&:coverage).compact.reduce(:+)
193
+ end
194
+
195
+ # Warning to identify condition from Issue #56
196
+ def coverage_exceeding_source_warn
197
+ warn "Warning: coverage data provided by Coverage [#{coverage_data['lines'].size}] exceeds number of lines in #{filename} [#{src.size}]"
198
+ end
199
+
200
+ #
201
+ # Build full branches report
202
+ # Root branches represent the wrapper of all condition state that
203
+ # have inside the branches
204
+ #
205
+ # @return [Hash]
206
+ #
207
+ def build_branches_report
208
+ branches.reject(&:skipped?).each_with_object({}) do |branch, coverage_statistics|
209
+ coverage_statistics[branch.report_line] ||= []
210
+ coverage_statistics[branch.report_line] << branch.report
189
211
  end
190
212
  end
191
213
 
192
- private
214
+ #
215
+ # Call recursive method that transform our static hash to array of objects
216
+ # @return [Array]
217
+ #
218
+ def build_branches
219
+ coverage_branch_data = coverage_data.fetch("branches", {})
220
+ branches = coverage_branch_data.flat_map do |condition, coverage_branches|
221
+ build_branches_from(condition, coverage_branches)
222
+ end
223
+
224
+ process_skipped_branches(branches)
225
+ end
226
+
227
+ def process_skipped_branches(branches)
228
+ return branches if no_cov_chunks.empty?
229
+
230
+ branches.each do |branch|
231
+ branch.skipped! if no_cov_chunks.any? { |no_cov_chunk| branch.overlaps_with?(no_cov_chunk) }
232
+ end
233
+
234
+ branches
235
+ end
236
+
237
+ # Since we are dumping to and loading from JSON, and we have arrays as keys those
238
+ # don't make their way back to us intact e.g. just as a string
239
+ #
240
+ # We should probably do something different here, but as it stands these are
241
+ # our data structures that we write so eval isn't _too_ bad.
242
+ #
243
+ # See #801
244
+ #
245
+ def restore_ruby_data_structure(structure)
246
+ # Tests use the real data structures (except for integration tests) so no need to
247
+ # put them through here.
248
+ return structure if structure.is_a?(Array)
249
+
250
+ # rubocop:disable Security/Eval
251
+ eval structure
252
+ # rubocop:enable Security/Eval
253
+ end
254
+
255
+ def build_branches_from(condition, branches)
256
+ # the format handed in from the coverage data is like this:
257
+ #
258
+ # [:then, 4, 6, 6, 6, 10]
259
+ #
260
+ # which is [type, id, start_line, start_col, end_line, end_col]
261
+ _condition_type, _condition_id, condition_start_line, * = restore_ruby_data_structure(condition)
262
+
263
+ branches.map do |branch_data, hit_count|
264
+ branch_data = restore_ruby_data_structure(branch_data)
265
+ build_branch(branch_data, hit_count, condition_start_line)
266
+ end
267
+ end
268
+
269
+ def build_branch(branch_data, hit_count, condition_start_line)
270
+ type, _id, start_line, _start_col, end_line, _end_col = branch_data
271
+
272
+ SourceFile::Branch.new(
273
+ start_line: start_line,
274
+ end_line: end_line,
275
+ coverage: hit_count,
276
+ inline: start_line == condition_start_line,
277
+ type: type
278
+ )
279
+ end
280
+
281
+ def line_coverage_statistics
282
+ {
283
+ line: CoverageStatistics.new(
284
+ total_strength: lines_strength,
285
+ covered: covered_lines.size,
286
+ missed: missed_lines.size
287
+ )
288
+ }
289
+ end
193
290
 
194
- # ruby 1.9 could use Float#round(places) instead
195
- # @return [Float]
196
- def round_float(float, places)
197
- factor = Float(10 * places)
198
- Float((float * factor).round / factor)
291
+ def branch_coverage_statistics
292
+ {
293
+ branch: CoverageStatistics.new(
294
+ covered: covered_branches.size,
295
+ missed: missed_branches.size
296
+ )
297
+ }
199
298
  end
200
299
  end
201
300
  end