1+ require 'rubytest'
12require 'rake/tasklib'
23
34module 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