Skip to content

Commit 530a1bd

Browse files
committed
Compartmentalize test helper support
1 parent 49ea8bd commit 530a1bd

File tree

4 files changed

+83
-77
lines changed

4 files changed

+83
-77
lines changed

test/support/rails_app.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Foo < Rails::Application
2+
if Rails::VERSION::MAJOR >= 4
3+
config.eager_load = false
4+
config.secret_key_base = 'abc123'
5+
config.action_controller.perform_caching = true
6+
config.active_support.test_order = :random
7+
config.logger = Logger.new(nil)
8+
ActionController::Base.cache_store = :memory_store
9+
end
10+
end
11+
Foo.initialize!
12+
13+
module TestHelper
14+
Routes = ActionDispatch::Routing::RouteSet.new
15+
Routes.draw do
16+
get ':controller(/:action(/:id))'
17+
get ':controller(/:action)'
18+
end
19+
20+
ActionController::Base.send :include, Routes.url_helpers
21+
end

test/support/stream_capture.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Use cleaner stream testing interface from Rails 5 if available
2+
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
3+
begin
4+
require "active_support/testing/stream"
5+
rescue LoadError
6+
module ActiveSupport
7+
module Testing
8+
module Stream #:nodoc:
9+
private
10+
11+
def silence_stream(stream)
12+
old_stream = stream.dup
13+
stream.reopen(IO::NULL)
14+
stream.sync = true
15+
yield
16+
ensure
17+
stream.reopen(old_stream)
18+
old_stream.close
19+
end
20+
21+
def quietly
22+
silence_stream(STDOUT) do
23+
silence_stream(STDERR) do
24+
yield
25+
end
26+
end
27+
end
28+
29+
def capture(stream)
30+
stream = stream.to_s
31+
captured_stream = Tempfile.new(stream)
32+
stream_io = eval("$#{stream}")
33+
origin_stream = stream_io.dup
34+
stream_io.reopen(captured_stream)
35+
36+
yield
37+
38+
stream_io.rewind
39+
return captured_stream.read
40+
ensure
41+
captured_stream.close
42+
captured_stream.unlink
43+
stream_io.reopen(origin_stream)
44+
end
45+
end
46+
end
47+
end
48+
end
49+

test/support/test_case.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ActionController::TestCase.class_eval do
2+
def setup
3+
@routes = TestHelper::Routes
4+
end
5+
end

test/test_helper.rb

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,95 +6,26 @@
66
require 'action_controller/test_case'
77
require 'action_controller/railtie'
88
require 'active_support/json'
9-
require 'minitest/autorun'
109
require 'fileutils'
10+
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
11+
12+
require 'minitest/autorun'
1113
# Ensure backward compatibility with Minitest 4
1214
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
1315

14-
require "capture_warnings"
16+
17+
require 'capture_warnings'
1518
@capture_warnings = CaptureWarnings.new(fail_build = false)
1619
@capture_warnings.before_tests
1720
at_exit do
1821
@capture_warnings.after_tests
1922
end
2023
require 'active_model_serializers'
2124

22-
# Use cleaner stream testing interface from Rails 5 if available
23-
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
24-
begin
25-
require "active_support/testing/stream"
26-
rescue LoadError
27-
module ActiveSupport
28-
module Testing
29-
module Stream #:nodoc:
30-
private
25+
require 'support/stream_capture'
3126

32-
def silence_stream(stream)
33-
old_stream = stream.dup
34-
stream.reopen(IO::NULL)
35-
stream.sync = true
36-
yield
37-
ensure
38-
stream.reopen(old_stream)
39-
old_stream.close
40-
end
41-
42-
def quietly
43-
silence_stream(STDOUT) do
44-
silence_stream(STDERR) do
45-
yield
46-
end
47-
end
48-
end
49-
50-
def capture(stream)
51-
stream = stream.to_s
52-
captured_stream = Tempfile.new(stream)
53-
stream_io = eval("$#{stream}")
54-
origin_stream = stream_io.dup
55-
stream_io.reopen(captured_stream)
56-
57-
yield
58-
59-
stream_io.rewind
60-
return captured_stream.read
61-
ensure
62-
captured_stream.close
63-
captured_stream.unlink
64-
stream_io.reopen(origin_stream)
65-
end
66-
end
67-
end
68-
end
69-
end
70-
71-
class Foo < Rails::Application
72-
if Rails::VERSION::MAJOR >= 4
73-
config.eager_load = false
74-
config.secret_key_base = 'abc123'
75-
config.action_controller.perform_caching = true
76-
config.active_support.test_order = :random
77-
config.logger = Logger.new(nil)
78-
ActionController::Base.cache_store = :memory_store
79-
end
80-
end
81-
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
82-
Foo.initialize!
27+
require 'support/rails_app'
8328

8429
require 'fixtures/poro'
8530

86-
module TestHelper
87-
Routes = ActionDispatch::Routing::RouteSet.new
88-
Routes.draw do
89-
get ':controller(/:action(/:id))'
90-
get ':controller(/:action)'
91-
end
92-
93-
ActionController::Base.send :include, Routes.url_helpers
94-
end
95-
96-
ActionController::TestCase.class_eval do
97-
def setup
98-
@routes = TestHelper::Routes
99-
end
100-
end
31+
require 'support/test_case'

0 commit comments

Comments
 (0)