Class/Module Index [+]

Quicksearch

SingleTest

Constants

CMD_LINE_MATCHER

Public Instance Methods

all_tests(type) click to toggle source
# File lib/single_test.rb, line 23
def all_tests(type)
  FileList["#{type}/**/*_#{type}.rb"].reject{|file|File.directory?(file)}
end
find_example_in_spec(file, test_name) click to toggle source
# File lib/single_test.rb, line 74
def find_example_in_spec(file, test_name)
  File.readlines(file).each do |line|
    return $2 if line =~ /.*it\s*(["'])(.*#{test_name}.*)\11\\s*do/
  end
  nil
end
find_test_file(type,file_name) click to toggle source

find files matching the given name, prefer lower folders and short names, since deep/long names can be found via a more precise search string

# File lib/single_test.rb, line 66
def find_test_file(type,file_name)
  regex = /#{file_name.gsub('*','.*').gsub('?','.')}/ # support *? syntax for search
  all_tests(type).grep(regex).sort_by do |path|
    parts = path.split('/')
    [parts.size, parts.last.size]
  end.first
end
last_modified_file(dir, options={}) click to toggle source
# File lib/single_test.rb, line 109
def last_modified_file(dir, options={})
  Dir["#{dir}/**/*#{options[:ext]}"].sort_by { |p| File.mtime(p) }.last
end
load_tasks() click to toggle source
# File lib/single_test.rb, line 8
def load_tasks
  load File.join(File.dirname(__FILE__), 'tasks', 'single_test.rake')
end
parse_cli(call) click to toggle source

spec:user:blah --> [spec,user,blah]

# File lib/single_test.rb, line 49
def parse_cli(call)
  raise "you should not have gotten here..." unless call =~ CMD_LINE_MATCHER

  # replace any :: with / for class names
  call = call.gsub /::/, '/'

  arguments = call.split(":",3)
  [
    arguments[0], #type
    class_to_filename(arguments[1]), # class or file name
    arguments[2].to_s.strip.empty? ? nil : arguments[2] #test name
  ]
end
run_from_cli(call) click to toggle source
# File lib/single_test.rb, line 37
def run_from_cli(call)
  type, file, test_name = parse_cli(call)
  file = find_test_file(type,file)
  return unless file

  #run the file
  puts "running: #{file}"
  ENV['RAILS_ENV'] = 'test' #current EVN['RAILS_ENV'] is 'development', and will also exist in all called commands
  run_test(type, file, test_name)
end
run_last(type) click to toggle source
# File lib/single_test.rb, line 12
def run_last(type)
  path = "app"
  last = last_modified_file(path,:ext=>'.rb')
  test = last.sub('app/',"#{type}/").sub('.rb',"_#{type}.rb")
  if File.exist?(test)
    run_test(type, test)
  else
    puts "could not find #{test}"
  end
end
run_one_by_one(type) click to toggle source
# File lib/single_test.rb, line 27
def run_one_by_one(type)
  tests = all_tests(type)
  puts "Running #{tests.size} #{type}s"
  tests.sort.each do |file|
    puts "Running #{file}"
    run_test(type, file)
    puts ''
  end
end
run_test(type, file, test_name=nil) click to toggle source
# File lib/single_test.rb, line 81
def run_test(type, file, test_name=nil)
  case type.to_s
  when 'test' then Rake.sh "ruby -Ilib:test #{file} -n /#{test_name}/"
  when 'spec' then
    executable = spec_executable
    options_file = "spec/spec.opts"
    options_file = (File.exist?(options_file) ? " --options #{options_file}" : "")
    command = "export RAILS_ENV=test ; #{executable}#{options_file} #{file}"
    command += test_name_matcher(executable, file, test_name)
    command += " -X" if ENV['X'] # run via drb ?
    Rake.sh command
  else raise "Unknown: #{type}"
  end
end
spec_executable() click to toggle source

copied from parallel_tests github.com/grosser/parallel_tests/blob/master/lib/parallel_specs.rb#L9

# File lib/single_test.rb, line 97
def spec_executable
  cmd = if File.file?("script/spec")
    "script/spec"
  elsif bundler_enabled?
    cmd = (`bundle show rspec` =~ %{/rspec-1[^/]+$} ? "spec" : "rspec")
    "bundle exec #{cmd}"
  else
    ]spec rspec].detect{|cmd| system "#{cmd} --version > /dev/null 2>&1" }
  end
  cmd or raise("Can't find executables rspec or spec")
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.