Permalink
Browse files

https://github.com/grosser/parallel_tests setup, now the default rake…

… task
  • Loading branch information...
1 parent 749763e commit a53fbc758c508db85068a9e44b7b49bc6cc06023 @jmgarnier jmgarnier committed Jun 15, 2012
Showing with 101 additions and 4 deletions.
  1. +1 −0 Gemfile
  2. +2 −2 Rakefile
  3. +36 −1 spec/support/rails_template.rb
  4. +60 −0 tasks/parallel_tests.rake
  5. +2 −1 tasks/test.rake
View
@@ -28,6 +28,7 @@ group :development, :test do
gem 'rdiscount' # For yard
gem "sprockets"
gem 'rails-i18n' # Gives us default i18n for many languages
+ gem 'parallel_tests'
end
group :test do
View
@@ -14,8 +14,8 @@ require File.expand_path('../spec/support/detect_rails_version', __FILE__)
# Import all our rake tasks
FileList['tasks/**/*.rake'].each { |task| import task }
-# Run the specs & cukes
-task :default => :test
+# Run the specs & cukes using parallel_tests
+task :default => :parallel_tests
begin
require 'jasmine'
@@ -22,7 +22,7 @@
# Generate a model with string ids
generate :model, "tag name:string"
-gsub_file(Dir['db/migrate/*_create_tags.rb'][0], /\:tags\sdo\s.*/, ":tags, :id => false, :primary_key => :id do |t|\n\t\t\tt.string :id\n" )
+gsub_file(Dir['db/migrate/*_create_tags.rb'][0], /\:tags\sdo\s.*/, ":tags, :id => false, :primary_key => :id do |t|\n\t\t\tt.string :id\n")
id_model_setup = <<-EOF
self.primary_key = :id
before_create :set_id
@@ -68,3 +68,38 @@ def set_id
rake "db:migrate"
rake "db:test:prepare"
run "/usr/bin/env RAILS_ENV=cucumber rake db:migrate"
+
+# Setup parallel_tests
+def setup_parallel_tests_database(after, force_insert_same_content = false)
+ inject_into_file 'config/database.yml', "<%= ENV['TEST_ENV_NUMBER'] %>", :after => after, :force => force_insert_same_content
+end
+
+setup_parallel_tests_database "test.sqlite3"
+setup_parallel_tests_database "cucumber.sqlite3", true
+
+# Note: this is hack!
+# Somehow, calling parallel_tests tasks from Rails generator using Thor does not work ...
+# RAILS_ENV variable never makes it to parallel_tests tasks.
+# We need to call these tasks in the after set up hook in order to creates cucumber DBs + run migrations on test & cucumber DBs
+create_file 'lib/tasks/parallel.rake' do
+ <<'RAKE'
+namespace :parallel do
+ def run_in_parallel(cmd, options)
+ count = "-n #{options[:count]}" if options[:count]
+ executable = 'parallel_test'
+ command = "#{executable} --exec '#{cmd}' #{count} #{'--non-parallel' if options[:non_parallel]}"
+ abort unless system(command)
+ end
+
+ desc "create cucumber databases via db:create --> parallel:create_cucumber_db[num_cpus]"
+ task :create_cucumber_db, :count do |t, args|
+ run_in_parallel("rake db:create RAILS_ENV=cucumber", args)
+ end
+
+ desc "load dumped schema for cucumber databases"
+ task :load_schema_cucumber_db, :count do |t,args|
+ run_in_parallel("rake db:schema:load RAILS_ENV=cucumber", args)
+ end
+end
+RAKE
+end
View
@@ -0,0 +1,60 @@
+require 'parallel'
+
+desc "Run the full suite using parallel_tests to run on multiple cores"
+task :parallel_tests => ['parallel:setup_parallel_tests', 'parallel:spec', 'parallel:features', 'cucumber:class_reloading']
+
+namespace :parallel do
+
+ def rails_app_rake(task)
+ require 'rails/version'
+ system "cd spec/rails/rails-#{Rails::VERSION::STRING}; rake #{task}"
+ end
+
+ task :after_setup_hook do
+ rails_app_rake "parallel:load_schema"
+ rails_app_rake "parallel:create_cucumber_db"
+ rails_app_rake "parallel:load_schema_cucumber_db"
+ end
+
+ def parallel_tests_setup?
+ require 'rails/version'
+ database_config = File.join "spec", "rails", "rails-#{Rails::VERSION::STRING}", "config", "database.yml"
+ File.read(database_config).include?("TEST_ENV_NUMBER")
+ end
+
+ desc "Setup parallel_tests DBs"
+ task :setup_parallel_tests do
+ unless parallel_tests_setup?
+ puts "parallel_tests is not set up. (Re)building spec/rails/rails-#{Rails::VERSION::STRING} App. Please wait."
+ require 'rails/version'
+ system("rm -Rf spec/rails/rails-#{Rails::VERSION::STRING}")
+ Rake::Task['setup'].invoke
+ end
+ end
+
+ def run_in_parallel(command)
+ system("ENV['TEST_ENV_NUMBER']=#{Parallel.processor_count} #{command}")
+ end
+
+ desc "Run the specs in parallel"
+ task :spec => :setup_parallel_tests do
+ run_in_parallel "parallel_rspec spec/"
+ end
+
+ namespace :spec do
+
+ %w(unit integration).each do |type|
+ desc "Run the #{type} specs in parallel"
+ task type => :setup_parallel_tests do
+ run_in_parallel "parallel_rspec spec/#{type}"
+ end
+ end
+
+ end
+
+ desc "Run the cucumber features in parallel"
+ task :features => :setup_parallel_tests do
+ run_in_parallel "parallel_cucumber features/"
+ end
+
+end
View
@@ -3,10 +3,11 @@ task :setup do
require 'rails/version'
system("mkdir spec/rails") unless File.exists?("spec/rails")
system "bundle exec rails new spec/rails/rails-#{Rails::VERSION::STRING} -m spec/support/rails_template.rb"
+ Rake::Task['parallel:after_setup_hook'].invoke
end
# Run specs and cukes
-desc "Run the full suite"
+desc "Run the full suite using 1 core"
task :test => ['spec:unit', 'spec:integration', 'cucumber', 'cucumber:class_reloading']
namespace :test do

0 comments on commit a53fbc7

Please sign in to comment.