2 require 'rake/rdoctask'
7 desc 'Generate documentation for the engines plugin.'
8 Rake::RDocTask.new(:doc) do |doc|
12 doc.rdoc_files.include("README", "CHANGELOG", "MIT-LICENSE")
13 doc.rdoc_files.include('lib/**/*.rb')
14 doc.options << '--line-numbers' << '--inline-source'
17 desc 'Run the engine plugin tests within their test harness'
19 # checkout the project into a temporary directory
21 test_dir = "#{Dir.tmpdir}/engines_plugin_#{version}_test"
22 puts "Checking out test harness for #{version} into #{test_dir}"
23 `svn co http://svn.rails-engines.org/test/engines/#{version} #{test_dir}`
25 # run all the tests in this project
28 puts "Running all tests in test harness"
29 ['db:migrate', 'test', 'test:plugins'].each do |t|
34 task :clean => [:clobber_doc, "test:clean"]
38 # Yields a block with STDOUT and STDERR silenced. If you *really* want
39 # to output something, the block is yielded with the original output
43 # puts 'hello!' # no output produced
44 # o.puts 'hello!' # output on STDOUT
47 # (based on silence_stream in ActiveSupport.)
49 yield(STDOUT, STDERR) if ENV['VERBOSE']
50 streams = [STDOUT, STDERR]
51 actual_stdout = STDOUT.dup
52 actual_stderr = STDERR.dup
54 s.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
57 yield actual_stdout, actual_stderr
59 STDOUT.reopen(actual_stdout)
60 STDERR.reopen(actual_stderr)
64 File.join(File.dirname(__FILE__), 'test_app')
68 cmd = cmd.join(" && ") if cmd.is_a?(Array)
69 system(cmd) || raise("failed running '#{cmd}'")
72 desc 'Remove the test application'
74 FileUtils.rm_r(test_app_dir) if File.exist?(test_app_dir)
77 desc 'Build the test rails application (use RAILS=[edge,<directory>] to test against specific version)'
80 out.puts "> Creating test application at #{test_app_dir}"
83 vendor_dir = File.join(test_app_dir, 'vendor')
84 FileUtils.mkdir_p vendor_dir
86 if ENV['RAILS'] == 'edge'
87 out.puts " Cloning Edge Rails from GitHub"
88 run "cd #{vendor_dir} && git clone --depth 1 git://github.com/rails/rails.git"
89 elsif ENV['RAILS'] =~ /\d\.\d\.\d/
91 out.puts " Cloning Rails Tag #{ENV['RAILS']} from GitHub using curl and tar"
92 run ["cd #{vendor_dir}",
95 "curl -s -L http://github.com/rails/rails/tarball/#{ENV['RAILS']} | tar xzv --strip-components 1"]
97 out.puts " Cloning Rails Tag #{ENV['RAILS']} from GitHub (can be slow - set CURL=true to use curl)"
98 run ["cd #{vendor_dir}",
99 "git clone git://github.com/rails/rails.git",
102 "git checkout v#{ENV['RAILS']}"]
104 elsif File.exist?(ENV['RAILS'])
105 out.puts " Linking rails from #{ENV['RAILS']}"
106 run "cd #{vendor_dir} && ln -s #{ENV['RAILS']} rails"
108 raise "Couldn't build test application from '#{ENV['RAILS']}'"
111 out.puts " generating rails default directory structure"
112 run "ruby #{File.join(vendor_dir, 'rails', 'railties', 'bin', 'rails')} #{test_app_dir}"
114 version = `rails --version`.chomp.split.last
115 out.puts " building rails using the 'rails' command (rails version: #{version})"
116 run "rails #{test_app_dir}"
119 # get the database config and schema in place
120 out.puts " writing database.yml"
122 File.open(File.join(test_app_dir, 'config', 'database.yml'), 'w') do |f|
123 f.write(%w(development test).inject({}) do |h, env|
124 h[env] = {"adapter" => "sqlite3", "database" => "engines_#{env}.sqlite3"} ; h
127 out.puts " installing exception_notification plugin"
128 run "cd #{test_app_dir} && ./script/plugin install git://github.com/rails/exception_notification.git"
132 # We can't link the plugin, as it needs to be present for script/generate to find
133 # the plugin generator.
134 # TODO: find and +1/create issue for loading generators from symlinked plugins
135 desc 'Mirror the engines plugin into the test application'
136 task :copy_engines_plugin do
137 puts "> Copying engines plugin into test application"
138 engines_plugin = File.join(test_app_dir, "vendor", "plugins", "engines")
139 FileUtils.rm_r(engines_plugin) if File.exist?(engines_plugin)
140 FileUtils.mkdir_p(engines_plugin)
141 FileList["*"].exclude("test_app").each do |file|
142 FileUtils.cp_r(file, engines_plugin)
146 def insert_line(line, options)
148 target_file = File.join(test_app_dir, options[:into])
149 lines = File.readlines(target_file)
150 return if lines.include?(line)
153 if options[:after].is_a?(String)
154 after_line = options[:after] + "\n"
156 after_line = lines.find { |l| l =~ options[:after] }
157 raise "couldn't find a line matching #{options[:after].inspect} in #{target_file}" unless after_line
159 index = lines.index(after_line)
160 raise "couldn't find line '#{after_line}' in #{target_file}" unless index
161 lines.insert(index + 1, line)
165 File.open(target_file, 'w') { |f| f.write lines.join }
168 def mirror_test_files(src, dest=nil)
169 destination_dir = File.join(*([test_app_dir, dest].compact))
170 FileUtils.cp_r(File.join(File.dirname(__FILE__), 'test', src), destination_dir)
173 desc 'Update the plugin and tests files in the test application from the plugin'
174 task :mirror_engine_files => [:test_app, :copy_engines_plugin] do
175 puts "> Tweaking generated application to be suitable for testing"
177 # Replace the Rails plugin loader with the engines one.
178 insert_line("require File.join(File.dirname(__FILE__), '../vendor/plugins/engines/boot')",
179 :into => 'config/environment.rb',
180 :after => "require File.join(File.dirname(__FILE__), 'boot')")
182 # Add the engines test helper to handle fixtures & stuff.
183 insert_line("require 'engines_test_helper'", :into => 'test/test_helper.rb')
185 # Run engine plugin tests when running the application
186 insert_line("task :test => ['test:engines:all']", :into => 'Rakefile')
188 # We want exceptions to be raised
189 insert_line("def rescue_action(e) raise e end;",
190 :into => "app/controllers/application_controller.rb",
191 :after => "class ApplicationController < ActionController::Base")
193 # We need this method to test where actions are being rendered from.
194 insert_line("include RenderInformation",
195 :into => "app/controllers/application_controller.rb",
196 :after => "class ApplicationController < ActionController::Base")
198 puts "> Mirroring test application files into #{test_app_dir}"
199 mirror_test_files('app')
200 mirror_test_files('lib')
201 mirror_test_files('plugins', 'vendor')
202 mirror_test_files('unit', 'test')
203 mirror_test_files('functional', 'test')
206 desc 'Prepare the engines test environment'
208 version_tag = File.join(test_app_dir, 'RAILS_VERSION')
209 existing_version = File.read(version_tag).chomp rescue 'unknown'
210 if existing_version == ENV['RAILS']
211 puts "> Reusing existing test application (#{ENV['RAILS']})"
213 puts "> Recreating test application"
214 Rake::Task["test:clean"].invoke
215 Rake::Task["test:generate_app"].invoke
217 File.open(version_tag, "w") { |f| f.write ENV['RAILS'] }
222 task :test => "test:mirror_engine_files" do
223 puts "> Loading the test application environment and running tests"
224 # We use exec here to replace the current running rake process
225 exec("cd #{test_app_dir} && rake db:migrate && rake")