copied from github.com/carlhuda/bundler Bundler::SharedHelpers#find_gemfile
# File lib/single_test.rb, line 137 def self.bundler_enabled? return true if Object.const_defined?(:Bundler) previous = nil current = File.expand_path(Dir.pwd) until !File.directory?(current) || current == previous filename = File.join(current, "Gemfile") return true if File.exists?(filename) current, previous = File.expand_path("..", current), current end false end
# File lib/single_test.rb, line 23 def all_tests(type) FileList["#{type}/**/*_#{type}.rb"].reject{|file|File.directory?(file)} end
# 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 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
# 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
# File lib/single_test.rb, line 8 def load_tasks load File.join(File.dirname(__FILE__), 'tasks', 'single_test.rake') end
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
# 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
# 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
# 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
# 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
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 = (%xbundle 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
converted from underscore to do a few more checks
# File lib/single_test.rb, line 116 def class_to_filename(suspect) word = suspect.to_s.dup return word unless word.match /^[A-Z]/ and not word.match %r{/[a-z]} word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end
# File lib/single_test.rb, line 128 def test_name_matcher(executable, file, test_name) if test_name and not executable.include?('rspec') # rspec 1 only supports full test names -> find a matching test-name test_name = find_example_in_spec(file, test_name) || test_name end test_name ? %Q( -e "#{test_name.sub('"',"\\\"")}") : '' end