Skip to content

Commit 2098cb0

Browse files
authored
Merge pull request rails#54647 from Edouard-chin/ec-test-root
Modify the Test Runner to allow running test at root:
2 parents 8019597 + f4408dd commit 2098cb0

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

railties/lib/rails/test_unit/runner.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,22 @@ def compose_filter(runnable, filter)
9191

9292
private
9393
def extract_filters(argv)
94+
previous_arg_was_a_flag = false
9495
# Extract absolute and relative paths but skip -n /.*/ regexp filters.
9596
argv.filter_map do |path|
96-
next unless path_argument?(path)
97+
current_arg_is_a_flag = /^-{1,2}[a-zA-Z0-9\-_.]+=?.*\Z/.match?(path)
98+
99+
if previous_arg_was_a_flag && !current_arg_is_a_flag
100+
# Handle the case where a flag is followed by another flag (e.g. --fail-fast --seed ...)
101+
previous_arg_was_a_flag = false
102+
next
103+
end
97104

98105
path = path.tr("\\", "/")
99106
case
107+
when current_arg_is_a_flag
108+
previous_arg_was_a_flag = true unless path.include?("=") # Handle the case when "--foo=bar" is used.
109+
next
100110
when /(:\d+(-\d+)?)+$/.match?(path)
101111
file, *lines = path.split(":")
102112
filters << [ file, lines ]
@@ -122,10 +132,6 @@ def regexp_filter?(arg)
122132
arg.start_with?("/") && arg.end_with?("/")
123133
end
124134

125-
def path_argument?(arg)
126-
PATH_ARGUMENT_PATTERN.match?(arg)
127-
end
128-
129135
def list_tests(patterns)
130136
tests = Rake::FileList[patterns.any? ? patterns : default_test_glob]
131137
tests.exclude(default_test_exclude_glob) if patterns.empty?

railties/test/application/test_runner_test.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,54 @@ def test_sanae
222222
end
223223
end
224224

225+
def test_run_test_at_root
226+
app_file "my_test.rb", <<-RUBY
227+
require "test_helper"
228+
229+
class MyTest < ActiveSupport::TestCase
230+
def test_rikka
231+
puts 'Rikka'
232+
end
233+
end
234+
RUBY
235+
236+
run_test_command("my_test.rb").tap do |output|
237+
assert_match "Rikka", output
238+
end
239+
end
240+
241+
def test_run_test_having_a_slash_in_its_name
242+
app_file "my_test.rb", <<-RUBY
243+
require "test_helper"
244+
245+
class MyTest < ActiveSupport::TestCase
246+
test "foo/foo" do
247+
puts 'Rikka'
248+
end
249+
end
250+
RUBY
251+
252+
run_test_command("my_test.rb -n foo\/foo").tap do |output|
253+
assert_match "Rikka", output
254+
end
255+
end
256+
257+
def test_run_test_with_flags_unordered
258+
app_file "my_test.rb", <<-RUBY
259+
require "test_helper"
260+
261+
class MyTest < ActiveSupport::TestCase
262+
test "foo/foo" do
263+
puts 'Rikka'
264+
end
265+
end
266+
RUBY
267+
268+
run_test_command("--seed 344 my_test.rb --fail-fast -n foo\/foo").tap do |output|
269+
assert_match "Rikka", output
270+
end
271+
end
272+
225273
def test_run_matched_test
226274
app_file "test/unit/chu_2_koi_test.rb", <<-RUBY
227275
require "test_helper"

0 commit comments

Comments
 (0)