coverband 3.0.0 → 3.0.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 (55) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -1
  3. data/.travis.yml +3 -0
  4. data/Gemfile +1 -0
  5. data/Gemfile.rails4 +7 -0
  6. data/Gemfile.rails4.lock +164 -0
  7. data/README.md +16 -14
  8. data/changes.md +17 -4
  9. data/coverband.gemspec +11 -4
  10. data/lib/coverband/adapters/base.rb +57 -8
  11. data/lib/coverband/adapters/file_store.rb +4 -22
  12. data/lib/coverband/adapters/redis_store.rb +7 -27
  13. data/lib/coverband/collectors/coverage.rb +5 -0
  14. data/lib/coverband/configuration.rb +4 -1
  15. data/lib/coverband/integrations/background.rb +25 -1
  16. data/lib/coverband/integrations/rack_server_check.rb +27 -0
  17. data/lib/coverband/reporters/simple_cov_report.rb +1 -7
  18. data/lib/coverband/reporters/web.rb +2 -20
  19. data/lib/coverband/utils/s3_report.rb +108 -0
  20. data/lib/coverband/version.rb +1 -1
  21. data/lib/coverband.rb +4 -1
  22. data/test/benchmarks/benchmark.rake +47 -1
  23. data/test/rails4_dummy/app/controllers/dummy_controller.rb +5 -0
  24. data/test/rails4_dummy/config/application.rb +15 -0
  25. data/test/rails4_dummy/config/boot.rb +3 -0
  26. data/test/rails4_dummy/config/coverband.rb +8 -0
  27. data/test/rails4_dummy/config/environment.rb +5 -0
  28. data/test/rails4_dummy/config/routes.rb +3 -0
  29. data/test/rails4_dummy/config/secrets.yml +3 -0
  30. data/test/rails4_dummy/config.ru +4 -0
  31. data/test/rails5_dummy/app/controllers/dummy_controller.rb +5 -0
  32. data/test/rails5_dummy/config/application.rb +16 -0
  33. data/test/rails5_dummy/config/coverband.rb +8 -0
  34. data/test/rails5_dummy/config/environment.rb +2 -0
  35. data/test/rails5_dummy/config/routes.rb +3 -0
  36. data/test/rails5_dummy/config.ru +3 -0
  37. data/test/rails_test_helper.rb +11 -0
  38. data/test/test_helper.rb +14 -9
  39. data/test/unit/adapters_base_test.rb +34 -13
  40. data/test/unit/adapters_file_store_test.rb +8 -4
  41. data/test/unit/adapters_redis_store_test.rb +7 -13
  42. data/test/unit/background_test.rb +16 -0
  43. data/test/unit/collectors_coverage_test.rb +24 -0
  44. data/test/unit/configuration_test.rb +12 -0
  45. data/test/unit/coverband_test.rb +26 -0
  46. data/test/unit/rack_server_checkout_test.rb +24 -0
  47. data/test/unit/rails_full_stack_test.rb +16 -0
  48. data/test/unit/reports_base_test.rb +0 -2
  49. data/test/unit/reports_console_test.rb +1 -0
  50. data/test/unit/reports_simple_cov_test.rb +1 -10
  51. data/test/unit/reports_web_test.rb +1 -3
  52. data/test/unit/utils_s3_report_test.rb +44 -0
  53. metadata +78 -9
  54. data/lib/coverband/utils/s3_report_writer.rb +0 -59
  55. data/test/unit/utils_s3_report_writer_test.rb +0 -30
@@ -71,7 +71,7 @@ module Coverband
71
71
  end
72
72
 
73
73
  def show
74
- html = s3.get_object(bucket: Coverband.configuration.s3_bucket, key: 'coverband/index.html').body.read
74
+ html = Coverband::Utils::S3Report.instance.retrieve
75
75
  # HACK: the static HTML assets to link to the path where this was mounted
76
76
  html = html.gsub("src='", "src='#{base_path}")
77
77
  html = html.gsub("href='", "href='#{base_path}")
@@ -126,30 +126,12 @@ module Coverband
126
126
 
