OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / vendor / plugins / engines / Rakefile
1 require 'rake'
2 require 'rake/rdoctask'
3 require 'tmpdir'
4
5 task :default => :doc
6
7 desc 'Generate documentation for the engines plugin.'
8 Rake::RDocTask.new(:doc) do |doc|
9   doc.rdoc_dir = 'doc'
10   doc.title    = 'Engines'
11   doc.main     = "README"
12   doc.rdoc_files.include("README", "CHANGELOG", "MIT-LICENSE")
13   doc.rdoc_files.include('lib/**/*.rb')
14   doc.options << '--line-numbers' << '--inline-source'
15 end
16
17 desc 'Run the engine plugin tests within their test harness'
18 task :cruise do
19   # checkout the project into a temporary directory
20   version = "rails_2.0"
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}`
24
25   # run all the tests in this project
26   Dir.chdir(test_dir)
27   load 'Rakefile'
28   puts "Running all tests in test harness"
29   ['db:migrate', 'test', 'test:plugins'].each do |t|
30     Rake::Task[t].invoke
31   end  
32 end
33
34 task :clean => [:clobber_doc, "test:clean"]
35
36 namespace :test do
37   
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
40   # streams, i.e.
41   #
42   #   silence do |o, e|
43   #     puts 'hello!' # no output produced
44   #     o.puts 'hello!' # output on STDOUT
45   #   end
46   #
47   # (based on silence_stream in ActiveSupport.)
48   def silence
49     yield(STDOUT, STDERR) if ENV['VERBOSE']
50     streams = [STDOUT, STDERR]
51     actual_stdout = STDOUT.dup
52     actual_stderr = STDERR.dup
53     streams.each do |s| 
54       s.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null') 
55       s.sync = true
56     end
57     yield actual_stdout, actual_stderr
58   ensure
59     STDOUT.reopen(actual_stdout)
60     STDERR.reopen(actual_stderr)
61   end
62   
63   def test_app_dir
64     File.join(File.dirname(__FILE__), 'test_app')
65   end
66   
67   def run(cmd)
68     cmd = cmd.join(" && ") if cmd.is_a?(Array)
69     system(cmd) || raise("failed running '#{cmd}'")
70   end
71   
72   desc 'Remove the test application'
73   task :clean do
74     FileUtils.rm_r(test_app_dir) if File.exist?(test_app_dir)
75   end
76   
77   desc 'Build the test rails application (use RAILS=[edge,<directory>] to test against specific version)'
78   task :generate_app do
79     silence do |out, err|
80       out.puts "> Creating test application at #{test_app_dir}"
81         
82       if ENV['RAILS']
83         vendor_dir = File.join(test_app_dir, 'vendor')
84         FileUtils.mkdir_p vendor_dir
85         
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/
90           if ENV['CURL']
91             out.puts "    Cloning Rails Tag #{ENV['RAILS']} from GitHub using curl and tar"
92             run ["cd #{vendor_dir}",
93                  "mkdir rails",
94                  "cd rails",
95                  "curl -s -L http://github.com/rails/rails/tarball/#{ENV['RAILS']} | tar xzv --strip-components 1"]
96           else
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",
100                  "cd rails",
101                  "git pull",
102                  "git checkout v#{ENV['RAILS']}"]
103           end
104         elsif File.exist?(ENV['RAILS'])
105           out.puts "    Linking rails from #{ENV['RAILS']}"
106           run "cd #{vendor_dir} && ln -s #{ENV['RAILS']} rails"
107         else
108           raise "Couldn't build test application from '#{ENV['RAILS']}'"
109         end
110       
111         out.puts "    generating rails default directory structure"
112         run "ruby #{File.join(vendor_dir, 'rails', 'railties', 'bin', 'rails')} #{test_app_dir}"
113       else
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}"
117       end
118     
119       # get the database config and schema in place
120       out.puts "    writing database.yml"
121       require 'yaml'
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
125         end.to_yaml)
126       end
127       out.puts "    installing exception_notification plugin"
128       run "cd #{test_app_dir} && ./script/plugin install git://github.com/rails/exception_notification.git"
129     end
130   end
131   
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)
143     end
144   end
145   
146   def insert_line(line, options)
147     line = line + "\n"
148     target_file = File.join(test_app_dir, options[:into])
149     lines = File.readlines(target_file)
150     return if lines.include?(line)
151     
152     if options[:after]
153       if options[:after].is_a?(String)
154         after_line = options[:after] + "\n"
155       else
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
158       end
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)
162     else
163       lines << line
164     end
165     File.open(target_file, 'w') { |f| f.write lines.join }
166   end
167   
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)
171   end
172   
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"
176     
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')")
181     
182     # Add the engines test helper to handle fixtures & stuff.
183     insert_line("require 'engines_test_helper'", :into => 'test/test_helper.rb')
184     
185     # Run engine plugin tests when running the application 
186     insert_line("task :test => ['test:engines:all']", :into => 'Rakefile')
187     
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")
192     
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")
197     
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')
204   end
205   
206   desc 'Prepare the engines test environment'
207   task :test_app do
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']})"
212     else
213       puts "> Recreating test application"
214       Rake::Task["test:clean"].invoke
215       Rake::Task["test:generate_app"].invoke
216       
217       File.open(version_tag, "w") { |f| f.write ENV['RAILS'] }
218     end
219   end
220 end
221
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")
226 end