namespaced_generators_test.rb 16.4 KB
Newer Older
1 2 3
require 'generators/generators_test_helper'
require 'rails/generators/rails/controller/controller_generator'
require 'rails/generators/rails/model/model_generator'
4
require 'rails/generators/mailer/mailer_generator'
5
require 'rails/generators/rails/scaffold/scaffold_generator'
6 7

class NamespacedGeneratorTestCase < Rails::Generators::TestCase
8 9
  include GeneratorsTestHelper

10
  def setup
11
    super
12
    Rails::Generators.namespace = TestApp
13 14 15 16 17 18 19 20 21 22 23
  end
end

class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase
  arguments %w(Account foo bar)
  tests Rails::Generators::ControllerGenerator

  setup :copy_routes

  def test_namespaced_controller_skeleton_is_created
    run_generator
24
    assert_file "app/controllers/test_app/account_controller.rb",
25
                /require_dependency "test_app\/application_controller"/,
26 27 28
                /module TestApp/,
                /  class AccountController < ApplicationController/

M
Mike Moore 已提交
29
    assert_file "test/controllers/test_app/account_controller_test.rb",
30 31
                /module TestApp/,
                /  class AccountControllerTest/
32 33 34 35 36 37 38 39 40 41
  end

  def test_skipping_namespace
    run_generator ["Account", "--skip-namespace"]
    assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/
    assert_file "app/helpers/account_helper.rb", /module AccountHelper/
  end

  def test_namespaced_controller_with_additional_namespace
    run_generator ["admin/account"]
42 43 44
    assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, /  class Admin::AccountController < ApplicationController/ do |contents|
      assert_match %r(require_dependency "test_app/application_controller"), contents
    end
45 46
  end

47
  def test_helper_is_also_namespaced
48 49 50 51 52 53
    run_generator
    assert_file "app/helpers/test_app/account_helper.rb", /module TestApp/, /  module AccountHelper/
  end

  def test_invokes_default_test_framework
    run_generator
M
Mike Moore 已提交
54
    assert_file "test/controllers/test_app/account_controller_test.rb"
55 56 57 58 59 60 61 62 63 64
  end

  def test_invokes_default_template_engine
    run_generator
    assert_file "app/views/test_app/account/foo.html.erb", %r(app/views/test_app/account/foo\.html\.erb)
    assert_file "app/views/test_app/account/bar.html.erb", %r(app/views/test_app/account/bar\.html\.erb)
  end

  def test_routes_should_not_be_namespaced
    run_generator
65
    assert_file "config/routes.rb", /get 'account\/foo'/, /get 'account\/bar'/
66
  end
A
Alexey Vakhov 已提交
67

68 69 70 71
  def test_invokes_default_template_engine_even_with_no_action
    run_generator ["account"]
    assert_file "app/views/test_app/account"
  end
72 73 74

  def test_namespaced_controller_dont_indent_blank_lines
    run_generator
75 76
    assert_file "app/controllers/test_app/account_controller.rb" do |content|
      content.split("\n").each do |line|
77
        assert_no_match(/^\s+$/, line, "Don't indent blank lines")
78
      end
79 80
    end
  end
81 82 83 84 85 86
end

class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase
  arguments %w(Account name:string age:integer)
  tests Rails::Generators::ModelGenerator

87 88 89 90 91
  def test_module_file_is_not_created
    run_generator
    assert_no_file "app/models/test_app.rb"
  end

92 93 94 95 96 97 98 99 100
  def test_adds_namespace_to_model
    run_generator
    assert_file "app/models/test_app/account.rb", /module TestApp/, /  class Account < ActiveRecord::Base/
  end

  def test_model_with_namespace
    run_generator ["admin/account"]
    assert_file "app/models/test_app/admin.rb", /module TestApp/, /module Admin/
    assert_file "app/models/test_app/admin.rb", /def self\.table_name_prefix/
