Skip to content

Commit 1bd8ea3

Browse files
committed
Add shell runner to rake task.
1 parent c2dc459 commit 1bd8ea3

2 files changed

Lines changed: 89 additions & 77 deletions

File tree

README.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ over as a test case with each entry handled in the same manner. All test
2929
objects must respond to `#to_s` so their description can be used in test
3030
reports.
3131

32-
Any raised exception that responds to `#assertion?` in the affirmative is taken
33-
to be a failed assertion rather than simply an error. Ruby Test extends the
34-
Exception class to support this method for all exceptions.
32+
Ruby Test handles assertions via [BRASS](http://rubyworks.github.com/brass)
33+
compliance. Any raised exception that responds to `#assertion?` in the
34+
affirmative is taken to be a failed assertion rather than simply an error.
3535

3636
A test framework may raise a `NotImplementedError` to have a test recorded
3737
as *todo* --a _pending_ exception to remind the developer of tests that still
@@ -46,7 +46,17 @@ additional features that can makes its usage even more convenient.
4646
See the [Wiki](http://github.com/rubyworks/test/wiki) for further details.
4747

4848

49-
## Usage
49+
## Installation
50+
51+
Ruby Test is available as a Gem package.
52+
53+
$ gem install rubytest
54+
55+
Ruby Test is compliant with Setup.rb layout standard, so it can
56+
also be installed in an FHS compliant fashion if necessary.
57+
58+
59+
## Running Tests
5060

5161
There are a few ways to run tests. First, there is the command line tool
5262
e.g.
@@ -81,42 +91,30 @@ There is also a Rake task. In your Rakefile,
8191
run.files << 'test/test_*.rb'
8292
end
8393

84-
A Detroit plugin is in the works and should be available soon.
85-
8694
See the Wiki for more information on the different ways to run tests.
8795

8896

89-
Notice the use of `Test.configure` which is .
90-
91-
92-
93-
## Installation
94-
95-
Ruby Test is available as Gem package.
96-
97-
$ gem install rubytest
98-
99-
10097
## Requirements
10198

10299
Ruby Test uses the [ANSI](http://rubyworks.github.com/ansi) gem for color output.
103100

104101
Because of the "foundational" nature of this library we will look at removing
105-
this dependencies for future versions, but for early development the
102+
this dependency for future versions, but for early development the
106103
requirements does the job and does it well.
107104

108105

109106
## Development
110107

111108
Ruby Test is still a "nuby" gem. Please feel OBLIGATED to help improve it ;-)
112109

113-
Ruby Test is a [RubyWorks](http://rubyworks.github.com) project. If you can't
110+
Ruby Test is a [Rubyworks](http://rubyworks.github.com) project. If you can't
114111
contribue code, you can still help out by contributing to our development fund.
115112

116113

117114
## Reference Material
118115

119-
[1] [Standard Definition Of Unit Test](http://c2.com/cgi/wiki?StandardDefinitionOfUnitTest)
116+
* [Standard Definition Of Unit Test](http://c2.com/cgi/wiki?StandardDefinitionOfUnitTest)
117+
* [BRASS Assertions Standard](http:rubyworks.github.com/brass)
120118

121119

122120
## Copyrights
@@ -125,5 +123,5 @@ Copyright (c) 2011 Rubyworks
125123

126124
Made available according to the terms of the <b>FreeBSD license</b>.
127125

128-
See NOTICE.md for details.
126+
See LICENSE.txt for details.
129127

lib/rubytest/rake.rb

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
require 'rubytest'
12
require 'rake/tasklib'
23

34
module Test
45

6+
##
7+
# Rake subspace.
58
#
69
module Rake
710

8-
# TODO: The test task uses #fork. Maybe it should shell out instead?
9-
# Or provide the option for either?
10-
11+
##
1112
# Define a test rake task.
1213
#
1314
# The `TEST` environment variable can be used to select tests
14-
# when using the task.
15+
# when using this task. Note, this is just a more convenient
16+
# way than using `RUBYTEST_FILES`.
1517
#
1618
class TestTask < ::Rake::TaskLib
1719

@@ -23,76 +25,67 @@ class TestTask < ::Rake::TaskLib
2325
'test/**/*_test.rb'
2426
]
2527

26-
# Test scripts to load. Can be a file glob.
27-
attr_accessor :tests
28-
29-
# Paths to add to $LOAD_PATH.
30-
attr_accessor :loadpath
31-
32-
# Scripts to load prior to loading tests.
33-
attr_accessor :requires
34-
35-
# Report format to use.
36-
attr_accessor :format
37-
38-
# Filter tests based by tags.
39-
attr_accessor :tags
40-
41-
# Filter tests by matching description.
42-
attr_accessor :match
43-
44-
# From Rake's own TestTask.
45-
alias_method :libs, :loadpath
46-
alias_method :test_files, :tests
28+
# Test run configuration.
29+
#
30+
# @return [Config]
31+
attr :config
4732

33+
# Initialize new Rake::TestTask instance.
4834
#
49-
def initialize(name='test', desc="run tests", &block)
50-
@name = name
51-
@desc = desc
35+
def initialize(name='test', desc='run tests', &block)
36+
@name = name || 'test'
37+
@desc = desc
38+
39+
@config = Test::Config.new
5240

53-
@loadpath = ['lib']
54-
@requires = []
55-
@tests = [ENV['TEST'] || DEFAULT_TESTS].flatten
56-
@format = nil
57-
@match = nil
58-
@tags = []
41+
@config.files << default_tests if @config.files.empty?
42+
@config.loadpath << 'lib' if @config.loadpath.empty?
5943

60-
block.call(self)
44+
block.call(@config)
6145

6246
define_task
6347
end
6448

49+
# Define rake task for testing.
6550
#
51+
# @return nothing
6652
def define_task
6753
desc @desc
6854
task @name do
69-
@tests ||= default_tests
70-
run
55+
config.mode == 'shell' ? run_shell : run
7156
end
7257
end
7358

59+
# Run tests, via fork is possible, otherwise straight out.
7460
#
61+
# @return nothing
7562
def run
76-
fork {
77-
#require 'rubytest'
78-
require 'rubytest/runner'
79-
80-
loadpath.each { |d| $LOAD_PATH.unshift(d) }
81-
requires.each { |f| require f }
82-
test_files.each { |f| require f }
83-
84-
suite = $TEST_SUITE || []
85-
runner = new(suite, :format=>format, :tags=>tags, :match=>match)
63+
if Process.respond_to?(:fork)
64+
fork {
65+
runner = Test::Runner.new(config)
66+
success = runner.run
67+
exit -1 unless success
68+
}
69+
Process.wait
70+
else
71+
runner = Test::Runner.new(config)
8672
success = runner.run
87-
8873
exit -1 unless success
89-
}
90-
Process.wait
74+
end
75+
end
76+
77+
# Run test via command line shell.
78+
#
79+
# @return nothing
80+
def shell_run
81+
success = ruby(*config.to_shellwords)
82+
exit -1 unless success
9183
end
9284

9385
# Resolve test globs.
9486
#
95-
# TODO: Can this code be simplifed?
87+
# @todo Implementation probably cna be simplified.
88+
# @return [Array<String>] List of test files.
9689
def test_files
9790
files = tests
9891
files = files.map{ |f| Dir[f] }.flatten
@@ -102,19 +95,40 @@ def test_files
10295
files
10396
end
10497

98+
# Default test globs. For extra convenience will look for list in
99+
# `ENV['TEST']` first.
105100
#
101+
# @return [Array<String>]
106102
def default_tests
107-
if ENV['tests']
108-
ENV['tests'].split(/[:;]/)
103+
if ENV['TEST']
104+
ENV['TEST'].split(/[:;]/)
109105
else
110106
DEFAULT_TESTS
111107
end
112108
end
113109

110+
=begin
111+
# Shell out to current ruby command.
114112
#
115-
#def ruby_command
116-
# File.join(RbConfig::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
117-
#end
113+
# @return [Boolean] Success of shell call.
114+
def ruby(*argv)
115+
system(ruby_command, *argv)
116+
end
117+
118+
# Get current ruby shell command.
119+
#
120+
# @return [String] Ruby shell command.
121+
def ruby_command
122+
@ruby_command ||= (
123+
require 'rbconfig'
124+
ENV['RUBY'] ||
125+
File.join(
126+
RbConfig::CONFIG['bindir'],
127+
RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']
128+
).sub(/.*\s.*/m, '"\&"')
129+
)
130+
end
131+
=end
118132

119133
end
120134

0 commit comments

Comments
 (0)