annotate .svn/pristine/78/7887ac0add3adb74049da4eb63c9169b67eaf06b.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 require 'rake'
Chris@909 2 require 'rake/rdoctask'
Chris@909 3 require 'tmpdir'
Chris@909 4
Chris@909 5 task :default => :doc
Chris@909 6
Chris@909 7 desc 'Generate documentation for the engines plugin.'
Chris@909 8 Rake::RDocTask.new(:doc) do |doc|
Chris@909 9 doc.rdoc_dir = 'doc'
Chris@909 10 doc.title = 'Engines'
Chris@909 11 doc.main = "README"
Chris@909 12 doc.rdoc_files.include("README", "CHANGELOG", "MIT-LICENSE")
Chris@909 13 doc.rdoc_files.include('lib/**/*.rb')
Chris@909 14 doc.options << '--line-numbers' << '--inline-source'
Chris@909 15 end
Chris@909 16
Chris@909 17 desc 'Run the engine plugin tests within their test harness'
Chris@909 18 task :cruise do
Chris@909 19 # checkout the project into a temporary directory
Chris@909 20 version = "rails_2.0"
Chris@909 21 test_dir = "#{Dir.tmpdir}/engines_plugin_#{version}_test"
Chris@909 22 puts "Checking out test harness for #{version} into #{test_dir}"
Chris@909 23 `svn co http://svn.rails-engines.org/test/engines/#{version} #{test_dir}`
Chris@909 24
Chris@909 25 # run all the tests in this project
Chris@909 26 Dir.chdir(test_dir)
Chris@909 27 load 'Rakefile'
Chris@909 28 puts "Running all tests in test harness"
Chris@909 29 ['db:migrate', 'test', 'test:plugins'].each do |t|
Chris@909 30 Rake::Task[t].invoke
Chris@909 31 end
Chris@909 32 end
Chris@909 33
Chris@909 34 task :clean => [:clobber_doc, "test:clean"]
Chris@909 35
Chris@909 36 namespace :test do
Chris@909 37
Chris@909 38 # Yields a block with STDOUT and STDERR silenced. If you *really* want
Chris@909 39 # to output something, the block is yielded with the original output
Chris@909 40 # streams, i.e.
Chris@909 41 #
Chris@909 42 # silence do |o, e|
Chris@909 43 # puts 'hello!' # no output produced
Chris@909 44 # o.puts 'hello!' # output on STDOUT
Chris@909 45 # end
Chris@909 46 #
Chris@909 47 # (based on silence_stream in ActiveSupport.)
Chris@909 48 def silence
Chris@909 49 yield(STDOUT, STDERR) if ENV['VERBOSE']
Chris@909 50 streams = [STDOUT, STDERR]
Chris@909 51 actual_stdout = STDOUT.dup
Chris@909 52 actual_stderr = STDERR.dup
Chris@909 53 streams.each do |s|
Chris@909 54 s.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
Chris@909 55 s.sync = true
Chris@909 56 end
Chris@909 57 yield actual_stdout, actual_stderr
Chris@909 58 ensure
Chris@909 59 STDOUT.reopen(actual_stdout)
Chris@909 60 STDERR.reopen(actual_stderr)
Chris@909 61 end
Chris@909 62
Chris@909 63 def test_app_dir
Chris@909 64 File.join(File.dirname(__FILE__), 'test_app')
Chris@909 65 end
Chris@909 66
Chris@909 67 def run(cmd)
Chris@909 68 cmd = cmd.join(" && ") if cmd.is_a?(Array)
Chris@909 69 system(cmd) || raise("failed running '#{cmd}'")
Chris@909 70 end
Chris@909 71
Chris@909 72 desc 'Remove the test application'
Chris@909 73 task :clean do
Chris@909 74 FileUtils.rm_r(test_app_dir) if File.exist?(test_app_dir)
Chris@909 75 end
Chris@909 76
Chris@909 77 desc 'Build the test rails application (use RAILS=[edge,<directory>] to test against specific version)'
Chris@909 78 task :generate_app do
Chris@909 79 silence do |out, err|
Chris@909 80 out.puts "> Creating test application at #{test_app_dir}"
Chris@909 81
Chris@909 82 if ENV['RAILS']
Chris@909 83 vendor_dir = File.join(test_app_dir, 'vendor')
Chris@909 84 FileUtils.mkdir_p vendor_dir
Chris@909 85
Chris@909 86 if ENV['RAILS'] == 'edge'
Chris@909 87 out.puts " Cloning Edge Rails from GitHub"
Chris@909 88 run "cd #{vendor_dir} && git clone --depth 1 git://github.com/rails/rails.git"
Chris@909 89 elsif ENV['RAILS'] =~ /\d\.\d\.\d/
Chris@909 90 if ENV['CURL']
Chris@909 91 out.puts " Cloning Rails Tag #{ENV['RAILS']} from GitHub using curl and tar"
Chris@909 92 run ["cd #{vendor_dir}",
Chris@909 93 "mkdir rails",
Chris@909 94 "cd rails",
Chris@909 95 "curl -s -L http://github.com/rails/rails/tarball/#{ENV['RAILS']} | tar xzv --strip-components 1"]
Chris@909 96 else
Chris@909 97 out.puts " Cloning Rails Tag #{ENV['RAILS']} from GitHub (can be slow - set CURL=true to use curl)"
Chris@909 98 run ["cd #{vendor_dir}",
Chris@909 99 "git clone git://github.com/rails/rails.git",
Chris@909 100 "cd rails",
Chris@909 101 "git pull",
Chris@909 102 "git checkout v#{ENV['RAILS']}"]
Chris@909 103 end
Chris@909 104 elsif File.exist?(ENV['RAILS'])
Chris@909 105 out.puts " Linking rails from #{ENV['RAILS']}"
Chris@909 106 run "cd #{vendor_dir} && ln -s #{ENV['RAILS']} rails"
Chris@909 107 else
Chris@909 108 raise "Couldn't build test application from '#{ENV['RAILS']}'"
Chris@909 109 end
Chris@909 110
Chris@909 111 out.puts " generating rails default directory structure"
Chris@909 112 run "ruby #{File.join(vendor_dir, 'rails', 'railties', 'bin', 'rails')} #{test_app_dir}"
Chris@909 113 else
Chris@909 114 version = `rails --version`.chomp.split.last
Chris@909 115 out.puts " building rails using the 'rails' command (rails version: #{version})"
Chris@909 116 run "rails #{test_app_dir}"
Chris@909 117 end
Chris@909 118
Chris@909 119 # get the database config and schema in place
Chris@909 120 out.puts " writing database.yml"
Chris@909 121 require 'yaml'
Chris@909 122 File.open(File.join(test_app_dir, 'config', 'database.yml'), 'w') do |f|
Chris@909 123 f.write(%w(development test).inject({}) do |h, env|
Chris@909 124 h[env] = {"adapter" => "sqlite3", "database" => "engines_#{env}.sqlite3"} ; h
Chris@909 125 end.to_yaml)
Chris@909 126 end
Chris@909 127 out.puts " installing exception_notification plugin"
Chris@909 128 run "cd #{test_app_dir} && ./script/plugin install git://github.com/rails/exception_notification.git"
Chris@909 129 end
Chris@909 130 end
Chris@909 131
Chris@909 132 # We can't link the plugin, as it needs to be present for script/generate to find
Chris@909 133 # the plugin generator.
Chris@909 134 # TODO: find and +1/create issue for loading generators from symlinked plugins
Chris@909 135 desc 'Mirror the engines plugin into the test application'
Chris@909 136 task :copy_engines_plugin do
Chris@909 137 puts "> Copying engines plugin into test application"
Chris@909 138 engines_plugin = File.join(test_app_dir, "vendor", "plugins", "engines")
Chris@909 139 FileUtils.rm_r(engines_plugin) if File.exist?(engines_plugin)
Chris@909 140 FileUtils.mkdir_p(engines_plugin)
Chris@909 141 FileList["*"].exclude("test_app").each do |file|
Chris@909 142 FileUtils.cp_r(file, engines_plugin)
Chris@909 143 end
Chris@909 144 end
Chris@909 145
Chris@909 146 def insert_line(line, options)
Chris@909 147 line = line + "\n"
Chris@909 148 target_file = File.join(test_app_dir, options[:into])
Chris@909 149 lines = File.readlines(target_file)
Chris@909 150 return if lines.include?(line)
Chris@909 151
Chris@909 152 if options[:after]
Chris@909 153 if options[:after].is_a?(String)
Chris@909 154 after_line = options[:after] + "\n"
Chris@909 155 else
Chris@909 156 after_line = lines.find { |l| l =~ options[:after] }
Chris@909 157 raise "couldn't find a line matching #{options[:after].inspect} in #{target_file}" unless after_line
Chris@909 158 end
Chris@909 159 index = lines.index(after_line)
Chris@909 160 raise "couldn't find line '#{after_line}' in #{target_file}" unless index
Chris@909 161 lines.insert(index + 1, line)
Chris@909 162 else
Chris@909 163 lines << line
Chris@909 164 end
Chris@909 165 File.open(target_file, 'w') { |f| f.write lines.join }
Chris@909 166 end
Chris@909 167
Chris@909 168 def mirror_test_files(src, dest=nil)
Chris@909 169 destination_dir = File.join(*([test_app_dir, dest].compact))
Chris@909 170 FileUtils.cp_r(File.join(File.dirname(__FILE__), 'test', src), destination_dir)
Chris@909 171 end
Chris@909 172
Chris@909 173 desc 'Update the plugin and tests files in the test application from the plugin'
Chris@909 174 task :mirror_engine_files => [:test_app, :copy_engines_plugin] do
Chris@909 175 puts "> Tweaking generated application to be suitable for testing"
Chris@909 176
Chris@909 177 # Replace the Rails plugin loader with the engines one.
Chris@909 178 insert_line("require File.join(File.dirname(__FILE__), '../vendor/plugins/engines/boot')",
Chris@909 179 :into => 'config/environment.rb',
Chris@909 180 :after => "require File.join(File.dirname(__FILE__), 'boot')")
Chris@909 181
Chris@909 182 # Add the engines test helper to handle fixtures & stuff.
Chris@909 183 insert_line("require 'engines_test_helper'", :into => 'test/test_helper.rb')
Chris@909 184
Chris@909 185 # Run engine plugin tests when running the application
Chris@909 186 insert_line("task :test => ['test:engines:all']", :into => 'Rakefile')
Chris@909 187
Chris@909 188 # We want exceptions to be raised
Chris@909 189 insert_line("def rescue_action(e) raise e end;",
Chris@909 190 :into => "app/controllers/application_controller.rb",
Chris@909 191 :after => "class ApplicationController < ActionController::Base")
Chris@909 192
Chris@909 193 # We need this method to test where actions are being rendered from.
Chris@909 194 insert_line("include RenderInformation",
Chris@909 195 :into => "app/controllers/application_controller.rb",
Chris@909 196 :after => "class ApplicationController < ActionController::Base")
Chris@909 197
Chris@909 198 puts "> Mirroring test application files into #{test_app_dir}"
Chris@909 199 mirror_test_files('app')
Chris@909 200 mirror_test_files('lib')
Chris@909 201 mirror_test_files('plugins', 'vendor')
Chris@909 202 mirror_test_files('unit', 'test')
Chris@909 203 mirror_test_files('functional', 'test')
Chris@909 204 end
Chris@909 205
Chris@909 206 desc 'Prepare the engines test environment'
Chris@909 207 task :test_app do
Chris@909 208 version_tag = File.join(test_app_dir, 'RAILS_VERSION')
Chris@909 209 existing_version = File.read(version_tag).chomp rescue 'unknown'
Chris@909 210 if existing_version == ENV['RAILS']
Chris@909 211 puts "> Reusing existing test application (#{ENV['RAILS']})"
Chris@909 212 else
Chris@909 213 puts "> Recreating test application"
Chris@909 214 Rake::Task["test:clean"].invoke
Chris@909 215 Rake::Task["test:generate_app"].invoke
Chris@909 216
Chris@909 217 File.open(version_tag, "w") { |f| f.write ENV['RAILS'] }
Chris@909 218 end
Chris@909 219 end
Chris@909 220 end
Chris@909 221
Chris@909 222 task :test => "test:mirror_engine_files" do
Chris@909 223 puts "> Loading the test application environment and running tests"
Chris@909 224 # We use exec here to replace the current running rake process
Chris@909 225 exec("cd #{test_app_dir} && rake db:migrate && rake")
Chris@909 226 end