Module: SingleTest

Extended by:
SingleTest
Included in:
SingleTest
Defined in:
lib/single_test.rb

Constant Summary collapse

CMD_LINE_MATCHER =
/^(spec|test)\:.*(\:.*)?$/

Instance Method Summary collapse

Instance Method Details

#all_tests(type) ⇒ Object



23
24
25
# 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) ⇒ Object



74
75
76
77
78
79
# 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}.*)\1\s*do/
  end
  nil
end

#find_test_file(type, file_name) ⇒ Object

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



66
67
68
69
70
71
72
# 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 = {}) ⇒ Object



109
110
111
# 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_tasksObject



8
9
10
# File 'lib/single_test.rb', line 8

def load_tasks
  load File.join(File.dirname(__FILE__), 'tasks', 'single_test.rake')
end

#parse_cli(call) ⇒ Object

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



49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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_executableObject



97
98
99
100
101
102
103
104
105
106
107
# 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` =~ %r{/rspec-1[^/]+$} ? "spec" : "rspec")
    "bundle exec #{cmd}"
  else
    %w[spec rspec].detect{|cmd| system "#{cmd} --version > /dev/null 2>&1" }
  end
  cmd or raise("Can't find executables rspec or spec")
end