simplecov 0.6.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +163 -80
  3. data/LICENSE +1 -1
  4. data/README.md +776 -277
  5. data/doc/alternate-formatters.md +71 -0
  6. data/doc/commercial-services.md +25 -0
  7. data/doc/editor-integration.md +18 -0
  8. data/lib/minitest/simplecov_plugin.rb +15 -0
  9. data/lib/simplecov/combine/branches_combiner.rb +32 -0
  10. data/lib/simplecov/combine/files_combiner.rb +24 -0
  11. data/lib/simplecov/combine/lines_combiner.rb +43 -0
  12. data/lib/simplecov/combine/results_combiner.rb +60 -0
  13. data/lib/simplecov/combine.rb +30 -0
  14. data/lib/simplecov/command_guesser.rb +53 -38
  15. data/lib/simplecov/configuration.rb +478 -193
  16. data/lib/simplecov/coverage_statistics.rb +56 -0
  17. data/lib/simplecov/default_formatter.rb +20 -0
  18. data/lib/simplecov/defaults.rb +40 -44
  19. data/lib/simplecov/exit_codes/exit_code_handling.rb +29 -0
  20. data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +83 -0
  21. data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +54 -0
  22. data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +53 -0
  23. data/lib/simplecov/exit_codes.rb +15 -0
  24. data/lib/simplecov/file_list.rb +112 -36
  25. data/lib/simplecov/filter.rb +54 -4
  26. data/lib/simplecov/formatter/multi_formatter.rb +32 -0
  27. data/lib/simplecov/formatter/simple_formatter.rb +21 -15
  28. data/lib/simplecov/formatter.rb +4 -1
  29. data/lib/simplecov/last_run.rb +28 -0
  30. data/lib/simplecov/lines_classifier.rb +48 -0
  31. data/lib/simplecov/load_global_config.rb +8 -0
  32. data/lib/simplecov/no_defaults.rb +4 -0
  33. data/lib/simplecov/process.rb +19 -0
  34. data/lib/simplecov/profiles/bundler_filter.rb +5 -0
  35. data/lib/simplecov/profiles/hidden_filter.rb +5 -0
  36. data/lib/simplecov/profiles/rails.rb +18 -0
  37. data/lib/simplecov/profiles/root_filter.rb +10 -0
  38. data/lib/simplecov/profiles/test_frameworks.rb +8 -0
  39. data/lib/simplecov/profiles.rb +35 -0
  40. data/lib/simplecov/result.rb +33 -64
  41. data/lib/simplecov/result_adapter.rb +30 -0
  42. data/lib/simplecov/result_merger.rb +178 -64
  43. data/lib/simplecov/simulate_coverage.rb +29 -0
  44. data/lib/simplecov/source_file/branch.rb +84 -0
  45. data/lib/simplecov/source_file/line.rb +72 -0
  46. data/lib/simplecov/source_file.rb +304 -123
  47. data/lib/simplecov/useless_results_remover.rb +18 -0
  48. data/lib/simplecov/version.rb +4 -2
  49. data/lib/simplecov.rb +396 -49
  50. metadata +81 -242
  51. data/.gitignore +0 -30
  52. data/.rvmrc +0 -1
  53. data/.travis.yml +0 -13
  54. data/Gemfile +0 -9
  55. data/Rakefile +0 -16
  56. data/cucumber.yml +0 -13
  57. data/features/config_adapters.feature +0 -44
  58. data/features/config_autoload.feature +0 -46
  59. data/features/config_command_name.feature +0 -33
  60. data/features/config_coverage_dir.feature +0 -20
  61. data/features/config_deactivate_merging.feature +0 -42
  62. data/features/config_merge_timeout.feature +0 -38
  63. data/features/config_nocov_token.feature +0 -79
  64. data/features/config_project_name.feature +0 -27
  65. data/features/config_styles.feature +0 -93
  66. data/features/cucumber_basic.feature +0 -29
  67. data/features/merging_test_unit_and_rspec.feature +0 -44
  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 -61
  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 -26
  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/lib/simplecov/adapters.rb +0 -29
  87. data/lib/simplecov/merge_helpers.rb +0 -39
  88. data/lib/simplecov/railtie.rb +0 -7
  89. data/lib/simplecov/railties/tasks.rake +0 -11
  90. data/simplecov.gemspec +0 -28
  91. data/test/faked_project/Gemfile +0 -6
  92. data/test/faked_project/Rakefile +0 -8
  93. data/test/faked_project/cucumber.yml +0 -13
  94. data/test/faked_project/features/step_definitions/my_steps.rb +0 -23
  95. data/test/faked_project/features/support/env.rb +0 -12
  96. data/test/faked_project/features/test_stuff.feature +0 -6
  97. data/test/faked_project/lib/faked_project/framework_specific.rb +0 -18
  98. data/test/faked_project/lib/faked_project/meta_magic.rb +0 -24
  99. data/test/faked_project/lib/faked_project/some_class.rb +0 -29
  100. data/test/faked_project/lib/faked_project.rb +0 -11
  101. data/test/faked_project/spec/faked_spec.rb +0 -11
  102. data/test/faked_project/spec/meta_magic_spec.rb +0 -10
  103. data/test/faked_project/spec/some_class_spec.rb +0 -10
  104. data/test/faked_project/spec/spec_helper.rb +0 -15
  105. data/test/faked_project/test/faked_test.rb +0 -11
  106. data/test/faked_project/test/meta_magic_test.rb +0 -13
  107. data/test/faked_project/test/some_class_test.rb +0 -15
  108. data/test/faked_project/test/test_helper.rb +0 -16
  109. data/test/fixtures/app/controllers/sample_controller.rb +0 -10
  110. data/test/fixtures/app/models/user.rb +0 -10
  111. data/test/fixtures/deleted_source_sample.rb +0 -15
  112. data/test/fixtures/frameworks/rspec_bad.rb +0 -9
  113. data/test/fixtures/frameworks/rspec_good.rb +0 -9
  114. data/test/fixtures/frameworks/testunit_bad.rb +0 -9
  115. data/test/fixtures/frameworks/testunit_good.rb +0 -9
  116. data/test/fixtures/resultset1.rb +0 -4
  117. data/test/fixtures/resultset2.rb +0 -5
  118. data/test/fixtures/sample.rb +0 -16
  119. data/test/fixtures/utf-8.rb +0 -3
  120. data/test/helper.rb +0 -35
  121. data/test/shoulda_macros.rb +0 -29
  122. data/test/test_1_8_fallbacks.rb +0 -33
  123. data/test/test_command_guesser.rb +0 -21
  124. data/test/test_deleted_source.rb +0 -16
  125. data/test/test_file_list.rb +0 -24
  126. data/test/test_filters.rb +0 -80
  127. data/test/test_merge_helpers.rb +0 -107
  128. data/test/test_result.rb +0 -147
  129. data/test/test_return_codes.rb +0 -39
  130. data/test/test_source_file.rb +0 -86
  131. data/test/test_source_file_line.rb +0 -110
