simplecov 0.8.0.pre2 → 0.18.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +368 -27
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +38 -7
  5. data/ISSUE_TEMPLATE.md +23 -0
  6. data/{MIT-LICENSE → LICENSE} +1 -1
  7. data/README.md +449 -230
  8. data/doc/alternate-formatters.md +61 -0
  9. data/doc/commercial-services.md +20 -0
  10. data/doc/editor-integration.md +18 -0
  11. data/lib/simplecov.rb +294 -59
  12. data/lib/simplecov/combine.rb +30 -0
  13. data/lib/simplecov/combine/branches_combiner.rb +32 -0
  14. data/lib/simplecov/combine/files_combiner.rb +25 -0
  15. data/lib/simplecov/combine/lines_combiner.rb +43 -0
  16. data/lib/simplecov/combine/results_combiner.rb +60 -0
  17. data/lib/simplecov/command_guesser.rb +46 -40
  18. data/lib/simplecov/configuration.rb +346 -221
  19. data/lib/simplecov/defaults.rb +35 -75
  20. data/lib/simplecov/exit_codes.rb +9 -4
  21. data/lib/simplecov/file_list.rb +80 -36
  22. data/lib/simplecov/filter.rb +51 -3
  23. data/lib/simplecov/formatter.rb +4 -2
  24. data/lib/simplecov/formatter/multi_formatter.rb +28 -19
  25. data/lib/simplecov/formatter/simple_formatter.rb +21 -15
  26. data/lib/simplecov/last_run.rb +21 -13
  27. data/lib/simplecov/lines_classifier.rb +48 -0
  28. data/lib/simplecov/load_global_config.rb +8 -0
  29. data/lib/simplecov/no_defaults.rb +4 -2
  30. data/lib/simplecov/profiles.rb +29 -23
  31. data/lib/simplecov/profiles/bundler_filter.rb +5 -0
  32. data/lib/simplecov/profiles/hidden_filter.rb +5 -0
  33. data/lib/simplecov/profiles/rails.rb +18 -0
  34. data/lib/simplecov/profiles/root_filter.rb +10 -0
  35. data/lib/simplecov/profiles/test_frameworks.rb +8 -0
  36. data/lib/simplecov/result.rb +39 -68
  37. data/lib/simplecov/result_adapter.rb +30 -0
  38. data/lib/simplecov/result_merger.rb +110 -60
  39. data/lib/simplecov/simulate_coverage.rb +29 -0
  40. data/lib/simplecov/source_file.rb +261 -135
  41. data/lib/simplecov/source_file/branch.rb +106 -0
  42. data/lib/simplecov/source_file/line.rb +72 -0
  43. data/lib/simplecov/useless_results_remover.rb +16 -0
  44. data/lib/simplecov/version.rb +4 -2
  45. metadata +48 -197
  46. data/.gitignore +0 -32
  47. data/.travis.yml +0 -28
  48. data/.yardopts +0 -1
  49. data/Appraisals +0 -8
  50. data/Gemfile +0 -16
  51. data/Rakefile +0 -32
  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 -52
  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/cucumber_basic.feature +0 -29
  64. data/features/maximum_coverage_drop.feature +0 -36
  65. data/features/merging_test_unit_and_rspec.feature +0 -44
  66. data/features/minimum_coverage.feature +0 -59
  67. data/features/refuse_coverage_drop.feature +0 -35
  68. data/features/rspec_basic.feature +0 -31
  69. data/features/rspec_fails_on_initialization.feature +0 -14
  70. data/features/rspec_groups_and_filters_basic.feature +0 -29
  71. data/features/rspec_groups_and_filters_complex.feature +0 -35
  72. data/features/rspec_groups_using_filter_class.feature +0 -40
  73. data/features/rspec_without_simplecov.feature +0 -20
  74. data/features/skipping_code_blocks_manually.feature +0 -70
  75. data/features/step_definitions/html_steps.rb +0 -45
  76. data/features/step_definitions/simplecov_steps.rb +0 -66
  77. data/features/step_definitions/transformers.rb +0 -13
  78. data/features/step_definitions/web_steps.rb +0 -64
  79. data/features/support/env.rb +0 -44
  80. data/features/test_unit_basic.feature +0 -34
  81. data/features/test_unit_groups_and_filters_basic.feature +0 -29
  82. data/features/test_unit_groups_and_filters_complex.feature +0 -35
  83. data/features/test_unit_groups_using_filter_class.feature +0 -40
  84. data/features/test_unit_without_simplecov.feature +0 -20
  85. data/features/unicode_compatiblity.feature +0 -67
  86. data/gemfiles/multi_json_legacy.gemfile +0 -12
  87. data/gemfiles/multi_json_new.gemfile +0 -12
  88. data/lib/simplecov/jruby16_fix.rb +0 -43
  89. data/lib/simplecov/json.rb +0 -27
  90. data/lib/simplecov/merge_helpers.rb +0 -39
  91. data/lib/simplecov/railtie.rb +0 -7
  92. data/lib/simplecov/railties/tasks.rake +0 -11
  93. data/simplecov.gemspec +0 -30
  94. data/test/faked_project/Gemfile +0 -6
  95. data/test/faked_project/Rakefile +0 -8
  96. data/test/faked_project/cucumber.yml +0 -13
  97. data/test/faked_project/features/step_definitions/my_steps.rb +0 -23
  98. data/test/faked_project/features/support/env.rb +0 -12
  99. data/test/faked_project/features/test_stuff.feature +0 -6
  100. data/test/faked_project/lib/faked_project.rb +0 -11
  101. data/test/faked_project/lib/faked_project/framework_specific.rb +0 -18
  102. data/test/faked_project/lib/faked_project/meta_magic.rb +0 -24
  103. data/test/faked_project/lib/faked_project/some_class.rb +0 -29
  104. data/test/faked_project/spec/faked_spec.rb +0 -11
  105. data/test/faked_project/spec/meta_magic_spec.rb +0 -10
  106. data/test/faked_project/spec/some_class_spec.rb +0 -10
  107. data/test/faked_project/spec/spec_helper.rb +0 -15
  108. data/test/faked_project/test/faked_test.rb +0 -11
  109. data/test/faked_project/test/meta_magic_test.rb +0 -13
  110. data/test/faked_project/test/some_class_test.rb +0 -15
  111. data/test/faked_project/test/test_helper.rb +0 -16
  112. data/test/fixtures/app/controllers/sample_controller.rb +0 -10
  113. data/test/fixtures/app/models/user.rb +0 -10
  114. data/test/fixtures/deleted_source_sample.rb +0 -15
  115. data/test/fixtures/frameworks/rspec_bad.rb +0 -9
  116. data/test/fixtures/frameworks/rspec_good.rb +0 -9
  117. data/test/fixtures/frameworks/testunit_bad.rb +0 -9
  118. data/test/fixtures/frameworks/testunit_good.rb +0 -9
  119. data/test/fixtures/iso-8859.rb +0 -3
  120. data/test/fixtures/resultset1.rb +0 -4
  121. data/test/fixtures/resultset2.rb +0 -5
  122. data/test/fixtures/sample.rb +0 -16
  123. data/test/fixtures/utf-8.rb +0 -3
  124. data/test/helper.rb +0 -34
  125. data/test/shoulda_macros.rb +0 -19
  126. data/test/test_1_8_fallbacks.rb +0 -31
  127. data/test/test_command_guesser.rb +0 -19
  128. data/test/test_deleted_source.rb +0 -14
  129. data/test/test_file_list.rb +0 -22
  130. data/test/test_filters.rb +0 -78
  131. data/test/test_merge_helpers.rb +0 -105
  132. data/test/test_result.rb +0 -160
  133. data/test/test_return_codes.rb +0 -37
  134. data/test/test_source_file.rb +0 -106
  135. data/test/test_source_file_line.rb +0 -106
