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