#!/usr/bin/env ruby

file, line = ARGV[0], nil
test_name_pattern = nil

if file =~ /:\d+/
  file, line = file.split(/:/)
end

if line
  text = File.read(file).split(/\r\n|\r|\n/)[line.to_i - 1]
  if text =~ /(?:context|should) ["'](.+?)["']/
    test_name_pattern = Regexp.escape($1)
  end
end

env_options = {
  "TEST" => file
}

env_options["TESTOPTS"] = "--name='/#{test_name_pattern}/'" if test_name_pattern

exec(env_options, "bundle exec rake test")