@@ -1,20 +1,28 @@
1
- module SimpleCov::LastRun
2
- class << self
3
- def last_run_path
4
- File.join(SimpleCov.coverage_path, '.last_run.json')
5
- end
1
+ # frozen_string_literal: true
6
2
 
7
- def read
8
- return nil unless File.exist?(last_run_path)
3
+ require "json"
9
4
 
10
- SimpleCov::JSON.parse(File.read(last_run_path))
11
- end
5
+ module SimpleCov
6
+ module LastRun
7
+ class << self
8
+ def last_run_path
9
+ File.join(SimpleCov.coverage_path, ".last_run.json")
10
+ end
11
+
12
+ def read
13
+ return nil unless File.exist?(last_run_path)
12
14
 
13
- def write(json)
14
- File.open(last_run_path, "w+") do |f|
15
- f.puts SimpleCov::JSON.dump(json)
15
+ json = File.read(last_run_path)
16
+ return nil if json.strip.empty?
17
+
18
+ JSON.parse(json)
19
+ end
20
+
21
+ def write(json)
22
+ File.open(last_run_path, "w+") do |f|
23
+ f.puts JSON.pretty_generate(json)
24
+ end
16
25
  end
17
26
  end
18
27
  end
19
28
  end
20
-
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ # Classifies whether lines are relevant for code coverage analysis.
5
+ # Comments & whitespace lines, and :nocov: token blocks, are considered not relevant.
6
+
7
+ class LinesClassifier
8
+ RELEVANT = 0
9
+ NOT_RELEVANT = nil
10
+
11
+ WHITESPACE_LINE = /^\s*$/.freeze
12
+ COMMENT_LINE = /^\s*#/.freeze
13
+ WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE)
14
+
15
+ def self.no_cov_line
16
+ /^(\s*)#(\s*)(\:#{SimpleCov.nocov_token}\:)/o
17
+ end
18
+
19
+ def self.no_cov_line?(line)
20
+ line =~ no_cov_line
21
+ rescue ArgumentError
22
+ # E.g., line contains an invalid byte sequence in UTF-8
23
+ false
24
+ end
25
+
26
+ def self.whitespace_line?(line)
27
+ line =~ WHITESPACE_OR_COMMENT_LINE
28
+ rescue ArgumentError
29
+ # E.g., line contains an invalid byte sequence in UTF-8
30
+ false
31
+ end
32
+
33
+ def classify(lines)
34
+ skipping = false
35
+
36
+ lines.map do |line|
37
+ if self.class.no_cov_line?(line)
38
+ skipping = !skipping
39
+ NOT_RELEVANT
40
+ elsif skipping || self.class.whitespace_line?(line)
41
+ NOT_RELEVANT
42
+ else
43
+ RELEVANT
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "etc"
4
+ home_dir = (ENV["HOME"] && File.expand_path("~")) || Etc.getpwuid.dir || (ENV["USER"] && File.expand_path("~#{ENV['USER']}"))
5
+ if home_dir
6
+ global_config_path = File.join(home_dir, ".simplecov")
7
+ load global_config_path if File.exist?(global_config_path)
8
+ end
@@ -1,2 +1,4 @@
1
- ENV['SIMPLECOV_NO_DEFAULTS'] = 'yes, no defaults'
2
- require 'simplecov'
1
+ # frozen_string_literal: true
2
+
3
+ ENV["SIMPLECOV_NO_DEFAULTS"] = "yes, no defaults"
4
+ require "simplecov"
@@ -1,29 +1,35 @@
1
- #
2
- # Profiles are SimpleCov configuration procs that can be easily
3
- # loaded using SimpleCov.start :rails and defined using
4
- # SimpleCov.profiles.define :foo do
5
- # # SimpleCov configuration here, same as in SimpleCov.configure
6
- # end
7
- #
8
- class SimpleCov::Profiles < Hash
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
9
4
  #
