coverband 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/coverband.gemspec CHANGED
@@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_runtime_dependency "simplecov"
24
+ spec.add_runtime_dependency "json"
23
25
  end
@@ -0,0 +1,86 @@
1
+ module Coverband
2
+ class Middleware
3
+
4
+ def initialize(app, settings={})
5
+ @app = app
6
+ root = settings[:root] || './'
7
+ @project_directory = File.expand_path(root+'../')
8
+ @enabled = true
9
+ @function_set = false
10
+ @files = {}
11
+
12
+ @sample_percentage = settings[:percentage] || 100.0
13
+ @reporter = settings[:reporter]
14
+ end
15
+
16
+ def call(env)
17
+ configure_sampling
18
+ record_coverage
19
+ results = @app.call(env)
20
+ report_coverage
21
+ results
22
+ end
23
+
24
+ private
25
+
26
+ def configure_sampling
27
+ if (rand * 100.0) > @sample_percentage
28
+ @enabled = false
29
+ else
30
+ @enabled = true
31
+ end
32
+ end
33
+
34
+ def record_coverage
35
+ if @enabled
36
+ unless @function_set
37
+ set_trace_func proc { |event, file, line, id, binding, classname|
38
+ add_file(file, line)
39
+ }
40
+ @function_set = true
41
+ end
42
+ else
43
+ if @function_set
44
+ set_trace_func(nil)
45
+ @function_set = false
46
+ end
47
+ end
48
+ end
49
+
50
+ def add_file(file, line)
51
+ unless !file.match(@project_directory)
52
+ if @files.include?(file)
53
+ @files[file] << line
54
+ @files[file].uniq!
55
+ else
56
+ @files[file] = [line]
57
+ end
58
+ end
59
+ end
60
+
61
+ def report_coverage
62
+ begin
63
+ if @enabled
64
+ if @reporter
65
+ if @reporter.is_a?(Redis)
66
+ #"/Users/danmayer/projects/cover_band_server/app.rb"=>[54, 55]
67
+ @files.each_pair do |key, values|
68
+ @reporter.sadd "coverband", key.gsub('/','.')
69
+ @reporter.sadd "coverband#{key.gsub('/','.')}", values
70
+ end
71
+ @files = {}
72
+ end
73
+ else
74
+ puts "coverage report: "
75
+ puts @files.inspect
76
+ end
77
+ else
78
+ puts "coverage disabled" if @reporter
79
+ end
80
+ rescue RuntimeError
81
+ puts "coverage missing" if @reporter
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,82 @@
1
+ require 'simplecov'
2
+
3
+ module Coverband
4
+
5
+ class Reporter
6
+
7
+ def self.baseline
8
+ require 'coverage'
9
+ Coverage.start
10
+ yield
11
+ @project_directory = File.expand_path(Dir.pwd+'../')
12
+ results = Coverage.result
13
+ results = results.reject{|key, val| !key.match(@project_directory)}
14
+ puts results.inspect
15
+
16
+ File.open('./tmp/coverband_baseline.json', 'w') {|f| f.write(results.to_json) }
17
+ end
18
+
19
+ def self.report(redis, options = {})
20
+ if options.fetch(:reporter){ 'rcov' }=='rcov'
21
+ report_rcov(redis, options['existing_coverage'])
22
+ else
23
+ redis.smembers('coverband').each{|key| report_line(redis, key) }
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def self.report_rcov(redis, existing_coverage)
30
+ rcov_style_report = {}
31
+ redis.smembers('coverband').each{|key| rcov_style_report.merge!(line_hash(redis, key)) }
32
+ rcov_style_report = merge_existing_coverage(rcov_style_report, existing_coverage)
33
+ puts "report:"
34
+ puts rcov_style_report.inspect
35
+ SimpleCov::Result.new(rcov_style_report).format!
36
+ `open coverage/index.html`
37
+ end
38
+
39
+ def self.merge_existing_coverage(rcov_style_report, existing_coverage)
40
+ existing_coverage.each_pair do |key, lines|
41
+ if current_lines = rcov_style_report[key]
42
+ lines.each_with_index do |line, index|
43
+ if line.nil? && current_lines[index].to_i==0
44
+ current_lines[index] = nil
45
+ else
46
+ current_lines[index] = current_lines[index] ? (current_lines[index].to_i + line.to_i) : nil
47
+ end
48
+ end
49
+ rcov_style_report[key] = current_lines
50
+ else
51
+ rcov_style_report[key] = lines
52
+ end
53
+ end
54
+ rcov_style_report
55
+ end
56
+
57
+ # /Users/danmayer/projects/cover_band_server/views/index/erb: ["0", "2", "3", "6", "65532", "65533"]
58
+ # /Users/danmayer/projects/cover_band_server/app/rb: ["54", "55"]
59
+ # /Users/danmayer/projects/cover_band_server/views/layout/erb: ["0", "33", "36", "37", "38", "39", "40", "62", "63", "66", "65532", "65533"]
60
+ def self.report_line(redis, key)
61
+ puts "#{key.gsub('.','/')}: #{redis.smembers("coverband#{key}").inspect}"
62
+ end
63
+
64
+ # >> puts Coverage.result.inspect
65
+ # {"/Users/danmayer/projects/hearno/script/tester.rb"=>[1, nil, 1, 1, nil, nil, nil]}
66
+ def self.line_hash(redis, key)
67
+ filename = key.gsub('.','/').gsub('/rb','.rb').gsub('/erb','.erb')
68
+
69
+ lines_hit = redis.smembers("coverband#{key}")
70
+ count = File.foreach(filename).inject(0) {|c, line| c+1}
71
+ if filename.match(/\.erb/)
72
+ line_array = Array.new(count, nil)
73
+ else
74
+ line_array = Array.new(count, 0)
75
+ end
76
+ line_array.each_with_index{|line,index| line_array[index]=1 if lines_hit.include?((index+1).to_s) }
77
+ {filename => line_array}
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -1,3 +1,3 @@
1
1
  module Coverband
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/coverband.rb CHANGED
@@ -1,75 +1,7 @@
1
1
  require "coverband/version"