127
127
  # This method should get the root mounted endpoint
128
128
  # for example if the app is mounted like so:
129
- # mount Coverband::S3Web, at: '/coverage'
129
+ # mount Coverband::Web, at: '/coverage'
130
130
  # "/coverage/collect_coverage?" become:
131
131
  # /coverage/
132
132
  def base_path
133
133
  request.path.match("\/.*\/") ? request.path.match("\/.*\/")[0] : '/'
134
134
  end
135
-
136
- def s3
137
- begin
138
- require 'aws-sdk'
139
- rescue StandardError
140
- Coverband.configuration.logger.error "coverband requires 'aws-sdk' in order use S3ReportWriter."
141
- return
142
- end
143
- @s3 ||= begin
144
- client_options = {
145
- region: Coverband.configuration.s3_region,
146
- access_key_id: Coverband.configuration.s3_access_key_id,
147
- secret_access_key: Coverband.configuration.s3_secret_access_key
148
- }
149
- client_options = {} if client_options.values.any?(&:nil?)
150
- Aws::S3::Client.new(client_options)
151
- end
152
- end
153
135
  end
154
136
  end
155
137
  end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coverband
4
+ module Utils
5
+ ###
6
+ # TODO: this is currently a html report writer
7
+ # this should support writing coverage the current method should be fine
8
+ # to write every report to S3 and sum them later or use the 2 pass
9
+ # method we do for redis if in a background thread
10
+ ###
11
+ class S3Report
12
+ def self.instance
13
+ s3_options = {
14
+ region: Coverband.configuration.s3_region,
15
+ access_key_id: Coverband.configuration.s3_access_key_id,
16
+ secret_access_key: Coverband.configuration.s3_secret_access_key
17
+ }
18
+ new(Coverband.configuration.s3_bucket, s3_options)
19
+ end
20
+
21
+ def initialize(bucket_name, options = {})
22
+ @bucket_name = bucket_name
23
+ @region = options[:region]
24
+ @access_key_id = options[:access_key_id]
25
+ @secret_access_key = options[:secret_access_key]
26
+ begin
27
+ require 'aws-sdk'
28
+ rescue StandardError
29
+ err_msg = 'coverband requires aws-sdk in order use S3Report.'
30
+ Coverband.configuration.logger.error err_msg
31
+ return
32
+ end
33
+ end
34
+
35
+ def persist!
36
+ if defined?(Aws)
37
+ object.put(body: coverage_content)
38
+ else
39
+ object.write(coverage_content)
40
+ end
41
+ end
42
+
43
+ def retrieve
44
+ if defined?(Aws)
45
+ s3_client.get_object(bucket: Coverband.configuration.s3_bucket,
46
+ key: 'coverband/index.html').body.read
47
+ else
48
+ object.read
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def coverage_content
55
+ version = Gem::Specification.find_by_name('simplecov-html').version.version
56
+ File.read("#{SimpleCov.coverage_dir}/index.html").gsub("./assets/#{version}/", '')
57
+ rescue StandardError
58
+ File.read("#{SimpleCov.coverage_dir}/index.html").to_s.gsub('./assets/0.10.1/', '')
59
+ end
60
+
61
+ def object
62
+ if defined?(Aws)
63
+ bucket.object('coverband/index.html')
64
+ else
65
+ bucket.objects['coverband/index.html']
66
+ end
67
+ end
68
+
69
+ def client_options
70
+ {
71
+ region: @region,
72
+ access_key_id: @access_key_id,
73
+ secret_access_key: @secret_access_key
74
+ }
75
+ end
76
+
77
+ def s3_client
78
+ if defined?(Aws)
79
+ # AWS SDK v2
80
+ Aws::S3::Client.new(client_options)
81
+ else
82
+ # AWS SDK v1
83
+ AWS::S3::Client.new(client_options)
84
+ end
85
+ end
86
+
87
+ def s3
88
+ resource_options = { client: s3_client }
89
+ resource_options = {} if client_options.values.any?(&:nil?)
90
+ if defined?(Aws)
91
+ # AWS SDK v2
92
+ Aws::S3::Resource.new(resource_options)
93
+ else
94
+ # AWS SDK v1
95
+ AWS::S3.new(resource_options)
96
+ end
97
+ end
98
+
99
+ def bucket
100
+ if defined?(Aws)
101
+ s3.bucket(@bucket_name)
102
+ else
103
+ s3.buckets[@bucket_name]
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coverband
4
- VERSION = '3.0.0'
4
+ VERSION = '3.0.1'
5
5
  end
