coverband 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -5
- data/lib/coverband.rb +3 -1
- data/lib/coverband/base.rb +52 -51
- data/lib/coverband/redis_store.rb +51 -0
- data/lib/coverband/version.rb +1 -1
- data/test/unit/base_test.rb +6 -5
- data/test/unit/middleware_test.rb +6 -5
- data/test/unit/redis_store_test.rb +83 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -65,11 +65,7 @@ Either add the below to your `Rakefile` or to a file included in your Rakefile
|
|
65
65
|
|
66
66
|
desc "report unused lines"
|
67
67
|
task :coverband => :environment do
|
68
|
-
|
69
|
-
|
70
|
-
root_paths = ['/app/']
|
71
|
-
coverband_options = {:existing_coverage => baseline, :roots => root_paths}
|
72
|
-
Coverband::Reporter.report(Redis.new, coverband_options)
|
68
|
+
Coverband::Reporter
|
73
69
|
end
|
74
70
|
|
75
71
|
desc "get coverage baseline"
|
data/lib/coverband.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'redis'
|
2
2
|
|
3
3
|
require 'coverband/version'
|
4
|
+
require 'coverband/redis_store'
|
4
5
|
require 'coverband/base'
|
5
6
|
require 'coverband/reporter'
|
6
7
|
require 'coverband/middleware'
|
@@ -17,11 +18,12 @@ module Coverband
|
|
17
18
|
end
|
18
19
|
|
19
20
|
class Configuration
|
20
|
-
attr_accessor :redis, :coverage_baseline, :root_paths, :root, :ignore, :percentage, :verbose, :reporter
|
21
|
+
attr_accessor :redis, :coverage_baseline, :root_paths, :root, :ignore, :percentage, :verbose, :reporter, :stats
|
21
22
|
|
22
23
|
def initialize
|
23
24
|
@root = Dir.pwd
|
24
25
|
@redis = nil
|
26
|
+
@stats = nil
|
25
27
|
@coverage_baseline = {}
|
26
28
|
@root_paths = []
|
27
29
|
@ignore = []
|
data/lib/coverband/base.rb
CHANGED
@@ -4,12 +4,13 @@ module Coverband
|
|
4
4
|
def initialize(options = {})
|
5
5
|
@project_directory = File.expand_path(Coverband.configuration.root)
|
6
6
|
@enabled = false
|
7
|
-
@
|
7
|
+
@tracer_set = false
|
8
8
|
@files = {}
|
9
9
|
@ignore_patterns = Coverband.configuration.ignore
|
10
10
|
@sample_percentage = Coverband.configuration.percentage
|
11
|
-
@reporter = Coverband.configuration.redis
|
12
|
-
@
|
11
|
+
@reporter = Coverband::RedisStore.new(Coverband.configuration.redis) if Coverband.configuration.redis
|
12
|
+
@stats = Coverband.configuration.stats
|
13
|
+
@verbose = Coverband.configuration.verbose
|
13
14
|
end
|
14
15
|
|
15
16
|
def start
|
@@ -19,10 +20,7 @@ module Coverband
|
|
19
20
|
|
20
21
|
def stop
|
21
22
|
@enabled = false
|
22
|
-
|
23
|
-
set_trace_func(nil)
|
24
|
-
@function_set = false
|
25
|
-
end
|
23
|
+
unset_tracer
|
26
24
|
end
|
27
25
|
|
28
26
|
def sample
|
@@ -50,17 +48,31 @@ module Coverband
|
|
50
48
|
|
51
49
|
def record_coverage
|
52
50
|
if @enabled
|
53
|
-
|
54
|
-
set_trace_func proc { |event, file, line, id, binding, classname|
|
55
|
-
add_file(file, line)
|
56
|
-
}
|
57
|
-
@function_set = true
|
58
|
-
end
|
51
|
+
set_tracer
|
59
52
|
else
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
53
|
+
unset_tracer
|
54
|
+
end
|
55
|
+
@stats.increment "coverband.request.recorded.#{@enabled}" if @stats
|
56
|
+
rescue RuntimeError => err
|
57
|
+
if @verbose
|
58
|
+
puts "error stating recording coverage"
|
59
|
+
puts "error: #{err.inspect} #{err.message}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_tracer
|
64
|
+
unless @tracer_set
|
65
|
+
set_trace_func proc { |event, file, line, id, binding, classname|
|
66
|
+
add_file(file, line)
|
67
|
+
}
|
68
|
+
@tracer_set = true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def unset_tracer
|
73
|
+
if @tracer_set
|
74
|
+
set_trace_func(nil)
|
75
|
+
@tracer_set = false
|
64
76
|
end
|
65
77
|
end
|
66
78
|
|
@@ -76,42 +88,31 @@ module Coverband
|
|
76
88
|
end
|
77
89
|
|
78
90
|
def report_coverage
|
79
|
-
|
80
|
-
if @
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
end
|
95
|
-
else
|
96
|
-
@reporter.sadd "coverband.#{key}", values
|
97
|
-
end
|
98
|
-
end
|
99
|
-
@files = {}
|
100
|
-
end
|
101
|
-
else
|
102
|
-
puts "coverage report: " if @verbose
|
103
|
-
puts @files.inspect if @verbose
|
104
|
-
end
|
105
|
-
else
|
106
|
-
puts "coverage disabled" if @verbose
|
107
|
-
end
|
108
|
-
rescue RuntimeError => err
|
109
|
-
if @verbose
|
110
|
-
puts "coverage missing"
|
111
|
-
puts "error: #{err.inspect} #{err.message}"
|
91
|
+
unless @enabled
|
92
|
+
puts "coverage disabled" if @verbose
|
93
|
+
return
|
94
|
+
end
|
95
|
+
|
96
|
+
unset_tracer
|
97
|
+
|
98
|
+
if @reporter
|
99
|
+
if @reporter.class.name.match(/redis/i)
|
100
|
+
before_time = Time.now
|
101
|
+
@stats.increment "coverband.files.recorded.#{@files.length}" if @stats
|
102
|
+
@reporter.store_report(@files)
|
103
|
+
time_spent = Time.now - before_time
|
104
|
+
@stats.timing "coverband.files.recorded_time", time_spent if @stats
|
105
|
+
@files = {}
|
112
106
|
end
|
107
|
+
elsif @verbose
|
108
|
+
puts "coverage report: "
|
109
|
+
puts @files.inspect
|
110
|
+
end
|
111
|
+
rescue RuntimeError => err
|
112
|
+
if @verbose
|
113
|
+
puts "coverage missing"
|
114
|
+
puts "error: #{err.inspect} #{err.message}"
|
113
115
|
end
|
114
116
|
end
|
115
|
-
|
116
117
|
end
|
117
118
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Coverband
|
2
|
+
class RedisStore
|
3
|
+
def initialize(redis)
|
4
|
+
@redis = redis
|
5
|
+
end
|
6
|
+
|
7
|
+
def store_report(report)
|
8
|
+
store_array('coverband', report.keys)
|
9
|
+
|
10
|
+
report.each do |file, lines|
|
11
|
+
store_array("coverband.#{file}", lines)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def sadd_supports_array?
|
16
|
+
# if the value is false, ||= would reevaluate the right side
|
17
|
+
return @_sadd_supports_array if defined?(@_sadd_supports_array)
|
18
|
+
|
19
|
+
@_sadd_supports_array = recent_gem_version? && recent_server_version?
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_reader :redis
|
25
|
+
|
26
|
+
def store_array(key, values)
|
27
|
+
if sadd_supports_array?
|
28
|
+
redis.sadd(key, values)
|
29
|
+
else
|
30
|
+
values.each do |value|
|
31
|
+
redis.sadd(key, value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
values
|
35
|
+
end
|
36
|
+
|
37
|
+
def recent_server_version?
|
38
|
+
info_data = redis.info
|
39
|
+
if info_data.is_a?(Hash)
|
40
|
+
Gem::Version.new(info_data['redis_version']) >= Gem::Version.new('2.4')
|
41
|
+
else
|
42
|
+
#guess supported
|
43
|
+
true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def recent_gem_version?
|
48
|
+
Gem::Version.new(Redis::VERSION) >= Gem::Version.new('3.0')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/coverband/version.rb
CHANGED
data/test/unit/base_test.rb
CHANGED
@@ -18,7 +18,7 @@ class BaseTest < Test::Unit::TestCase
|
|
18
18
|
assert_equal true, coverband.instance_variable_get("@enabled")
|
19
19
|
coverband.stop
|
20
20
|
assert_equal false, coverband.instance_variable_get("@enabled")
|
21
|
-
assert_equal false, coverband.instance_variable_get("@
|
21
|
+
assert_equal false, coverband.instance_variable_get("@tracer_set")
|
22
22
|
end
|
23
23
|
|
24
24
|
should "allow for sampling with a block and enable when 100 percent sample" do
|
@@ -49,10 +49,11 @@ class BaseTest < Test::Unit::TestCase
|
|
49
49
|
coverband = Coverband::Base.new
|
50
50
|
coverband.instance_variable_set("@sample_percentage", 100.0)
|
51
51
|
coverband.instance_variable_set("@verbose", true)
|
52
|
-
|
53
|
-
coverband.instance_variable_set("@reporter",
|
54
|
-
|
55
|
-
|
52
|
+
store = Coverband::RedisStore.new(Redis.new)
|
53
|
+
coverband.instance_variable_set("@reporter", store)
|
54
|
+
store.expects(:store_report).once.with { |files|
|
55
|
+
files.keys.include?("#{File.expand_path('../../../', __FILE__)}/lib/coverband/base.rb")
|
56
|
+
}
|
56
57
|
assert_equal false, coverband.instance_variable_get("@enabled")
|
57
58
|
coverband.start
|
58
59
|
1 + 1
|
@@ -39,19 +39,19 @@ class MiddlewareTest < Test::Unit::TestCase
|
|
39
39
|
should 'always unset function when sampling' do
|
40
40
|
request = Rack::MockRequest.env_for("/anything.json")
|
41
41
|
middleware = Coverband::Middleware.new(fake_app)
|
42
|
-
assert_equal false, middleware.instance_variable_get("@
|
42
|
+
assert_equal false, middleware.instance_variable_get("@tracer_set")
|
43
43
|
middleware.instance_variable_set("@sample_percentage", 100.0)
|
44
44
|
results = middleware.call(request)
|
45
|
-
assert_equal false, middleware.instance_variable_get("@
|
45
|
+
assert_equal false, middleware.instance_variable_get("@tracer_set")
|
46
46
|
end
|
47
47
|
|
48
48
|
should 'always unset function when not sampling' do
|
49
49
|
request = Rack::MockRequest.env_for("/anything.json")
|
50
50
|
middleware = Coverband::Middleware.new(fake_app)
|
51
|
-
assert_equal false, middleware.instance_variable_get("@
|
51
|
+
assert_equal false, middleware.instance_variable_get("@tracer_set")
|
52
52
|
middleware.instance_variable_set("@sample_percentage", 0.0)
|
53
53
|
results = middleware.call(request)
|
54
|
-
assert_equal false, middleware.instance_variable_get("@
|
54
|
+
assert_equal false, middleware.instance_variable_get("@tracer_set")
|
55
55
|
end
|
56
56
|
|
57
57
|
should 'always record coverage, set trace func, and add_files when sampling' do
|
@@ -70,7 +70,8 @@ class MiddlewareTest < Test::Unit::TestCase
|
|
70
70
|
assert_equal false, middleware.instance_variable_get("@enabled")
|
71
71
|
middleware.instance_variable_set("@sample_percentage", 100.0)
|
72
72
|
fake_redis = Redis.new
|
73
|
-
middleware.instance_variable_set("@reporter", fake_redis)
|
73
|
+
middleware.instance_variable_set("@reporter", Coverband::RedisStore.new(fake_redis))
|
74
|
+
fake_redis.stubs(:info).returns({'redis_version' => 3.0})
|
74
75
|
fake_redis.expects(:sadd).at_least_once
|
75
76
|
fake_redis.expects(:sadd).at_least_once.with("coverband.#{FAKE_RACK_APP_PATH}", [4,5,6])
|
76
77
|
results = middleware.call(request)
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.expand_path('../test_helper', File.dirname(__FILE__))
|
2
|
+
|
3
|
+
class RedisStoreTest < Test::Unit::TestCase
|
4
|
+
context '#store_report' do
|
5
|
+
setup do
|
6
|
+
@redis = Redis.current.tap { |redis|
|
7
|
+
redis.stubs(:sadd).with(anything, anything)
|
8
|
+
redis.stubs(:info).returns({'redis_version' => 3.0})
|
9
|
+
}
|
10
|
+
|
11
|
+
@store = Coverband::RedisStore.new(@redis)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "it stores the files into coverband" do
|
15
|
+
@redis.expects(:sadd).with('coverband', [
|
16
|
+
'/Users/danmayer/projects/cover_band_server/app.rb',
|
17
|
+
'/Users/danmayer/projects/cover_band_server/server.rb'
|
18
|
+
])
|
19
|
+
|
20
|
+
@store.store_report(test_data)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "it stores the file lines of the file app.rb" do
|
24
|
+
@redis.expects(:sadd).with(
|
25
|
+
'coverband./Users/danmayer/projects/cover_band_server/app.rb',
|
26
|
+
[54, 55]
|
27
|
+
)
|
28
|
+
|
29
|
+
@store.store_report(test_data)
|
30
|
+
end
|
31
|
+
|
32
|
+
should "it stores the file lines of the file server.rb" do
|
33
|
+
@redis.expects(:sadd).with(
|
34
|
+
'coverband./Users/danmayer/projects/cover_band_server/server.rb',
|
35
|
+
[5]
|
36
|
+
)
|
37
|
+
|
38
|
+
@store.store_report(test_data)
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when the redis server version is too old' do
|
42
|
+
setup do
|
43
|
+
@redis.stubs(:info).returns("redis_version"=>"2.2.3")
|
44
|
+
end
|
45
|
+
|
46
|
+
should "it store the files with separate calls into coverband" do
|
47
|
+
@redis.expects(:sadd).with('coverband', '/Users/danmayer/projects/cover_band_server/app.rb')
|
48
|
+
@redis.expects(:sadd).with('coverband', '/Users/danmayer/projects/cover_band_server/server.rb')
|
49
|
+
|
50
|
+
@store.store_report(test_data)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when the redis gem version is too old' do
|
55
|
+
setup do
|
56
|
+
@gem_version = Redis::VERSION
|
57
|
+
Redis.send(:remove_const, :VERSION)
|
58
|
+
Redis::VERSION = '2.2.2'
|
59
|
+
end
|
60
|
+
|
61
|
+
teardown do
|
62
|
+
Redis.send(:remove_const, :VERSION)
|
63
|
+
Redis::VERSION = @gem_version
|
64
|
+
end
|
65
|
+
|
66
|
+
should "it store the files with separate calls into coverband" do
|
67
|
+
@redis.expects(:sadd).with('coverband', '/Users/danmayer/projects/cover_band_server/app.rb')
|
68
|
+
@redis.expects(:sadd).with('coverband', '/Users/danmayer/projects/cover_band_server/server.rb')
|
69
|
+
|
70
|
+
@store.store_report(test_data)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def test_data
|
78
|
+
{
|
79
|
+
"/Users/danmayer/projects/cover_band_server/app.rb"=>[54, 55],
|
80
|
+
"/Users/danmayer/projects/cover_band_server/server.rb"=>[5]
|
81
|
+
}
|
82
|
+
end
|
83
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coverband
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -158,12 +158,14 @@ files:
|
|
158
158
|
- lib/coverband.rb
|
159
159
|
- lib/coverband/base.rb
|
160
160
|
- lib/coverband/middleware.rb
|
161
|
+
- lib/coverband/redis_store.rb
|
161
162
|
- lib/coverband/reporter.rb
|
162
163
|
- lib/coverband/version.rb
|
163
164
|
- test/fake_app/basic_rack.rb
|
164
165
|
- test/test_helper.rb
|
165
166
|
- test/unit/base_test.rb
|
166
167
|
- test/unit/middleware_test.rb
|
168
|
+
- test/unit/redis_store_test.rb
|
167
169
|
- test/unit/reporter_test.rb
|
168
170
|
homepage: ''
|
169
171
|
licenses:
|
@@ -180,7 +182,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
180
182
|
version: '0'
|
181
183
|
segments:
|
182
184
|
- 0
|
183
|
-
hash:
|
185
|
+
hash: -1687477973538567061
|
184
186
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
185
187
|
none: false
|
186
188
|
requirements:
|
@@ -189,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
191
|
version: '0'
|
190
192
|
segments:
|
191
193
|
- 0
|
192
|
-
hash:
|
194
|
+
hash: -1687477973538567061
|
193
195
|
requirements: []
|
194
196
|
rubyforge_project:
|
195
197
|
rubygems_version: 1.8.23
|
@@ -201,4 +203,5 @@ test_files:
|
|
201
203
|
- test/test_helper.rb
|
202
204
|
- test/unit/base_test.rb
|
203
205
|
- test/unit/middleware_test.rb
|
206
|
+
- test/unit/redis_store_test.rb
|
204
207
|
- test/unit/reporter_test.rb
|