Skip to content

Instantly share code, notes, and snippets.

@mgarriss
Created August 16, 2010 21:24
Show Gist options
  • Save mgarriss/527772 to your computer and use it in GitHub Desktop.
Save mgarriss/527772 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
class SpecFinder
attr_reader :prefix
def initialize(prefix)
@prefix = prefix
end
def name_to_file(name)
"#{prefix}/#{name}_spec.rb"
end
def file_to_name(file)
file.sub(%r{^#{Regexp.escape(prefix)}/}, '').sub(%r{_spec\.rb$}, '')
end
def spec_files
@spec_files ||= Dir["#{prefix}/**/*_spec.rb"]
end
def spec_names
@spec_names ||= spec_files.collect {|file| file_to_name(file) }
end
def run(specs)
if specs.empty?
if spec_files.empty?
puts "Nothing to do."
else
run(spec_files)
end
else
specs = specs.collect {|spec| force_spec_file(spec) }
command = command_to_run(specs)
puts command
exec command
end
end
def list
puts spec_names.sort.join("\n")
end
private
def force_spec_file(spec)
if spec_files.include?(spec)
spec
elsif spec_names.include?(spec)
name_to_file(spec)
else
raise "No such spec: #{spec.inspect}"
end
end
def command_to_run(files)
files_string = files.join(" ")
"spec -cfs #{files_string}"
end
end
list = ARGV.delete('-l') || ARGV.delete('--list')
prefix = ARGV.shift
prefix = prefix ? "spec/#{prefix}" : 'spec'
finder = SpecFinder.new(prefix)
specs = ARGV.dup
if list
finder.list
else
finder.run(specs)
end
#!/usr/bin/env ruby
require 'fileutils'
class TestFinder
attr_reader :prefix
def initialize(prefix)
@prefix = prefix
end
def name_to_file(name)
"#{prefix}/#{name}_test.rb"
end
def file_to_name(file)
file.sub(%r{^#{Regexp.escape(prefix)}/}, '').sub(%r{_test\.rb$}, '')
end
def test_files
@test_files ||= Dir["#{prefix}/**/*_test.rb"]
end
def test_names
@test_names ||= test_files.collect {|file| file_to_name(file) }
end
def run(tests)
if tests.empty?
if test_files.empty?
puts "Nothing to do."
else
run(test_files)
end
else
tests = tests.collect {|test| force_test_file(test) }
command = command_to_run(tests)
puts command
exec command
end
end
def list
puts test_names.sort.join("\n")
end
private
def force_test_file(test)
if test_files.include?(test)
test
elsif test_names.include?(test)
name_to_file(test)
else
raise "No such test: #{test.inspect}"
end
end
def command_to_run(files)
files_string = files.inspect
require_string = 'require "#{f}"'
%{ruby -Itest -e '#{files_string}.each {|f| #{require_string} }'}
end
end
list = ARGV.delete('-l') || ARGV.delete('--list')
prefix = ARGV.shift
prefix = prefix ? "test/#{prefix}" : 'test'
finder = TestFinder.new(prefix)
tests = ARGV.dup
if list
finder.list
else
finder.run(tests)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment