|
| 1 | +gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9 |
| 2 | +rspec_plugin_dir = File.expand_path(File.dirname(__FILE__) + '/../../vendor/plugins/rspec') |
| 3 | +$LOAD_PATH.unshift("#{rspec_plugin_dir}/lib") if File.exist?(rspec_plugin_dir) |
| 4 | + |
| 5 | +# Don't load rspec if running "rake gems:*" |
| 6 | +unless ARGV.any? {|a| a =~ /^gems/} |
| 7 | + |
| 8 | +begin |
| 9 | + require 'spec/rake/spectask' |
| 10 | +rescue MissingSourceFile |
| 11 | + module Spec |
| 12 | + module Rake |
| 13 | + class SpecTask |
| 14 | + def initialize(name) |
| 15 | + task name do |
| 16 | + # if rspec-rails is a configured gem, this will output helpful material and exit ... |
| 17 | + require File.expand_path(File.dirname(__FILE__) + "/../../config/environment") |
| 18 | + |
| 19 | + # ... otherwise, do this: |
| 20 | + raise <<-MSG |
| 21 | +
|
| 22 | +#{"*" * 80} |
| 23 | +* You are trying to run an rspec rake task defined in |
| 24 | +* #{__FILE__}, |
| 25 | +* but rspec can not be found in vendor/gems, vendor/plugins or system gems. |
| 26 | +#{"*" * 80} |
| 27 | +MSG |
| 28 | + end |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +Rake.application.instance_variable_get('@tasks').delete('default') |
| 36 | + |
| 37 | +spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop |
| 38 | +task :noop do |
| 39 | +end |
| 40 | + |
| 41 | +task :default => :spec |
| 42 | +task :stats => "spec:statsetup" |
| 43 | + |
| 44 | +desc "Run all specs in spec directory (excluding plugin specs)" |
| 45 | +Spec::Rake::SpecTask.new(:spec => spec_prereq) do |t| |
| 46 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 47 | + t.spec_files = FileList['spec/**/*/*_spec.rb'] |
| 48 | +end |
| 49 | + |
| 50 | +namespace :spec do |
| 51 | + desc "Run all specs in spec directory with RCov (excluding plugin specs)" |
| 52 | + Spec::Rake::SpecTask.new(:rcov) do |t| |
| 53 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 54 | + t.spec_files = FileList['spec/**/*/*_spec.rb'] |
| 55 | + t.rcov = true |
| 56 | + t.rcov_opts = lambda do |
| 57 | + IO.readlines("#{RAILS_ROOT}/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + desc "Print Specdoc for all specs (excluding plugin specs)" |
| 62 | + Spec::Rake::SpecTask.new(:doc) do |t| |
| 63 | + t.spec_opts = ["--format", "specdoc", "--dry-run"] |
| 64 | + t.spec_files = FileList['spec/**/*/*_spec.rb'] |
| 65 | + end |
| 66 | + |
| 67 | + desc "Print Specdoc for all plugin examples" |
| 68 | + Spec::Rake::SpecTask.new(:plugin_doc) do |t| |
| 69 | + t.spec_opts = ["--format", "specdoc", "--dry-run"] |
| 70 | + t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*') |
| 71 | + end |
| 72 | + |
| 73 | + [:models, :controllers, :views, :helpers, :lib].each do |sub| |
| 74 | + desc "Run the code examples in spec/#{sub}" |
| 75 | + Spec::Rake::SpecTask.new(sub => spec_prereq) do |t| |
| 76 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 77 | + t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"] |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + desc "Run the code examples in vendor/plugins (except RSpec's own)" |
| 82 | + Spec::Rake::SpecTask.new(:plugins => spec_prereq) do |t| |
| 83 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 84 | + t.spec_files = FileList['vendor/plugins/**/spec/**/*/*_spec.rb'].exclude('vendor/plugins/rspec/*').exclude("vendor/plugins/rspec-rails/*") |
| 85 | + end |
| 86 | + |
| 87 | + namespace :plugins do |
| 88 | + desc "Runs the examples for rspec_on_rails" |
| 89 | + Spec::Rake::SpecTask.new(:rspec_on_rails) do |t| |
| 90 | + t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] |
| 91 | + t.spec_files = FileList['vendor/plugins/rspec-rails/spec/**/*/*_spec.rb'] |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # Setup specs for stats |
| 96 | + task :statsetup do |
| 97 | + require 'code_statistics' |
| 98 | + ::STATS_DIRECTORIES << %w(Model\ specs spec/models) if File.exist?('spec/models') |
| 99 | + ::STATS_DIRECTORIES << %w(View\ specs spec/views) if File.exist?('spec/views') |
| 100 | + ::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers) if File.exist?('spec/controllers') |
| 101 | + ::STATS_DIRECTORIES << %w(Helper\ specs spec/helpers) if File.exist?('spec/helpers') |
| 102 | + ::STATS_DIRECTORIES << %w(Library\ specs spec/lib) if File.exist?('spec/lib') |
| 103 | + ::STATS_DIRECTORIES << %w(Routing\ specs spec/routing) if File.exist?('spec/routing') |
| 104 | + ::CodeStatistics::TEST_TYPES << "Model specs" if File.exist?('spec/models') |
| 105 | + ::CodeStatistics::TEST_TYPES << "View specs" if File.exist?('spec/views') |
| 106 | + ::CodeStatistics::TEST_TYPES << "Controller specs" if File.exist?('spec/controllers') |
| 107 | + ::CodeStatistics::TEST_TYPES << "Helper specs" if File.exist?('spec/helpers') |
| 108 | + ::CodeStatistics::TEST_TYPES << "Library specs" if File.exist?('spec/lib') |
| 109 | + ::CodeStatistics::TEST_TYPES << "Routing specs" if File.exist?('spec/routing') |
| 110 | + end |
| 111 | + |
| 112 | + namespace :db do |
| 113 | + namespace :fixtures do |
| 114 | + desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z." |
| 115 | + task :load => :environment do |
| 116 | + ActiveRecord::Base.establish_connection(Rails.env) |
| 117 | + base_dir = File.join(Rails.root, 'spec', 'fixtures') |
| 118 | + fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir |
| 119 | + |
| 120 | + require 'active_record/fixtures' |
| 121 | + (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir.glob(File.join(fixtures_dir, '*.{yml,csv}'))).each do |fixture_file| |
| 122 | + Fixtures.create_fixtures(File.dirname(fixture_file), File.basename(fixture_file, '.*')) |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + namespace :server do |
| 129 | + daemonized_server_pid = File.expand_path("#{RAILS_ROOT}/tmp/pids/spec_server.pid") |
| 130 | + |
| 131 | + desc "start spec_server." |
| 132 | + task :start do |
| 133 | + if File.exist?(daemonized_server_pid) |
| 134 | + $stderr.puts "spec_server is already running." |
| 135 | + else |
| 136 | + $stderr.puts %Q{Starting up spec_server ...} |
| 137 | + FileUtils.mkdir_p('tmp/pids') unless test ?d, 'tmp/pids' |
| 138 | + system("ruby", "script/spec_server", "--daemon", "--pid", daemonized_server_pid) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + desc "stop spec_server." |
| 143 | + task :stop do |
| 144 | + unless File.exist?(daemonized_server_pid) |
| 145 | + $stderr.puts "No server running." |
| 146 | + else |
| 147 | + $stderr.puts "Shutting down spec_server ..." |
| 148 | + system("kill", "-s", "TERM", File.read(daemonized_server_pid).strip) && |
| 149 | + File.delete(daemonized_server_pid) |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + desc "restart spec_server." |
| 154 | + task :restart => [:stop, :start] |
| 155 | + |
| 156 | + desc "check if spec server is running" |
| 157 | + task :status do |
| 158 | + if File.exist?(daemonized_server_pid) |
| 159 | + $stderr.puts %Q{spec_server is running (PID: #{File.read(daemonized_server_pid).gsub("\n","")})} |
| 160 | + else |
| 161 | + $stderr.puts "No server running." |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +end |
0 commit comments