10
- # Define a SimpleCov profile:
11
- # SimpleCov.profiles.define 'rails' do
12
- # # Same as SimpleCov.configure do .. here
5
+ # Profiles are SimpleCov configuration procs that can be easily
6
+ # loaded using SimpleCov.start :rails and defined using
7
+ # SimpleCov.profiles.define :foo do
8
+ # # SimpleCov configuration here, same as in SimpleCov.configure
13
9
  # end
14
10
  #
15
- def define(name, &blk)
16
- name = name.to_sym
17
- raise "SimpleCov Profile '#{name}' is already defined" unless self[name].nil?
18
- self[name] = blk
19
- end
11
+ class Profiles < Hash
12
+ #
13
+ # Define a SimpleCov profile:
14
+ # SimpleCov.profiles.define 'rails' do
15
+ # # Same as SimpleCov.configure do .. here
16
+ # end
17
+ #
18
+ def define(name, &blk)
19
+ name = name.to_sym
20
+ raise "SimpleCov Profile '#{name}' is already defined" unless self[name].nil?
20
21
 
21
- #
22
- # Applies the profile of given name on SimpleCov.configure
23
- #
24
- def load(name)
25
- name = name.to_sym
26
- raise "Could not find SimpleCov Profile called '#{name}'" unless has_key?(name)
27
- SimpleCov.configure(&self[name])
22
+ self[name] = blk
23
+ end
24
+
25
+ #
26
+ # Applies the profile of given name on SimpleCov.configure
27
+ #
28
+ def load(name)
29
+ name = name.to_sym
30
+ raise "Could not find SimpleCov Profile called '#{name}'" unless key?(name)
31
+
32
+ SimpleCov.configure(&self[name])
33
+ end
28
34
  end
