Gentoo Archives: gentoo-commits

From: Alex Legler <a3li@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/glsamaker:master commit in: doc/, lib/tasks/
Date: Fri, 25 Feb 2011 19:56:40
Message-Id: 486951510d509368259e07fd2dd5b85bae8bc9ea.a3li@gentoo
1 commit: 486951510d509368259e07fd2dd5b85bae8bc9ea
2 Author: Alex Legler <alex <AT> a3li <DOT> li>
3 AuthorDate: Fri Feb 25 19:55:46 2011 +0000
4 Commit: Alex Legler <a3li <AT> gentoo <DOT> org>
5 CommitDate: Fri Feb 25 19:55:46 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/glsamaker.git;a=commit;h=48695151
7
8 Add the rails rcov plugin from http://svn.codahale.com/rails_rcov
9
10 ---
11 doc/README_RCOV_PLUGIN | 86 ++++++++++++++++++++++++++
12 lib/tasks/rails_rcov.rake | 150 +++++++++++++++++++++++++++++++++++++++++++++
13 2 files changed, 236 insertions(+), 0 deletions(-)
14
15 diff --git a/doc/README_RCOV_PLUGIN b/doc/README_RCOV_PLUGIN
16 new file mode 100644
17 index 0000000..eb9d84c
18 --- /dev/null
19 +++ b/doc/README_RCOV_PLUGIN
20 @@ -0,0 +1,86 @@
21 + = rails_rcov plugin for Rails
22 +
23 +rails_rcov provides easy-to-use Rake tasks to determine the code coverage of
24 +your unit, functional, and integration tests using Mauricio Fernandez's rcov
25 +tool.
26 +
27 +== Installation
28 +
29 +First, install rcov from Mauricio's web site
30 +[http://eigenclass.org/hiki.rb?rcov]. Make sure it's on your system path, so
31 +that typing +rcov+ on the command line actually runs it. THIS PLUGIN DOESN'T DO
32 +ANYTHING BESIDES GENERATE ERRORS UNLESS YOU INSTALL RCOV FIRST. RCOV CONTAINS
33 +ALL THE MAGIC, THIS PLUGIN JUST RUNS IT.
34 +
35 +Second, install this plugin. If your project is source-controlled by Subversion
36 +(which it should be, really), the easiest way to install this is via Rails'
37 +plugin script:
38 +
39 + ./script/plugin install -x http://svn.codahale.com/rails_rcov
40 +
41 +If you're not using Subversion, or if you don't want it adding
42 +<tt>svn:externals</tt> in your project, remove the <tt>-x</tt> switch:
43 +
44 + ./script/plugin install http://svn.codahale.com/rails_rcov
45 +
46 +== Usage
47 +
48 +For each <tt>test:blah</tt> task you have for your Rails project, rails_rcov
49 +adds two more: <tt>test:blah:rcov</tt> and <tt>test:blah:clobber_rcov</tt>.
50 +
51 +Running <tt>rake test:units:rcov</tt>, for example, will run your unit tests
52 +through rcov and write the code coverage reports to
53 +<tt>your_rails_app/coverage/units</tt>.
54 +
55 +Running <tt>test:units:clobber_rcov</tt> will erase the generated report for the
56 +unit tests.
57 +
58 +Each rcov task takes two optional parameters: RCOV_PARAMS, whose argument is
59 +passed along to rcov, and SHOW_ONLY, which limits the files displayed in the
60 +report.
61 +
62 +RCOV_PARAMS:
63 + # sort by coverage
64 + rake test:units:rcov RCOV_PARAMS="--sort=coverage"
65 +
66 + # show callsites and hide fully covered files
67 + rake test:units:rcov RCOV_PARAMS="--callsites --only-uncovered"
68 +
69 +Check the rcov documentation for more details.
70 +
71 +SHOW_ONLY is a comma-separated list of the files you'd like to see. Right now
72 +there are four types of files rake_rcov recognizes: models, helpers,
73 +controllers, and lib. These can be abbreviated to their first letters:
74 +
75 + # only show files from app/models
76 + rake test:units:rcov SHOW_ONLY=models
77 +
78 + # only show files from app/helpers and app/controllers
79 + rake test:units:rcov SHOW_ONLY=helpers,controllers
80 +
81 + # only show files from app/helpers and app/controllers, with less typing
82 + rake test:units:rcov SHOW_ONLY=h,c
83 +
84 +Please note that rails_rcov has only been tested with a Bash shell, and any
85 +other environment could well explode in your face. If you're having trouble
86 +getting this to work on Windows, please take the time to figure out what's not
87 +working. Most of the time it boils down to the different ways the Window shell
88 +and the Bash shell escape metacharacters. Play around with the way rcov_rake
89 +escapes data (like on line 73, or 78) and send me a fix. I don't have a working
90 +Windows environment anymore, so leaving it up to me won't solve anything. ;-)
91 +
92 +== Resources
93 +
94 +=== Subversion
95 +
96 +* http://svn.codahale.com/rails_rcov
97 +
98 +=== Blog
99 +
100 +* http://blog.codahale.com
101 +
102 +== Credits
103 +
104 +Written by Coda Hale <coda.hale@×××××.com>. Thanks to Nils Franzen for a Win32
105 +escaping patch. Thanks to Alex Wayne for suggesting how to make SHOW_ONLY not be
106 +useless.
107 \ No newline at end of file
108
109 diff --git a/lib/tasks/rails_rcov.rake b/lib/tasks/rails_rcov.rake
110 new file mode 100644
111 index 0000000..4fd798d
112 --- /dev/null
113 +++ b/lib/tasks/rails_rcov.rake
114 @@ -0,0 +1,150 @@
115 +# This File Uses Magic
116 +# ====================
117 +# Here's an example of how this file works. As an example, let's say you typed
118 +# this into your terminal:
119 +#
120 +# $ rake --tasks
121 +#
122 +# The rake executable goes through all the various places .rake files can be,
123 +# accumulates them all, and then runs them. When this file is loaded by Rake,
124 +# it iterates through all the tasks, and for each task named 'test:blah' adds
125 +# test:blah:rcov and test:blah:rcov_clobber.
126 +#
127 +# So you've seen all the tasks, and you type this into your terminal:
128 +#
129 +# $ rake test:units:rcov
130 +#
131 +# Rake does the same thing as above, but it runs the test:units:rcov task, which
132 +# pretty much just does this:
133 +#
134 +# $ ruby [this file] [the test you want to run] [some options]
135 +#
136 +# Now this file is run via the Ruby interpreter, and after glomming up the
137 +# options, it acts just like the Rake executable, with a slight difference: it
138 +# passes all the arguments to rcov, not ruby, so all your unit tests get some
139 +# rcov sweet loving.
140 +
141 +if ARGV.grep(/--run-rake-task=/).empty?
142 + # Define all our Rake tasks
143 +
144 + require 'rake/clean'
145 + require 'rcov/rcovtask'
146 +
147 + def to_rcov_task_sym(s)
148 + s = s.gsub(/(test:)/,'')
149 + s.empty? ? nil : s.intern
150 + end
151 +
152 + def to_rcov_task_name(s)
153 + s = s.gsub(/(test:)/,'')
154 + s =~ /s$/i ? s[0..-2] : s
155 + end
156 +
157 + def new_rcov_task(test_name)
158 + output_dir = "./coverage/#{test_name.gsub('test:','')}"
159 + CLOBBER.include(output_dir)
160 +
161 + # Add a task to run the rcov process
162 + desc "Run all #{to_rcov_task_name(test_name)} tests with Rcov to measure coverage"
163 + task :rcov => [:clobber_rcov] do |t|
164 + run_code = '"' << File.expand_path(__FILE__) << '"'
165 + run_code << " --run-rake-task=#{test_name}"
166 +
167 + params = String.new
168 + if ENV['RCOV_PARAMS']
169 + params << ENV['RCOV_PARAMS']
170 + end
171 +
172 + # rake test:units:rcov SHOW_ONLY=models,controllers,lib,helpers
173 + # rake test:units:rcov SHOW_ONLY=m,c,l,h
174 + if ENV['SHOW_ONLY']
175 + show_only = ENV['SHOW_ONLY'].to_s.split(',').map{|x|x.strip}
176 + if show_only.any?
177 + reg_exp = []
178 + for show_type in show_only
179 + reg_exp << case show_type
180 + when 'm', 'models' : 'app\/models'
181 + when 'c', 'controllers' : 'app\/controllers'
182 + when 'h', 'helpers' : 'app\/helpers'
183 + when 'l', 'lib' : 'lib'
184 + else
185 + show_type
186 + end
187 + end
188 + reg_exp.map!{ |m| "(#{m})" }
189 + params << " -x \\\"^(?!#{reg_exp.join('|')})\\\""
190 + end
191 + end
192 +
193 + unless params.empty?
194 + run_code << " --rcov-params=\"#{params}\""
195 + end
196 +
197 + ruby run_code
198 + end
199 +
200 + # Add a task to clean up after ourselves
201 + desc "Remove Rcov reports for #{to_rcov_task_name(test_name)} tests"
202 + task :clobber_rcov do |t|
203 + rm_r output_dir, :force => true
204 + end
205 +
206 + # Link our clobber task to the main one
207 + task :clobber => [:clobber_rcov]
208 + end
209 +
210 + test_tasks = Rake::Task.tasks.select{ |t| t.comment && t.name =~ /^test/ }
211 + for test_task in test_tasks
212 + namespace :test do
213 + if sym = to_rcov_task_sym(test_task.name)
214 + namespace sym do
215 + new_rcov_task(test_task.name)
216 + end
217 + end
218 + end
219 + end
220 +else
221 + # Load rake tasks, hijack ruby, and redirect the task through rcov
222 + require 'rubygems'
223 + require 'rake'
224 +
225 + module RcovTestSettings
226 + class << self
227 + attr_accessor :output_dir, :options
228 + def to_params
229 + "-o \"#{@output_dir}\" -T -x \"rubygems/*,rcov*\" --rails #{@options}"
230 + end
231 + end
232 +
233 + # load options and arguments from command line
234 + unless (cmd_line = ARGV.grep(/--rcov-params=/)).empty?
235 + @options = cmd_line.first.gsub(/--rcov-params=/, '')
236 + end
237 + end
238 +
239 + def is_windows?
240 + processor, platform, *rest = RUBY_PLATFORM.split("-")
241 + platform == 'mswin32'
242 + end
243 +
244 + # intercept what Rake *would* be doing with Ruby, and send it through Rcov instead
245 + module RakeFileUtils
246 + alias :ruby_without_rcov :ruby
247 + def ruby(*args, &block)
248 + cmd = (is_windows? ? 'rcov.cmd' : 'rcov') << " #{RcovTestSettings.to_params} #{args}"
249 + status = sh(cmd, {}, &block)
250 + puts "View the full results at <file://#{RcovTestSettings.output_dir}/index.html>"
251 + return status
252 + end
253 + end
254 +
255 + # read the test name and execute it (through Rcov)
256 + unless (cmd_line = ARGV.grep(/--run-rake-task=/)).empty?
257 + test_name = cmd_line.first.gsub(/--run-rake-task=/,'')
258 + ARGV.clear; ARGV << test_name
259 + RcovTestSettings.output_dir = File.expand_path("./coverage/#{test_name.gsub('test:','')}")
260 + Rake.application.run
261 + else
262 + raise "No test to execute!"
263 + end
264 +end
265 \ No newline at end of file