aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/actions_test.rb
blob: c4b64413972bd77961a5f9390f3d479f8daab87a (plain) (blame)
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
require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator'
require 'env_helpers'
require 'mocha/setup' # FIXME: stop using mocha

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'/
  end

  def test_gem_should_put_gem_dependency_in_gemfile
    run_generator
    action :gem, 'will-paginate'
    assert_file 'Gemfile', /gem 'will\-paginate'/
  end

  def test_gem_with_version_should_include_version_in_gemfile
    run_generator

    action :gem, 'rspec', '>=2.0.0.a5'

    assert_file 'Gemfile', /gem 'rspec', '>=2.0.0.a5'/
  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_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', /\ngroup :development, :test do\n  gem 'rspec-rails'\nend\n\ngroup :test do\n  gem 'fakeweb'\nend/
  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)}/
  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)}/
  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_git_with_symbol_should_run_command_using_git_scm
    generator.expects(:run).once.with('git init')
    action :git, :init
  end

  def test_git_with_hash_should_run_each_command_using_git_scm
    generator.expects(:run).times(2)
    action :git, rm: 'README', add: '.'
  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'
  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'
  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]'
  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'
  end

  def test_generate_should_run_script_generate_with_argument_and_options
    generator.expects(:run_ruby_script).once.with('bin/rails generate model MyModel', verbose: false)
    action :generate, 'model', 'MyModel'
  end

  def test_rake_should_run_rake_command_with_default_env
    generator.expects(:run).once.with("rake log:clear RAILS_ENV=development", verbose: false)
    with_rails_env nil do
      action :rake, 'log:clear'
    end
  end

  def test_rake_with_env_option_should_run_rake_command_in_env
    generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false)
    action :rake, 'log:clear', env: 'production'
  end

  def test_rake_with_rails_env_variable_should_run_rake_command_in_env
    generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false)
    with_rails_env "production" do
      action :rake, 'log:clear'
    end
  end

  def test_env_option_should_win_over_rails_env_variable_when_running_rake
    generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false)
    with_rails_env "staging" do
      action :rake, 'log:clear', env: 'production'
    end
  end

  def test_rake_with_sudo_option_should_run_rake_command_with_sudo
    generator.expects(:run).once.with("sudo rake log:clear RAILS_ENV=development", verbose: false)
    with_rails_env nil do
      action :rake, 'log:clear', sudo: true
    end
  end

  def test_capify_should_run_the_capify_command
    generator.expects(:run).once.with('capify .', verbose: false)
    action :capify!
  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_readme
    run_generator
    Rails::Generators::AppGenerator.expects(:source_root).times(2).returns(destination_root)
    assert_match "application up and running", action(:readme, "README.rdoc")
  end

  def test_readme_with_quiet
    generator(default_arguments, quiet: true)
    run_generator
    Rails::Generators::AppGenerator.expects(:source_root).times(2).returns(destination_root)
    assert_no_match "application up and running", action(:readme, "README.rdoc")
  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

  protected

    def action(*args, &block)
      capture(:stdout){ generator.send(*args, &block) }
    end

end