29
35
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ SimpleCov.profiles.define "bundler_filter" do
4
+ add_filter "/vendor/bundle/"
5
+ end
@@ -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,36 +1,35 @@
1
- require 'digest/sha1'
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/sha1"
4
+ require "forwardable"
2
5
 
3
6
  module SimpleCov
4
7
  #
5
- # 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
6
9
  # library generates (Coverage.result).
7
10
  #
8
11
  class Result
12
+ extend Forwardable
9
13
  # Returns the original Coverage.result used for this instance of SimpleCov::Result
10
14
  attr_reader :original_result
11
15
  # Returns all files that are applicable to this result (sans filters!) as instances of SimpleCov::SourceFile. Aliased as :source_files
12
16
  attr_reader :files
13
- alias_method :source_files, :files
17
+ alias source_files files
14
18
  # Explicitly set the Time this result has been created
15
19
  attr_writer :created_at
16
20
  # Explicitly set the command name that was used for this coverage result. Defaults to SimpleCov.command_name
17
21
  attr_writer :command_name
18
22
 
23
+ def_delegators :files, :covered_percent, :covered_percentages, :least_covered_file, :covered_strength, :covered_lines, :missed_lines, :total_branches, :covered_branches, :missed_branches
24
+ def_delegator :files, :lines_of_code, :total_lines
25
+
19
26
  # Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of
20
27
  # coverage data)
21
28
  def initialize(original_result)