data/lib/coverband.rb CHANGED
@@ -8,11 +8,13 @@ require 'coverband/configuration'
8
8
  require 'coverband/adapters/base'
9
9
  require 'coverband/adapters/redis_store'
10
10
  require 'coverband/adapters/file_store'
11
- require 'coverband/utils/s3_report_writer'
11
+ require 'coverband/utils/s3_report'
12
12
  require 'coverband/collectors/coverage'
13
13
  require 'coverband/reporters/base'
14
14
  require 'coverband/reporters/simple_cov_report'
15
15
  require 'coverband/reporters/console_report'
16
+ require 'coverband/integrations/background'
17
+ require 'coverband/integrations/rack_server_check'
16
18
  require 'coverband/reporters/web'
17
19
  require 'coverband/integrations/middleware'
18
20
  require 'coverband/integrations/background'
@@ -44,5 +46,6 @@ module Coverband
44
46
 
45
47
  def self.start
46
48
  Coverband::Collectors::Coverage.instance
49
+ Background.start if configuration.background_reporting_enabled && !RackServerCheck.running?
47
50
  end
48
51
  end
@@ -166,9 +166,24 @@ namespace :benchmarks do
166
166
  report
167
167
  end
168
168
 
169
+ def mock_files(store)
170
+ ###
171
+ # this is a hack because in the benchmark we don't have real files
172
+ ###
173
+ def store.file_hash(file)
174
+ if @file_hash_cache[file]
175
+ @file_hash_cache[file]
176
+ else
177
+ @file_hash_cache[file] = Digest::MD5.file(__FILE__).hexdigest
178
+ end
179
+ end
180
+ end
181
+
169
182
  def reporting_speed
170
183
  report = fake_report
171
184
  store = benchmark_redis_store
185
+ store.clear!
186
+ mock_files(store)
172
187
 
173
188
  5.times { store.save_report(report) }
174
189
  Benchmark.ips do |x|
@@ -177,6 +192,36 @@ namespace :benchmarks do
177
192
  end
178
193
  end
179
194
 
195
+ def measure_memory
196
+ require 'memory_profiler'
197
+ report = fake_report
198
+ store = benchmark_redis_store
199
+ store.clear!
200
+ mock_files(store)
201
+
202
+ # warmup
203
+ 3.times { store.save_report(report) }
204
+
205
+ previous_out = $stdout
206
+ capture = StringIO.new
207
+ $stdout = capture
208
+
209
+ MemoryProfiler.report do
210
+ 10.times { store.save_report(report) }
211
+ end.pretty_print
212
+ data = $stdout.string
213
+ $stdout = previous_out
214
+ raise 'leaking memory!!!' unless data.match('Total retained: 0 bytes')
215
+ ensure
216
+ $stdout = previous_out
217
+ end
218
+
219
+ desc 'runs memory reporting on Redis store'
220
+ task memory_reporting: [:setup] do
221
+ puts 'runs memory benchmarking to ensure we dont leak'
222
+ measure_memory
223
+ end
224
+
180
225
  desc 'runs benchmarks on reporting large sets of files to redis'
181
226
  task redis_reporting: [:setup] do
182
227
  puts 'runs benchmarks on reporting large sets of files to redis'
@@ -198,7 +243,7 @@ namespace :benchmarks do
198
243
  desc 'benchmarks external requests to coverband_demo site'
199
244
  task :coverband_demo do
200
245
  # for local testing
201
- # puts `ab -n 200 -c 5 "http://127.0.0.1:3000/posts"`
246
+ # puts `ab -n 500 -c 5 "http://127.0.0.1:3000/posts"`
202
247
  puts `ab -n 2000 -c 10 "https://coverband-demo.herokuapp.com/posts"`
