Permalink
Browse files

Always letting DummyApp prepare the test database

Introducing test/integrator.rb which outputs extra integration tests stats (woot!)
  • Loading branch information...
1 parent ac8056a commit 1a89fe8eae76b6143582028238feba38c9839a64 Paul Engel committed Feb 6, 2011
View
@@ -1,6 +1,7 @@
require "rake"
require "rake/testtask"
require "rake/rdoctask"
+require "test/integrator"
begin
require "jeweler"
@@ -27,14 +28,14 @@ task :test do
Rake::Task["test:all"].execute
end
-task :stash do
- Rake::Task["stash:all"].execute
-end
-
task :restore do
Rake::Task["restore:all"].execute
end
+task :stash do
+ Rake::Task["stash:all"].execute
+end
+
namespace :test do
desc "Test the rich_cms plugin in Rails 2 and 3."
task :all do
@@ -57,60 +58,59 @@ namespace :test do
end
desc "Run all integration tests (non-authenticated, with Devise, with Authlogic) in Rails 2 and 3."
task :integration do
- system "rake test:integration:rails-2"
- system "rake test:integration:rails-3"
+ Integrator.run do |test|
+ test.all
+ end
end
namespace :integration do
desc "Run all integration tests (non-authenticated, with Devise, with Authlogic) in Rails 2."
task :"rails-2" do
- system "rake restore:rails-2"
- %w(non_authenticated authenticated/devise_test authenticated/authlogic).each do |file|
- system "ruby test/rails-2/rich_cms/app/integration/#{file}.rb"
+ Integrator.run do |test|
+ test.rails 2
end
end
desc "Run all integration tests (non-authenticated, with Devise, with Authlogic) in Rails 3."
task :"rails-3" do
- system "rake restore:rails-3"
- %w(non_authenticated authenticated/devise_test authenticated/authlogic).each do |file|
- system "ruby test/rails-3/rich_cms/app/integration/#{file}.rb"
+ Integrator.run do |test|
+ test.rails 2
end
end
end
end
-namespace :stash do
- desc "Stash the Rails 2 and 3 dummy apps."
+namespace :restore do
+ desc "Restore the Rails 2 and 3 dummy apps."
task :all do
- system "rake stash:rails-2"
- system "rake stash:rails-3"
+ system "rake restore:rails-2"
+ system "rake restore:rails-3"
end
- desc "Stash the Rails 2 dummy app."
+ desc "Restore the Rails 2 dummy app."
task :"rails-2" do
require "test/rails-2/rich_cms/dummy_app.rb"
- DummyApp.stash_all
+ DummyApp.restore_all
end
- desc "Stash the Rails 3 dummy app."
+ desc "Restore the Rails 3 dummy app."
task :"rails-3" do
require "test/rails-3/rich_cms/dummy_app.rb"
- DummyApp.stash_all
+ DummyApp.restore_all
end
end
-namespace :restore do
- desc "Restore the Rails 2 and 3 dummy apps."
+namespace :stash do
+ desc "Stash the Rails 2 and 3 dummy apps."
task :all do
- system "rake restore:rails-2"
- system "rake restore:rails-3"
+ system "rake stash:rails-2"
+ system "rake stash:rails-3"
end
- desc "Restore the Rails 2 dummy app."
+ desc "Stash the Rails 2 dummy app."
task :"rails-2" do
require "test/rails-2/rich_cms/dummy_app.rb"
- DummyApp.restore_all
+ DummyApp.stash_all
end
- desc "Restore the Rails 3 dummy app."
+ desc "Stash the Rails 3 dummy app."
task :"rails-3" do
require "test/rails-3/rich_cms/dummy_app.rb"
- DummyApp.restore_all
+ DummyApp.stash_all
end
end
@@ -122,4 +122,5 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include "README"
rdoc.rdoc_files.include "MIT-LICENSE"
rdoc.rdoc_files.include "lib/**/*.rb"
-end
+end
+
View
@@ -0,0 +1,80 @@
+STDOUT.sync = true
+
+module Integrator
+ extend self
+
+ def run(&block)
+ tester = Tester.new
+ yield tester
+ tester.summarize
+ end
+
+private
+
+ MAJOR_RAILS_VERSIONS = [2, 3]
+ DESCRIPTION_MATCH = /^Setting up integration test: (.*)$/
+ TIME_MATCH = /^Finished in (.+) seconds\.$/
+ SUMMARY_MATCH = /^(\d+) (\w+), (\d+) (\w+), (\d+) (\w+), (\d+) (\w+)$/
+
+ class Tester
+ def initialize
+ @output = []
+ end
+
+ def test_all
+ MAJOR_RAILS_VERSIONS.each{|version| test_rails version}
+ end
+ alias_method :all, :test_all
+
+ def test_rails(major_version)
+ %w(non_authenticated authenticated/devise_test authenticated/authlogic).each do |file|
+ run "ruby test/rails-#{major_version}/rich_cms/app/integration/#{file}.rb"
+ end
+ end
+ alias_method :rails, :test_rails
+
+ def run(command)
+ IO.popen(command) do |io|
+ until io.eof?
+ puts (@output << io.gets).last
+ end
+ end
+ end
+
+ def summarize
+ integration_tests = @output.inject([]) do |tests, line|
+ if line.match(DESCRIPTION_MATCH)
+ tests << [$1.gsub(DESCRIPTION_MATCH, ""), {}]
+ end
+ if line.match(TIME_MATCH)
+ tests[-1][-1][:time] = $1
+ end
+ if line.match(SUMMARY_MATCH)
+ tests[-1][-1][$2.to_sym] = $1
+ tests[-1][-1][$4.to_sym] = $3
+ tests[-1][-1][$6.to_sym] = $5
+ tests[-1][-1][$8.to_sym] = $7
+ end
+ tests
+ end
+
+ return if integration_tests.size == 0
+
+ keys = [:time, :tests, :assertions, :failures, :errors]
+
+ puts "========================================================="
+ puts "\n"
+ puts "Integration test results"
+ integration_tests.each do |description, stats|
+ puts "---------------------------------------------------------"
+ puts " Description".ljust(18, ".") + ": #{description}"
+ stats.values_at(*keys).each_with_index do |value, index|
+ puts " #{keys[index].to_s.capitalize}".ljust(18, ".") + ": #{value}"
+ end
+ puts "\n"
+ end
+ puts "========================================================="
+ end
+ end
+
+end
@@ -1,9 +1,9 @@
ENV["RAILS_ENV"] = "test"
-require File.expand_path("../rich_cms/dummy_app.rb" , __FILE__)
-DummyApp.restore_all
+require File.expand_path("../rich_cms/dummy_app.rb", __FILE__)
-`cd #{File.expand_path("../dummy", __FILE__)} && rake db:test:load`
+DummyApp.restore_all
+DummyApp.prepare_database
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require "rails/test_help"
@@ -1,6 +1,6 @@
require File.expand_path("../../../../dummy_app.rb", __FILE__)
-DummyApp.setup do |app|
+DummyApp.setup "Authlogic" do |app|
app.generate_cms_admin :authlogic
app.restore_admin_fixtures
app.generate_cms_content
@@ -1,6 +1,6 @@
require File.expand_path("../../../../dummy_app.rb", __FILE__)
-DummyApp.setup do |app|
+DummyApp.setup "Devise" do |app|
app.generate_cms_admin
app.restore_admin_fixtures
app.replace_devise_pepper
@@ -1,6 +1,6 @@
require File.expand_path("../../../dummy_app.rb", __FILE__)
-DummyApp.setup do |app|
+DummyApp.setup "Non authenticated" do |app|
app.generate_cms_content
end
@@ -1,23 +1,29 @@
module DummyApp
extend self
- def setup(stash_files = true, &block)
+ def setup(description, &block)
+ puts "\nSetting up integration test: Rails #{major_rails_version} - #{description}\n\n"
+
restore_all
- stash_all if stash_files
- @prepared = true
+ stash_all
yield self if block_given?
+ prepare_database
+ @prepared = true
+
require File.expand_path("../../test_helper.rb", __FILE__)
end
- def stash_all
- stash "Gemfile", :gemfile
- stash "Gemfile.lock"
- stash "app/models/*.rb"
- stash "config/initializers/devise.rb"
- stash "config/initializers/enrichments.rb"
- stash "config/routes.rb", :routes
- stash "test/fixtures/*_users.yml"
- delete "db/migrate/*.rb"
+ def prepare_database
+ return if @db_prepared
+ if @ran_generator
+ puts "\n"
+ stash "db/schema.rb", :schema
+ run "Purging test database" , "rake db:test:purge"
+ run "Migrating test database", "RAILS_ENV=test rake db:migrate"
+ else
+ run "Loading test database", "rake db:test:load"
+ end
+ @db_prepared = true
end
def restore_all(force = nil)
@@ -36,13 +42,19 @@ def restore_all(force = nil)
restore "**/*.#{STASHED_EXT}"
end
- def restore_admin_fixtures
- puts "\n"
- restore "test/fixtures/*.yml.#{STASHED_EXT}"
+ def stash_all
+ stash "Gemfile", :gemfile
+ stash "Gemfile.lock"
+ stash "app/models/*.rb"
+ stash "config/initializers/devise.rb"
+ stash "config/initializers/enrichments.rb"
+ stash "config/routes.rb", :routes
+ stash "test/fixtures/*_users.yml"
+ delete "db/migrate/*.rb"
end
def generate_cms_admin(logic = :devise)
- logic_option = {:devise => "d", :authlogic => "a"}[logic]
+ logic_option = {:devise => "d", :authlogic => "a"}[@logic = logic]
if logic_option
klass = "#{logic.to_s.capitalize}User"
@@ -55,6 +67,13 @@ def generate_cms_admin(logic = :devise)
"rails g rich:cms_admin #{klass} -#{logic_option}"
end
end
+
+ @ran_generator = true
+ end
+
+ def restore_admin_fixtures
+ puts "\n"
+ restore "test/fixtures/#{@logic}_users.yml.#{STASHED_EXT}"
end
def replace_devise_pepper
@@ -79,6 +98,8 @@ def generate_cms_content
when 3
"rails g rich:cms_content #{klass}"
end
+
+ @ran_generator = true
end
private
@@ -109,16 +130,6 @@ def stashed(file)
"#{file}.#{STASHED_EXT}"
end
- def stash(string, replacement = nil)
- Dir[expand_path(string)].each do |file|
- unless File.exists?(stashed(file))
- puts "Stashing #{target(file).inspect}"
- File.rename target(file), stashed(file)
- replace(file, replacement)
- end
- end
- end
-
def restore(string)
Dir[expand_path(string)].each do |file|
if File.exists?(stashed(file))
@@ -129,6 +140,16 @@ def restore(string)
end
end
+ def stash(string, replacement = nil)
+ Dir[expand_path(string)].each do |file|
+ unless File.exists?(stashed(file))
+ puts "Stashing #{target(file).inspect}"
+ File.rename target(file), stashed(file)
+ replace(file, replacement)
+ end
+ end
+ end
+
def delete(string)
Dir[expand_path(string)].each do |file|
puts "Deleting #{file.inspect}"
@@ -164,6 +185,11 @@ def replace(file, replacement)
gem "capybara"
gem "launchy"
CONTENT
+ when :schema
+ <<-CONTENT.gsub(/^ {18}/, "")
+ ActiveRecord::Schema.define(:version => 19820801180828) do
+ end
+ CONTENT
when :routes
case major_rails_version
when 2

0 comments on commit 1a89fe8

Please sign in to comment.