101
    assert_file "app/models/test_app/admin.rb", /'test_app_admin_'/
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
    assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ActiveRecord::Base/
  end

  def test_migration
    run_generator
    assert_migration "db/migrate/create_test_app_accounts.rb", /create_table :test_app_accounts/, /class CreateTestAppAccounts < ActiveRecord::Migration/
  end

  def test_migration_with_namespace
    run_generator ["Gallery::Image"]
    assert_migration "db/migrate/create_test_app_gallery_images", /class CreateTestAppGalleryImages < ActiveRecord::Migration/
    assert_no_migration "db/migrate/create_test_app_images"
  end

  def test_migration_with_nested_namespace
    run_generator ["Admin::Gallery::Image"]
    assert_no_migration "db/migrate/create_images"
    assert_no_migration "db/migrate/create_gallery_images"
    assert_migration "db/migrate/create_test_app_admin_gallery_images", /class CreateTestAppAdminGalleryImages < ActiveRecord::Migration/
    assert_migration "db/migrate/create_test_app_admin_gallery_images", /create_table :test_app_admin_gallery_images/
  end

  def test_migration_with_nested_namespace_without_pluralization
    ActiveRecord::Base.pluralize_table_names = false
    run_generator ["Admin::Gallery::Image"]
    assert_no_migration "db/migrate/create_images"
    assert_no_migration "db/migrate/create_gallery_images"
    assert_no_migration "db/migrate/create_test_app_admin_gallery_images"
    assert_migration "db/migrate/create_test_app_admin_gallery_image", /class CreateTestAppAdminGalleryImage < ActiveRecord::Migration/
    assert_migration "db/migrate/create_test_app_admin_gallery_image", /create_table :test_app_admin_gallery_image/
  ensure
    ActiveRecord::Base.pluralize_table_names = true
  end

  def test_invokes_default_test_framework
    run_generator
M
Mike Moore 已提交
138
    assert_file "test/models/test_app/account_test.rb", /module TestApp/, /class AccountTest < ActiveSupport::TestCase/
139 140 141
    assert_file "test/fixtures/test_app/accounts.yml", /name: MyString/, /age: 1/
  end
end
142

143 144 145 146 147 148
class NamespacedMailerGeneratorTest < NamespacedGeneratorTestCase
  arguments %w(notifier foo bar)
  tests Rails::Generators::MailerGenerator

  def test_mailer_skeleton_is_created
    run_generator
149
    assert_file "app/mailers/test_app/notifier_mailer.rb" do |mailer|
150
      assert_match(/module TestApp/, mailer)
151
      assert_match(/class NotifierMailer < ApplicationMailer/, mailer)
152
      assert_no_match(/default from: "from@example.com"/, mailer)
153 154 155 156 157
    end
  end

  def test_mailer_with_i18n_helper
    run_generator
158
    assert_file "app/mailers/test_app/notifier_mailer.rb" do |mailer|
C
Carlos Souza 已提交
159 160
      assert_match(/en\.notifier_mailer\.foo\.subject/, mailer)
      assert_match(/en\.notifier_mailer\.bar\.subject/, mailer)
161 162 163 164 165
    end
  end

  def test_invokes_default_test_framework
    run_generator
166
    assert_file "test/mailers/test_app/notifier_mailer_test.rb" do |test|
167
      assert_match(/module TestApp/, test)
168
      assert_match(/class NotifierMailerTest < ActionMailer::TestCase/, test)
169 170
      assert_match(/test "foo"/, test)
      assert_match(/test "bar"/, test)
171 172 173 174 175
    end
  end

  def test_invokes_default_template_engine
    run_generator
Y
yuuji.yaginuma 已提交
176 177
    assert_file "app/views/test_app/notifier_mailer/foo.text.erb" do |view|
      assert_match(%r(app/views/test_app/notifier_mailer/foo\.text\.erb), view)
178
      assert_match(/<%= @greeting %>/, view)
