Repository: rails/rails
File: railties/lib/rails/test_unit/runner.rb (Ruby)

Code before:
            end
          end
        end

        def compose_filter(runnable, filter)
          filter = normalize_declarative_test_filter(filter)

          if filters.any? { |_, lines| lines.any? }
            CompositeFilter.new(runnable, filter, filters)
          else
            filter
          end
        end

        private
          def extract_filters(argv)
            # Extract absolute and relative paths but skip -n /.*/ regexp filters.
            argv.filter_map do |path|
              next unless path_argument?(path)

              path = path.tr("\\", "/")
              case
              when /(:\d+(-\d+)?)+$/.match?(path)
                file, *lines = path.split(":")
                filters << [ file, lines ]
                file
              when Dir.exist?(path)
                "#{path}/**/*_test.rb"
              else
                filters << [ path, [] ]
                path
              end
            end
          end

          def default_test_glob
            ENV["DEFAULT_TEST"] || "test/**/*_test.rb"
          end

          def default_test_exclude_glob
            ENV["DEFAULT_TEST_EXCLUDE"] || "test/{system,dummy,fixtures}/**/*_test.rb"
          end

          def regexp_filter?(arg)
            arg.start_with?("/") && arg.end_with?("/")
          end

          def path_argument?(arg)
            PATH_ARGUMENT_PATTERN.match?(arg)
          end


Review comment (suggestion):
I get the need, but this code seems a bit fragile to me. Rather than all this extra logic, have you tried to improve `path_argument?` ?

I think it could ultimately use `File.exist?` to figure out that something is a path.

Code after:
            end
          end
        end

        def compose_filter(runnable, filter)
          filter = normalize_declarative_test_filter(filter)

          if filters.any? { |_, lines| lines.any? }
            CompositeFilter.new(runnable, filter, filters)
          else
            filter
          end
        end

        private
          def extract_filters(argv)
            previous_arg_was_a_flag = false
            # Extract absolute and relative paths but skip -n /.*/ regexp filters.
            argv.filter_map do |path|
              current_arg_is_a_flag = /^-{1,2}[a-zA-Z0-9\-_.]+=?.*\Z/.match?(path)

              if previous_arg_was_a_flag && !current_arg_is_a_flag
                # Handle the case where a flag is followed by another flag (e.g. --fail-fast --seed ...)
                previous_arg_was_a_flag = false
                next
              end

              path = path.tr("\\", "/")
              case
              when current_arg_is_a_flag
                previous_arg_was_a_flag = true unless path.include?("=") # Handle the case when "--foo=bar" is used.
                next
              when /(:\d+(-\d+)?)+$/.match?(path)
                file, *lines = path.split(":")
                filters << [ file, lines ]
                file
              when Dir.exist?(path)
                "#{path}/**/*_test.rb"
              else
                filters << [ path, [] ]
                path
              end
            end
          end

          def default_test_glob
            ENV["DEFAULT_TEST"] || "test/**/*_test.rb"
          end

          def default_test_exclude_glob
            ENV["DEFAULT_TEST_EXCLUDE"] || "test/{system,dummy,fixtures}/**/*_test.rb"