Skip to content

Commit c8197eb

Browse files
author
jimweirich
committed
added rcov and unit tests
git-svn-id: svn+ssh://rubyforge.org/var/svn/rake/trunk@564 5af023f1-ac1a-0410-98d6-829a145c37ef
1 parent 0cb938c commit c8197eb

18 files changed

+711
-53
lines changed

rake/.cvsignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
coverage
12
testdata
23
html
34
pkg

rake/CHANGES

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Payton Quackenbush).
77
* Better error handling on invalid command line arguments (from Payton
88
Quackenbush).
9+
* Added rcov task and updated unit testing for better code coverage.
10+
* Fixed some bugs where the application object was going to the global
11+
appliation instead of using its own data.
912

1013
== Version 0.7.1
1114

rake/Rakefile

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CLOBBER.include('test/data/chains/play.*')
2323
CLOBBER.include('test/data/file_creation_task/build')
2424
CLOBBER.include('test/data/file_creation_task/src')
2525
CLOBBER.include('TAGS')
26+
CLOBBER.include('coverage')
2627

2728
def announce(msg='')
2829
STDERR.puts msg
@@ -87,6 +88,18 @@ Rake::TestTask.new(:test_contribs) do |t|
8788
t.warning = true
8889
end
8990

91+
require 'rcov/rcovtask'
92+
93+
Rcov::RcovTask.new do |t|
94+
t.libs << "test"
95+
t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '--text-report']
96+
t.test_files = FileList[
97+
'test/test*.rb',
98+
'test/contrib/test*.rb'
99+
]
100+
t.verbose = true
101+
end
102+
90103
directory 'testdata'
91104
[:test_all, :test_units, :test_contribs, :test_functional].each do |t|
92105
task t => ['testdata']
@@ -244,12 +257,12 @@ load "publish.rf" if File.exist? "publish.rf"
244257

