rcov_stats 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,8 @@
1
+ 2.1.0 (September 2010)
2
+ replaced ReXML with Hpricot
3
+ made it working only with bundler > 0.9
4
+ made it working with rcov 0.9.9
5
+ 2.0.1 (May 2010)
6
+ fixed bug for merb with wrong bracket use
7
+ 2.0.0 (April 2010)
8
+ refactor
data/README CHANGED
@@ -1,52 +1,36 @@
1
- RcovStats
2
- =========
3
-
4
- RcovStats provides rcov extension, so you can select test files and test covered files for unit and functional tests.
5
-
6
- How to install :
7
-
8
- 1) As a Rails plugin:
9
- ./script/plugin install git://github.com/bartes/rcov_stats.git
10
- 2) As a gem for Rails:
11
- gem install rcov_stats
12
- cd your_application
13
- rake gems:unpack GEM=rcov_stats
14
- #add in RakeFile.rb
15
- Dir[File.dirname(__FILE__) + "/vendor/gems/**/rcov_stats*/tasks/*.rake"].each do |f|
16
- import f
17
- end
18
- 3) As a gem for Merb:
19
- gem install rcov_stats #from gemcutter
20
- ## puts this dependency inside your config/init.rb file ##
21
- dependency "rcov_stats"
22
- 5) for bundler
23
- ## puts this into Gemfile file ##
24
- gem "rcov_stats"
25
- #add in RakeFile.rb
26
- Dir[File.dirname(__FILE__) + "/vendor/bundler_gems/**/rcov_stats*/tasks/*.rake"].each do |f|
27
- import f
28
- end
29
-
30
- How to configure
31
-
32
- After installation (initialization) you can see rcov_stats.yml in /config directory.
33
- You can specify there :
34
- - files or directories to be covered by unit tests to cover (units_files_to_cover)
35
- - files or directories to be covered by functional tests to cover (functionals_files_to_cover)
36
- - test files or directories with test files which will be used for unit testing (units_files_to_test)
37
- - test files or directories with test files which will be used for functional testing (functionals_files_to_test)
38
- - action which will be used before running test suite (before_rcov -> by default db:test:prepare)
39
-
40
-
41
- You can run:
42
- - rake rcov:stats (includes rcov:units and rcov:functionals)
43
- - rake rcov:units
44
- - rake rcov:functionals
45
- - rake rcov:general (includes rcov:units and rcov:functionals, but counts one general coverage and puts all results in one output directory)
46
-
47
-
48
- You can grab rcov gem with :
49
- gem install rcov
50
-
51
-
52
- Copyright (c) 2010 [bartes], released under the MIT license
1
+ RcovStats
2
+ =========
3
+
4
+ RcovStats provides rcov extension, so you can select test files and test covered files for unit and functional tests.
5
+
6
+ How to install :
7
+
8
+ ** for Merb:
9
+ gem install rcov_stats #from gemcutter
10
+ ## puts this dependency inside your config/init.rb file ##
11
+ dependency "rcov_stats"
12
+ ** for Rails
13
+ ## puts this into Gemfile file ##
14
+ gem "rcov_stats"
15
+
16
+
17
+ How to configure
18
+
19
+ After installation (initialization) you can see rcov_stats.yml in /config directory.
20
+ You can specify there :
21
+ - files or directories to be covered by unit tests to cover (units_files_to_cover)
22
+ - files or directories to be covered by functional tests to cover (functionals_files_to_cover)
23
+ - test files or directories with test files which will be used for unit testing (units_files_to_test)
24
+ - test files or directories with test files which will be used for functional testing (functionals_files_to_test)
25
+ - action which will be used before running test suite (before_rcov -> by default db:test:prepare)
26
+
27
+
28
+ You can run:
29
+ - rake rcov:stats (includes rcov:units and rcov:functionals)
30
+ - rake rcov:units
31
+ - rake rcov:functionals
32
+ - rake rcov:general (includes rcov:units and rcov:functionals, but counts one general coverage and puts all results in one output directory)
33
+
34
+
35
+
36
+ Copyright (c) 2010 [bartes], released under the MIT license
@@ -1,13 +1,13 @@
1
- units_files_to_cover:
2
- - app/models
3
- functionals_files_to_cover:
4
- - app/controllers
5
- - app/helpers
6
- units_files_to_test:
7
- - models
8
- functionals_files_to_test:
9
- - controllers
10
- - helpers
11
- before_rcov: db:test:prepare
12
-
1
+ units_files_to_cover:
2
+ - app/models
3
+ functionals_files_to_cover:
4
+ - app/controllers
5
+ - app/helpers
6
+ units_files_to_test:
7
+ - models
8
+ functionals_files_to_test:
9
+ - controllers
10
+ - helpers
11
+ before_rcov: db:test:prepare
12
+
13
13
  #you can select files also this way app/*/*.rb
