class ParallelTests::Gherkin::Runner

def cucumber_opts(given)

def cucumber_opts(given)
  if given =~ /--profile/ or given =~ /(^|\s)-p /
    given
  else
    [given, profile_from_config].compact.join(" ")
  end
end

def determine_executable

def determine_executable
  case
  when File.exist?("bin/#{name}")
    ParallelTests.with_ruby_binary("bin/#{name}")
  when ParallelTests.bundler_enabled?
    "bundle exec #{name}"
  when File.file?("script/#{name}")
    ParallelTests.with_ruby_binary("script/#{name}")
  else
    "#{name}"
  end
end

def line_is_result?(line)

def line_is_result?(line)
  line =~ /^\d+ (steps?|scenarios?)/
end

def profile_from_config

def profile_from_config
  # copied from https://github.com/cucumber/cucumber/blob/master/lib/cucumber/cli/profile_loader.rb#L85
  config = Dir.glob("{,.config/,config/}#{name}{.yml,.yaml}").first
  if config && File.read(config) =~ /^parallel:/
    "--profile parallel"
  end
end

def run_tests(test_files, process_number, num_processes, options)

def run_tests(test_files, process_number, num_processes, options)
  combined_scenarios = test_files
  if options[:group_by] == :scenarios
    grouped = test_files.map { |t| t.split(':') }.group_by(&:first)
    combined_scenarios = grouped.map {|file,files_and_lines| "#{file}:#{files_and_lines.map(&:last).join(':')}" }
  end
  sanitized_test_files = combined_scenarios.map { |val| WINDOWS ? "\"#{val}\"" : Shellwords.escape(val) }
  options[:env] ||= {}
  options[:env] = options[:env].merge({'AUTOTEST' => '1'}) if $stdout.tty? # display color when we are in a terminal
  cmd = [
    executable,
    (runtime_logging if File.directory?(File.dirname(runtime_log))),
    cucumber_opts(options[:test_options]),
    *sanitized_test_files
  ].compact.reject(&:empty?).join(' ')
  execute_command(cmd, process_number, num_processes, options)
end

def runtime_log

def runtime_log
  "tmp/parallel_runtime_#{name}.log"
end

def runtime_logging

def runtime_logging
  "--format ParallelTests::Gherkin::RuntimeLogger --out #{runtime_log}"
end

def summarize_results(results)

1 step (1 failed)
1 scenario (1 failed)
cucumber has 2 result lines per test run, that cannot be added
def summarize_results(results)
  sort_order = %w[scenario step failed flaky undefined skipped pending passed]
  %w[scenario step].map do |group|
    group_results = results.grep(/^\d+ #{group}/)
    next if group_results.empty?
    sums = sum_up_results(group_results)
    sums = sums.sort_by { |word, _| sort_order.index(word) || 999 }
    sums.map! do |word, number|
      plural = "s" if word == group and number != 1
      "#{number} #{word}#{plural}"
    end
    "#{sums[0]} (#{sums[1..-1].join(", ")})"
  end.compact.join("\n")
end

def test_file_name

def test_file_name
  @test_file_name || 'feature'
end

def test_suffix

def test_suffix
  /\.feature$/
end

def tests_in_groups(tests, num_groups, options={})

def tests_in_groups(tests, num_groups, options={})
  if options[:group_by] == :scenarios
    @test_file_name = "scenario"
  end
  method = "by_#{options[:group_by]}"
  if Grouper.respond_to?(method)
    Grouper.send(method, find_tests(tests, options), num_groups, options)
  else
    super
  end
end