Skip to content

Commit

Permalink
Merge pull request #47942 from ghiculescu/declarative-specs
Browse files Browse the repository at this point in the history
Fix regression in test name filters with specs
  • Loading branch information
jonathanhefner committed Apr 17, 2023
2 parents f7a4022 + 4dcf592 commit 539144d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
11 changes: 8 additions & 3 deletions railties/lib/rails/test_unit/runner.rb
Expand Up @@ -104,9 +104,14 @@ def list_tests(patterns)
end

def escape_declarative_test_filter(filter)
if filter.is_a?(String) && !filter.start_with?("test_")
filter = "test_#{filter}" unless regexp_filter?(filter)
filter = filter.gsub(/\s+/, "_")
# NOTE: This method may be applied multiple times, so any
# transformations MUST BE idempotent.
if filter.is_a?(String)
if regexp_filter?(filter)
filter = filter.gsub(/\s+/, '[\s_]+')
elsif !filter.start_with?("test_")
filter = "test_#{filter.gsub(/\s+/, "_")}"
end
end
filter
end
Expand Down
42 changes: 38 additions & 4 deletions railties/test/application/test_runner_test.rb
Expand Up @@ -595,7 +595,7 @@ class PostTest < ActiveSupport::TestCase
assert true
end
test "foo again" do
test "foo + + again" do
puts "hello again"
assert true
end
Expand All @@ -611,7 +611,7 @@ class PostTest < ActiveSupport::TestCase
assert_match "1 runs, 1 assertions, 0 failures", output
end

run_test_command("test/models/post_test.rb -n 'foo again'").tap do |output|
run_test_command("test/models/post_test.rb -n 'foo + + again'").tap do |output|
assert_match "hello again", output
assert_match "1 runs, 1 assertions, 0 failures", output
end
Expand All @@ -632,7 +632,7 @@ class PostTest < ActiveSupport::TestCase
assert true
end
test "greets bar" do
test "greets + + bar" do
puts "hello bar"
assert true
end
Expand All @@ -643,7 +643,41 @@ class PostTest < ActiveSupport::TestCase
end
RUBY

run_test_command("test/models/post_test.rb -n '/greets foo|greets bar/'").tap do |output|
run_test_command("test/models/post_test.rb -n '/greets foo|greets . . bar/'").tap do |output|
assert_match "hello foo", output
assert_match "hello again foo", output
assert_match "hello bar", output
assert_match "3 runs, 3 assertions, 0 failures", output
end
end

def test_declarative_style_regexp_filter_with_minitest_spec
app_file "test/models/post_test.rb", <<~RUBY
require "minitest/spec"
class PostTest < Minitest::Spec
it "greets foo" do
puts "hello foo"
assert true
end
it "greets foo again" do
puts "hello again foo"
assert true
end
it "greets + + bar" do
puts "hello bar"
assert true
end
it "greets no one" do
assert false
end
end
RUBY

run_test_command("test/models/post_test.rb -n '/greets foo|greets . . bar/'").tap do |output|
assert_match "hello foo", output
assert_match "hello again foo", output
assert_match "hello bar", output
Expand Down

0 comments on commit 539144d

Please sign in to comment.