@@ -1,13 +1,13 @@
1
- units_files_to_cover:
2
- - app/models
3
- functionals_files_to_cover:
4
- - app/controllers
5
- - app/helpers
6
- units_files_to_test:
7
- - unit
8
- functionals_files_to_test:
9
- - functional
10
- - integration
11
- before_rcov: db:test:prepare
12
-
13
- #you can select files also this way app/*/*.rb
1
+ units_files_to_cover:
2
+ - app/models
3
+ functionals_files_to_cover:
4
+ - app/controllers
5
+ - app/helpers
6
+ units_files_to_test:
7
+ - unit
8
+ functionals_files_to_test:
9
+ - functional
10
+ - integration
11
+ before_rcov: db:test:prepare
12
+
13
+ #you can select files also this way app/*/*.rb
data/lib/rcov_stats.rb CHANGED
@@ -1,147 +1,166 @@
1
- require 'fileutils'
2
- require "rexml/document"
3
-
4
- class RcovStats
5
-
6
- TYPES = ["units", "functionals"]
7
-
8
- def self.cattr_accessor_with_default(name, value = nil)
9
- cattr_accessor name
10
- self.send("#{name}=", value) if value
11
- end
12
-
13
- cattr_accessor_with_default :is_rails, Object.const_defined?('Rails')
14
- cattr_accessor_with_default :is_merb, Object.const_defined?('Merb')
15
- cattr_accessor_with_default :root, ((@@is_rails && Rails.root) or (@@is_merb && Merb.root) or nil)
16
- cattr_accessor_with_default :rcov_stats_dir, File.dirname(__FILE__)
17
- cattr_accessor_with_default :rcov_stats_config_file, File.join(@@root, 'config', 'rcov_stats.yml')
18
- cattr_accessor_with_default :use_rspec, File.exists?(File.join(@@root, 'spec'))
19
- cattr_accessor_with_default :test_name, @@use_rspec ? "spec" : "test"
20
- cattr_accessor_with_default :test_file_indicator, "*_#{@@test_name}.rb"
21
- cattr_accessor_with_default :cover_file_indicator, "*.rb"
22
-
23
- attr_accessor :name, :sections
24
-
25
- def initialize(name_, sections_ = nil)
26
- self.name = name_
27
- self.sections = sections_.blank? ? [name_] : sections_
28
- end
29
-
30
- def self.get_config(option)
31
- YAML::load(File.open(File.join(@@root, 'config', 'rcov_stats.yml')))[option]
32
- end
33
-
34
- def self.before_rcov
35
- (pre_rcov = get_config("before_rcov")).blank? ? nil : pre_rcov
36
- end
37
-
38
- def get_array_data(type)
39
- @sections.map do |section|
40
- (self.class.get_config("#{section}_#{type}") || []).reject { |d| d.blank? }
41
- end.flatten.uniq
42
- end
43
-
44
- def files_to_cover
45
- get_array_data "files_to_cover"
46
- end
47
-
48
- def files_to_test
49
- get_array_data "files_to_test"
50
- end
51
-
52
- def parse_file_to_test(list)
53
- result = []
54
- list.each do |f|
55
- file_list = File.directory?(File.join(@@root, @@test_name, f)) ? File.join(@@test_name, f, "**", @@test_file_indicator) : File.join(@@test_name, f)
56
- unless (list_of_read_files = Dir[file_list]).empty?
57
- result += list_of_read_files
58
- end
59
- end
60
- result.uniq
61
- end
62
-
63
- def parse_file_to_cover(list)
64
- result = []
65
- list.each do |f|
66
- file_list = File.directory?(File.join(@@root, f)) ? File.join(f, "**", @@cover_file_indicator) : File.join(f)
67
- unless (list_of_read_files = Dir[file_list]).empty?
68
- result += list_of_read_files
69
- end
70
- end
71
- result.uniq
72
- end
73
-
74
- def invoke_rcov_task
75
- require 'rake/win32'
76
- files_to_cover_parsed = parse_file_to_cover(files_to_cover).map { |f| "(#{f})".gsub("/", "\/") }.join("|")
77
- rcov_settings = "--sort coverage --text-summary -x \"^(?!(#{files_to_cover_parsed}))\" "
78
- rcov_settings +="--output=#{File.join(@@root, "coverage", @name)} "
79
- rcov_tests = parse_file_to_test(files_to_test)
80
- return false if rcov_tests.empty?
81
- rcov_settings += rcov_tests.join(' ')
82
- cmd = "rcov #{rcov_settings}"
83
- Rake::Win32.windows? ? Rake::Win32.rake_system(cmd) : system(cmd)
84
- end
85
-
86
- def invoke_rcov_spec_task
87
- require 'spec/rake/spectask'
88
- rcov_tests = parse_file_to_test(files_to_test)
89
- return false if rcov_tests.empty?
90
- Spec::Rake::SpecTask.new(@name) do |t|
91
- spec_opts = File.join(@@root, @@test_name, 'spec.opts')
92
- t.spec_opts = ['--options', "\"#{spec_opts}\""] if File.exists?(spec_opts)
93
- t.spec_files = rcov_tests
94
- t.rcov = true
95
- t.rcov_dir = File.join(@@root, "coverage", @name)
96
- files_to_cover_parsed = parse_file_to_cover(files_to_cover).map { |f| "(#{f})".gsub("/", "\/") }.join("|")
97
- t.rcov_opts = ["--text-summary", "--sort", "coverage", "-x", "\"^(?!(#{files_to_cover_parsed}))\""]
98
- end
99
- end
100
-
101
- def invoke
102
- @@use_rspec ? invoke_rcov_spec_task : invoke_rcov_task
103
- end
104
-
105
- def generate_index
106
- Dir[File.join(@@rcov_stats_dir, '..', 'templates/*')].each do |i|
107
- FileUtils.cp(i, File.join(@@root, 'coverage', i.split("/").last))
108
- end
109
- @sections.each do |i|
110
- coverage_index = File.join(@@root, 'coverage', i, "index.html")
111
- next unless File.exists?(coverage_index)
112
- isource = IO.read(coverage_index)
113
- idoc = REXML::Document.new(isource.gsub(/\&/, ""))
114
- footer_tts = idoc.get_elements("//tfoot/tr/td//tt")
115
- footer_div_covered = idoc.get_elements("//tfoot/tr/td//div[@class='covered']")
116
- footer_div_uncovered = idoc.get_elements("//tfoot/tr/td//div[@class='uncovered']")
117
- curr_source = IO.read(File.join(@@root, 'coverage', "index.html"))
118
- curr_source.gsub!("#{i}_total_lines", footer_tts[0].text)
119
- curr_source.gsub!("#{i}_code_lines", footer_tts[1].text)
120
- curr_source.gsub!("#{i}_total_result", footer_tts[2].text)
121
- curr_source.gsub!("#{i}_code_result", footer_tts[3].text)
122
- curr_source.gsub!("#{i}_total_rpx", footer_div_covered[0].attribute("style").value)
123
- curr_source.gsub!("#{i}_total_cpx", footer_div_covered[1].attribute("style").value)
124
- curr_source.gsub!("#{i}_total_lrpx", footer_div_uncovered[0].attribute("style").value)
125
- curr_source.gsub!("#{i}_total_lcpx", footer_div_uncovered[1].attribute("style").value)
126
- curr_source.gsub!(/\<p\>*\<\/p\>/, "Generated on #{Time.now}")
127
- File.open(File.join(@@root, 'coverage', "index.html"), "w+") do |f|
128
- f.write(curr_source)
129
- end
130
- end
131
- end
132
-
133
- def self.setup
134
- if @@is_merb
135
- Merb::Plugins.add_rakefiles(File.join(@@rcov_stats_dir, "rcov_stats_tasks"))
136
- end
137
- unless File.exists?(@@rcov_stats_config_file)
138
- which_conf_use = (@@use_rspec ? 'rcov_rspec' : 'rcov_standard') + '.yml'
139
- FileUtils.cp(File.join(@@rcov_stats_dir, '..', 'config', which_conf_use), @@rcov_stats_config_file)
140
- end
141
- end
142
- end
143
-
144
- RcovStats.setup
145
-
146
-
147
-
1
+ require "fileutils"
2
+ require "erb"
3
+
4
+ class RcovStatsForErb
5
+
6
+ def initialize( options )
7
+ options.each_pair do |key, value|
8
+ instance_variable_set(:"@#{key}",value)
9
+ end
10
+ end
11
+
12
+ def get_binding
13
+ binding
14
+ end
15
+
16
+ end
17
+
18
+
19
+ class RcovStats
20
+
21
+ TYPES = ["units", "functionals"]
22
+
23
+ def self.cattr_accessor_with_default(name, value = nil)
24
+ cattr_accessor name
25
+ self.send("#{name}=", value) if value
26
+ end
27
+
28
+ cattr_accessor_with_default :is_rails, Object.const_defined?('Rails')
29
+ cattr_accessor_with_default :is_merb, Object.const_defined?('Merb')
30
+ cattr_accessor_with_default :root, ((@@is_rails && Rails.root) or (@@is_merb && Merb.root) or nil)
31
+ cattr_accessor_with_default :rcov_stats_dir, File.dirname(__FILE__)
32
+ cattr_accessor_with_default :rcov_stats_config_file, File.join(@@root, 'config', 'rcov_stats.yml')
33
+ cattr_accessor_with_default :use_rspec, File.exists?(File.join(@@root, 'spec'))
34
+ cattr_accessor_with_default :test_name, @@use_rspec ? "spec" : "test"
35
+ cattr_accessor_with_default :test_file_indicator, "*_#{@@test_name}.rb"
36
+ cattr_accessor_with_default :cover_file_indicator, "*.rb"
37
+
38
+ attr_accessor :name, :sections
39
+
40
+ def initialize(name_, sections_ = nil)
41
+ self.name = name_
42
+ self.sections = sections_.blank? ? [name_] : sections_
43
+ end
44
+
45
+ def self.get_config(option)
46
+ YAML::load(File.open(File.join(@@root, 'config', 'rcov_stats.yml')))[option]
47
+ end
48
+
49
+ def self.before_rcov
50
+ (pre_rcov = get_config("before_rcov")).blank? ? nil : pre_rcov
51
+ end
52
+
53
+ def get_array_data(type)
54
+ @sections.map do |section|
55
+ (self.class.get_config("#{section}_#{type}") || []).reject { |d| d.blank? }
56
+ end.flatten.uniq
57
+ end
58
+
59
+ def files_to_cover
60
+ get_array_data "files_to_cover"
61
+ end
62
+
63
+ def files_to_test
64
+ get_array_data "files_to_test"
65
+ end
66
+
67
+ def parse_file_to_test(list)
68
+ result = []
69
+ list.each do |f|
70
+ file_list = File.directory?(File.join(@@root, @@test_name, f)) ? File.join(@@test_name, f, "**", @@test_file_indicator) : File.join(@@test_name, f)
71
+ unless (list_of_read_files = Dir[file_list]).empty?
72
+ result += list_of_read_files
73
+ end
74
+ end
75
+ result.uniq
76
+ end
77
+
78
+ def parse_file_to_cover(list)
79
+ result = []
80
+ list.each do |f|
81
+ file_list = File.directory?(File.join(@@root, f)) ? File.join(f, "**", @@cover_file_indicator) : File.join(f)
82
+ unless (list_of_read_files = Dir[file_list]).empty?
83
+ result += list_of_read_files
84
+ end
85
+ end
86
+ result.uniq
87
+ end
88
+
89
+ def invoke_rcov_task
90
+ require 'rake/win32'
91
+ files_to_cover_parsed = parse_file_to_cover(files_to_cover).map { |f| "(#{f})".gsub("/", "\/") }.join("|")
92
+ rcov_settings = "--sort coverage --text-summary -x \"^(?!(#{files_to_cover_parsed}))\" "
93
+ rcov_settings +="--output=#{File.join(@@root, "coverage", @name)} "
94
+ rcov_tests = parse_file_to_test(files_to_test)
95
+ return false if rcov_tests.empty?
96
+ rcov_settings += rcov_tests.join(' ')
97
+ cmd = "bundle exec rcov #{rcov_settings}"
98
+ Rake::Win32.windows? ? Rake::Win32.rake_system(cmd) : system(cmd)
99
+ end
100
+
101
+ def invoke_rcov_spec_task
102
+ require 'spec/rake/spectask'
103
+ rcov_tests = parse_file_to_test(files_to_test)
104
+ return false if rcov_tests.empty?
105
+ Spec::Rake::SpecTask.new(@name) do |t|
106
+ spec_opts = File.join(@@root, @@test_name, 'spec.opts')
107
+ t.spec_opts = ['--options', "\"#{spec_opts}\""] if File.exists?(spec_opts)
108
+ t.spec_files = rcov_tests
109
+ t.rcov = true
110
+ t.rcov_dir = File.join(@@root, "coverage", @name)
111
+ files_to_cover_parsed = parse_file_to_cover(files_to_cover).map { |f| "(#{f})".gsub("/", "\/") }.join("|")
112
+ t.rcov_opts = ["--text-summary", "--sort", "coverage", "-x", "\"^(?!(#{files_to_cover_parsed}))\""]
113
+ end
114
+ end
115
+
116
+ def invoke
117
+ @@use_rspec ? invoke_rcov_spec_task : invoke_rcov_task
118
+ end
119
+
120
+ def generate_index
121
+ Dir[File.join(@@rcov_stats_dir, '..', 'templates/*')].each do |i|
122
+ FileUtils.cp(i, File.join(@@root, 'coverage', i.split("/").last))
123
+ end
124
+
125
+
126
+
127
+ @sections.each do |i|
128
+ coverage_index = File.join(@@root, 'coverage', i, "index.html")
129
+ next unless File.exists?(coverage_index)
130
+ doc = open(coverage_index) { |f| Hpricot(f) }
131
+ footer_tts = doc.search("//tfoot/tr/td//tt")
132
+ footer_div_covered = doc.search("//tfoot/tr/td//div[@class='covered']")
133
+ footer_div_uncovered = doc.search("//tfoot/tr/td//div[@class='uncovered']")
134
+
135
+ template_object = {}
136
+ template_object["#{i}_total_lines"] = footer_tts[0].inner_text
137
+ template_object["#{i}_code_lines"] = footer_tts[1].inner_text
138
+ template_object["#{i}_total_result"] = footer_tts[2].inner_text
139
+ template_object["#{i}_code_result"] = footer_tts[3].inner_text
140
+ template_object["#{i}_total_rpx"] = footer_div_covered[0].get_attribute("style")
141
+ template_object["#{i}_total_cpx"] = footer_div_covered[1].get_attribute("style")
142
+ template_object["#{i}_total_lrpx"] = footer_div_uncovered[0].get_attribute("style")
143
+ template_object["#{i}_total_lcpx"] = footer_div_uncovered[1].get_attribute("style")
144
+ template_object["generated_on"] = "Generated on #{Time.now}"
145
+
146
+
147
+ template_source = ERB.new(IO.read(File.join(@@root, 'coverage', "index.html")))
148
+
149
+ File.open(File.join(@@root, 'coverage', "index.html"), "w+") do |f|
150
+ f.write( template_source.result(RcovStatsForErb.new(template_object).get_binding))
151
+ end
152
+ end
153
+ end
154
+
155
+ def self.setup
156
+ if @@is_merb
157
+ Merb::Plugins.add_rakefiles(File.join(@@rcov_stats_dir, "rcov_stats_tasks"))
158
+ end
159
+ unless File.exists?(@@rcov_stats_config_file)
160
+ which_conf_use = (@@use_rspec ? 'rcov_rspec' : 'rcov_standard') + '.yml'
161
+ FileUtils.cp(File.join(@@rcov_stats_dir, '..', 'config', which_conf_use), @@rcov_stats_config_file)
162
+ end
163
+ end
164
+ end
165
+
166
+ RcovStats.setup
@@ -1,34 +1,34 @@
1
- namespace :rcov do
2
- require File.join(File.dirname(__FILE__), "rcov_stats.rb")
3
- desc "run rcov for units tests"
4
- task(RcovStats.before_rcov ? ({:units => RcovStats.before_rcov}) : :units) do
5
- puts '** rcov:units **'
6
- RcovStats.new('units').invoke
7
- end
8
-
9
- desc "run rcov for functionals tests"
10
- task(RcovStats.before_rcov ? ({:functionals => RcovStats.before_rcov}) : :functionals) do
11
- puts '** rcov:functionals **'
12
- RcovStats.new('functionals').invoke
13
- end
14
-
15
- desc "run rcov for functionals and units tests"
16
- task(RcovStats.before_rcov ? ({:stats => RcovStats.before_rcov}) : :stats) do
17
- puts '** rcov:stats **'
18
- Rake::Task['rcov:units'].invoke
19
- Rake::Task['rcov:functionals'].invoke
20
- Rake::Task['rcov:generate_index'].invoke
21
- end
22
-
23
- desc "run general rcov tests"
24
- task(RcovStats.before_rcov ? ({:general => RcovStats.before_rcov}) : :general) do
25
- puts '** rcov:general **'
26
- RcovStats.new('general',["units", "functionals"]).invoke
27
- end
28
-
29
- desc "generate index for all suites"
30
- task :generate_index do
31
- puts '** rcov:generate_index **'
32
- RcovStats.new('general',["units", "functionals"]).generate_index
33
- end
34
- end
1
+ namespace :rcov do
2
+ require File.join(File.dirname(__FILE__), "rcov_stats.rb")
3
+ desc "run rcov for units tests"
4
+ task(RcovStats.before_rcov ? ({:units => RcovStats.before_rcov}) : :units) do
5
+ puts '** rcov:units **'
6
+ RcovStats.new('units').invoke
7
+ end
8
+
9
+ desc "run rcov for functionals tests"
10
+ task(RcovStats.before_rcov ? ({:functionals => RcovStats.before_rcov}) : :functionals) do
11
+ puts '** rcov:functionals **'
12
+ RcovStats.new('functionals').invoke
13
+ end
14
+
15
+ desc "run rcov for functionals and units tests"
16
+ task(RcovStats.before_rcov ? ({:stats => RcovStats.before_rcov}) : :stats) do
17
+ puts '** rcov:stats **'
18
+ Rake::Task['rcov:units'].invoke
19
+ Rake::Task['rcov:functionals'].invoke
20
+ Rake::Task['rcov:generate_index'].invoke
21
+ end
22
+
23
+ desc "run general rcov tests"
24
+ task(RcovStats.before_rcov ? ({:general => RcovStats.before_rcov}) : :general) do
25
+ puts '** rcov:general **'
26
+ RcovStats.new('general',["units", "functionals"]).invoke
27
+ end
28
+
29
+ desc "generate index for all suites"
30
+ task :generate_index do
31
+ puts '** rcov:generate_index **'
32
+ RcovStats.new('general',["units", "functionals"]).generate_index
33
+ end
34
+ end