179 180
    end

Y
yuuji.yaginuma 已提交
181 182
    assert_file "app/views/test_app/notifier_mailer/bar.text.erb" do |view|
      assert_match(%r(app/views/test_app/notifier_mailer/bar\.text\.erb), view)
183
      assert_match(/<%= @greeting %>/, view)
184 185 186 187 188
    end
  end

  def test_invokes_default_template_engine_even_with_no_action
    run_generator ["notifier"]
Y
yuuji.yaginuma 已提交
189
    assert_file "app/views/test_app/notifier_mailer"
190 191
  end
end
192 193 194 195 196 197 198 199 200 201 202 203 204

class NamespacedScaffoldGeneratorTest < NamespacedGeneratorTestCase
  include GeneratorsTestHelper
  arguments %w(product_line title:string price:integer)
  tests Rails::Generators::ScaffoldGenerator

  setup :copy_routes

  def test_scaffold_on_invoke
    run_generator

    # Model
    assert_file "app/models/test_app/product_line.rb", /module TestApp\n  class ProductLine < ActiveRecord::Base/
M
Mike Moore 已提交
205
    assert_file "test/models/test_app/product_line_test.rb", /module TestApp\n  class ProductLineTest < ActiveSupport::TestCase/
206 207 208 209 210 211 212 213 214
    assert_file "test/fixtures/test_app/product_lines.yml"
    assert_migration "db/migrate/create_test_app_product_lines.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_match(/resources :product_lines$/, route)
    end

    # Controller
215
    assert_file "app/controllers/test_app/product_lines_controller.rb",
216
                /require_dependency "test_app\/application_controller"/,
217 218
                /module TestApp/,
                /class ProductLinesController < ApplicationController/
219

M
Mike Moore 已提交
220
    assert_file "test/controllers/test_app/product_lines_controller_test.rb",
221 222 223
                /module TestApp\n  class ProductLinesControllerTest < ActionController::TestCase/

    # Views
224 225 226
    %w(index edit new show _form).each do |view|
      assert_file "app/views/test_app/product_lines/#{view}.html.erb"
    end
227 228 229 230 231 232
    assert_no_file "app/views/layouts/test_app/product_lines.html.erb"

    # Helpers
    assert_file "app/helpers/test_app/product_lines_helper.rb"

    # Stylesheets
233
    assert_file "app/assets/stylesheets/scaffold.css"
234 235 236 237
  end

  def test_scaffold_on_revoke
    run_generator
238
    run_generator ["product_line"], behavior: :revoke
239 240 241

    # Model
    assert_no_file "app/models/test_app/product_line.rb"
M
Mike Moore 已提交
242
    assert_no_file "test/models/test_app/product_line_test.rb"
243 244 245 246 247 248 249 250 251 252
    assert_no_file "test/fixtures/test_app/product_lines.yml"
    assert_no_migration "db/migrate/create_test_app_product_lines.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_no_match(/resources :product_lines$/, route)
    end

    # Controller
    assert_no_file "app/controllers/test_app/product_lines_controller.rb"
M
Mike Moore 已提交
253
    assert_no_file "test/controllers/test_app/product_lines_controller_test.rb"
254 255 256 257 258 259 260 261 262

    # Views
    assert_no_file "app/views/test_app/product_lines"
    assert_no_file "app/views/test_app/layouts/product_lines.html.erb"

    # Helpers
    assert_no_file "app/helpers/test_app/product_lines_helper.rb"

    # Stylesheets (should not be removed)
263
    assert_file "app/assets/stylesheets/scaffold.css"
264 265 266 267 268 269 270 271
  end

  def test_scaffold_with_namespace_on_invoke
    run_generator [ "admin/role", "name:string", "description:string" ]

    # Model
    assert_file "app/models/test_app/admin.rb", /module TestApp\n  module Admin/
    assert_file "app/models/test_app/admin/role.rb", /module TestApp\n  class Admin::Role < ActiveRecord::Base/