22
- @original_result = original_result.dup
23
-
24
- # Squeeze filepaths (i.e. "/a/b/../c" becomes "/a/c")
25
- @original_result.keys.each do |filename|
26
- expanded_filename = File.expand_path filename
27
- @original_result[expanded_filename] = @original_result.delete filename
28
- end
29
-
30
- @files = SimpleCov::FileList.new(@original_result.map do |filename, coverage|
31
- SimpleCov::SourceFile.new(filename, coverage) if File.file?(filename)
29
+ @original_result = original_result.freeze
30
+ @files = SimpleCov::FileList.new(original_result.map do |filename, coverage|
31
+ SimpleCov::SourceFile.new(filename, JSON.parse(JSON.dump(coverage), :symbolize_names => true)) if File.file?(filename.to_s)
32
32
  end.compact.sort_by(&:filename))
33
-
34
33
  filter!
35
34
  end
36
35
 
@@ -44,56 +43,6 @@ module SimpleCov
44
43
  @groups ||= SimpleCov.grouped(files)
45
44
  end
46
45
 
47
- # The overall percentual coverage for this result
48
- def covered_percent
49
- # Make sure that weird rounding error from #15, #23 and #24 does not occur again!
50
- total_lines.zero? ? 0 : 100.0 * covered_lines / total_lines
51
- end
52
-
53
- # The multiple of coverage for this result
54
- def covered_strength
55
- return 0 if total_lines.zero?
56
- return @covered_strength if @covered_strength
57
- m = 0
58
- @files.each do |file|
59
- original_result[file.filename].each do |line_result|
60
- if line_result
61
- m += line_result
62
- end
63
- end
64
- end
65
- @covered_strength = m.to_f / total_lines
66
- end
67
-
68
- # Returns the count of lines that are covered
69
- def covered_lines
70
- return @covered_lines if defined? @covered_lines
71
- @covered_lines = 0
72
- @files.each do |file|
73
- original_result[file.filename].each do |line_result|
74
- @covered_lines += 1 if line_result and line_result > 0
75
- end
76
- end
77
- @covered_lines
78
- end
79
-
80
- # Returns the count of missed lines
81
- def missed_lines
82
- return @missed_lines if defined? @missed_lines
83
- @missed_lines = 0
84
- @files.each do |file|
85
- original_result[file.filename].each do |line_result|
86
- @missed_lines += 1 if line_result == 0
87
- end
88
- end
89
- @missed_lines
90
- end
91
-
92
- # Total count of relevant lines (covered + missed)
93
- def total_lines
94
- @total_lines ||= (covered_lines + missed_lines)
95
- end
96
-
97
46
  # Applies the configured SimpleCov.formatter on this result
98
47
  def format!
99
48
  SimpleCov.formatter.new.format(self)
@@ -110,21 +59,43 @@ module SimpleCov
110
59
  @command_name ||= SimpleCov.command_name
111
60
  end
112
61
 
113
- # Returns a hash representation of this Result that can be used for marshalling it into YAML
62
+ # Returns a hash representation of this Result that can be used for marshalling it into JSON
114
63
  def to_hash
115
- {command_name => {"coverage" => original_result.reject {|filename, result| !filenames.include?(filename) }, "timestamp" => created_at.to_i}}
64
+ {command_name => {"coverage" => coverage, "timestamp" => created_at.to_i}}
116
65
  end
117
66
 
118
67
  # Loads a SimpleCov::Result#to_hash dump
119
68
  def self.from_hash(hash)
120
69
  command_name, data = hash.first
121
- result = SimpleCov::Result.new(data["coverage"])
70
+
71
+ result = SimpleCov::Result.new(
72
+ symbolize_names_of_coverage_results(data["coverage"])
73
+ )
74
+
122
75
  result.command_name = command_name
123
76
  result.created_at = Time.at(data["timestamp"])
124
77
  result
125
78
  end
126
79
 
127
- private
80
+ # Manage symbolize the keys of coverage hash.
81
+ # JSON.parse gives coverage hash with stringified keys what breaks some logics
82
+ # inside the process that expects them as symboles.
83
+ #
84
+ # @return [Hash]
85
+ def self.symbolize_names_of_coverage_results(coverage_data)
86
+ coverage_data.each_with_object({}) do |(file_name, file_coverage_result), coverage_results|
87
+ coverage_results[file_name] = file_coverage_result.each_with_object({}) do |(k, v), cov_elem|
88
+ cov_elem[k.to_sym] = v
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def coverage
96
+ keys = original_result.keys & filenames
97
+ Hash[keys.zip(original_result.values_at(*keys))]
98
+ end
128
99
 
129
100
  # Applies all configured SimpleCov filters on this result's source files
130
101
  def filter!
@@ -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,74 +1,124 @@
1
- #
2
- # Singleton that is responsible for caching, loading and merging
3
- # SimpleCov::Results into a single result for coverage analysis based
4
- # upon multiple test suites.
5
- #
6
- module SimpleCov::ResultMerger
7
- class << self
8
- # The path to the .resultset.json cache file
9
- def resultset_path
10
- File.join(SimpleCov.coverage_path, '.resultset.json')
11
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
12
4
 
13
- # Loads the cached resultset from YAML and returns it as a Hash
14
- def resultset
15
- if stored_data
16
- SimpleCov::JSON.parse(stored_data)
17
- else
18
- {}
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
+ #
11
+ module ResultMerger
12
+ class << self
13
+ # The path to the .resultset.json cache file
14
+ def resultset_path
15
+ File.join(SimpleCov.coverage_path, ".resultset.json")
19
16
  end
20
- end
21
17
 
22
- # Returns the contents of the resultset cache as a string or if the file is missing or empty nil
23
- def stored_data
24
- if File.exist?(resultset_path) and stored_data = File.read(resultset_path) and stored_data.length >= 2
25
- stored_data
26
- else
27
- nil
18
+ def resultset_writelock
19
+ File.join(SimpleCov.coverage_path, ".resultset.json.lock")
28
20
  end
29
- end
30
21
 
31
- # Gets the resultset hash and re-creates all included instances
32
- # of SimpleCov::Result from that.
33
- # All results that are above the SimpleCov.merge_timeout will be
34
- # dropped. Returns an array of SimpleCov::Result items.
35
- def results
36
- results = []
37
- resultset.each do |command_name, data|
38
- result = SimpleCov::Result.from_hash(command_name => data)
39
- # Only add result if the timeout is above the configured threshold
40
- if (Time.now - result.created_at) < SimpleCov.merge_timeout
41
- results << result
22
+ # Loads the cached resultset from JSON and returns it as a Hash,
23
+ # caching it for subsequent accesses.
24
+ def resultset
25
+ @resultset ||= begin
26
+ data = stored_data
27
+ if data
28
+ begin
29
+ JSON.parse(data) || {}
30
+ rescue StandardError
31
+ {}
32
+ end
33
+ else
34
+ {}
35
+ end
42
36
  end
43
37
  end
44
- results
45
- end
46
38
 
47
- #
48
- # Gets all SimpleCov::Results from cache, merges them and produces a new
49
- # SimpleCov::Result with merged coverage data and the command_name
50
- # for the result consisting of a join on all source result's names
51
- #
52
- def merged_result
53
- merged = {}
54
- results.each do |result|
55
- merged = result.original_result.merge_resultset(merged)
39
+ # Returns the contents of the resultset cache as a string or if the file is missing or empty nil
40
+ def stored_data
41
+ synchronize_resultset do
42
+ return unless File.exist?(resultset_path)
43
+
44
+ data = File.read(resultset_path)
45
+ return if data.nil? || data.length < 2
46
+
47
+ data
48
+ end
49
+ end
50
+
51
+ # Gets the resultset hash and re-creates all included instances
52
+ # of SimpleCov::Result from that.
53
+ # All results that are above the SimpleCov.merge_timeout will be
54
+ # dropped. Returns an array of SimpleCov::Result items.
55
+ def results
56
+ results = []
57
+ resultset.each do |command_name, data|
58
+ result = SimpleCov::Result.from_hash(command_name => data)
59
+ # Only add result if the timeout is above the configured threshold
60
+ results << result if (Time.now - result.created_at) < SimpleCov.merge_timeout
61
+ end
62
+ results
63
+ end
64
+
65
+ # Merge two or more SimpleCov::Results into a new one with merged
66
+ # coverage data and the command_name for the result consisting of a join
67
+ # on all source result's names
68
+ def merge_results(*results)
69
+ parsed_results = JSON.parse(JSON.dump(results.map(&:original_result)), :symbolize_names => true)
70
+ combined_result = SimpleCov::Combine::ResultsCombiner.combine(*parsed_results)
71
+ result = SimpleCov::Result.new(combined_result)
72
+ # Specify the command name
73
+ result.command_name = results.map(&:command_name).sort.join(", ")
74
+ result
75
+ end
76
+
77
+ #
78
+ # Gets all SimpleCov::Results from cache, merges them and produces a new
79
+ # SimpleCov::Result with merged coverage data and the command_name
80
+ # for the result consisting of a join on all source result's names
81
+ #
82
+ def merged_result
83
+ merge_results(*results)
84
+ end
85
+
86
+ # Saves the given SimpleCov::Result in the resultset cache
87
+ def store_result(result)
88
+ synchronize_resultset do
89
+ # Ensure we have the latest, in case it was already cached
90
+ clear_resultset
91
+ new_set = resultset
92
+ command_name, data = result.to_hash.first
93
+ new_set[command_name] = data
94
+ File.open(resultset_path, "w+") do |f_|
95
+ f_.puts JSON.pretty_generate(new_set)
96
+ end
97
+ end
98
+ true
99
+ end
100
+
101
+ # Ensure only one process is reading or writing the resultset at any
102
+ # given time
103
+ def synchronize_resultset
104
+ # make it reentrant
105
+ return yield if defined?(@resultset_locked) && @resultset_locked
106
+
107
+ begin
108
+ @resultset_locked = true
109
+ File.open(resultset_writelock, "w+") do |f|
110
+ f.flock(File::LOCK_EX)
111
+ yield
112
+ end
113
+ ensure
114
+ @resultset_locked = false
115
+ end
56
116
  end
57
- result = SimpleCov::Result.new(merged)
58
- # Specify the command name
59
- result.command_name = results.map(&:command_name).sort.join(", ")
60
- result
61
- end
62
117
 
63
- # Saves the given SimpleCov::Result in the resultset cache
64
- def store_result(result)
65
- new_set = resultset
66
- command_name, data = result.to_hash.first
67
- new_set[command_name] = data
68
- File.open(resultset_path, "w+") do |f|
69
- f.puts SimpleCov::JSON.dump(new_set)
118
+ # Clear out the previously cached .resultset
119
+ def clear_resultset
120
+ @resultset = nil
70
121
  end
71
- true
72
122
  end
73
123
  end
74
124
  end