203
248
  end
204
249
 
@@ -206,6 +251,7 @@ namespace :benchmarks do
206
251
  task :coverband_demo_graph do
207
252
  # for local testing
208
253
  # puts `ab -n 200 -c 5 "http://127.0.0.1:3000/posts"`
254
+ # puts `ab -n 500 -c 10 -g tmp/ab_brench.tsv "http://127.0.0.1:3000/posts"`
209
255
  puts `ab -n 2000 -c 10 -g tmp/ab_brench.tsv "https://coverband-demo.herokuapp.com/posts"`
210
256
  puts `test/benchmarks/graph_bench.sh`
211
257
  `open tmp/timeseries.jpg`
@@ -0,0 +1,5 @@
1
+ class DummyController < ActionController::Base
2
+ def show
3
+ render plain: "I am no dummy"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "rails"
4
+ require "action_controller/railtie"
5
+ module Rails4Dummy
6
+ class Application < Rails::Application
7
+ # Coverband needs to be setup before any of the initializers to capture usage of them
8
+ Coverband.configure(File.open("#{Rails.root}/config/coverband.rb"))
9
+ config.middleware.use Coverband::Middleware
10
+ config.before_initialize do
11
+ Coverband.start
12
+ end
13
+ config.eager_load = false
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
2
+
3
+ require 'bundler/setup' # Set up gems listed in the Gemfile.
@@ -0,0 +1,8 @@
1
+ Coverband.configure do |config|
2
+ config.root = Dir.pwd
3
+ config.store = Coverband::Adapters::RedisStore.new(Redis.new(url: ENV['REDIS_URL'])) if defined? Redis
4
+ config.ignore = %w[vendor .erb$ .slim$]
5
+ config.root_paths = []
6
+ config.reporting_frequency = 100.0
7
+ config.logger = Rails.logger
8
+ end
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ get 'dummy/show'
3
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ secret_key_base: 8080f2894307a3dcf72127c0a279a729c58c7c10c11a15de761c8e16017e0e478647d1a7ac11bf143730cac7b6901fa000428c6b4873d9298250f8ca4657b5c6
3
+
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,5 @@
1
+ class DummyController < ActionController::API
2
+ def show
3
+ render plain: "I am no dummy"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require "rails"
2
+ require "action_controller/railtie"
3
+ require 'coverband'
4
+
5
+ module Dummy
6
+ class Application < Rails::Application
7
+ # Coverband needs to be setup before any of the initializers to capture usage of them
8
+ Coverband.configure(File.open("#{Rails.root}/config/coverband.rb"))
9
+ config.middleware.use Coverband::Middleware
10
+ config.before_initialize do
11
+ Coverband.start
12
+ end
13
+ config.eager_load = false
14
+ end
15
+ end
16
+
@@ -0,0 +1,8 @@
1
+ Coverband.configure do |config|
2
+ config.root = Dir.pwd
3
+ config.store = Coverband::Adapters::RedisStore.new(Redis.new(url: ENV['REDIS_URL'])) if defined? Redis
4
+ config.ignore = %w[vendor .erb$ .slim$]
5
+ config.root_paths = []
6
+ config.reporting_frequency = 100.0
7
+ config.logger = Rails.logger
8
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'application'
2
+ Rails.application.initialize!
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ get 'dummy/show'
3
+ end
@@ -0,0 +1,3 @@
1
+ require_relative 'config/environment'
2
+
3
+ run Rails.application
@@ -0,0 +1,11 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+ require 'rails'
4
+ require File.expand_path('./test_helper', File.dirname(__FILE__))
5
+
6
+ if Rails::VERSION::MAJOR >= 5
7
+ require_relative "../test/rails5_dummy/config/environment"
8
+ else
9
+ require_relative "../test/rails4_dummy/config/environment"
10
+ end
11
+
data/test/test_helper.rb CHANGED
@@ -7,6 +7,7 @@ require 'mocha/setup'
7
7
  require 'ostruct'
8
8
  require 'json'
9
9
  require 'redis'