@@ -1,19 +1,25 @@
1
- #
2
- # A ridiculously simple formatter for SimpleCov results.
3
- #
4
- class SimpleCov::Formatter::SimpleFormatter
5
- # Takes a SimpleCov::Result and generates a string out of it
6
- def format(result)
7
- output = ""
8
- result.groups.each do |name, files|
9
- output << "Group: #{name}\n"
10
- output << "="*40
11
- output << "\n"
12
- files.each do |file|
13
- output << "#{file.filename} (coverage: #{file.covered_percent.round(2)}%)\n"
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module Formatter
5
+ #
6
+ # A ridiculously simple formatter for SimpleCov results.
7
+ #
8
+ class SimpleFormatter
9
+ # Takes a SimpleCov::Result and generates a string out of it
10
+ def format(result)
11
+ output = +""
12
+ result.groups.each do |name, files|
13
+ output << "Group: #{name}\n"
14
+ output << "=" * 40
15
+ output << "\n"
16
+ files.each do |file|
17
+ output << "#{file.filename} (coverage: #{file.covered_percent.round(2)}%)\n"
18
+ end
19
+ output << "\n"
20
+ end
21
+ output
14
22
  end
15
- output << "\n"
16
23
  end
17
- output
18
24
  end
19
25
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SimpleCov
2
4
  # TODO: Documentation on how to build your own formatters
3
5
  module Formatter
4
6
  end
5
7
  end
6
8
 
