Handle paths with trailing slashes in rails test

The `rails test` command scans its arguments for test paths to load
before handing off option parsing to Minitest. To avoid incorrectly
interpreting a `-n /regex/` pattern as an absolute path to a directory,
it skips arguments that end with a slash. However a relative path ending
in a slash is not ambiguous, so we can safely treat those as test paths.

This is especially useful in bash, where tab completing a directory
leaves a trailing slash in place.
This commit is contained in:
Eugene Kenny 2020-03-25 00:54:12 +00:00
parent 931f958695
commit 326d502714
3 changed files with 23 additions and 1 deletions

View File

@ -1,3 +1,7 @@
* Allow relative paths with trailing slashes to be passed to `rails test`.
*Eugene Kenny*
* Add `rack-mini-profiler` gem to the default `Gemfile`.
`rack-mini-profiler` displays performance information such as SQL time and flame graphs.

View File

@ -61,7 +61,7 @@ module Rails
private
def extract_filters(argv)
# Extract absolute and relative paths but skip -n /.*/ regexp filters.
argv.select { |arg| %r%^/?\w+/%.match?(arg) && !arg.end_with?("/") }.map do |path|
argv.select { |arg| path_argument?(arg) && !regexp_filter?(arg) }.map do |path|
case
when /(:\d+)+$/.match?(path)
file, *lines = path.split(":")
@ -75,6 +75,14 @@ module Rails
end
end
end
def regexp_filter?(arg)
arg.start_with?("/") && arg.end_with?("/")
end
def path_argument?(arg)
%r%^/?\w+/%.match?(arg)
end
end
end

View File

@ -310,6 +310,16 @@ module ApplicationTests
end
end
def test_run_relative_path_with_trailing_slash
create_test_file :models, "account"
create_test_file :controllers, "accounts_controller"
run_test_command("test/models/").tap do |output|
assert_match "AccountTest", output
assert_match "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips", output
end
end
def test_run_with_ruby_command
app_file "test/models/post_test.rb", <<-RUBY
require 'test_helper'