10
+ require 'pry-byebug'
10
11
 
11
12
  SimpleCov.start do
12
13
  add_filter 'specs/ruby/1.9.1/gems/'
@@ -14,10 +15,12 @@ SimpleCov.start do
14
15
  add_filter '/config/'
15
16
  end
16
17
 
17
- TEST_COVERAGE_FILE = '/tmp/fake_file.json'.freeze
18
+ TEST_COVERAGE_FILE = '/tmp/fake_file.json'
18
19
 
19
20
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
21
  $LOAD_PATH.unshift(File.dirname(__FILE__))
22
+
23
+ Mocha::Configuration.prevent(:stubbing_method_unnecessarily)
21
24
  Mocha::Configuration.prevent(:stubbing_non_existent_method)
22
25
 
23
26
  def test(name, &block)
@@ -37,6 +40,12 @@ def test(name, &block)
37
40
  end
38
41
  end
39
42
 
43
+ def mock_file_hash
44
+ mock_file = mock('mock_file')
45
+ mock_file.expects(:hexdigest).at_least_once.returns('abcd')
46
+ Digest::MD5.expects(:file).at_least_once.returns(mock_file)
47
+ end
48
+
40
49
  def example_line
41
50
  [0, 1, 2]
42
51
  end
@@ -59,12 +68,8 @@ end
59
68
 
60
69
  require 'coverband'
61
70
 
62
- Coverband.configure do |config|
63
- config.root = Dir.pwd
64
- config.s3_bucket = nil
65
- config.root_paths = ['/app_path/']
66
- config.ignore = ['vendor']
67
- config.reporting_frequency = 100.0
68
- config.reporter = 'std_out'
69
- config.store = Coverband::Adapters::RedisStore.new(Redis.new)
71
+ Coverband::Configuration.class_eval do
72
+ def test_env
73
+ true
74
+ end
70
75
  end
@@ -6,23 +6,44 @@ class AdaptersBaseTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @test_file_path = '/tmp/coverband_filestore_test_path.json'
8
8
  @store = Coverband::Adapters::FileStore.new(@test_file_path)
9
+ mock_file_hash
9
10
  end
10
11
 
11
12
  def test_covered_merge
12
- old_report = { '/Users/danmayer/projects/coverband_demo/config/coverband.rb' => [5, 7, nil],
13
- '/Users/danmayer/projects/coverband_demo/config/initializers/assets.rb' => [5, 5, nil],
14
- '/Users/danmayer/projects/coverband_demo/config/initializers/cookies_serializer.rb' => [5, 5, nil] }
15
- new_report = { '/Users/danmayer/projects/coverband_demo/config/coverband.rb' => [5, 7, nil],
16
- '/Users/danmayer/projects/coverband_demo/config/initializers/filter_logging.rb' => [5, 5, nil],
17
- '/Users/danmayer/projects/coverband_demo/config/initializers/wrap_parameters.rb' => [5, 5, nil],
18
- '/Users/danmayer/projects/coverband_demo/app/controllers/application_controller.rb' => [5, 5, nil] }
13
+ old_time = 1541958097
14
+ current_time = Time.now.to_i
15
+ old_data = {
16
+ 'first_updated_at' => old_time,
17
+ 'last_updated_at' => current_time,
18
+ 'file_hash' => 'abcd',
19
+ 'data' => [5, 7, nil]
20
+ }
21
+ old_report = { '/projects/coverband_demo/config/coverband.rb' => old_data,
22
+ '/projects/coverband_demo/config/initializers/assets.rb' => old_data,
23
+ '/projects/coverband_demo/config/initializers/cookies_serializer.rb' => old_data }
24
+ new_report = { '/projects/coverband_demo/config/coverband.rb' => [5, 7, nil],
25
+ '/projects/coverband_demo/config/initializers/filter_logging.rb' => [5, 7, nil],
26
+ '/projects/coverband_demo/config/initializers/wrap_parameters.rb' => [5, 7, nil],
27
+ '/projects/coverband_demo/app/controllers/application_controller.rb' => [5, 7, nil] }
28
+ expected_merge = {
29
+ 'first_updated_at' => old_time,
30
+ 'last_updated_at' => current_time,
31
+ 'file_hash' => 'abcd',
32
+ 'data' => [10, 14, nil]
33
+ }
34
+ new_data = {
35
+ 'first_updated_at' => current_time,
36
+ 'last_updated_at' => current_time,
37
+ 'file_hash' => 'abcd',
38
+ 'data' => [5, 7, nil]
39
+ }
19
40
  expected_result = {
20
- '/Users/danmayer/projects/coverband_demo/app/controllers/application_controller.rb' => [5, 5, nil],
21
- '/Users/danmayer/projects/coverband_demo/config/coverband.rb' => [10, 14, nil],
22
- '/Users/danmayer/projects/coverband_demo/config/initializers/assets.rb' => [5, 5, nil],
23
- '/Users/danmayer/projects/coverband_demo/config/initializers/cookies_serializer.rb' => [5, 5, nil],
24
- '/Users/danmayer/projects/coverband_demo/config/initializers/filter_logging.rb' => [5, 5, nil],
25
- '/Users/danmayer/projects/coverband_demo/config/initializers/wrap_parameters.rb' => [5, 5, nil]
41
+ '/projects/coverband_demo/app/controllers/application_controller.rb' => new_data,
42
+ '/projects/coverband_demo/config/coverband.rb' => expected_merge,
43
+ '/projects/coverband_demo/config/initializers/assets.rb' => old_data,
44
+ '/projects/coverband_demo/config/initializers/cookies_serializer.rb' => old_data,
45
+ '/projects/coverband_demo/config/initializers/filter_logging.rb' => new_data,
46
+ '/projects/coverband_demo/config/initializers/wrap_parameters.rb' => new_data
26
47
  }
