1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
# coding:utf-8
require "isolation/abstract_unit"
module ApplicationTests
class RakeTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf("#{app_path}/config/environments")
end
def teardown
teardown_app
end
def test_gems_tasks_are_loaded_first_than_application_ones
app_file "lib/tasks/app.rake", <<-RUBY
$task_loaded = Rake::Task.task_defined?("db:create:all")
RUBY
require "#{app_path}/config/environment"
::Rails.application.load_tasks
assert $task_loaded
end
def test_environment_is_required_in_rake_tasks
app_file "config/environment.rb", <<-RUBY
SuperMiddleware = Struct.new(:app)
AppTemplate::Application.configure do
config.middleware.use SuperMiddleware
end
AppTemplate::Application.initialize!
RUBY
assert_match("SuperMiddleware", Dir.chdir(app_path){ `rake middleware` })
end
def test_initializers_are_executed_in_rake_tasks
add_to_config <<-RUBY
initializer "do_something" do
puts "Doing something..."
end
rake_tasks do
task :do_nothing => :environment do
end
end
RUBY
output = Dir.chdir(app_path){ `rake do_nothing` }
assert_match "Doing something...", output
end
def test_code_statistics_sanity
assert_match "Code LOC: 5 Test LOC: 0 Code to Test Ratio: 1:0.0",
Dir.chdir(app_path){ `rake stats` }
end
def test_rake_test_error_output
Dir.chdir(app_path){ `rake db:migrate` }
app_file "config/database.yml", <<-RUBY
development:
RUBY
app_file "test/unit/one_unit_test.rb", <<-RUBY
RUBY
app_file "test/functional/one_functional_test.rb", <<-RUBY
raise RuntimeError
RUBY
app_file "test/integration/one_integration_test.rb", <<-RUBY
raise RuntimeError
RUBY
silence_stderr do
output = Dir.chdir(app_path){ `rake test` }
assert_match(/Errors running test:units! #<ActiveRecord::AdapterNotSpecified/, output)
assert_match(/Errors running test:functionals! #<RuntimeError/, output)
assert_match(/Errors running test:integration! #<RuntimeError/, output)
end
end
def test_rake_routes_output_strips_anchors_from_http_verbs
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
get '/cart', :to => 'cart#show'
end
RUBY
assert_equal "cart GET /cart(.:format) cart#show\n", Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_custom_assets
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
get '/custom/assets', :to => 'custom_assets#show'
end
RUBY
assert_equal "custom_assets GET /custom/assets(.:format) custom_assets#show\n",
Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_resources_route
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
resources :articles
end
RUBY
expected =
" articles GET /articles(.:format) articles#index\n" <<
" POST /articles(.:format) articles#create\n" <<
" new_article GET /articles/new(.:format) articles#new\n" <<
"edit_article GET /articles/:id/edit(.:format) articles#edit\n" <<
" article GET /articles/:id(.:format) articles#show\n" <<
" PUT /articles/:id(.:format) articles#update\n" <<
" DELETE /articles/:id(.:format) articles#destroy\n"
assert_equal expected, Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_root_route
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
root :to => 'pages#main'
end
RUBY
assert_equal "root / pages#main\n", Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_controller_and_action_only_route
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller/:action'
end
RUBY
assert_equal " /:controller/:action(.:format) \n", Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_controller_and_action_route_with_constraints
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match ':controller(/:action(/:id))', :id => /\\d+/
end
RUBY
assert_equal " /:controller(/:action(/:id))(.:format) {:id=>/\\d+/}\n", Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_route_with_defaults
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match 'photos/:id' => 'photos#show', :defaults => {:format => 'jpg'}
end
RUBY
assert_equal %Q[ /photos/:id(.:format) photos#show {:format=>"jpg"}\n], Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_route_with_constraints
app_file "config/routes.rb", <<-RUBY
AppTemplate::Application.routes.draw do
match 'photos/:id' => 'photos#show', :id => /[A-Z]\\d{5}/
end
RUBY
assert_equal " /photos/:id(.:format) photos#show {:id=>/[A-Z]\\d{5}/}\n", Dir.chdir(app_path){ `rake routes` }
end
def test_rake_routes_shows_route_with_rack_app
app_file "lib/rack_app.rb", <<-RUBY
class RackApp
class << self
def call(env)
end
end
end
RUBY
app_file "config/routes.rb", <<-RUBY
require 'rack_app'
AppTemplate::Application.routes.draw do
match 'foo/:id' => RackApp, :id => /[A-Z]\\d{5}/
end
RUBY
assert_equal " /foo/:id(.:format) RackApp {:id=>/[A-Z]\\d{5}/}\n", Dir.chdir(app_path){ `rake routes` }
end
def test_logger_is_flushed_when_exiting_production_rake_tasks
add_to_config <<-RUBY
rake_tasks do
task :log_something => :environment do
Rails.logger.error("Sample log message")
end
end
RUBY
output = Dir.chdir(app_path){ `rake log_something RAILS_ENV=production && cat log/production.log` }
assert_match "Sample log message", output
end
def test_model_and_migration_generator_with_change_syntax
Dir.chdir(app_path) do
`rails generate model user username:string password:string`
`rails generate migration add_email_to_users email:string`
end
output = Dir.chdir(app_path){ `rake db:migrate` }
assert_match(/create_table\(:users\)/, output)
assert_match(/CreateUsers: migrated/, output)
assert_match(/add_column\(:users, :email, :string\)/, output)
assert_match(/AddEmailToUsers: migrated/, output)
output = Dir.chdir(app_path){ `rake db:rollback STEP=2` }
assert_match(/drop_table\("users"\)/, output)
assert_match(/CreateUsers: reverted/, output)
assert_match(/remove_column\("users", :email\)/, output)
assert_match(/AddEmailToUsers: reverted/, output)
end
def test_loading_specific_fixtures
Dir.chdir(app_path) do
`rails generate model user username:string password:string`
`rails generate model product name:string`
`rake db:migrate`
end
require "#{rails_root}/config/environment"
# loading a specific fixture
errormsg = Dir.chdir(app_path) { `rake db:fixtures:load FIXTURES=products` }
assert $?.success?, errormsg
assert_equal 2, ::AppTemplate::Application::Product.count
assert_equal 0, ::AppTemplate::Application::User.count
end
def test_scaffold_tests_pass_by_default
content = Dir.chdir(app_path) do
`rails generate scaffold user username:string password:string`
`bundle exec rake db:migrate db:test:clone test`
end
assert_match(/7 tests, 10 assertions, 0 failures, 0 errors/, content)
end
def test_assets_precompile_with_utf8_filename
add_to_config <<-RUBY
config.assets.precompile = [ /\.png$$/, /application.(css|js)$/ ]
RUBY
Dir.chdir(app_path) do
`cp app/assets/images/rails.png app/assets/images/レイルズ.png`
`rake assets:precompile`
open("public/assets/manifest.yml") do |f|
assert_match(/レイルズ.png/, f.read)
end
end
end
def test_assets_precompile_ignore_asset_host
add_to_config <<-RUBY
config.action_controller.asset_host = Proc.new { |source, request| "http://www.example.com/" }
RUBY
app_file "app/assets/javascripts/test.js.erb", <<-RUBY
alert("<%= asset_path "rails.png" %>");
RUBY
Dir.chdir(app_path) do
`rake assets:precompile`
open("public/assets/application.js") do |f|
assert_match(/\"\/assets\/rails.png\"/, f.read)
end
end
end
end
end
|