Gentoo Archives: gentoo-commits

From: Alex Legler <a3li@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/glsamaker:master commit in: /, lib/tasks/
Date: Sat, 26 Feb 2011 01:02:05
Message-Id: 55b3061e412b3a9c1595c4d273a5aa21bbcf7262.a3li@gentoo
1 commit: 55b3061e412b3a9c1595c4d273a5aa21bbcf7262
2 Author: Alex Legler <alex <AT> a3li <DOT> li>
3 AuthorDate: Sat Feb 26 01:01:24 2011 +0000
4 Commit: Alex Legler <a3li <AT> gentoo <DOT> org>
5 CommitDate: Sat Feb 26 01:01:24 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/glsamaker.git;a=commit;h=55b3061e
7
8 Swap rcov task, new one is from https://github.com/commondream/rcov_plugin
9
10 ---
11 .gitignore | 1 +
12 lib/tasks/rails_rcov.rake | 150 ---------------------------------------------
13 lib/tasks/rcov.rake | 82 ++++++++++++++++++++++++
14 3 files changed, 83 insertions(+), 150 deletions(-)
15
16 diff --git a/.gitignore b/.gitignore
17 index 69411a5..2efa19a 100644
18 --- a/.gitignore
19 +++ b/.gitignore
20 @@ -10,3 +10,4 @@ public/stylesheets/all.css
21 public/javascripts/all.js
22 /.loadpath
23 /MYTODO
24 +coverage
25
26 diff --git a/lib/tasks/rails_rcov.rake b/lib/tasks/rails_rcov.rake
27 deleted file mode 100644
28 index 4fd798d..0000000
29 --- a/lib/tasks/rails_rcov.rake
30 +++ /dev/null
31 @@ -1,150 +0,0 @@
32 -# This File Uses Magic
33 -# ====================
34 -# Here's an example of how this file works. As an example, let's say you typed
35 -# this into your terminal:
36 -#
37 -# $ rake --tasks
38 -#
39 -# The rake executable goes through all the various places .rake files can be,
40 -# accumulates them all, and then runs them. When this file is loaded by Rake,
41 -# it iterates through all the tasks, and for each task named 'test:blah' adds
42 -# test:blah:rcov and test:blah:rcov_clobber.
43 -#
44 -# So you've seen all the tasks, and you type this into your terminal:
45 -#
46 -# $ rake test:units:rcov
47 -#
48 -# Rake does the same thing as above, but it runs the test:units:rcov task, which
49 -# pretty much just does this:
50 -#
51 -# $ ruby [this file] [the test you want to run] [some options]
52 -#
53 -# Now this file is run via the Ruby interpreter, and after glomming up the
54 -# options, it acts just like the Rake executable, with a slight difference: it
55 -# passes all the arguments to rcov, not ruby, so all your unit tests get some
56 -# rcov sweet loving.
57 -
58 -if ARGV.grep(/--run-rake-task=/).empty?
59 - # Define all our Rake tasks
60 -
61 - require 'rake/clean'
62 - require 'rcov/rcovtask'
63 -
64 - def to_rcov_task_sym(s)
65 - s = s.gsub(/(test:)/,'')
66 - s.empty? ? nil : s.intern
67 - end
68 -
69 - def to_rcov_task_name(s)
70 - s = s.gsub(/(test:)/,'')
71 - s =~ /s$/i ? s[0..-2] : s
72 - end
73 -
74 - def new_rcov_task(test_name)
75 - output_dir = "./coverage/#{test_name.gsub('test:','')}"
76 - CLOBBER.include(output_dir)
77 -
78 - # Add a task to run the rcov process
79 - desc "Run all #{to_rcov_task_name(test_name)} tests with Rcov to measure coverage"
80 - task :rcov => [:clobber_rcov] do |t|
81 - run_code = '"' << File.expand_path(__FILE__) << '"'
82 - run_code << " --run-rake-task=#{test_name}"
83 -
84 - params = String.new
85 - if ENV['RCOV_PARAMS']
86 - params << ENV['RCOV_PARAMS']
87 - end
88 -
89 - # rake test:units:rcov SHOW_ONLY=models,controllers,lib,helpers
90 - # rake test:units:rcov SHOW_ONLY=m,c,l,h
91 - if ENV['SHOW_ONLY']
92 - show_only = ENV['SHOW_ONLY'].to_s.split(',').map{|x|x.strip}
93 - if show_only.any?
94 - reg_exp = []
95 - for show_type in show_only
96 - reg_exp << case show_type
97 - when 'm', 'models' : 'app\/models'
98 - when 'c', 'controllers' : 'app\/controllers'
99 - when 'h', 'helpers' : 'app\/helpers'
100 - when 'l', 'lib' : 'lib'
101 - else
102 - show_type
103 - end
104 - end
105 - reg_exp.map!{ |m| "(#{m})" }
106 - params << " -x \\\"^(?!#{reg_exp.join('|')})\\\""
107 - end
108 - end
109 -
110 - unless params.empty?
111 - run_code << " --rcov-params=\"#{params}\""
112 - end
113 -
114 - ruby run_code
115 - end
116 -
117 - # Add a task to clean up after ourselves
118 - desc "Remove Rcov reports for #{to_rcov_task_name(test_name)} tests"
119 - task :clobber_rcov do |t|
120 - rm_r output_dir, :force => true
121 - end
122 -
123 - # Link our clobber task to the main one
124 - task :clobber => [:clobber_rcov]
125 - end
126 -
127 - test_tasks = Rake::Task.tasks.select{ |t| t.comment && t.name =~ /^test/ }
128 - for test_task in test_tasks
129 - namespace :test do
130 - if sym = to_rcov_task_sym(test_task.name)
131 - namespace sym do
132 - new_rcov_task(test_task.name)
133 - end
134 - end
135 - end
136 - end
137 -else
138 - # Load rake tasks, hijack ruby, and redirect the task through rcov
139 - require 'rubygems'
140 - require 'rake'
141 -
142 - module RcovTestSettings
143 - class << self
144 - attr_accessor :output_dir, :options
145 - def to_params
146 - "-o \"#{@output_dir}\" -T -x \"rubygems/*,rcov*\" --rails #{@options}"
147 - end
148 - end
149 -
150 - # load options and arguments from command line
151 - unless (cmd_line = ARGV.grep(/--rcov-params=/)).empty?
152 - @options = cmd_line.first.gsub(/--rcov-params=/, '')
153 - end
154 - end
155 -
156 - def is_windows?
157 - processor, platform, *rest = RUBY_PLATFORM.split("-")
158 - platform == 'mswin32'
159 - end
160 -
161 - # intercept what Rake *would* be doing with Ruby, and send it through Rcov instead
162 - module RakeFileUtils
163 - alias :ruby_without_rcov :ruby
164 - def ruby(*args, &block)
165 - cmd = (is_windows? ? 'rcov.cmd' : 'rcov') << " #{RcovTestSettings.to_params} #{args}"
166 - status = sh(cmd, {}, &block)
167 - puts "View the full results at <file://#{RcovTestSettings.output_dir}/index.html>"
168 - return status
169 - end
170 - end
171 -
172 - # read the test name and execute it (through Rcov)
173 - unless (cmd_line = ARGV.grep(/--run-rake-task=/)).empty?
174 - test_name = cmd_line.first.gsub(/--run-rake-task=/,'')
175 - ARGV.clear; ARGV << test_name
176 - RcovTestSettings.output_dir = File.expand_path("./coverage/#{test_name.gsub('test:','')}")
177 - Rake.application.run
178 - else
179 - raise "No test to execute!"
180 - end
181 -end
182 \ No newline at end of file
183
184 diff --git a/lib/tasks/rcov.rake b/lib/tasks/rcov.rake
185 new file mode 100644
186 index 0000000..4e1ae8d
187 --- /dev/null
188 +++ b/lib/tasks/rcov.rake
189 @@ -0,0 +1,82 @@
190 +def run_coverage(files)
191 + rm_f "coverage"
192 + rm_f "coverage.data"
193 +
194 + # turn the files we want to run into a string
195 + if files.empty?
196 + puts "No files were specified for testing"
197 + return
198 + end
199 +
200 + files = files.join(" ")
201 +
202 + if RUBY_PLATFORM =~ /darwin/
203 + exclude = '--exclude "gems/*" --exclude "Library/Frameworks/*"'
204 + elsif RUBY_PLATFORM =~ /java/
205 + exclude = '--exclude "rubygems/*,jruby/*,parser*,gemspec*,_DELEGATION*,eval*,recognize_optimized*,yaml,yaml/*,fcntl"'
206 + else
207 + exclude = '--exclude "rubygems/*"'
208 + end
209 + # rake test:units:rcov SHOW_ONLY=models,controllers,lib,helpers
210 + # rake test:units:rcov SHOW_ONLY=m,c,l,h
211 + if ENV['SHOW_ONLY']
212 + params = String.new
213 + show_only = ENV['SHOW_ONLY'].to_s.split(',').map{|x|x.strip}
214 + if show_only.any?
215 + reg_exp = []
216 + for show_type in show_only
217 + reg_exp << case show_type
218 + when 'm', 'models' then 'app\/models'
219 + when 'c', 'controllers' then 'app\/controllers'
220 + when 'h', 'helpers' then 'app\/helpers'
221 + when 'l', 'lib' then 'lib'
222 + else
223 + show_type
224 + end
225 + end
226 + reg_exp.map!{ |m| "(#{m})" }
227 + params << " --exclude \"^(?!#{reg_exp.join('|')})\""
228 + end
229 + exclude = exclude + params
230 + end
231 +
232 + rcov_bin = RUBY_PLATFORM =~ /java/ ? "jruby -S rcov" : "rcov"
233 + rcov = "#{rcov_bin} --rails -Ilib:test --sort coverage --text-report #{exclude}"
234 + puts
235 + puts
236 + puts "Running tests..."
237 + cmd = "#{rcov} #{files}"
238 + puts cmd
239 + begin
240 + sh cmd
241 + rescue Exception => e
242 + $stderr.puts "#{e.message}"
243 + $stderr.puts "The tests likely failed. Not aborting to not break Hudson builds"
244 + end
245 +end
246 +
247 +namespace :test do
248 +
249 + desc "Measures unit, functional, and integration test coverage"
250 + task :coverage do
251 + run_coverage Dir["test/unit/**/*.rb", "test/functional/**/*.rb", "test/integration/**/*.rb"]
252 + end
253 +
254 + namespace :coverage do
255 + desc "Runs coverage on unit tests"
256 + task :units do
257 + run_coverage Dir["test/unit/**/*.rb"]
258 + end
259 +
260 + desc "Runs coverage on functional tests"
261 + task :functionals do
262 + run_coverage Dir["test/functional/**/*.rb"]
263 + end
264 +
265 + desc "Runs coverage on integration tests"
266 + task :integration do
267 + run_coverage Dir["test/integration/**/*.rb"]
268 + end
269 + end
270 +end
271 +