Skip to content

Commit 4986cfb

Browse files
Merge pull request rails#49141 from ghiculescu/declarative-specs-2
Fix more bugs in declarative specs filter
2 parents 4a08111 + 0a8e537 commit 4986cfb

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

railties/lib/rails/test_unit/line_filtering.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Rails
66
module LineFiltering # :nodoc:
77
def run(reporter, options = {})
8-
options[:filter] = Rails::TestUnit::Runner.compose_filter(self, options[:filter])
8+
options = options.merge(filter: Rails::TestUnit::Runner.compose_filter(self, options[:filter]))
99

1010
super
1111
end

railties/lib/rails/test_unit/runner.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def load_tests(argv)
5252
end
5353

5454
def compose_filter(runnable, filter)
55-
filter = escape_declarative_test_filter(filter)
55+
filter = normalize_declarative_test_filter(filter)
5656

5757
if filters.any? { |_, lines| lines.any? }
5858
CompositeFilter.new(runnable, filter, filters)
@@ -104,12 +104,12 @@ def list_tests(patterns)
104104
tests
105105
end
106106

107-
def escape_declarative_test_filter(filter)
108-
# NOTE: This method may be applied multiple times, so any
109-
# transformations MUST BE idempotent.
107+
def normalize_declarative_test_filter(filter)
110108
if filter.is_a?(String)
111109
if regexp_filter?(filter)
112-
filter = filter.gsub(/\s+/, '[\s_]+')
110+
# Minitest::Spec::DSL#it does not replace whitespace in method
111+
# names, so match unmodified method names as well.
112+
filter = filter.gsub(/\s+/, "_").delete_suffix("/") + "|" + filter.delete_prefix("/")
113113
elsif !filter.start_with?("test_")
114114
filter = "test_#{filter.gsub(/\s+/, "_")}"
115115
end

railties/test/application/test_runner_test.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ class PostTest < ActiveSupport::TestCase
693693
end
694694
RUBY
695695

696-
run_test_command("test/models/post_test.rb -n '/greets foo|greets . . bar/'").tap do |output|
696+
run_test_command("test/models/post_test.rb -n '/greets foo|greets . .\\ bar/'").tap do |output|
697697
assert_match "hello foo", output
698698
assert_match "hello again foo", output
699699
assert_match "hello bar", output
@@ -703,9 +703,12 @@ class PostTest < ActiveSupport::TestCase
703703

704704
def test_declarative_style_regexp_filter_with_minitest_spec
705705
app_file "test/models/post_test.rb", <<~RUBY
706+
require "test_helper"
706707
require "minitest/spec"
707708
708-
class PostTest < Minitest::Spec
709+
class PostTest < ActiveSupport::TestCase
710+
extend Minitest::Spec::DSL
711+
709712
it "greets foo" do
710713
puts "hello foo"
711714
assert true
@@ -727,7 +730,7 @@ class PostTest < Minitest::Spec
727730
end
728731
RUBY
729732

730-
run_test_command("test/models/post_test.rb -n '/greets foo|greets . . bar/'").tap do |output|
733+
run_test_command("test/models/post_test.rb -n '/greets foo|greets . .\\ bar/'").tap do |output|
731734
assert_match "hello foo", output
732735
assert_match "hello again foo", output
733736
assert_match "hello bar", output

0 commit comments

Comments
 (0)