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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
# frozen_string_literal: true
require "generators/generators_test_helper"
require "rails/generators/rails/app/app_generator"
require "env_helpers"
class ActionsTest < Rails::Generators::TestCase
include GeneratorsTestHelper
include EnvHelpers
tests Rails::Generators::AppGenerator
arguments [destination_root]
def setup
Rails.application = TestApp::Application
super
end
def teardown
Rails.application = TestApp::Application.instance
end
def test_invoke_other_generator_with_shortcut
action :invoke, "model", ["my_model"]
assert_file "app/models/my_model.rb", /MyModel/
end
def test_invoke_other_generator_with_full_namespace
action :invoke, "rails:model", ["my_model"]
assert_file "app/models/my_model.rb", /MyModel/
end
def test_create_file_should_write_data_to_file_path
action :create_file, "lib/test_file.rb", "heres test data"
assert_file "lib/test_file.rb", "heres test data"
end
def test_create_file_should_write_block_contents_to_file_path
action(:create_file, "lib/test_file.rb") { "heres block data" }
assert_file "lib/test_file.rb", "heres block data"
end
def test_add_source_adds_source_to_gemfile
run_generator
action :add_source, "http://gems.github.com"
assert_file "Gemfile", /source 'http:\/\/gems\.github\.com'\n/
end
def test_add_source_with_block_adds_source_to_gemfile_with_gem
run_generator
action :add_source, "http://gems.github.com" do
gem "rspec-rails"
end
assert_file "Gemfile", /\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\z/
end
def test_add_source_with_block_adds_source_to_gemfile_after_gem
run_generator
action :gem, "will-paginate"
action :add_source, "http://gems.github.com" do
gem "rspec-rails"
end
assert_file "Gemfile", /\ngem 'will-paginate'\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\z/
end
def test_add_source_should_create_newline_between_blocks
run_generator
action :add_source, "http://gems.github.com" do
gem "rspec-rails"
end
action :add_source, "http://gems2.github.com" do
gem "fakeweb"
end
assert_file "Gemfile", /\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\nsource 'http:\/\/gems2\.github\.com' do\n gem 'fakeweb'\nend\n\z/
end
def test_gem_should_put_gem_dependency_in_gemfile
run_generator
action :gem, "will-paginate"
assert_file "Gemfile", /gem 'will\-paginate'\n\z/
end
def test_gem_with_version_should_include_version_in_gemfile
run_generator
action :gem, "rspec", ">= 2.0.0.a5"
action :gem, "RedCloth", ">= 4.1.0", "< 4.2.0"
action :gem, "nokogiri", version: ">= 1.4.2"
action :gem, "faker", version: [">= 0.1.0", "< 0.3.0"]
assert_file "Gemfile" do |content|
assert_match(/gem 'rspec', '>= 2\.0\.0\.a5'/, content)
assert_match(/gem 'RedCloth', '>= 4\.1\.0', '< 4\.2\.0'/, content)
assert_match(/gem 'nokogiri', '>= 1\.4\.2'/, content)
assert_match(/gem 'faker', '>= 0\.1\.0', '< 0\.3\.0'/, content)
end
end
def test_gem_should_insert_on_separate_lines
run_generator
File.open("Gemfile", "a") { |f| f.write("# Some content...") }
action :gem, "rspec"
action :gem, "rspec-rails"
assert_file "Gemfile", /^gem 'rspec'$/
assert_file "Gemfile", /^gem 'rspec-rails'$/
end
def test_gem_should_include_options
run_generator
action :gem, "rspec", github: "dchelimsky/rspec", tag: "1.2.9.rc1"
assert_file "Gemfile", /gem 'rspec', github: 'dchelimsky\/rspec', tag: '1\.2\.9\.rc1'/
end
def test_gem_with_non_string_options
run_generator
action :gem, "rspec", require: false
action :gem, "rspec-rails", group: [:development, :test]
assert_file "Gemfile", /^gem 'rspec', require: false$/
assert_file "Gemfile", /^gem 'rspec-rails', group: \[:development, :test\]$/
end
def test_gem_falls_back_to_inspect_if_string_contains_single_quote
run_generator
action :gem, "rspec", ">=2.0'0"
assert_file "Gemfile", /^gem 'rspec', ">=2\.0'0"$/
end
def test_gem_works_even_if_frozen_string_is_passed_as_argument
run_generator
action :gem, -"frozen_gem", -"1.0.0"
assert_file "Gemfile", /^gem 'frozen_gem', '1.0.0'$/
end
def test_gem_group_should_wrap_gems_in_a_group
run_generator
action :gem_group, :development, :test do
gem "rspec-rails"
end
action :gem_group, :test do
gem "fakeweb"
end
assert_file "Gemfile", /\n\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend\n\z/
end
def test_github_should_create_an_indented_block
run_generator
action :github, "user/repo" do
gem "foo"
gem "bar"
gem "baz"
end
assert_file "Gemfile", /\n\ngithub 'user\/repo' do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
end
def test_github_should_create_an_indented_block_with_options
run_generator
action :github, "user/repo", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
assert_file "Gemfile", /\n\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
end
def test_github_should_create_an_indented_block_within_a_group
run_generator
action :gem_group, :magic do
github "user/repo", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
github "user/repo2", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
end
assert_file "Gemfile", /\n\ngroup :magic do\n github 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\n github 'user\/repo2', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\nend\n\z/
end
def test_github_should_create_newline_between_blocks
run_generator
action :github, "user/repo", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
action :github, "user/repo2", a: "correct", other: true do
gem "foo"
gem "bar"
gem "baz"
end
assert_file "Gemfile", /\n\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\ngithub 'user\/repo2', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
end
def test_gem_with_gemfile_without_newline_at_the_end
run_generator
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
action :gem, "will-paginate"
assert_file "Gemfile", /gem 'rspec-rails'\ngem 'will-paginate'\n\z/
end
def test_gem_group_with_gemfile_without_newline_at_the_end
run_generator
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
action :gem_group, :test do
gem "fakeweb"
end
assert_file "Gemfile", /gem 'rspec-rails'\n\ngroup :test do\n gem 'fakeweb'\nend\n\z/
end
def test_add_source_with_gemfile_without_newline_at_the_end
run_generator
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
action :add_source, "http://gems.github.com" do
gem "fakeweb"
end
assert_file "Gemfile", /gem 'rspec-rails'\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'fakeweb'\nend\n\z/
end
def test_github_with_gemfile_without_newline_at_the_end
run_generator
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
action :github, "user/repo" do
gem "fakeweb"
end
assert_file "Gemfile", /gem 'rspec-rails'\n\ngithub 'user\/repo' do\n gem 'fakeweb'\nend\n\z/
end
def test_environment_should_include_data_in_environment_initializer_block
run_generator
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
action :environment, autoload_paths
assert_file "config/application.rb", / class Application < Rails::Application\n #{Regexp.escape(autoload_paths)}\n/
end
def test_environment_should_include_data_in_environment_initializer_block_with_env_option
run_generator
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
action :environment, autoload_paths, env: "development"
assert_file "config/environments/development.rb", /Rails\.application\.configure do\n #{Regexp.escape(autoload_paths)}\n/
end
def test_environment_with_block_should_include_block_contents_in_environment_initializer_block
run_generator
action :environment do
_ = "# This wont be added" # assignment to silence parse-time warning "unused literal ignored"
"# This will be added"
end
assert_file "config/application.rb" do |content|
assert_match(/# This will be added/, content)
assert_no_match(/# This wont be added/, content)
end
end
def test_environment_with_block_should_include_block_contents_with_multiline_data_in_environment_initializer_block
run_generator
data = <<-RUBY
config.encoding = "utf-8"
config.time_zone = "UTC"
RUBY
action(:environment) { data }
assert_file "config/application.rb", / class Application < Rails::Application\n#{Regexp.escape(data.strip_heredoc.indent(4))}/
end
def test_environment_should_include_block_contents_with_multiline_data_in_environment_initializer_block_with_env_option
run_generator
data = <<-RUBY
config.encoding = "utf-8"
config.time_zone = "UTC"
RUBY
action(:environment, nil, env: "development") { data }
assert_file "config/environments/development.rb", /Rails\.application\.configure do\n#{Regexp.escape(data.strip_heredoc.indent(2))}/
end
def test_git_with_symbol_should_run_command_using_git_scm
assert_called_with(generator, :run, ["git init"]) do
action :git, :init
end
end
def test_git_with_hash_should_run_each_command_using_git_scm
assert_called_with(generator, :run, [ ["git rm README"], ["git add ."] ]) do
action :git, rm: "README", add: "."
end
end
def test_vendor_should_write_data_to_file_in_vendor
action :vendor, "vendor_file.rb", "# vendor data"
assert_file "vendor/vendor_file.rb", "# vendor data\n"
end
def test_vendor_should_write_data_to_file_with_block_in_vendor
code = <<-RUBY
puts "one"
puts "two"
puts "three"
RUBY
action(:vendor, "vendor_file.rb") { code }
assert_file "vendor/vendor_file.rb", code.strip_heredoc
end
def test_lib_should_write_data_to_file_in_lib
action :lib, "my_library.rb", "class MyLibrary"
assert_file "lib/my_library.rb", "class MyLibrary\n"
end
def test_lib_should_write_data_to_file_with_block_in_lib
code = <<-RUBY
class MyLib
MY_CONSTANT = 123
end
RUBY
action(:lib, "my_library.rb") { code }
assert_file "lib/my_library.rb", code.strip_heredoc
end
def test_rakefile_should_write_date_to_file_in_lib_tasks
action :rakefile, "myapp.rake", "task run: [:environment]"
assert_file "lib/tasks/myapp.rake", "task run: [:environment]\n"
end
def test_rakefile_should_write_date_to_file_with_block_in_lib_tasks
code = <<-RUBY
task rock: :environment do
puts "Rockin'"
end
RUBY
action(:rakefile, "myapp.rake") { code }
assert_file "lib/tasks/myapp.rake", code.strip_heredoc
end
def test_initializer_should_write_date_to_file_in_config_initializers
action :initializer, "constants.rb", "MY_CONSTANT = 42"
assert_file "config/initializers/constants.rb", "MY_CONSTANT = 42\n"
end
def test_initializer_should_write_date_to_file_with_block_in_config_initializers
code = <<-RUBY
MyLib.configure do |config|
config.value = 123
end
RUBY
action(:initializer, "constants.rb") { code }
assert_file "config/initializers/constants.rb", code.strip_heredoc
end
def test_generate_should_run_script_generate_with_argument_and_options
run_generator
action :generate, "model", "MyModel"
assert_file "app/models/my_model.rb", /MyModel/
end
def test_generate_aborts_when_subprocess_fails_if_requested
run_generator
content = capture(:stderr) do
assert_raises SystemExit do
action :generate, "model", "MyModel:ADsad", abort_on_failure: true
action :generate, "model", "MyModel"
end
end
assert_match(/wrong constant name MyModel:aDsad/, content)
assert_no_file "app/models/my_model.rb"
end
def test_generate_should_run_command_without_env
assert_called_with(generator, :run, ["rails generate model MyModel name:string", verbose: false]) do
action :generate, "model", "MyModel", "name:string"
end
end
def test_rake_should_run_rake_command_with_default_env
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rake, "log:clear"
end
end
end
def test_rake_with_env_option_should_run_rake_command_in_env
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
action :rake, "log:clear", env: "production"
end
end
test "rake with RAILS_ENV variable should run rake command in env" do
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
with_rails_env "production" do
action :rake, "log:clear"
end
end
end
test "env option should win over RAILS_ENV variable when running rake" do
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
with_rails_env "staging" do
action :rake, "log:clear", env: "production"
end
end
end
test "rake with sudo option should run rake command with sudo" do
assert_called_with(generator, :run, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rake, "log:clear", sudo: true
end
end
end
test "rake command with capture option should run rake command with capture" do
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false, capture: true]) do
with_rails_env nil do
action :rake, "log:clear", capture: true
end
end
end
test "rails command should run rails_command with default env" do
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rails_command, "log:clear"
end
end
end
test "rails command with env option should run rails_command with same env" do
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
action :rails_command, "log:clear", env: "production"
end
end
test "rails command with RAILS_ENV variable should run rails_command in env" do
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
with_rails_env "production" do
action :rails_command, "log:clear"
end
end
end
def test_env_option_should_win_over_rails_env_variable_when_running_rails
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
with_rails_env "staging" do
action :rails_command, "log:clear", env: "production"
end
end
end
test "rails command with sudo option should run rails_command with sudo" do
assert_called_with(generator, :run, ["sudo rails log:clear RAILS_ENV=development", verbose: false]) do
with_rails_env nil do
action :rails_command, "log:clear", sudo: true
end
end
end
test "rails command with capture option should run rails_command with capture" do
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false, capture: true]) do
with_rails_env nil do
action :rails_command, "log:clear", capture: true
end
end
end
def test_route_should_add_data_to_the_routes_block_in_config_routes
run_generator
route_command = "route '/login', controller: 'sessions', action: 'new'"
action :route, route_command
assert_file "config/routes.rb", /#{Regexp.escape(route_command)}/
end
def test_route_should_be_idempotent
run_generator
route_path = File.expand_path("config/routes.rb", destination_root)
# runs first time, not asserting
action :route, "root 'welcome#index'"
content_1 = File.read(route_path)
# runs second time
action :route, "root 'welcome#index'"
content_2 = File.read(route_path)
assert_equal content_1, content_2
end
def test_route_should_add_data_with_an_new_line
run_generator
action :route, "root 'welcome#index'"
route_path = File.expand_path("config/routes.rb", destination_root)
content = File.read(route_path)
# Remove all of the comments and blank lines from the routes file
content.gsub!(/^ \#.*\n/, "")
content.gsub!(/^\n/, "")
File.write(route_path, content)
routes = <<-F
Rails.application.routes.draw do
root 'welcome#index'
end
F
assert_file "config/routes.rb", routes
action :route, "resources :product_lines"
routes = <<-F
Rails.application.routes.draw do
resources :product_lines
root 'welcome#index'
end
F
assert_file "config/routes.rb", routes
end
def test_readme
run_generator
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
assert_match "application up and running", action(:readme, "README.md")
end
end
def test_readme_with_quiet
generator(default_arguments, quiet: true)
run_generator
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
assert_no_match "application up and running", action(:readme, "README.md")
end
end
def test_log
assert_equal("YES\n", action(:log, "YES"))
end
def test_log_with_status
assert_equal(" yes YES\n", action(:log, :yes, "YES"))
end
def test_log_with_quiet
generator(default_arguments, quiet: true)
assert_equal("", action(:log, "YES"))
end
def test_log_with_status_with_quiet
generator(default_arguments, quiet: true)
assert_equal("", action(:log, :yes, "YES"))
end
private
def action(*args, &block)
capture(:stdout) { generator.send(*args, &block) }
end
end
|