1 # In rails 1.2, plugins aren't available in the path until they're loaded.
2 # Check to see if the rspec plugin is installed first and require
3 # it if it is. If not, use the gem version.
4 rspec_base = File.expand_path(File.dirname(__FILE__) + '/../../rspec/lib')
5 $LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
6 require 'spec/rake/spectask'
7 require 'spec/translator'
9 spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
13 task :default => :spec
14 task :stats => "spec:statsetup"
16 desc "Run all specs in spec directory (excluding plugin specs)"
17 Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t|
18 t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
19 t.spec_files = FileList['spec/**/*_spec.rb']
23 desc "Run all specs in spec directory with RCov (excluding plugin specs)"
24 Spec::Rake::SpecTask.new(:rcov) do |t|
25 t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
26 t.spec_files = FileList['spec/**/*_spec.rb']
28 t.rcov_opts = lambda do
29 IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
33 desc "Print Specdoc for all specs (excluding plugin specs)"
34 Spec::Rake::SpecTask.new(:doc) do |t|
35 t.spec_opts = ["--format", "specdoc", "--dry-run"]
36 t.spec_files = FileList['spec/**/*_spec.rb']
39 desc "Print Specdoc for all plugin specs"
40 Spec::Rake::SpecTask.new(:plugin_doc) do |t|
41 t.spec_opts = ["--format", "specdoc", "--dry-run"]
42 t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*')
45 [:models, :controllers, :views, :helpers, :lib].each do |sub|
46 desc "Run the specs under spec/#{sub}"
47 Spec::Rake::SpecTask.new(sub => spec_prereq) do |t|
48 t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
49 t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
53 desc "Run the specs under vendor/plugins (except RSpec's own)"
54 Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t|
55 t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
56 t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec_on_rails/*")
60 desc "Runs the examples for rspec_on_rails"
61 Spec::Rake::SpecTask.new(:rspec_on_rails => spec_prereq) do |t|
62 t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
63 t.spec_files = FileList['vendor/plugins/rspec_on_rails/spec/**/*_spec.rb']
67 desc "Translate/upgrade specs using the built-in translator"
69 translator = ::Spec::Translator.new
70 dir = RAILS_ROOT + '/spec'
71 translator.translate(dir, dir)
74 # Setup specs for stats
76 require 'code_statistics'
77 ::STATS_DIRECTORIES << %w(Model\ specs spec/models)
78 ::STATS_DIRECTORIES << %w(View\ specs spec/views)
79 ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
80 ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers)
81 ::CodeStatistics::TEST_TYPES << "Model specs"
82 ::CodeStatistics::TEST_TYPES << "View specs"
83 ::CodeStatistics::TEST_TYPES << "Controller specs"
84 ::CodeStatistics::TEST_TYPES << "Helper specs"
85 ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
89 namespace :fixtures do
90 desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
91 task :load => :environment do
92 require 'active_record/fixtures'
93 ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
94 (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
95 Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
102 daemonized_server_pid = File.expand_path("spec_server.pid", RAILS_ROOT + "/tmp")
104 desc "start spec_server."
106 if File.exist?(daemonized_server_pid)
107 $stderr.puts "spec_server is already running."
109 $stderr.puts "Starting up spec server."
110 system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid)
114 desc "stop spec_server."
116 unless File.exist?(daemonized_server_pid)
117 $stderr.puts "No server running."
119 $stderr.puts "Shutting down spec_server."
120 system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) &&
121 File.delete(daemonized_server_pid)
125 desc "reload spec_server."
127 unless File.exist?(daemonized_server_pid)
128 $stderr.puts "No server running."
130 $stderr.puts "Reloading down spec_server."
131 system("kill", "-s", "USR2", File.read(daemonized_server_pid).strip)