isolation_test 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Isolation
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Isolation'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task :default => 'test:isolated'
38
+ # task :default => :test
39
+ # task :test => 'test:isolated'
40
+
41
+ ## This is required until the regular test task
42
+ ## below passes. It's not ideal, but at least
43
+ ## we can see the failures
44
+ namespace :test do
45
+ task :isolated do
46
+ dir = ENV["TEST_DIR"] || "**"
47
+ Dir["test/#{dir}/*_test.rb"].each do |file|
48
+ next true if file.include?("fixtures")
49
+ dash_i = [
50
+ 'test',
51
+ 'lib',
52
+ # "#{File.dirname(__FILE__)}/../activesupport/lib",
53
+ # "#{File.dirname(__FILE__)}/../actionpack/lib"
54
+ ]
55
+ ruby "-I#{dash_i.join ':'}", file
56
+ end
57
+ end
58
+ end
data/lib/isolation.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Isolation
2
+
3
+ end
@@ -0,0 +1,289 @@
1
+ # Note:
2
+ # It is important to keep this file as light as possible
3
+ # the goal for tests that require this is to test booting up
4
+ # rails from an empty state, so anything added here could
5
+ # hide potential failures
6
+ #
7
+ # It is also good to know what is the bare minimum to get
8
+ # Rails booted up.
9
+ require 'fileutils'
10
+
11
+ require 'rubygems'
12
+ require 'minitest/autorun'
13
+ require 'active_support/test_case'
14
+
15
+ RAILS_BIN = Gem.bin_path('rails', 'rails')
16
+
17
+ # These files do not require any others and are needed
18
+ # to run the tests
19
+ require 'active_support/testing/isolation'
20
+ require 'active_support/testing/declarative'
21
+ require 'active_support/core_ext/kernel/reporting'
22
+
23
+ module TestHelpers
24
+ module Paths
25
+ module_function
26
+
27
+ TMP_PATH = File.expand_path(File.join(File.dirname(__FILE__), *%w[.. .. tmp]))
28
+
29
+ def tmp_path(*args)
30
+ File.join(TMP_PATH, *args)
31
+ end
32
+
33
+ def app_path(*args)
34
+ tmp_path(*%w[app] + args)
35
+ end
36
+
37
+ def framework_path
38
+ File.join(Gem.loaded_specs['rails'].full_gem_path, 'lib')
39
+ end
40
+
41
+ def rails_root
42
+ app_path
43
+ end
44
+ end
45
+
46
+ module Rack
47
+ def app(env = "production")
48
+ old_env = ENV["RAILS_ENV"]
49
+ @app ||= begin
50
+ ENV["RAILS_ENV"] = env
51
+ require "#{app_path}/config/environment"
52
+ Rails.application
53
+ end
54
+ ensure
55
+ ENV["RAILS_ENV"] = old_env
56
+ end
57
+
58
+ def extract_body(response)
59
+ "".tap do |body|
60
+ response[2].each {|chunk| body << chunk }
61
+ end
62
+ end
63
+
64
+ def get(path)
65
+ @app.call(::Rack::MockRequest.env_for(path))
66
+ end
67
+
68
+ def assert_welcome(resp)
69
+ assert_equal 200, resp[0]
70
+ assert resp[1]["Content-Type"] = "text/html"
71
+ assert extract_body(resp).match(/Welcome aboard/)
72
+ end
73
+
74
+ def assert_success(resp)
75
+ assert_equal 202, resp[0]
76
+ end
77
+
78
+ def assert_missing(resp)
79
+ assert_equal 404, resp[0]
80
+ end
81
+
82
+ def assert_header(key, value, resp)
83
+ assert_equal value, resp[1][key.to_s]
84
+ end
85
+
86
+ def assert_body(expected, resp)
87
+ assert_equal expected, extract_body(resp)
88
+ end
89
+ end
90
+
91
+ module Generation
92
+ # Build an application by invoking the generator and going through the whole stack.
93
+ def build_app(options = {})
94
+ @prev_rails_env = ENV['RAILS_ENV']
95
+ ENV['RAILS_ENV'] = 'development'
96
+
97
+ FileUtils.rm_rf(app_path)
98
+ FileUtils.cp_r(tmp_path('app_template'), app_path)
99
+
100
+ # Delete the initializers unless requested
101
+ unless options[:initializers]
102
+ Dir["#{app_path}/config/initializers/*.rb"].each do |initializer|
103
+ File.delete(initializer)
104
+ end
105
+ end
106
+
107
+ unless options[:gemfile]
108
+ File.delete"#{app_path}/Gemfile"
109
+ end
110
+
111
+ routes = File.read("#{app_path}/config/routes.rb")
112
+ if routes =~ /(\n\s*end\s*)\Z/
113
+ File.open("#{app_path}/config/routes.rb", 'w') do |f|
114
+ f.puts $` + "\nmatch ':controller(/:action(/:id))(.:format)'\n" + $1
115
+ end
116
+ end
117
+
118
+ add_to_config 'config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"; config.session_store :cookie_store, :key => "_myapp_session"; config.active_support.deprecation = :log'
119
+ end
120
+
121
+ def teardown_app
122
+ ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env
123
+ end
124
+
125
+ # Make a very basic app, without creating the whole directory structure.
126
+ # This is faster and simpler than the method above.
127
+ def make_basic_app
128
+ require "rails"
129
+ require "action_controller/railtie"
130
+
131
+ app = Class.new(Rails::Application)
132
+ app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
133
+ app.config.session_store :cookie_store, :key => "_myapp_session"
134
+ app.config.active_support.deprecation = :log
135
+
136
+ yield app if block_given?
137
+ app.initialize!
138
+
139
+ app.routes.draw do
140
+ match "/" => "omg#index"
141
+ end
142
+
143
+ require 'rack/test'
144
+ extend ::Rack::Test::Methods
145
+ end
146
+
147
+ def simple_controller
148
+ controller :foo, <<-RUBY
149
+ class FooController < ApplicationController
150
+ def index
151
+ render :text => "foo"
152
+ end
153
+ end
154
+ RUBY
155
+
156
+ app_file 'config/routes.rb', <<-RUBY
157
+ AppTemplate::Application.routes.draw do
158
+ match ':controller(/:action)'
159
+ end
160
+ RUBY
161
+ end
162
+
163
+ class Bukkit
164
+ attr_reader :path
165
+
166
+ def initialize(path)
167
+ @path = path
168
+ end
169
+
170
+ def write(file, string)
171
+ path = "#{@path}/#{file}"
172
+ FileUtils.mkdir_p(File.dirname(path))
173
+ File.open(path, "w") {|f| f.puts string }
174
+ end
175
+
176
+ def delete(file)
177
+ File.delete("#{@path}/#{file}")
178
+ end
179
+ end
180
+
181
+ def engine(name)
182
+ dir = "#{app_path}/random/#{name}"
183
+ FileUtils.mkdir_p(dir)
184
+
185
+ app = File.readlines("#{app_path}/config/application.rb")
186
+ app.insert(2, "$:.unshift(\"#{dir}/lib\")")
187
+ app.insert(3, "require #{name.inspect}")
188
+
189
+ File.open("#{app_path}/config/application.rb", 'r+') do |f|
190
+ f.puts app
191
+ end
192
+
193
+ Bukkit.new(dir).tap do |bukkit|
194
+ yield bukkit if block_given?
195
+ end
196
+ end
197
+
198
+ def script(script)
199
+ Dir.chdir(app_path) do
200
+ `#{Gem.ruby} #{app_path}/script/rails #{script}`
201
+ end
202
+ end
203
+
204
+ def add_to_config(str)
205
+ environment = File.read("#{app_path}/config/application.rb")
206
+ if environment =~ /(\n\s*end\s*end\s*)\Z/
207
+ File.open("#{app_path}/config/application.rb", 'w') do |f|
208
+ f.puts $` + "\n#{str}\n" + $1
209
+ end
210
+ end
211
+ end
212
+
213
+ def add_to_env_config(env, str)
214
+ environment = File.read("#{app_path}/config/environments/#{env}.rb")
215
+ if environment =~ /(\n\s*end\s*)\Z/
216
+ File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f|
217
+ f.puts $` + "\n#{str}\n" + $1
218
+ end
219
+ end
220
+ end
221
+
222
+ def remove_from_config(str)
223
+ file = "#{app_path}/config/application.rb"
224
+ contents = File.read(file)
225
+ contents.sub!(/#{str}/, "")
226
+ File.open(file, "w+") { |f| f.puts contents }
227
+ end
228
+
229
+ def app_file(path, contents)
230
+ FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
231
+ File.open("#{app_path}/#{path}", 'w') do |f|
232
+ f.puts contents
233
+ end
234
+ end
235
+
236
+ def remove_file(path)
237
+ FileUtils.rm_rf "#{app_path}/#{path}"
238
+ end
239
+
240
+ def controller(name, contents)
241
+ app_file("app/controllers/#{name}_controller.rb", contents)
242
+ end
243
+
244
+ def use_frameworks(arr)
245
+ to_remove = [:actionmailer,
246
+ :activemodel,
247
+ :activerecord,
248
+ :activeresource] - arr
249
+ remove_from_config "config.active_record.identity_map = true" if to_remove.include? :activerecord
250
+ $:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
251
+ end
252
+
253
+ def boot_rails
254
+ require File.expand_path('../load_paths', __FILE__)
255
+ end
256
+ end
257
+ end
258
+
259
+ class ActiveSupport::TestCase
260
+ include TestHelpers::Paths
261
+ include TestHelpers::Rack
262
+ include TestHelpers::Generation
263
+ extend ActiveSupport::Testing::Declarative
264
+ end
265
+
266
+ # Create a scope and build a fixture rails app
267
+ Module.new do
268
+ extend TestHelpers::Paths
269
+ # Build a rails app
270
+ if File.exist?(tmp_path)
271
+ FileUtils.rm_rf(tmp_path)
272
+ end
273
+ FileUtils.mkdir(tmp_path)
274
+
275
+ environment = File.expand_path('../load_paths', __FILE__)
276
+ if File.exist?("#{environment}.rb")
277
+ require_environment = "-r #{environment}"
278
+ end
279
+
280
+ `#{Gem.ruby} #{require_environment} #{RAILS_BIN} new #{tmp_path('app_template')}`
281
+ File.open("#{tmp_path}/app_template/config/boot.rb", 'w') do |f|
282
+ if require_environment
283
+ f.puts "Dir.chdir('#{File.dirname(environment)}') do"
284
+ f.puts " require '#{environment}'"
285
+ f.puts "end"
286
+ end
287
+ f.puts "require 'rails/all'"
288
+ end
289
+ end unless defined?(RAILS_ISOLATED_ENGINE)
@@ -0,0 +1,4 @@
1
+ # bust gem prelude
2
+ require 'rubygems' unless defined? Gem
3
+ require 'bundler'
4
+ Bundler.setup
@@ -0,0 +1,3 @@
1
+ module Isolation
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ ## This is required until the regular test task
2
+ ## below passes. It's not ideal, but at least
3
+ ## we can see the failures
4
+ namespace :isolation do
5
+ task :test do
6
+ dir = ENV["TEST_DIR"] || "**"
7
+ Dir["test/#{dir}/*_test.rb"].each do |file|
8
+ next true if file.include?("fixtures")
9
+ dash_i = [
10
+ 'test',
11
+ # 'lib',
12
+ # "#{File.dirname(__FILE__)}/../activesupport/lib",
13
+ # "#{File.dirname(__FILE__)}/../actionpack/lib"
14
+ ]
15
+ ruby "-I#{dash_i.join ':'}", file
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'isolation/abstract_unit'
2
+
3
+ class IsolationTest < ActiveSupport::TestCase
4
+ include ActiveSupport::Testing::Isolation
5
+
6
+ def setup
7
+ build_app
8
+ boot_rails
9
+ end
10
+
11
+ def teardown
12
+ teardown_app
13
+ end
14
+
15
+ end
@@ -0,0 +1,116 @@
1
+ require 'isolation/abstract_unit'
2
+
3
+ # This is a sample test taken from
4
+ # https://github.com/rails/rails/tree/v3.1.3/railties/test
5
+ # application/loading_test.rb
6
+ class RailsLoadingTest < ActiveSupport::TestCase
7
+ include ActiveSupport::Testing::Isolation
8
+
9
+ def setup
10
+ build_app
11
+ boot_rails
12
+ end
13
+
14
+ def teardown
15
+ teardown_app
16
+ end
17
+
18
+ def app
19
+ @app ||= Rails.application
20
+ end
21
+
22
+ def test_constants_in_app_are_autoloaded
23
+ app_file "app/models/post.rb", <<-MODEL
24
+ class Post < ActiveRecord::Base
25
+ validates_acceptance_of :title, :accept => "omg"
26
+ end
27
+ MODEL
28
+
29
+ require "#{rails_root}/config/environment"
30
+ setup_ar!
31
+
32
+ p = Post.create(:title => 'omg')
33
+ assert_equal 1, Post.count
34
+ assert_equal 'omg', p.title
35
+ p = Post.first
36
+ assert_equal 'omg', p.title
37
+ end
38
+
39
+ def test_models_without_table_do_not_panic_on_scope_definitions_when_loaded
40
+ app_file "app/models/user.rb", <<-MODEL
41
+ class User < ActiveRecord::Base
42
+ default_scope where(:published => true)
43
+ end
44
+ MODEL
45
+
46
+ require "#{rails_root}/config/environment"
47
+ setup_ar!
48
+
49
+ User
50
+ end
51
+
52
+ test "load config/environments/environment before Bootstrap initializers" do
53
+ app_file "config/environments/development.rb", <<-RUBY
54
+ AppTemplate::Application.configure do
55
+ config.development_environment_loaded = true
56
+ end
57
+ RUBY
58
+
59
+ add_to_config <<-RUBY
60
+ config.before_initialize do
61
+ config.loaded = config.development_environment_loaded
62
+ end
63
+ RUBY
64
+
65
+ require "#{app_path}/config/environment"
66
+ assert ::AppTemplate::Application.config.loaded
67
+ end
68
+
69
+ def test_descendants_are_cleaned_on_each_request_without_cache_classes
70
+ add_to_config <<-RUBY
71
+ config.cache_classes = false
72
+ RUBY
73
+
74
+ app_file "app/models/post.rb", <<-MODEL
75
+ class Post < ActiveRecord::Base
76
+ end
77
+ MODEL
78
+
79
+ app_file 'config/routes.rb', <<-RUBY
80
+ AppTemplate::Application.routes.draw do
81
+ match '/load', :to => lambda { |env| [200, {}, Post.all] }
82
+ match '/unload', :to => lambda { |env| [200, {}, []] }
83
+ end
84
+ RUBY
85
+
86
+ require 'rack/test'
87
+ extend Rack::Test::Methods
88
+
89
+ require "#{rails_root}/config/environment"
90
+ setup_ar!
91
+
92
+ assert_equal [], ActiveRecord::Base.descendants
93
+ get "/load"
94
+ assert_equal [Post], ActiveRecord::Base.descendants
95
+ get "/unload"
96
+ assert_equal [], ActiveRecord::Base.descendants
97
+ end
98
+
99
+ test "initialize_cant_be_called_twice" do
100
+ require "#{app_path}/config/environment"
101
+ assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! }
102
+ end
103
+
104
+ protected
105
+
106
+ def setup_ar!
107
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
108
+ ActiveRecord::Migration.verbose = false
109
+ ActiveRecord::Schema.define(:version => 1) do
110
+ create_table :posts do |t|
111
+ t.string :title
112
+ end
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,10 @@
1
+ # # Configure Rails Environment
2
+ # ENV["RAILS_ENV"] = "test"
3
+ #
4
+ # require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ # require "rails/test_help"
6
+ #
7
+ # Rails.backtrace_cleaner.remove_silencers!
8
+ #
9
+ # # Load support files
10
+ # Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: isolation_test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Reinaldo de Souza Junior
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70294749505920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70294749505920
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70294749505480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70294749505480
36
+ description:
37
+ email:
38
+ - juniorz@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/isolation/abstract_unit.rb
44
+ - lib/isolation/load_paths.rb
45
+ - lib/isolation/version.rb
46
+ - lib/isolation.rb
47
+ - lib/tasks/isolation_tasks.rake
48
+ - MIT-LICENSE
49
+ - Rakefile
50
+ - README.rdoc
51
+ - test/isolation_test.rb
52
+ - test/rails_loading_test.rb
53
+ - test/test_helper.rb
54
+ homepage: https://github.com/juniorz/isolation_test
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: 253863503296580160
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 253863503296580160
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.10
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Testing Rails::Engine boot proccess.
84
+ test_files:
85
+ - test/isolation_test.rb
86
+ - test/rails_loading_test.rb
87
+ - test/test_helper.rb