M
Mike Moore 已提交
272
    assert_file "test/models/test_app/admin/role_test.rb", /module TestApp\n  class Admin::RoleTest < ActiveSupport::TestCase/
273 274 275 276 277
    assert_file "test/fixtures/test_app/admin/roles.yml"
    assert_migration "db/migrate/create_test_app_admin_roles.rb"

    # Route
    assert_file "config/routes.rb" do |route|
278
      assert_match(/^  namespace :admin do\n    resources :roles\n  end$/, route)
279 280 281 282 283
    end

    # Controller
    assert_file "app/controllers/test_app/admin/roles_controller.rb" do |content|
      assert_match(/module TestApp\n  class Admin::RolesController < ApplicationController/, content)
284
      assert_match(%r(require_dependency "test_app/application_controller"), content)
285 286
    end

M
Mike Moore 已提交
287
    assert_file "test/controllers/test_app/admin/roles_controller_test.rb",
288 289 290
                /module TestApp\n  class Admin::RolesControllerTest < ActionController::TestCase/

    # Views
291 292 293
    %w(index edit new show _form).each do |view|
      assert_file "app/views/test_app/admin/roles/#{view}.html.erb"
    end
294 295 296 297 298 299
    assert_no_file "app/views/layouts/admin/roles.html.erb"

    # Helpers
    assert_file "app/helpers/test_app/admin/roles_helper.rb"

    # Stylesheets
300
    assert_file "app/assets/stylesheets/scaffold.css"
301 302 303 304
  end

  def test_scaffold_with_namespace_on_revoke
    run_generator [ "admin/role", "name:string", "description:string" ]
305
    run_generator [ "admin/role" ], behavior: :revoke
306 307 308 309

    # Model
    assert_file "app/models/test_app/admin.rb"	# ( should not be remove )
    assert_no_file "app/models/test_app/admin/role.rb"
M
Mike Moore 已提交
310
    assert_no_file "test/models/test_app/admin/role_test.rb"
311 312 313 314 315
    assert_no_file "test/fixtures/test_app/admin/roles.yml"
    assert_no_migration "db/migrate/create_test_app_admin_roles.rb"

    # Route
    assert_file "config/routes.rb" do |route|
316
      assert_no_match(/^  namespace :admin do\n    resources :roles\n  end$$/, route)
317 318 319 320
    end

    # Controller
    assert_no_file "app/controllers/test_app/admin/roles_controller.rb"
M
Mike Moore 已提交
321
    assert_no_file "test/controllers/test_app/admin/roles_controller_test.rb"
322 323 324 325 326 327 328 329 330

    # Views
    assert_no_file "app/views/test_app/admin/roles"
    assert_no_file "app/views/layouts/test_app/admin/roles.html.erb"

    # Helpers
    assert_no_file "app/helpers/test_app/admin/roles_helper.rb"

    # Stylesheets (should not be removed)
331
    assert_file "app/assets/stylesheets/scaffold.css"
332
  end
M
Mike Moore 已提交
333

334 335 336 337 338 339
  def test_scaffold_with_nested_namespace_on_invoke
    run_generator [ "admin/user/special/role", "name:string", "description:string" ]

    # Model
    assert_file "app/models/test_app/admin/user/special.rb", /module TestApp\n  module Admin/
    assert_file "app/models/test_app/admin/user/special/role.rb", /module TestApp\n  class Admin::User::Special::Role < ActiveRecord::Base/
M
Mike Moore 已提交
340
    assert_file "test/models/test_app/admin/user/special/role_test.rb", /module TestApp\n  class Admin::User::Special::RoleTest < ActiveSupport::TestCase/
