Skip to content

Commit 80f1517

Browse files
davidcpellzamith
authored andcommitted
Allow to run tests on an entire tree of directories
* Set up `-r` and `--recursive` options in Parser * Add more nested subdirectories under examples/subdir/ to use in EverythingTest#test_running_tests_recursively. * Update README.md with an example of -r usage
1 parent 9db0a8d commit 80f1517

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ Want to run the whole test? Just leave off the line number.
114114

115115
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
116116

117+
If you want to run all the tests in a directory as well as its subdirectories, use the `-r` flag:
118+
119+
$ m -r test/models
120+
"Searching provided directory for tests recursively"
121+
Run options:
122+
123+
..
124+
125+
Finished in 3.459902s, 45.0880 runs/s, 87.5747 assertions/s.
126+
127+
156 tests, 303 assertions, 0 failures, 0 errors, 13 skips
128+
117129

118130
SUPPORT
119131
=======

lib/m/parser.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def parse
2525
Rake::TestTask.new(:m_custom) do |t|
2626
t.libs << 'test'
2727
t.libs << 'spec'
28-
t.test_files = FileList["#{testable.file}/*test*.rb", "#{testable.file}/*spec*.rb"]
28+
t.test_files = FileList[wildcard("test"), wildcard("spec")]
2929
end
3030
# Invoke the rake task and exit, hopefully it'll work!
3131
begin
@@ -67,8 +67,21 @@ def parse_options!(argv)
6767
testable.line = line
6868
end
6969

70+
opts.on '-r', '--recursive DIR', 'Search provided directory recursively.' do |directory|
71+
testable.recursive = true
72+
argv << directory
73+
end
74+
7075
opts.parse! argv
7176
end
7277
end
78+
79+
def wildcard(type)
80+
if testable.recursive
81+
"#{testable.file}/**/*#{type}*.rb"
82+
else
83+
"#{testable.file}/*#{type}*.rb"
84+
end
85+
end
7386
end
7487
end

lib/m/testable.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
module M
22
class Testable
3-
attr_accessor :file
3+
attr_accessor :file, :recursive
44
attr_reader :line
55

6-
def initialize(file = "", line = nil)
6+
def initialize(file = "", line = nil, recursive = false)
77
@file = file
88
@line = line
9+
@recursive = recursive
910
end
1011

1112
def line=(line)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require_relative '../../../test_helper'
2+
3+
class DTest < MTest
4+
def test_d
5+
assert_equal 1, 1
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require_relative '../../../../test_helper'
2+
3+
class ETest < MTest
4+
def test_e
5+
assert_equal 1, 1
6+
end
7+
end

test/options_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,14 @@ def test_line_option_has_precedence_over_colon_format
3030
output = m('--line 20 examples/minitest_4_example_test.rb:2')
3131
assert_output(/1 tests, 1 assertions/, output)
3232
end
33+
34+
def test_recursive_option
35+
output = m('-r examples/subdir')
36+
assert_output(/5 tests/, output)
37+
end
38+
39+
def test_recursive_option_without_directory_arg_fails
40+
output = m('-r')
41+
assert_match(/OptionParser::MissingArgument/, output)
42+
end
3343
end

0 commit comments

Comments
 (0)