2
+ require "coverband/reporter"
3
+ require "coverband/middleware"
2
4
 
3
5
  module Coverband
4
- class Middleware
5
-
6
- def initialize(app, settings={})
7
- @app = app
8
- root = settings[:root] || './'
9
- @project_directory = File.expand_path(root+'../')
10
- @enabled = true
11
- @sample_percentage = 100.0
12
- @function_set = false
13
- @files = {}
14
- puts @project_directory
15
- puts "*"*40
16
- end
17
-
18
- def call(env)
19
- configure_sampling
20
- record_coverage
21
- results = @app.call(env)
22
- report_coverage
23
- results
24
- end
25
-
26
- private
27
-
28
- def configure_sampling
29
- if rand > 1.0
30
- @enabled = false
31
- end
32
- end
33
-
34
- def record_coverage
35
- if @enabled
36
- unless @function_set
37
- set_trace_func proc { |event, file, line, id, binding, classname|
38
- add_file(file, line)
39
- }
40
- @function_set = true
41
- end
42
- else
43
- if @function_set
44
- set_trace_func(nil)
45
- @function_set = false
46
- end
47
- end
48
- end
49
-
50
- def add_file(file, line)
51
- unless !file.match(@project_directory)
52
- if @files.include?(file)
53
- @files[file] << line
54
- @files[file].uniq!
55
- else
56
- @files[file] = [line]
57
- end
58
- end
59
- end
60
-
61
- def report_coverage
62
- begin
63
- if @enabled
64
- puts "coverage report: "
65
- puts @files.inspect
66
- else
67
- puts "coverage disabled"
68
- end
69
- rescue RuntimeError
70
- puts "coverage missing"
71
- end
72
- end
73
-
74
- end
6
+
75
7
  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.1
4
+ version: 0.0.2
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: 2013-11-04 00:00:00.000000000 Z
12
+ date: 2013-11-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,6 +43,38 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
46
78
  description: Rack middleware to help measure production code coverage
47
79
  email:
48
80
  - dan@mayerdan.com
@@ -58,6 +90,8 @@ files:
58
90
  - Rakefile
59
91
  - coverband.gemspec
60
92
  - lib/coverband.rb
93
+ - lib/coverband/middleware.rb
94
+ - lib/coverband/reporter.rb
61
95
  - lib/coverband/version.rb
62
96
  homepage: ''
63
97
  licenses:
@@ -74,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
108
  version: '0'
75
109
  segments:
76
110
  - 0
77
- hash: 1504221757513089007
111
+ hash: -3551265887896435389
78
112
  required_rubygems_version: !ruby/object:Gem::Requirement
79
113
  none: false
80
114
  requirements:
@@ -83,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
117
  version: '0'
84
118
  segments:
85
119
  - 0
86
- hash: 1504221757513089007
120
+ hash: -3551265887896435389
87
121
  requirements: []
88
122
  rubyforge_project:
89
123
  rubygems_version: 1.8.25