27
48
  assert_equal expected_result, @store.send(:merge_reports, new_report, old_report)
28
49
  end
@@ -10,8 +10,8 @@ class AdaptersFileStoreTest < Test::Unit::TestCase
10
10
  end
11
11
 
12
12
  def test_covered_lines_for_file
13
- assert_equal @store.covered_lines_for_file('dog.rb')['1'], 1
14
- assert_equal @store.covered_lines_for_file('dog.rb')['2'], 2
13
+ assert_equal @store.covered_lines_for_file('dog.rb')[0], 1
14
+ assert_equal @store.covered_lines_for_file('dog.rb')[1], 2
15
15
  end
16
16
 
17
17
  def test_covered_lines_when_null
@@ -28,7 +28,8 @@ class AdaptersFileStoreTest < Test::Unit::TestCase
28
28
  end
29
29
 
30
30
  def test_save_report
31
- @store.send(:save_report, 'cat.rb' => [0,1])
31
+ mock_file_hash
32
+ @store.send(:save_report, 'cat.rb' => [0, 1])
32
33
  assert_equal @store.covered_lines_for_file('cat.rb')[1], 1
33
34
  end
34
35
 
@@ -36,7 +37,10 @@ class AdaptersFileStoreTest < Test::Unit::TestCase
36
37
 
37
38
  def test_data
38
39
  {
39
- 'dog.rb' => { 1 => 1, 2 => 2 }
40
+ 'dog.rb' => { 'data' => [1, 2, nil],
41
+ 'file_hash' => 'abcd',
42
+ 'first_updated_at' => 1541968729,
43
+ 'last_updated_at' => 1541968729 }
40
44
  }
41
45
  end
42
46
  end
@@ -12,27 +12,30 @@ class RedisTest < Test::Unit::TestCase
12
12
  end
13
13
 
14
14
  def test_coverage
15
+ mock_file_hash
15
16
  expected = basic_coverage
16
17
  @store.save_report(expected)
17
18
  assert_equal expected, @store.coverage
18
19
  end
19
20
 
20
21
  def test_coverage_increments
21
- expected = basic_coverage
22
- @store.save_report(expected)
22
+ mock_file_hash
23
+ expected = basic_coverage.dup
24
+ @store.save_report(basic_coverage.dup)
23
25
  assert_equal expected, @store.coverage