341 342 343 344 345 346 347 348 349 350 351 352 353
    assert_file "test/fixtures/test_app/admin/user/special/roles.yml"
    assert_migration "db/migrate/create_test_app_admin_user_special_roles.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_match(/^  namespace :admin do\n    namespace :user do\n      namespace :special do\n        resources :roles\n      end\n    end\n  end$/, route)
    end

    # Controller
    assert_file "app/controllers/test_app/admin/user/special/roles_controller.rb" do |content|
      assert_match(/module TestApp\n  class Admin::User::Special::RolesController < ApplicationController/, content)
    end

M
Mike Moore 已提交
354
    assert_file "test/controllers/test_app/admin/user/special/roles_controller_test.rb",
355 356 357
                /module TestApp\n  class Admin::User::Special::RolesControllerTest < ActionController::TestCase/

    # Views
358 359 360
    %w(index edit new show _form).each do |view|
      assert_file "app/views/test_app/admin/user/special/roles/#{view}.html.erb"
    end
361 362 363 364 365 366 367 368 369 370 371
    assert_no_file "app/views/layouts/admin/user/special/roles.html.erb"

    # Helpers
    assert_file "app/helpers/test_app/admin/user/special/roles_helper.rb"

    # Stylesheets
    assert_file "app/assets/stylesheets/scaffold.css"
  end

  def test_scaffold_with_nested_namespace_on_revoke
    run_generator [ "admin/user/special/role", "name:string", "description:string" ]
372
    run_generator [ "admin/user/special/role" ], behavior: :revoke
373 374 375 376

    # Model
    assert_file "app/models/test_app/admin/user/special.rb"	# ( should not be remove )
    assert_no_file "app/models/test_app/admin/user/special/role.rb"
M
Mike Moore 已提交
377
    assert_no_file "test/models/test_app/admin/user/special/role_test.rb"
378 379 380 381 382 383 384 385 386 387
    assert_no_file "test/fixtures/test_app/admin/user/special/roles.yml"
    assert_no_migration "db/migrate/create_test_app_admin_user_special_roles.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_no_match(/^  namespace :admin do\n    namespace :user do\n      namespace :special do\n        resources :roles\n      end\n    end\n  end$/, route)
    end

    # Controller
    assert_no_file "app/controllers/test_app/admin/user/special/roles_controller.rb"
M
Mike Moore 已提交
388
    assert_no_file "test/controllers/test_app/admin/user/special/roles_controller_test.rb"
389 390 391 392 393 394 395 396 397 398

    # Views
    assert_no_file "app/views/test_app/admin/user/special/roles"

    # Helpers
    assert_no_file "app/helpers/test_app/admin/user/special/roles_helper.rb"

    # Stylesheets (should not be removed)
    assert_file "app/assets/stylesheets/scaffold.css"
  end
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

  def test_api_scaffold_with_namespace_on_invoke
    run_generator [ "admin/role", "name:string", "description:string", "--api" ]

    # Model
    assert_file "app/models/test_app/admin.rb", /module TestApp\n  module Admin/
    assert_file "app/models/test_app/admin/role.rb", /module TestApp\n  class Admin::Role < ActiveRecord::Base/
    assert_file "test/models/test_app/admin/role_test.rb", /module TestApp\n  class Admin::RoleTest < ActiveSupport::TestCase/
    assert_file "test/fixtures/test_app/admin/roles.yml"
    assert_migration "db/migrate/create_test_app_admin_roles.rb"

    # Route
    assert_file "config/routes.rb" do |route|
      assert_match(/^  namespace :admin do\n    resources :roles\n  end$/, route)
    end

    # Controller
    assert_file "app/controllers/test_app/admin/roles_controller.rb" do |content|
      assert_match(/module TestApp\n  class Admin::RolesController < ApplicationController/, content)
      assert_match(%r(require_dependency "test_app/application_controller"), content)
    end
    assert_file "test/controllers/test_app/admin/roles_controller_test.rb",
                /module TestApp\n  class Admin::RolesControllerTest < ActionController::TestCase/
  end
423
end