7
- require 'simplecov/formatter/simple_formatter'
9
+ require_relative "formatter/simple_formatter"
10
+ require_relative "formatter/multi_formatter"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
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)
14
+
15
+ json = File.read(last_run_path)
16
+ return nil if json.strip.empty?
17
+
18
+ JSON.parse(json, symbolize_names: true)
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
25
+ end
26
+ end
27
+ end
28
+ end
@@ -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
+ no_cov_line.match?(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
+ WHITESPACE_OR_COMMENT_LINE.match?(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
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV["SIMPLECOV_NO_DEFAULTS"] = "yes, no defaults"
4
+ require_relative "../simplecov"
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Process
4
+ class << self
5
+ def fork_with_simplecov(&block)
6
+ if defined?(SimpleCov) && SimpleCov.running
7
+ fork_without_simplecov do
8
+ SimpleCov.at_fork.call(Process.pid)
9
+ block.call if block_given?
10
+ end
11
+ else
12
+ fork_without_simplecov(&block)
13
+ end
14
+ end
15
+
16
+ alias fork_without_simplecov fork
17
+ alias fork fork_with_simplecov
18
+ end
19
+ 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
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ #
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
9
+ # end
10
+ #
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?
21
+
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
34
+ end
35
+ end
@@ -1,27 +1,37 @@
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, :coverage_statistics, :coverage_statistics_by_file
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
- def initialize(original_result)
22
- @original_result = original_result.freeze
23
- @files = SimpleCov::FileList.new(original_result.map do |filename, coverage|
24
- SimpleCov::SourceFile.new(filename, coverage) if File.file?(filename)
28
+ def initialize(original_result, command_name: nil, created_at: nil)
29
+ result = original_result
30
+ @original_result = result.freeze
31
+ @command_name = command_name
32
+ @created_at = created_at
33
+ @files = SimpleCov::FileList.new(result.map do |filename, coverage|
34
+ SimpleCov::SourceFile.new(filename, JSON.parse(JSON.dump(coverage))) if File.file?(filename)
25
35
  end.compact.sort_by(&:filename))
26
36
  filter!
27
37
  end
@@ -36,55 +46,6 @@ module SimpleCov
36
46
  @groups ||= SimpleCov.grouped(files)
37
47
  end
38
48
 
39
- # The overall percentual coverage for this result
40
- def covered_percent
41
- # Make sure that weird rounding error from #15, #23 and #24 does not occur again!
42
- total_lines.zero? ? 0 : 100.0 * covered_lines / total_lines
43
- end
44
-
45
- # The multiple of coverage for this result
46
- def covered_strength
47
- return @covered_strength if @covered_strength
48
- m = 0
49
- @files.each do |file|
50
- original_result[file.filename].each do |line_result|
51
- if line_result
52
- m += line_result
53
- end
54
- end
55
- end
56
- @covered_strength = m.to_f / total_lines
57
- end
58
-
59
- # Returns the count of lines that are covered
60
- def covered_lines
61
- return @covered_lines if @covered_lines
62
- @covered_lines = 0
63
- @files.each do |file|
64
- original_result[file.filename].each do |line_result|
65
- @covered_lines += 1 if line_result and line_result > 0
66
- end
67
- end
68
- @covered_lines
69
- end
70
-
71
- # Returns the count of missed lines
72
- def missed_lines
73
- return @missed_lines if @missed_lines
74
- @missed_lines = 0
75
- @files.each do |file|
76
- original_result[file.filename].each do |line_result|
77
- @missed_lines += 1 if line_result == 0
78
- end
79
- end
80
- @missed_lines
81
- end
82
-
83
- # Total count of relevant lines (covered + missed)
84
- def total_lines
85
- @total_lines ||= (covered_lines + missed_lines)
86
- end
87
-
88
49
  # Applies the configured SimpleCov.formatter on this result
89
50
  def format!
90
51
  SimpleCov.formatter.new.format(self)
@@ -101,21 +62,29 @@ module SimpleCov
101
62
  @command_name ||= SimpleCov.command_name
102
63
  end
103
64
 
104
- # Returns a hash representation of this Result that can be used for marshalling it into YAML
65
+ # Returns a hash representation of this Result that can be used for marshalling it into JSON
105
66
  def to_hash
106
- {command_name => {"coverage" => original_result.reject {|filename, result| !filenames.include?(filename) }, "timestamp" => created_at.to_i}}
67
+ {
68
+ command_name => {
69
+ "coverage" => coverage,
70
+ "timestamp" => created_at.to_i
71
+ }
72
+ }
107
73
  end
108
74
 
109
75
  # Loads a SimpleCov::Result#to_hash dump
110
76
  def self.from_hash(hash)
111
- command_name, data = hash.first
112
- result = SimpleCov::Result.new(data["coverage"])
113
- result.command_name = command_name
114
- result.created_at = Time.at(data["timestamp"])
115
- result
77
+ hash.map do |command_name, data|
78
+ new(data.fetch("coverage"), command_name: command_name, created_at: Time.at(data["timestamp"]))
79
+ end
116
80
  end
117
81
 
118
- private
82
+ private
83
+
84
+ def coverage
85
+ keys = original_result.keys & filenames
86
+ Hash[keys.zip(original_result.values_at(*keys))]
87
+ end
119
88
 
120
89
  # Applies all configured SimpleCov filters on this result's source files
121
90
  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