24
- @store.save_report(expected)
26
+ @store.save_report(basic_coverage.dup)
25
27
  assert_equal [0, 2, 4], @store.coverage['app_path/dog.rb']
26
28
  end
27
29
 
28
30
  def test_covered_lines_for_file
31
+ mock_file_hash
29
32
  expected = basic_coverage
30
33
  @store.save_report(expected)
31
34
  assert_equal example_line, @store.covered_lines_for_file('app_path/dog.rb')
32
35
  end
33
36
 
34
37
  def test_covered_lines_when_null
35
- assert_equal nil, @store.covered_lines_for_file('app_path/dog.rb')
38
+ assert_equal [], @store.covered_lines_for_file('app_path/dog.rb')
36
39
  end
37
40
 
38
41
  def test_clear
@@ -42,15 +45,6 @@ class RedisTest < Test::Unit::TestCase
42
45
 
43
46
  private
44
47
 
45
- def combined_report
46
- {
47
- "#{BASE_KEY}.dog.rb" => {
48
- new: example_hash,
49
- existing: {}
50
- }
51
- }
52
- end
53
-
54
48
  def test_data
55
49
  {
56
50
  '/Users/danmayer/projects/cover_band_server/app.rb' => { 54 => 1, 55 => 2 },
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('../test_helper', File.dirname(__FILE__))
4
+
5
+ class BackgroundTest < Test::Unit::TestCase
6
+ def test_start
7
+ Thread.expects(:new).yields
8
+ Coverband::Background.expects(:loop).yields
9
+ Coverband::Collectors::Coverage.instance.expects(:report_coverage)
10
+ Coverband::Background.expects(:sleep).with(30)
11
+ Coverband::Background.expects(:at_exit).yields
12
+ Coverband::Collectors::Coverage.instance.expects(:report_coverage)
13
+ 2.times { Coverband::Background.start }
14
+ end
15
+ end
16
+
@@ -29,5 +29,29 @@ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.3.0')
29
29
  assert_equal Coverband::Adapters::RedisStore, coverband.instance_variable_get('@store').class
30
30
  end
31
31
 
32
+ test 'reports coverage in background when background reporting enabled' do
33
+ Coverband.configuration.stubs(:background_reporting_enabled).returns(true)
34
+ @coverband.reset_instance
35
+ Coverband::Background.expects(:start)
36
+ @coverband.report_coverage
37
+ end
38
+
39
+ test 'report_coverage raises errors in tests' do
40
+ Coverband.configuration.stubs(:background_reporting_enabled).returns(true)
41
+ @coverband.reset_instance
42
+ Coverband::Background.expects(:start).raises("Oh no")
43
+ assert_raise RuntimeError do
44
+ @coverband.report_coverage
45
+ end
46
+ end
47
+
48
+ test 'report_coverage does not raise errors in non-test mode' do
49
+ Coverband.configuration.stubs(:background_reporting_enabled).returns(true)
50
+ Coverband.configuration.stubs(:test_env).returns(false)
51
+ @coverband.reset_instance
52
+ Coverband::Background.expects(:start).raises("Oh no")
53
+ @coverband.report_coverage
54
+ end
55
+
32
56
  end
33
57
  end
@@ -3,6 +3,18 @@
3
3
  require File.expand_path('../test_helper', File.dirname(__FILE__))
4
4
 
5
5
  class BaseTest < Test::Unit::TestCase
6
+ def setup
7
+ Coverband.configure do |config|
8
+ config.root = Dir.pwd
9
+ config.s3_bucket = nil
10
+ config.root_paths = ['/app_path/']
11
+ config.ignore = ['vendor']
12
+ config.reporting_frequency = 100.0
13
+ config.reporter = 'std_out'
14
+ config.store = Coverband::Adapters::RedisStore.new(Redis.new)
15
+ end
16
+ end
17
+
6
18
  test 'defaults ' do
7
19
  coverband = Coverband::Collectors::Coverage.instance.reset_instance
8
20
  assert_equal ['vendor', 'internal:prelude', 'schema.rb'], coverband.instance_variable_get('@ignore_patterns')