245258
desc "Look for TODO and FIXME tags in the code"
246259
task :todo do
247-
FileList['**/*.rb'].exclude('pkg').egrep /#.*(FIXME|TODO|TBD)/
260+
FileList['**/*.rb'].exclude('pkg').egrep(/#.*(FIXME|TODO|TBD)/)
248261
end
249262

250263
desc "Look for Debugging print lines"
251264
task :dbg do
252-
FileList['**/*.rb'].egrep /\bDBG|\bbreakpoint\b/
265+
FileList['**/*.rb'].egrep(/\bDBG|\bbreakpoint\b/)
253266
end
254267

255268
desc "List all ruby files"

rake/lib/rake.rb

+25-37
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def needed?
395395
# Timestamp for this task. Basic tasks return the current time for
396396
# their time stamp. Other tasks can be more sophisticated.
397397
def timestamp
398-
@prerequisites.collect { |p| Rake::Task[p].timestamp }.max || Time.now
398+
@prerequisites.collect { |p| application[p].timestamp }.max || Time.now
399399
end
400400

401401
# Add a comment to the task. If a comment alread exists, separate
@@ -419,12 +419,12 @@ def investigation
419419
result << "task needed: #{needed?}\n"
420420
result << "timestamp: #{timestamp}\n"
421421
result << "pre-requisites: \n"
422-
prereqs = @prerequisites.collect {|name| Rake::Task[name]}
422+
prereqs = @prerequisites.collect {|name| application[name]}
423423
prereqs.sort! {|a,b| a.timestamp <=> b.timestamp}
424424
prereqs.each do |p|
425425
result << "--#{p.name} (#{p.timestamp})\n"
426426
end
427-
latest_prereq = @prerequisites.collect{|n| Rake::Task[n].timestamp}.max
427+
latest_prereq = @prerequisites.collect{|n| application[n].timestamp}.max
428428
result << "latest-prerequisite time: #{latest_prereq}\n"
429429
result << "................................\n\n"
430430
return result
@@ -513,7 +513,7 @@ def timestamp
513513
# Are there any prerequisites with a later time than the given
514514
# time stamp?
515515
def out_of_date?(stamp)
516-
@prerequisites.any? { |n| Rake::Task[n].timestamp > stamp}
516+
@prerequisites.any? { |n| application[n].timestamp > stamp}
517517
end
518518

519519
# ----------------------------------------------------------------
@@ -555,7 +555,7 @@ def timestamp
555555
class MultiTask < Task
556556
def invoke_prerequisites
557557
threads = @prerequisites.collect { |p|
558-
Thread.new(p) { |r| Task[r].invoke }
558+
Thread.new(p) { |r| application[r].invoke }
559559
}
560560
threads.each { |t| t.join }
561561
end
@@ -710,11 +710,7 @@ module FileUtils
710710
# end
711711
#
712712
def sh(*cmd, &block)
713-
if Hash === cmd.last then
714-
options = cmd.pop
715-
else
716-
options = {}
717-
end
713+
options = (Hash === cmd.last) ? cmd.pop : {}
718714
unless block_given?
719715
show_command = cmd.join(" ")
720716
show_command = show_command[0,42] + "..." if show_command.length > 45
@@ -736,11 +732,7 @@ def sh(*cmd, &block)
736732
# ruby %{-pe '$_.upcase!' <README}
737733
#
738734
def ruby(*args,&block)
739-
if Hash === args.last
740-
options = args.pop
741-
else
742-
options = {}
743-
end
735+
options = (Hash === args.last) ? args.pop : {}
744736
if args.length > 1 then
745737
sh(*([RUBY] + args + [options]), &block)
746738
else
@@ -758,7 +750,7 @@ def safe_ln(*args)
758750
else
759751
begin
760752
ln(*args)
761-
rescue StandardError, NotImplementedError
753+
rescue StandardError, NotImplementedError => ex
762754
LN_SUPPORTED[0] = false
763755
cp(*args)
764756
end
@@ -1125,7 +1117,7 @@ def calculate_exclude_regexp
11251117
case pat
11261118
when Regexp
11271119
ignores << pat
1128-
when /[*.]/
1120+
when /[*?]/
11291121
Dir[pat].each do |p| ignores << p end
11301122
else
11311123
ignores << Regexp.quote(pat)
@@ -1141,8 +1133,6 @@ def calculate_exclude_regexp
11411133

11421134
def resolve_add(fn)
11431135
case fn
1144-
when Array
1145-
fn.each { |f| self.resolve_add(f) }
11461136
when %r{[*?]}
11471137
add_matching(fn)
11481138
else
@@ -1156,7 +1146,7 @@ def resolve_exclude
11561146
case pat
11571147
when Regexp
11581148
reject! { |fn| fn =~ pat }
1159-
when /[*.]/
1149+
when /[*?]/
11601150
reject_list = Dir[pat]
11611151
reject! { |fn| reject_list.include?(fn) }
11621152
else
@@ -1427,7 +1417,6 @@ def define_task(task_class, args, &block)
14271417
deps = [deps] unless deps.respond_to?(:to_ary)
14281418
deps = deps.collect {|d| d.to_s }
14291419
task = intern(task_class, task_name)
1430-
task.application = self
14311420
task.add_comment(@last_comment)
14321421
@last_comment = nil
14331422
task.enhance(deps, &block)
@@ -1602,9 +1591,9 @@ class Application
16021591
include TaskManager
16031592

16041593
# The original directory where rake was invoked.
1605-
attr_reader :original_dir
1594+
attr_reader :original_dir, :rakefile
16061595

1607-
RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb']
1596+
DEFAULT_RAKEFILES = ['rakefile', 'Rakefile', 'rakefile.rb', 'Rakefile.rb'].freeze
16081597

16091598
OPTIONS = [
16101599
['--dry-run', '-n', GetoptLong::NO_ARGUMENT,
@@ -1644,6 +1633,7 @@ class Application
16441633
# Create a Rake::Application object.
16451634
def initialize
16461635
super
1636+
@rakefiles = DEFAULT_RAKEFILES.dup
16471637
@rakefile = nil
16481638
@pending_imports = []
16491639
@imported = []
@@ -1662,7 +1652,7 @@ def options
16621652
# True if one of the files in RAKEFILES is in the current directory.
16631653
# If a match is found, it is copied into @rakefile.
16641654
def have_rakefile
1665-
RAKEFILES.each do |fn|
1655+
@rakefiles.each do |fn|
16661656
if File.exist?(fn) || fn == ''
16671657
@rakefile = fn
16681658
return true
@@ -1695,20 +1685,18 @@ def help
16951685

16961686
# Display the tasks and dependencies.
16971687
def display_tasks_and_comments
1698-
displayable_tasks = Rake::Task.tasks.select { |t|
1688+
displayable_tasks = tasks.select { |t|
16991689
t.comment && t.name =~ options.show_task_pattern
17001690
}
1701-
width = displayable_tasks.collect { |t|
1702-
t.name.length
1703-
}.max
1691+
width = displayable_tasks.collect { |t| t.name.length }.max
17041692
displayable_tasks.each do |t|
17051693
printf "rake %-#{width}s # %s\n", t.name, t.comment
17061694
end
17071695
end
17081696

17091697
# Display the tasks and prerequisites
17101698
def display_prerequisites
1711-
Rake::Task.tasks.each do |t|
1699+
tasks.each do |t|
17121700
puts "rake #{t.name}"
17131701
t.prerequisites.each { |pre| puts " #{pre}" }
17141702
end
@@ -1740,8 +1728,8 @@ def do_option(opt, value)
17401728
when '--quiet'
17411729
verbose(false)
17421730
when '--rakefile'
1743-
RAKEFILES.clear
1744-
RAKEFILES << value
1731+
@rakefiles.clear
1732+
@rakefiles << value
17451733
when '--rakelibdir'
17461734
options.rakelib = value.split(':')
17471735
when '--require'
@@ -1774,8 +1762,6 @@ def do_option(opt, value)
17741762
when '--classic-namespace'
17751763
require 'rake/classic_namespace'
17761764
options.classic_namespace = true
1777-
else
1778-
fail "Unknown option: #{opt}"
17791765
end
17801766
end
17811767

@@ -1819,7 +1805,7 @@ def load_rakefile
18191805
while ! have_rakefile
18201806
Dir.chdir("..")
18211807
if Dir.pwd == here || options.nosearch
1822-
fail "No Rakefile found (looking for: #{RAKEFILES.join(', ')})"
1808+
fail "No Rakefile found (looking for: #{@rakefiles.join(', ')})"
18231809
end
18241810
here = Dir.pwd
18251811
end
@@ -1833,7 +1819,7 @@ def load_rakefile
18331819
end
18341820

18351821
# Collect the list of tasks on the command line. If no tasks are
1836-
# give, return a list containing only the default task.
1822+
# given, return a list containing only the default task.
18371823
# Environmental assignments are processed at this time as well.
18381824
def collect_tasks
18391825
tasks = []
@@ -1857,7 +1843,9 @@ def add_import(fn)
18571843
def load_imports
18581844
while fn = @pending_imports.shift
18591845
next if @imported.member?(fn)
1860-
Rake::Task[fn].invoke if Rake::Task.task_defined?(fn)
1846+
if fn_task = lookup(fn)
1847+
fn_task.invoke
1848+
end
18611849
ext = File.extname(fn)
18621850
loader = @loaders[ext] || @default_loader
18631851
loader.load(fn)
@@ -1903,7 +1891,7 @@ def run
19031891
elsif options.show_prereqs
19041892
display_prerequisites
19051893
else
1906-
tasks.each { |task_name| Rake::Task[task_name].invoke }
1894+
tasks.each { |task_name| self[task_name].invoke }
19071895
end
19081896
rescue SystemExit, GetoptLong::InvalidOption => ex
19091897
# Exit silently

rake/test/capture_stdout.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'stringio'
4+
5+
# Mix-in for capturing standard output.
6+
module CaptureStdout
7+
def capture_stdout
8+
s = StringIO.new
9+
oldstdout = $stdout
10+
$stdout = s
11+
yield
12+
s.string
13+
ensure
14+
$stdout = oldstdout
15+
end
16+
17+
def capture_stderr
18+
s = StringIO.new
19+
oldstderr = $stderr
20+
$stderr = s
21+
yield
22+
s.string
23+
ensure
24+
$stderr = oldstderr
25+
end
26+
end

rake/test/reqfile.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# For --require testing
2+
3+
TESTING_REQUIRE << 1

rake/test/reqfile2.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# For --require testing
2+
3+
TESTING_REQUIRE << 2

rake/test/reqfile3.rake

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# For --require testing
2+
3+
TESTING_REQUIRE << 3

0 commit comments

Comments
 (0)