aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/rails_template_runner_test.rb
blob: fcc020603dcbd8b64d6e2aa2fc2058bc519eb2c0 (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
require 'abstract_unit'
require 'generators/generator_test_helper'

class RailsTemplateRunnerTest < GeneratorTestCase
  def setup
    Rails::Generator::Base.use_application_sources!
    run_generator('app', [RAILS_ROOT])
    # generate empty template
    @template_path = File.join(RAILS_ROOT, 'template.rb')
    File.open(File.join(@template_path), 'w') {|f| f << '' }

    @git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git'
    @svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
  end

  def teardown
    super
    rm_rf "#{RAILS_ROOT}/README"
    rm_rf "#{RAILS_ROOT}/Rakefile"
    rm_rf "#{RAILS_ROOT}/doc"
    rm_rf "#{RAILS_ROOT}/lib"
    rm_rf "#{RAILS_ROOT}/log"
    rm_rf "#{RAILS_ROOT}/script"
    rm_rf "#{RAILS_ROOT}/vendor"
    rm_rf "#{RAILS_ROOT}/tmp"
    rm_rf "#{RAILS_ROOT}/Capfile"
    rm_rf @template_path
  end

  def test_initialize_should_load_template
    Rails::TemplateRunner.any_instance.expects(:load_template).with(@template_path)
    silence_generator do
      Rails::TemplateRunner.new(@template_path, RAILS_ROOT)
    end
  end

  def test_initialize_should_raise_error_on_missing_template_file
    assert_raise(RuntimeError) do
      silence_generator do
        Rails::TemplateRunner.new('non/existent/path/to/template.rb', RAILS_ROOT)
      end
    end
  end

  def test_file_should_write_data_to_file_path
    run_template_method(:file, 'lib/test_file.rb', 'heres test data')
    assert_generated_file_with_data 'lib/test_file.rb', 'heres test data'
  end

  def test_file_should_write_block_contents_to_file_path
    run_template_method(:file, 'lib/test_file.rb') { 'heres block data' }
    assert_generated_file_with_data 'lib/test_file.rb', 'heres block data'
  end

  def test_plugin_with_git_option_should_run_plugin_install
    expects_run_with_command("script/plugin install #{@git_plugin_uri}")
    run_template_method(:plugin, 'restful-authentication', :git => @git_plugin_uri)
  end

  def test_plugin_with_svn_option_should_run_plugin_install
    expects_run_with_command("script/plugin install #{@svn_plugin_uri}")
    run_template_method(:plugin, 'restful-authentication', :svn => @svn_plugin_uri)
  end

  def test_plugin_with_git_option_and_submodule_should_use_git_scm
    Rails::Git.expects(:run).with("submodule add #{@git_plugin_uri} vendor/plugins/rest_auth")
    run_template_method(:plugin, 'rest_auth', :git => @git_plugin_uri, :submodule => true)
  end

  def test_plugin_with_no_options_should_skip_method
    Rails::TemplateRunner.any_instance.expects(:run).never
    run_template_method(:plugin, 'rest_auth', {})
  end

  def test_gem_should_put_gem_dependency_in_enviroment
    run_template_method(:gem, 'will-paginate')
    assert_rails_initializer_includes("config.gem 'will-paginate'")
  end

  def test_gem_with_options_should_include_options_in_gem_dependency_in_environment
    run_template_method(:gem, 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com')
    assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'")
  end

  def test_environment_should_include_data_in_environment_initializer_block
    load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]'
    run_template_method(:environment, load_paths)
    assert_rails_initializer_includes(load_paths)
  end

  def test_environment_with_block_should_include_block_contents_in_environment_initializer_block
    run_template_method(:environment) do
      '# This wont be added'
      '# This will be added'
    end
    assert_rails_initializer_includes('# This will be added')
  end

  def test_git_with_symbol_should_run_command_using_git_scm
    Rails::Git.expects(:run).once.with('init')
    run_template_method(:git, :init)
  end

  def test_git_with_hash_should_run_each_command_using_git_scm
    Rails::Git.expects(:run).times(2)
    run_template_method(:git, {:init => '', :add => '.'})
  end

  def test_vendor_should_write_data_to_file_in_vendor
    run_template_method(:vendor, 'vendor_file.rb', '# vendor data')
    assert_generated_file_with_data('vendor/vendor_file.rb', '# vendor data')
  end

  def test_lib_should_write_data_to_file_in_lib
    run_template_method(:lib, 'my_library.rb', 'class MyLibrary')
    assert_generated_file_with_data('lib/my_library.rb', 'class MyLibrary')
  end

  def test_rakefile_should_write_date_to_file_in_lib_tasks
    run_template_method(:rakefile, 'myapp.rake', 'task :run => [:environment]')
    assert_generated_file_with_data('lib/tasks/myapp.rake', 'task :run => [:environment]')
  end

  def test_initializer_should_write_date_to_file_in_config_initializers
    run_template_method(:initializer, 'constants.rb', 'MY_CONSTANT = 42')
    assert_generated_file_with_data('config/initializers/constants.rb', 'MY_CONSTANT = 42')
  end

  def test_generate_should_run_script_generate_with_argument_and_options
    expects_run_with_command('script/generate model MyModel')
    run_template_method(:generate, 'model', 'MyModel')
  end

  def test_rake_should_run_rake_command_with_development_env
    expects_run_with_command('rake log:clear RAILS_ENV=development')
    run_template_method(:rake, 'log:clear')
  end

  def test_rake_with_env_option_should_run_rake_command_in_env
    expects_run_with_command('rake log:clear RAILS_ENV=production')
    run_template_method(:rake, 'log:clear', :env => 'production')
  end

  def test_rake_with_sudo_option_should_run_rake_command_with_sudo
    expects_run_with_command('sudo rake log:clear RAILS_ENV=development')
    run_template_method(:rake, 'log:clear', :sudo => true)
  end

  def test_capify_should_run_the_capify_command
    expects_run_with_command('capify .')
    run_template_method(:capify!)
  end

  def test_freeze_should_freeze_rails_edge
    expects_run_with_command('rake rails:freeze:edge')
    run_template_method(:freeze!)
  end

  def test_route_should_add_data_to_the_routes_block_in_config_routes
    route_command = "map.route '/login', :controller => 'sessions', :action => 'new'"
    run_template_method(:route, route_command)
    assert_generated_file_with_data 'config/routes.rb', route_command
  end

  protected
  def run_template_method(method_name, *args, &block)
    silence_generator do
      @template_runner = Rails::TemplateRunner.new(@template_path, RAILS_ROOT)
      @template_runner.send(method_name, *args, &block)
    end
  end

  def expects_run_with_command(command)
    Rails::TemplateRunner.any_instance.stubs(:run).once.with(command, false)
  end

  def assert_rails_initializer_includes(data, message = nil)
    message ||= "Rails::Initializer should include #{data}"
    assert_generated_file 'config/environment.rb' do |body|
      assert_match(/#{Regexp.escape("Rails::Initializer.run do |config|")}.+#{Regexp.escape(data)}.+end/m, body, message)
    end
  end

  def assert_generated_file_with_data(file, data, message = nil)
    message ||= "#{file} should include '#{data}'"
    assert_generated_file(file) do |file|
      assert_match(/#{Regexp.escape(data)}/,file, message)
    end
  end
end