|
| 1 | +raise "To avoid rake task loading problems: run 'rake clobber' in vendor/plugins/rspec" if File.directory?(File.join(File.dirname(__FILE__), *%w[.. .. vendor plugins rspec pkg])) |
| 2 | +raise "To avoid rake task loading problems: run 'rake clobber' in vendor/plugins/rspec-rails" if File.directory?(File.join(File.dirname(__FILE__), *%w[.. .. vendor plugins rspec-rails pkg])) |
| 3 | + |
| 4 | +# In rails 1.2, plugins aren't available in the path until they're loaded. |
| 5 | +# Check to see if the rspec plugin is installed first and require |
| 6 | +# it if it is. If not, use the gem version. |
| 7 | +rspec_base = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec/lib') |
| 8 | +$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base) |
| 9 | + |
| 10 | +begin |
| 11 | + require 'spec/rake/spectask' |
| 12 | + Rake.application.instance_variable_get('@tasks').delete('default') |
| 13 | + |
| 14 | + spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop |
| 15 | + task :noop do |
| 16 | + end |
| 17 | + |
| 18 | + task :default => :spec |
| 19 | + task :stats => "spec:statsetup" |
| 20 | + |
| 21 | + desc "Run all specs in spec directory (excluding plugin specs)" |
| 22 | + Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t| |
| 23 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 24 | + t.spec_files = FileList['spec/**/*_spec.rb'] |
| 25 | + end |
| 26 | + |
| 27 | + namespace :spec do |
| 28 | + desc "Run all specs in spec directory with RCov (excluding plugin specs)" |
| 29 | + Spec::Rake::SpecTask.new(:rcov) do |t| |
| 30 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 31 | + t.spec_files = FileList['spec/**/*_spec.rb'] |
| 32 | + t.rcov = true |
| 33 | + t.rcov_opts = lambda do |
| 34 | + IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + desc "Print Specdoc for all specs (excluding plugin specs)" |
| 39 | + Spec::Rake::SpecTask.new(:doc) do |t| |
| 40 | + t.spec_opts = ["--format", "specdoc", "--dry-run"] |
| 41 | + t.spec_files = FileList['spec/**/*_spec.rb'] |
| 42 | + end |
| 43 | + |
| 44 | + desc "Print Specdoc for all plugin examples" |
| 45 | + Spec::Rake::SpecTask.new(:plugin_doc) do |t| |
| 46 | + t.spec_opts = ["--format", "specdoc", "--dry-run"] |
| 47 | + t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*') |
| 48 | + end |
| 49 | + |
| 50 | + [:models, :controllers, :views, :helpers, :lib].each do |sub| |
| 51 | + desc "Run the code examples in spec/#{sub}" |
| 52 | + Spec::Rake::SpecTask.new(sub => spec_prereq) do |t| |
| 53 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 54 | + t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"] |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + desc "Run the code examples in vendor/plugins (except RSpec's own)" |
| 59 | + Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t| |
| 60 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 61 | + t.spec_files = FileList['vendor/plugins/**/spec/**/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*") |
| 62 | + end |
| 63 | + |
| 64 | + namespace :plugins do |
| 65 | + desc "Runs the examples for rspec_on_rails" |
| 66 | + Spec::Rake::SpecTask.new(:rspec_on_rails) do |t| |
| 67 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 68 | + t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*_spec.rb'] |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + # Setup specs for stats |
| 73 | + task :statsetup do |
| 74 | + require 'code_statistics' |
| 75 | + ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models') |
| 76 | + ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views') |
| 77 | + ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers') |
| 78 | + ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers') |
| 79 | + ::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib') |
| 80 | + ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models') |
| 81 | + ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views') |
| 82 | + ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers') |
| 83 | + ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers') |
| 84 | + ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib') |
| 85 | + ::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/} |
| 86 | + end |
| 87 | + |
| 88 | + namespace :db do |
| 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, '.*')) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + namespace :server do |
| 102 | + daemonized_server_pid = File.expand_path("spec_server.pid", RAILS_ROOT + "/tmp") |
| 103 | + |
| 104 | + desc "start spec_server." |
| 105 | + task :start do |
| 106 | + if File.exist?(daemonized_server_pid) |
| 107 | + $stderr.puts "spec_server is already running." |
| 108 | + else |
| 109 | + $stderr.puts "Starting up spec server." |
| 110 | + system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + desc "stop spec_server." |
| 115 | + task :stop do |
| 116 | + unless File.exist?(daemonized_server_pid) |
| 117 | + $stderr.puts "No server running." |
| 118 | + else |
| 119 | + $stderr.puts "Shutting down spec_server." |
| 120 | + system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) && |
| 121 | + File.delete(daemonized_server_pid) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + desc "reload spec_server." |
| 126 | + task :restart do |
| 127 | + unless File.exist?(daemonized_server_pid) |
| 128 | + $stderr.puts "No server running." |
| 129 | + else |
| 130 | + $stderr.puts "Reloading down spec_server." |
| 131 | + system("kill", "-s", "USR2", File.read(daemonized_server_pid).strip) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | +rescue MissingSourceFile |
| 137 | + # if rspec-rails is a configured gem, this will output helpful material and exit ... |
| 138 | + require File.expand_path(File.dirname(__FILE__) + "/../../config/environment") |
| 139 | + |
| 140 | + # ... otherwise, do this: |
| 141 | + raise <<-MSG |
| 142 | +
|
| 143 | + You have rspec-rails rake tasks installed in |
| 144 | + #{__FILE__}, |
| 145 | + but rspec-rails is not configured as a gem in |
| 146 | + config/environment.rb |
| 147 | +
|
| 148 | + Either remove #{__FILE__} |
| 149 | + or configure the rspec-rails gem in config/environment.rb. |
| 150 | +
|
| 151 | +MSG |
| 152 | +end |
| 153 | + |
0 commit comments