Module: TestHelpers::Generation
- Included in:
- ActiveSupport::TestCase
- Defined in:
- lib/isolation/abstract_unit.rb
Defined Under Namespace
Classes: Bukkit
Instance Method Summary collapse
- #add_to_config(str) ⇒ Object
- #add_to_env_config(env, str) ⇒ Object
- #app_file(path, contents) ⇒ Object
- #boot_rails ⇒ Object
-
#build_app(options = {}) ⇒ Object
Build an application by invoking the generator and going through the whole stack.
- #controller(name, contents) ⇒ Object
- #engine(name) ⇒ Object
-
#make_basic_app {|app| ... } ⇒ Object
Make a very basic app, without creating the whole directory structure.
- #remove_file(path) ⇒ Object
- #remove_from_config(str) ⇒ Object
- #script(script) ⇒ Object
- #simple_controller ⇒ Object
- #teardown_app ⇒ Object
- #use_frameworks(arr) ⇒ Object
Instance Method Details
#add_to_config(str) ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/isolation/abstract_unit.rb', line 204 def add_to_config(str) environment = File.read("#{app_path}/config/application.rb") if environment =~ /(\n\s*end\s*end\s*)\Z/ File.open("#{app_path}/config/application.rb", 'w') do |f| f.puts $` + "\n#{str}\n" + $1 end end end |
#add_to_env_config(env, str) ⇒ Object
213 214 215 216 217 218 219 220 |
# File 'lib/isolation/abstract_unit.rb', line 213 def add_to_env_config(env, str) environment = File.read("#{app_path}/config/environments/#{env}.rb") if environment =~ /(\n\s*end\s*)\Z/ File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f| f.puts $` + "\n#{str}\n" + $1 end end end |
#app_file(path, contents) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/isolation/abstract_unit.rb', line 229 def app_file(path, contents) FileUtils.mkdir_p File.dirname("#{app_path}/#{path}") File.open("#{app_path}/#{path}", 'w') do |f| f.puts contents end end |
#boot_rails ⇒ Object
253 254 255 |
# File 'lib/isolation/abstract_unit.rb', line 253 def boot_rails require File.('../load_paths', __FILE__) end |
#build_app(options = {}) ⇒ Object
Build an application by invoking the generator and going through the whole stack.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/isolation/abstract_unit.rb', line 93 def build_app( = {}) @prev_rails_env = ENV['RAILS_ENV'] ENV['RAILS_ENV'] = 'development' FileUtils.rm_rf(app_path) FileUtils.cp_r(tmp_path('app_template'), app_path) # Delete the initializers unless requested unless [:initializers] Dir["#{app_path}/config/initializers/*.rb"].each do |initializer| File.delete(initializer) end end unless [:gemfile] File.delete"#{app_path}/Gemfile" end routes = File.read("#{app_path}/config/routes.rb") if routes =~ /(\n\s*end\s*)\Z/ File.open("#{app_path}/config/routes.rb", 'w') do |f| f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)'\n" + $1 end end add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"; config.active_support.deprecation = :log' end |
#controller(name, contents) ⇒ Object
240 241 242 |
# File 'lib/isolation/abstract_unit.rb', line 240 def controller(name, contents) app_file("app/controllers/#{name}_controller.rb", contents) end |
#engine(name) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/isolation/abstract_unit.rb', line 181 def engine(name) dir = "#{app_path}/random/#{name}" FileUtils.mkdir_p(dir) app = File.readlines("#{app_path}/config/application.rb") app.insert(2, "$:.unshift(\"#{dir}/lib\")") app.insert(3, "require #{name.inspect}") File.open("#{app_path}/config/application.rb", 'r+') do |f| f.puts app end Bukkit.new(dir).tap do |bukkit| yield bukkit if block_given? end end |
#make_basic_app {|app| ... } ⇒ Object
Make a very basic app, without creating the whole directory structure. This is faster and simpler than the method above.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/isolation/abstract_unit.rb', line 127 def make_basic_app require "rails" require "action_controller/railtie" app = Class.new(Rails::Application) app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4" app.config.session_store :cookie_store, :key => "_myapp_session" app.config.active_support.deprecation = :log yield app if block_given? app.initialize! app.routes.draw do match "/" => "omg#index" end require 'rack/test' extend ::Rack::Test::Methods end |
#remove_file(path) ⇒ Object
236 237 238 |
# File 'lib/isolation/abstract_unit.rb', line 236 def remove_file(path) FileUtils.rm_rf "#{app_path}/#{path}" end |
#remove_from_config(str) ⇒ Object
222 223 224 225 226 227 |
# File 'lib/isolation/abstract_unit.rb', line 222 def remove_from_config(str) file = "#{app_path}/config/application.rb" contents = File.read(file) contents.sub!(/#{str}/, "") File.open(file, "w+") { |f| f.puts contents } end |
#script(script) ⇒ Object
198 199 200 201 202 |
# File 'lib/isolation/abstract_unit.rb', line 198 def script(script) Dir.chdir(app_path) do `#{Gem.ruby} #{app_path}/script/rails #{script}` end end |
#simple_controller ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/isolation/abstract_unit.rb', line 147 def simple_controller controller :foo, <<-RUBY class FooController < ApplicationController def index render :text => "foo" end end RUBY app_file 'config/routes.rb', <<-RUBY AppTemplate::Application.routes.draw do match ':controller(/:action)' end RUBY end |
#teardown_app ⇒ Object
121 122 123 |
# File 'lib/isolation/abstract_unit.rb', line 121 def teardown_app ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env end |
#use_frameworks(arr) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/isolation/abstract_unit.rb', line 244 def use_frameworks(arr) to_remove = [:actionmailer, :activemodel, :activerecord, :activeresource] - arr remove_from_config "config.active_record.identity_map = true" if to_remove.include? :activerecord $:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' } end |