diff options
Diffstat (limited to 'railties/test/generators')
32 files changed, 6921 insertions, 0 deletions
diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb new file mode 100644 index 0000000000..af475400a1 --- /dev/null +++ b/railties/test/generators/actions_test.rb @@ -0,0 +1,515 @@ +# 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'/ + 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", /source 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend/ + 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", /gem 'will-paginate'\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend/ + 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" + 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", /\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend/ + 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", /\ngithub 'user\/repo' do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend/ + 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", /\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend/ + 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 + end + + assert_file "Gemfile", /\ngroup :magic do\n github 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\nend\n/ + 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_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_capify_should_run_the_capify_command + content = capture(:stderr) do + assert_called_with(generator, :run, ["capify .", verbose: false]) do + action :capify! + end + end + assert_match(/DEPRECATION WARNING: `capify!` is deprecated/, content) + 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 diff --git a/railties/test/generators/api_app_generator_test.rb b/railties/test/generators/api_app_generator_test.rb new file mode 100644 index 0000000000..4b9878187b --- /dev/null +++ b/railties/test/generators/api_app_generator_test.rb @@ -0,0 +1,181 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/app/app_generator" + +class ApiAppGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + tests Rails::Generators::AppGenerator + + arguments [destination_root, "--api"] + + def setup + Rails.application = TestApp::Application + super + + Kernel.silence_warnings do + Thor::Base.shell.attr_accessor :always_force + @shell = Thor::Base.shell.new + @shell.always_force = true + end + end + + def teardown + super + Rails.application = TestApp::Application.instance + end + + def test_skeleton_is_created + run_generator + + default_files.each { |path| assert_file path } + skipped_files.each { |path| assert_no_file path } + end + + def test_api_modified_files + run_generator + + assert_file ".gitignore" do |content| + assert_no_match(/\/public\/assets/, content) + end + + assert_file "Gemfile" do |content| + assert_no_match(/gem 'sass-rails'/, content) + assert_no_match(/gem 'web-console'/, content) + assert_no_match(/gem 'capybara'/, content) + assert_no_match(/gem 'selenium-webdriver'/, content) + assert_match(/# gem 'jbuilder'/, content) + assert_match(/# gem 'rack-cors'/, content) + end + + assert_file "config/application.rb", /config\.api_only = true/ + assert_file "app/controllers/application_controller.rb", /ActionController::API/ + end + + def test_generator_if_skip_action_cable_is_given + run_generator [destination_root, "--api", "--skip-action-cable"] + assert_file "config/application.rb", /#\s+require\s+["']action_cable\/engine["']/ + assert_no_file "config/cable.yml" + assert_no_file "app/channels" + assert_file "Gemfile" do |content| + assert_no_match(/redis/, content) + end + end + + def test_generator_if_skip_action_mailer_is_given + run_generator [destination_root, "--api", "--skip-action-mailer"] + assert_file "config/application.rb", /#\s+require\s+["']action_mailer\/railtie["']/ + assert_file "config/environments/development.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_file "config/environments/test.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_file "config/environments/production.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_no_directory "app/mailers" + assert_no_directory "test/mailers" + assert_no_directory "app/views" + end + + def test_app_update_does_not_generate_unnecessary_config_files + run_generator + + generator = Rails::Generators::AppGenerator.new ["rails"], + { api: true, update: true }, { destination_root: destination_root, shell: @shell } + quietly { generator.send(:update_config_files) } + + assert_no_file "config/initializers/cookies_serializer.rb" + assert_no_file "config/initializers/assets.rb" + assert_no_file "config/initializers/content_security_policy.rb" + end + + def test_app_update_does_not_generate_unnecessary_bin_files + run_generator + + generator = Rails::Generators::AppGenerator.new ["rails"], + { api: true, update: true }, { destination_root: destination_root, shell: @shell } + quietly { generator.send(:update_bin_files) } + + assert_no_file "bin/yarn" + end + + private + + def default_files + %w(.gitignore + .ruby-version + README.md + Gemfile + Rakefile + config.ru + app/channels + app/controllers + app/mailers + app/models + app/views/layouts + app/views/layouts/mailer.html.erb + app/views/layouts/mailer.text.erb + bin/rails + bin/rake + bin/setup + bin/update + config/application.rb + config/boot.rb + config/cable.yml + config/environment.rb + config/environments + config/environments/development.rb + config/environments/production.rb + config/environments/test.rb + config/initializers + config/initializers/application_controller_renderer.rb + config/initializers/backtrace_silencers.rb + config/initializers/cors.rb + config/initializers/filter_parameter_logging.rb + config/initializers/inflections.rb + config/initializers/mime_types.rb + config/initializers/wrap_parameters.rb + config/locales + config/locales/en.yml + config/puma.rb + config/routes.rb + config/credentials.yml.enc + config/spring.rb + config/storage.yml + db + db/seeds.rb + lib + lib/tasks + log + test/fixtures + test/controllers + test/integration + test/models + tmp + vendor + ) + end + + def skipped_files + %w(app/assets + app/helpers + app/views/layouts/application.html.erb + bin/yarn + config/initializers/assets.rb + config/initializers/cookies_serializer.rb + config/initializers/content_security_policy.rb + lib/assets + test/helpers + tmp/cache/assets + public/404.html + public/422.html + public/500.html + public/apple-touch-icon-precomposed.png + public/apple-touch-icon.png + public/favicon.ico + package.json + ) + end +end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb new file mode 100644 index 0000000000..154cd3e80c --- /dev/null +++ b/railties/test/generators/app_generator_test.rb @@ -0,0 +1,1076 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/app/app_generator" +require "generators/shared_generator_tests" + +DEFAULT_APP_FILES = %w( + .gitignore + .ruby-version + README.md + Gemfile + Rakefile + config.ru + app/assets/config/manifest.js + app/assets/images + app/javascript + app/javascript/channels + app/javascript/channels/consumer.js + app/javascript/channels/index.js + app/javascript/packs/application.js + app/assets/stylesheets + app/assets/stylesheets/application.css + app/channels/application_cable/channel.rb + app/channels/application_cable/connection.rb + app/controllers + app/controllers/application_controller.rb + app/controllers/concerns + app/helpers + app/helpers/application_helper.rb + app/mailers + app/mailers/application_mailer.rb + app/models + app/models/application_record.rb + app/models/concerns + app/jobs + app/jobs/application_job.rb + app/views/layouts + app/views/layouts/application.html.erb + app/views/layouts/mailer.html.erb + app/views/layouts/mailer.text.erb + bin/rails + bin/rake + bin/setup + bin/update + bin/yarn + config/application.rb + config/boot.rb + config/cable.yml + config/environment.rb + config/environments + config/environments/development.rb + config/environments/production.rb + config/environments/test.rb + config/initializers + config/initializers/application_controller_renderer.rb + config/initializers/assets.rb + config/initializers/backtrace_silencers.rb + config/initializers/cookies_serializer.rb + config/initializers/content_security_policy.rb + config/initializers/filter_parameter_logging.rb + config/initializers/inflections.rb + config/initializers/mime_types.rb + config/initializers/wrap_parameters.rb + config/locales + config/locales/en.yml + config/puma.rb + config/routes.rb + config/credentials.yml.enc + config/spring.rb + config/storage.yml + db + db/seeds.rb + lib + lib/tasks + lib/assets + log + package.json + public + storage + test/application_system_test_case.rb + test/test_helper.rb + test/fixtures + test/fixtures/files + test/controllers + test/models + test/helpers + test/mailers + test/integration + test/system + vendor + tmp + tmp/cache + tmp/cache/assets + tmp/storage +) + +class AppGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments [destination_root] + + # brings setup, teardown, and some tests + include SharedGeneratorTests + + def default_files + ::DEFAULT_APP_FILES + end + + def test_skip_bundle + assert_not_called(generator([destination_root], skip_bundle: true, skip_webpack_install: true), :bundle_command) do + quietly { generator.invoke_all } + # skip_bundle is only about running bundle install, ensure the Gemfile is still + # generated. + assert_file "Gemfile" + end + end + + def test_assets + run_generator + + assert_file("app/views/layouts/application.html.erb", /stylesheet_link_tag\s+'application', media: 'all', 'data-turbolinks-track': 'reload'/) + assert_file("app/views/layouts/application.html.erb", /javascript_pack_tag\s+'application', 'data-turbolinks-track': 'reload'/) + assert_file("app/assets/stylesheets/application.css") + assert_file("app/javascript/packs/application.js") + end + + def test_application_job_file_present + run_generator + assert_file("app/jobs/application_job.rb") + end + + def test_invalid_application_name_raises_an_error + content = capture(:stderr) { run_generator [File.join(destination_root, "43-things")] } + assert_equal "Invalid application name 43-things. Please give a name which does not start with numbers.\n", content + end + + def test_invalid_application_name_is_fixed + run_generator [File.join(destination_root, "things-43")] + assert_file "things-43/config/environment.rb", /Rails\.application\.initialize!/ + assert_file "things-43/config/application.rb", /^module Things43$/ + end + + def test_application_new_exits_with_non_zero_code_on_invalid_application_name + quietly { system "rails new test --no-rc" } + assert_equal false, $?.success? + end + + def test_application_new_exits_with_message_and_non_zero_code_when_generating_inside_existing_rails_directory + app_root = File.join(destination_root, "myfirstapp") + run_generator [app_root] + output = nil + Dir.chdir(app_root) do + output = `rails new mysecondapp` + end + assert_equal "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\nType 'rails' for help.\n", output + assert_equal false, $?.success? + end + + def test_application_new_show_help_message_inside_existing_rails_directory + app_root = File.join(destination_root, "myfirstapp") + run_generator [app_root] + output = Dir.chdir(app_root) do + `rails new --help` + end + assert_match(/rails new APP_PATH \[options\]/, output) + assert_equal true, $?.success? + end + + def test_application_name_is_detected_if_it_exists_and_app_folder_renamed + app_root = File.join(destination_root, "myapp") + app_moved_root = File.join(destination_root, "myapp_moved") + + run_generator [app_root] + + stub_rails_application(app_moved_root) do + Rails.application.stub(:is_a?, -> *args { Rails::Application }) do + FileUtils.mv(app_root, app_moved_root) + + # make sure we are in correct dir + FileUtils.cd(app_moved_root) + + generator = Rails::Generators::AppGenerator.new ["rails"], [], + destination_root: app_moved_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file "myapp_moved/config/environment.rb", /Rails\.application\.initialize!/ + end + end + end + + def test_app_update_generates_correct_session_key + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + end + end + + def test_new_application_use_json_serialzier + run_generator + + assert_file("config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/) + end + + def test_new_application_not_include_api_initializers + run_generator + + assert_no_file "config/initializers/cors.rb" + end + + def test_new_application_doesnt_need_defaults + run_generator + assert_no_file "config/initializers/new_framework_defaults_6_0.rb" + end + + def test_new_application_load_defaults + app_root = File.join(destination_root, "myfirstapp") + run_generator [app_root] + + output = nil + + assert_file "#{app_root}/config/application.rb", /\s+config\.load_defaults #{Rails::VERSION::STRING.to_f}/ + + Dir.chdir(app_root) do + output = `SKIP_REQUIRE_WEBPACKER=true ./bin/rails r "puts Rails.application.config.assets.unknown_asset_fallback"` + end + + assert_equal "false\n", output + end + + def test_csp_initializer_include_connect_src_example + run_generator + + assert_file "config/initializers/content_security_policy.rb" do |content| + assert_match(/# policy\.connect_src/, content) + end + end + + def test_app_update_keep_the_cookie_serializer_if_it_is_already_configured + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file("#{app_root}/config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/) + end + end + + def test_app_update_set_the_cookie_serializer_to_marshal_if_it_is_not_already_configured + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + FileUtils.rm("#{app_root}/config/initializers/cookies_serializer.rb") + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file("#{app_root}/config/initializers/cookies_serializer.rb", + /Valid options are :json, :marshal, and :hybrid\.\nRails\.application\.config\.action_dispatch\.cookies_serializer = :marshal/) + end + end + + def test_app_update_create_new_framework_defaults + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + assert_no_file "#{app_root}/config/initializers/new_framework_defaults_6_0.rb" + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], { update: true }, { destination_root: app_root, shell: @shell } + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + + assert_file "#{app_root}/config/initializers/new_framework_defaults_6_0.rb" + end + end + + def test_app_update_does_not_create_rack_cors + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_no_file "#{app_root}/config/initializers/cors.rb" + end + end + + def test_app_update_does_not_remove_rack_cors_if_already_present + app_root = File.join(destination_root, "myapp") + run_generator [app_root] + + FileUtils.touch("#{app_root}/config/initializers/cors.rb") + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], [], destination_root: app_root, shell: @shell + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + assert_file "#{app_root}/config/initializers/cors.rb" + end + end + + def test_app_update_does_not_generate_yarn_contents_when_bin_yarn_is_not_used + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-javascript"] + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], { update: true, skip_javascript: true }, { destination_root: app_root, shell: @shell } + generator.send(:app_const) + quietly { generator.send(:update_bin_files) } + + assert_no_file "#{app_root}/bin/yarn" + + assert_file "#{app_root}/bin/setup" do |content| + assert_no_match(/system\('bin\/yarn'\)/, content) + end + + assert_file "#{app_root}/bin/update" do |content| + assert_no_match(/system\('bin\/yarn'\)/, content) + end + end + end + + def test_app_update_does_not_generate_assets_initializer_when_skip_sprockets_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-sprockets"] + + stub_rails_application(app_root) do + generator = Rails::Generators::AppGenerator.new ["rails"], { update: true, skip_sprockets: true }, { destination_root: app_root, shell: @shell } + generator.send(:app_const) + quietly { generator.send(:update_config_files) } + + assert_no_file "#{app_root}/config/initializers/assets.rb" + end + end + + def test_app_update_does_not_generate_spring_contents_when_skip_spring_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-spring"] + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_no_file "#{app_root}/config/spring.rb" + end + + def test_app_update_does_not_generate_action_cable_contents_when_skip_action_cable_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-action-cable"] + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_no_file "#{app_root}/config/cable.yml" + assert_file "#{app_root}/config/environments/production.rb" do |content| + assert_no_match(/config\.action_cable/, content) + end + end + + def test_app_update_does_not_generate_bootsnap_contents_when_skip_bootsnap_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-bootsnap"] + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_file "#{app_root}/config/boot.rb" do |content| + assert_no_match(/require 'bootsnap\/setup'/, content) + end + end + + def test_gem_for_active_storage + run_generator + assert_file "Gemfile", /^# gem 'image_processing'/ + end + + def test_gem_for_active_storage_when_skip_active_storage_is_given + run_generator [destination_root, "--skip-active-storage"] + + assert_no_gem "image_processing" + end + + def test_app_update_does_not_generate_active_storage_contents_when_skip_active_storage_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-active-storage"] + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_file "#{app_root}/config/environments/development.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{app_root}/config/environments/production.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{app_root}/config/environments/test.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_no_file "#{app_root}/config/storage.yml" + end + + def test_app_update_does_not_generate_active_storage_contents_when_skip_active_record_is_given + app_root = File.join(destination_root, "myapp") + run_generator [app_root, "--skip-active-record"] + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_file "#{app_root}/config/environments/development.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{app_root}/config/environments/production.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{app_root}/config/environments/test.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_no_file "#{app_root}/config/storage.yml" + end + + def test_app_update_does_not_change_config_target_version + run_generator + + FileUtils.cd(destination_root) do + config = "config/application.rb" + content = File.read(config) + File.write(config, content.gsub(/config\.load_defaults #{Rails::VERSION::STRING.to_f}/, "config.load_defaults 5.1")) + quietly { system("bin/rails app:update") } + end + + assert_file "config/application.rb", /\s+config\.load_defaults 5\.1/ + end + + def test_app_update_does_not_change_app_name_when_app_name_is_hyphenated_name + app_root = File.join(destination_root, "hyphenated-app") + run_generator [app_root, "-d", "postgresql"] + + assert_file "#{app_root}/config/database.yml" do |content| + assert_match(/hyphenated_app_development/, content) + assert_no_match(/hyphenated-app_development/, content) + end + + assert_file "#{app_root}/config/cable.yml" do |content| + assert_match(/hyphenated_app/, content) + assert_no_match(/hyphenated-app/, content) + end + + FileUtils.cd(app_root) do + quietly { system("bin/rails app:update") } + end + + assert_file "#{app_root}/config/cable.yml" do |content| + assert_match(/hyphenated_app/, content) + assert_no_match(/hyphenated-app/, content) + end + end + + def test_application_names_are_not_singularized + run_generator [File.join(destination_root, "hats")] + assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/ + end + + def test_gemfile_has_no_whitespace_errors + run_generator + absolute = File.expand_path("Gemfile", destination_root) + File.open(absolute, "r") do |f| + f.each_line do |line| + assert_no_match %r{/^[ \t]+$/}, line + end + end + end + + def test_config_database_is_added_by_default + run_generator + assert_file "config/database.yml", /sqlite3/ + if defined?(JRUBY_VERSION) + assert_gem "activerecord-jdbcsqlite3-adapter" + else + assert_gem "sqlite3" + end + end + + def test_config_mysql_database + run_generator([destination_root, "-d", "mysql"]) + assert_file "config/database.yml", /mysql/ + if defined?(JRUBY_VERSION) + assert_gem "activerecord-jdbcmysql-adapter" + else + assert_gem "mysql2", "'>= 0.4.4'" + end + end + + def test_config_database_app_name_with_period + run_generator [File.join(destination_root, "common.usage.com"), "-d", "postgresql"] + assert_file "common.usage.com/config/database.yml", /common_usage_com/ + end + + def test_config_postgresql_database + run_generator([destination_root, "-d", "postgresql"]) + assert_file "config/database.yml", /postgresql/ + if defined?(JRUBY_VERSION) + assert_gem "activerecord-jdbcpostgresql-adapter" + else + assert_gem "pg", "'>= 0.18', '< 2.0'" + end + end + + def test_config_jdbcmysql_database + run_generator([destination_root, "-d", "jdbcmysql"]) + assert_file "config/database.yml", /mysql/ + assert_gem "activerecord-jdbcmysql-adapter" + end + + def test_config_jdbcsqlite3_database + run_generator([destination_root, "-d", "jdbcsqlite3"]) + assert_file "config/database.yml", /sqlite3/ + assert_gem "activerecord-jdbcsqlite3-adapter" + end + + def test_config_jdbcpostgresql_database + run_generator([destination_root, "-d", "jdbcpostgresql"]) + assert_file "config/database.yml", /postgresql/ + assert_gem "activerecord-jdbcpostgresql-adapter" + end + + def test_config_jdbc_database + run_generator([destination_root, "-d", "jdbc"]) + assert_file "config/database.yml", /jdbc/ + assert_file "config/database.yml", /mssql/ + assert_gem "activerecord-jdbc-adapter" + end + + if defined?(JRUBY_VERSION) + def test_config_jdbc_database_when_no_option_given + run_generator + assert_file "config/database.yml", /sqlite3/ + assert_gem "activerecord-jdbcsqlite3-adapter" + end + end + + def test_generator_defaults_to_puma_version + run_generator [destination_root] + assert_gem "puma", "'~> 3.11'" + end + + def test_generator_if_skip_puma_is_given + run_generator [destination_root, "--skip-puma"] + assert_no_file "config/puma.rb" + assert_no_gem "puma" + end + + def test_generator_has_assets_gems + run_generator + + assert_gem "sass-rails" + end + + def test_action_cable_redis_gems + run_generator + assert_file "Gemfile", /^# gem 'redis'/ + end + + def test_generator_if_skip_test_is_given + run_generator [destination_root, "--skip-test"] + + assert_file "config/application.rb", /#\s+require\s+["']rails\/test_unit\/railtie["']/ + + assert_no_gem "capybara" + assert_no_gem "selenium-webdriver" + assert_no_gem "chromedriver-helper" + + assert_no_directory("test") + end + + def test_generator_if_skip_system_test_is_given + run_generator [destination_root, "--skip-system-test"] + assert_no_gem "capybara" + assert_no_gem "selenium-webdriver" + assert_no_gem "chromedriver-helper" + + assert_directory("test") + + assert_no_directory("test/system") + end + + def test_does_not_generate_system_test_files_if_skip_system_test_is_given + run_generator [destination_root, "--skip-system-test"] + + Dir.chdir(destination_root) do + quietly { `./bin/rails g scaffold User` } + + assert_no_file("test/application_system_test_case.rb") + assert_no_file("test/system/users_test.rb") + end + end + + def test_javascript_is_skipped_if_required + run_generator [destination_root, "--skip-javascript"] + + assert_no_file "app/javascript" + + assert_file "app/views/layouts/application.html.erb" do |contents| + assert_match(/stylesheet_link_tag\s+'application', media: 'all' %>/, contents) + assert_no_match(/javascript_pack_tag\s+'application'/, contents) + end + end + + def test_inclusion_of_jbuilder + run_generator + assert_gem "jbuilder" + end + + def test_inclusion_of_a_debugger + run_generator + if defined?(JRUBY_VERSION) || RUBY_ENGINE == "rbx" + assert_no_gem "byebug" + else + assert_gem "byebug" + end + end + + def test_inclusion_of_listen_related_configuration_by_default + run_generator + if RbConfig::CONFIG["host_os"] =~ /darwin|linux/ + assert_listen_related_configuration + else + assert_no_listen_related_configuration + end + end + + def test_non_inclusion_of_listen_related_configuration_if_skip_listen + run_generator [destination_root, "--skip-listen"] + assert_no_listen_related_configuration + end + + def test_evented_file_update_checker_config + run_generator + assert_file "config/environments/development.rb" do |content| + if RbConfig::CONFIG["host_os"] =~ /darwin|linux/ + assert_match(/^\s*config\.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + else + assert_match(/^\s*# config\.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + end + end + end + + def test_template_from_dir_pwd + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) + end + + def test_usage_read_from_file + assert_called(File, :read, returns: "USAGE FROM FILE") do + assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc + end + end + + def test_default_usage + assert_called(Rails::Generators::AppGenerator, :usage_path, returns: nil) do + assert_match(/Create rails files for app generator/, Rails::Generators::AppGenerator.desc) + end + end + + def test_default_namespace + assert_match "rails:app", Rails::Generators::AppGenerator.namespace + end + + def test_file_is_added_for_backwards_compatibility + action :file, "lib/test_file.rb", "heres test data" + assert_file "lib/test_file.rb", "heres test data" + end + + def test_pretend_option + output = run_generator [File.join(destination_root, "myapp"), "--pretend"] + assert_no_match(/run bundle install/, output) + assert_no_match(/run git init/, output) + end + + def test_quiet_option + output = run_generator [File.join(destination_root, "myapp"), "--quiet"] + assert_empty output + end + + def test_force_option_overwrites_every_file_except_master_key + run_generator [File.join(destination_root, "myapp")] + output = run_generator [File.join(destination_root, "myapp"), "--force"] + assert_match(/force/, output) + assert_no_match("force config/master.key", output) + end + + def test_application_name_with_spaces + path = File.join(destination_root, "foo bar") + + # This also applies to MySQL apps but not with SQLite + run_generator [path, "-d", "postgresql"] + + assert_file "foo bar/config/database.yml", /database: foo_bar_development/ + end + + def test_web_console + run_generator + assert_gem "web-console" + end + + def test_web_console_with_dev_option + run_generator [destination_root, "--dev", "--skip-bundle"] + + assert_file "Gemfile" do |content| + assert_match(/gem 'web-console',\s+github: 'rails\/web-console'/, content) + assert_no_match(/\Agem 'web-console', '>= 3\.3\.0'\z/, content) + end + end + + def test_web_console_with_edge_option + run_generator [destination_root, "--edge"] + + assert_file "Gemfile" do |content| + assert_match(/gem 'web-console',\s+github: 'rails\/web-console'/, content) + assert_no_match(/\Agem 'web-console', '>= 3\.3\.0'\z/, content) + end + end + + def test_generation_runs_bundle_install + generator([destination_root], skip_webpack_install: true) + + assert_bundler_command_called("install") + end + + def test_dev_option + generator([destination_root], dev: true, skip_webpack_install: true) + + assert_bundler_command_called("install") + rails_path = File.expand_path("../../..", Rails.root) + assert_file "Gemfile", /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/ + end + + def test_edge_option + generator([destination_root], edge: true, skip_webpack_install: true) + + assert_bundler_command_called("install") + assert_file "Gemfile", %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$} + end + + def test_spring + run_generator + assert_gem "spring" + end + + def test_bundler_binstub + generator([destination_root], skip_webpack_install: true) + + assert_bundler_command_called("binstubs bundler") + end + + def test_spring_binstubs + jruby_skip "spring doesn't run on JRuby" + + generator([destination_root], skip_webpack_install: true) + + assert_bundler_command_called("exec spring binstub --all") + end + + def test_spring_no_fork + jruby_skip "spring doesn't run on JRuby" + assert_called_with(Process, :respond_to?, [[:fork], [:fork]], returns: false) do + run_generator + + assert_no_gem "spring" + end + end + + def test_skip_spring + run_generator [destination_root, "--skip-spring"] + + assert_no_file "config/spring.rb" + assert_no_gem "spring" + end + + def test_spring_with_dev_option + run_generator [destination_root, "--dev", "--skip-bundle"] + + assert_no_gem "spring" + end + + def test_skip_javascript_option + command_check = -> command, *_ do + @called ||= 0 + if command == "webpacker:install" + @called += 1 + assert_equal 0, @called, "webpacker:install expected not to be called, but was called #{@called} times." + end + end + + generator([destination_root], skip_javascript: true).stub(:rails_command, command_check) do + generator.stub :bundle_command, nil do + quietly { generator.invoke_all } + end + end + + assert_no_gem "webpacker" + assert_file "config/initializers/content_security_policy.rb" do |content| + assert_no_match(/policy\.connect_src/, content) + end + end + + def test_webpack_option_with_js_framework + command_check = -> command, *_ do + case command + when "webpacker:install" + @webpacker ||= 0 + @webpacker += 1 + assert_equal 1, @webpacker, "webpacker:install expected to be called once, but was called #{@webpacker} times." + when "webpacker:install:react" + @react ||= 0 + @react += 1 + assert_equal 1, @react, "webpacker:install:react expected to be called once, but was called #{@react} times." + end + end + + generator([destination_root], webpack: "react").stub(:rails_command, command_check) do + generator.stub :bundle_command, nil do + quietly { generator.invoke_all } + end + end + + assert_gem "webpacker" + end + + def test_skip_webpack_install + command_check = -> command do + if command == "webpacker:install" + assert false, "webpacker:install expected not to be called." + end + end + + generator([destination_root], skip_webpack_install: true).stub(:rails_command, command_check) do + quietly { generator.invoke_all } + end + + assert_gem "webpacker" + end + + def test_generator_if_skip_turbolinks_is_given + run_generator [destination_root, "--skip-turbolinks"] + + assert_no_gem "turbolinks" + assert_file "app/views/layouts/application.html.erb" do |content| + assert_no_match(/data-turbolinks-track/, content) + end + assert_file "app/javascript/packs/application.js" do |content| + assert_no_match(/turbolinks/, content) + end + end + + def test_bootsnap + run_generator [destination_root, "--no-skip-bootsnap"] + + unless defined?(JRUBY_VERSION) + assert_gem "bootsnap" + assert_file "config/boot.rb" do |content| + assert_match(/require 'bootsnap\/setup'/, content) + end + else + assert_no_gem "bootsnap" + assert_file "config/boot.rb" do |content| + assert_no_match(/require 'bootsnap\/setup'/, content) + end + end + end + + def test_skip_bootsnap + run_generator [destination_root, "--skip-bootsnap"] + + assert_no_gem "bootsnap" + assert_file "config/boot.rb" do |content| + assert_no_match(/require 'bootsnap\/setup'/, content) + end + end + + def test_bootsnap_with_dev_option + run_generator [destination_root, "--dev", "--skip-bundle"] + + assert_no_gem "bootsnap" + assert_file "config/boot.rb" do |content| + assert_no_match(/require 'bootsnap\/setup'/, content) + end + end + + def test_inclusion_of_ruby_version + run_generator + + assert_file "Gemfile" do |content| + assert_match(/ruby '#{RUBY_VERSION}'/, content) + end + assert_file ".ruby-version" do |content| + if ENV["RBENV_VERSION"] + assert_match(/#{ENV["RBENV_VERSION"]}/, content) + elsif ENV["rvm_ruby_string"] + assert_match(/#{ENV["rvm_ruby_string"]}/, content) + else + assert_match(/#{RUBY_ENGINE}-#{RUBY_ENGINE_VERSION}/, content) + end + end + end + + def test_version_control_initializes_git_repo + run_generator [destination_root] + assert_directory ".git" + end + + def test_create_keeps + run_generator + folders_with_keep = %w( + app/assets/images + app/controllers/concerns + app/models/concerns + lib/tasks + lib/assets + log + test/fixtures + test/fixtures/files + test/controllers + test/mailers + test/models + test/helpers + test/integration + tmp + ) + folders_with_keep.each do |folder| + assert_file("#{folder}/.keep") + end + end + + def test_psych_gem + run_generator + gem_regex = /gem 'psych',\s+'~> 2\.0',\s+platforms: :rbx/ + + assert_file "Gemfile" do |content| + if defined?(Rubinius) + assert_match(gem_regex, content) + else + assert_no_match(gem_regex, content) + end + end + end + + def test_after_bundle_callback + path = "http://example.org/rails_template" + template = +%{ after_bundle { run 'echo ran after_bundle' } } + template.instance_eval "def read; self; end" # Make the string respond to read + + check_open = -> *args do + assert_equal [ path, "Accept" => "application/x-thor-template" ], args + template + end + + sequence = ["git init", "install", "binstubs bundler", "exec spring binstub --all", "webpacker:install", "echo ran after_bundle"] + @sequence_step ||= 0 + ensure_bundler_first = -> command, options = nil do + assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}" + @sequence_step += 1 + end + + generator([destination_root], template: path).stub(:open, check_open, template) do + generator.stub(:bundle_command, ensure_bundler_first) do + generator.stub(:run, ensure_bundler_first) do + generator.stub(:rails_command, ensure_bundler_first) do + quietly { generator.invoke_all } + end + end + end + end + + assert_equal 6, @sequence_step + end + + def test_gitignore + run_generator + + assert_file ".gitignore" do |content| + assert_match(/config\/master\.key/, content) + end + end + + def test_system_tests_directory_generated + run_generator + + assert_directory("test/system") + assert_file("test/system/.keep") + end + + unless Gem.win_platform? + def test_master_key_is_only_readable_by_the_owner + run_generator + + stat = File.stat("config/master.key") + assert_equal "100600", sprintf("%o", stat.mode) + end + end + + private + def stub_rails_application(root) + Rails.application.config.root = root + Rails.application.class.stub(:name, "Myapp") do + yield + end + end + + def action(*args, &block) + capture(:stdout) { generator.send(*args, &block) } + end + + def assert_gem(gem, constraint = nil) + if constraint + assert_file "Gemfile", /^\s*gem\s+["']#{gem}["'], #{constraint}$*/ + else + assert_file "Gemfile", /^\s*gem\s+["']#{gem}["']$*/ + end + end + + def assert_no_gem(gem) + assert_file "Gemfile" do |content| + assert_no_match(gem, content) + end + end + + def assert_listen_related_configuration + assert_gem "listen" + assert_gem "spring-watcher-listen" + + assert_file "config/environments/development.rb" do |content| + assert_match(/^\s*config\.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + end + end + + def assert_no_listen_related_configuration + assert_no_gem "listen" + + assert_file "config/environments/development.rb" do |content| + assert_match(/^\s*# config\.file_watcher = ActiveSupport::EventedFileUpdateChecker/, content) + end + end + + def assert_bundler_command_called(target_command) + command_check = -> (command, env = {}) do + @command_called ||= 0 + + case command + when target_command + @command_called += 1 + assert_equal 1, @command_called, "#{command} expected to be called once, but was called #{@command_called} times." + end + end + + generator.stub :bundle_command, command_check do + quietly { generator.invoke_all } + end + end +end diff --git a/railties/test/generators/application_record_generator_test.rb b/railties/test/generators/application_record_generator_test.rb new file mode 100644 index 0000000000..2c0aa7211b --- /dev/null +++ b/railties/test/generators/application_record_generator_test.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/application_record/application_record_generator" + +class ApplicationRecordGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_application_record_skeleton_is_created + run_generator + assert_file "app/models/application_record.rb" do |record| + assert_match(/class ApplicationRecord < ActiveRecord::Base/, record) + assert_match(/self\.abstract_class = true/, record) + end + end +end diff --git a/railties/test/generators/argv_scrubber_test.rb b/railties/test/generators/argv_scrubber_test.rb new file mode 100644 index 0000000000..9ef61dc978 --- /dev/null +++ b/railties/test/generators/argv_scrubber_test.rb @@ -0,0 +1,137 @@ +# frozen_string_literal: true + +require "active_support/test_case" +require "active_support/testing/autorun" +require "rails/generators/rails/app/app_generator" +require "tempfile" + +module Rails + module Generators + class ARGVScrubberTest < ActiveSupport::TestCase # :nodoc: + # Future people who read this... These tests are just to surround the + # current behavior of the ARGVScrubber, they do not mean that the class + # *must* act this way, I just want to prevent regressions. + + def test_version + ["-v", "--version"].each do |str| + scrubber = ARGVScrubber.new [str] + output = nil + exit_code = nil + scrubber.extend(Module.new { + define_method(:puts) { |string| output = string } + define_method(:exit) { |code| exit_code = code } + }) + scrubber.prepare! + assert_equal "Rails #{Rails::VERSION::STRING}", output + assert_equal 0, exit_code + end + end + + def test_default_help + argv = ["zomg", "how", "are", "you"] + scrubber = ARGVScrubber.new argv + args = scrubber.prepare! + assert_equal ["--help"] + argv.drop(1), args + end + + def test_prepare_returns_args + scrubber = ARGVScrubber.new ["hi mom"] + args = scrubber.prepare! + assert_equal "--help", args.first + end + + def test_no_mutations + scrubber = ARGVScrubber.new ["hi mom"].freeze + args = scrubber.prepare! + assert_equal "--help", args.first + end + + def test_new_command_no_rc + scrubber = Class.new(ARGVScrubber) { + def self.default_rc_file + File.join(Dir.tmpdir, "whatever") + end + }.new ["new"] + args = scrubber.prepare! + assert_equal [], args + end + + def test_new_homedir_rc + file = Tempfile.new "myrcfile" + file.puts "--hello-world" + file.flush + + message = nil + scrubber = Class.new(ARGVScrubber) { + define_singleton_method(:default_rc_file) do + file.path + end + define_method(:puts) { |msg| message = msg } + }.new ["new"] + args = scrubber.prepare! + assert_equal ["--hello-world"], args + assert_match "hello-world", message + assert_match file.path, message + ensure + file.close + file.unlink + end + + def test_rc_whitespace_separated + file = Tempfile.new "myrcfile" + file.puts "--hello --world" + file.flush + + scrubber = Class.new(ARGVScrubber) { + define_method(:puts) { |msg| } + }.new ["new", "--rc=#{file.path}"] + args = scrubber.prepare! + assert_equal ["--hello", "--world"], args + ensure + file.close + file.unlink + end + + def test_new_rc_option + file = Tempfile.new "myrcfile" + file.puts "--hello-world" + file.flush + + message = nil + scrubber = Class.new(ARGVScrubber) { + define_method(:puts) { |msg| message = msg } + }.new ["new", "--rc=#{file.path}"] + args = scrubber.prepare! + assert_equal ["--hello-world"], args + assert_match "hello-world", message + assert_match file.path, message + ensure + file.close + file.unlink + end + + def test_new_rc_option_and_custom_options + file = Tempfile.new "myrcfile" + file.puts "--hello" + file.puts "--world" + file.flush + + scrubber = Class.new(ARGVScrubber) { + define_method(:puts) { |msg| } + }.new ["new", "tenderapp", "--love", "--rc=#{file.path}"] + + args = scrubber.prepare! + assert_equal ["tenderapp", "--hello", "--world", "--love"], args + ensure + file.close + file.unlink + end + + def test_no_rc + scrubber = ARGVScrubber.new ["new", "--no-rc"] + args = scrubber.prepare! + assert_equal [], args + end + end + end +end diff --git a/railties/test/generators/assets_generator_test.rb b/railties/test/generators/assets_generator_test.rb new file mode 100644 index 0000000000..83d2429acf --- /dev/null +++ b/railties/test/generators/assets_generator_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/assets/assets_generator" + +class AssetsGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(posts) + + def test_assets + run_generator + assert_file "app/assets/stylesheets/posts.css" + end + + def test_skipping_assets + run_generator ["posts", "--no-stylesheets"] + assert_no_file "app/assets/stylesheets/posts.css" + end +end diff --git a/railties/test/generators/channel_generator_test.rb b/railties/test/generators/channel_generator_test.rb new file mode 100644 index 0000000000..1cb8465539 --- /dev/null +++ b/railties/test/generators/channel_generator_test.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/channel/channel_generator" + +class ChannelGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + tests Rails::Generators::ChannelGenerator + + def test_application_cable_skeleton_is_created + run_generator ["books"] + + assert_file "app/channels/application_cable/channel.rb" do |cable| + assert_match(/module ApplicationCable\n class Channel < ActionCable::Channel::Base\n/, cable) + end + + assert_file "app/channels/application_cable/connection.rb" do |cable| + assert_match(/module ApplicationCable\n class Connection < ActionCable::Connection::Base\n/, cable) + end + end + + def test_channel_is_created + run_generator ["chat"] + + assert_file "app/channels/chat_channel.rb" do |channel| + assert_match(/class ChatChannel < ApplicationCable::Channel/, channel) + end + + assert_file "app/javascript/channels/chat_channel.js" do |channel| + assert_match(/import consumer from "\.\/consumer"\s+consumer\.subscriptions\.create\("ChatChannel/, channel) + end + end + + def test_channel_with_multiple_actions_is_created + run_generator ["chat", "speak", "mute"] + + assert_file "app/channels/chat_channel.rb" do |channel| + assert_match(/class ChatChannel < ApplicationCable::Channel/, channel) + assert_match(/def speak/, channel) + assert_match(/def mute/, channel) + end + + assert_file "app/javascript/channels/chat_channel.js" do |channel| + assert_match(/import consumer from "\.\/consumer"\s+consumer\.subscriptions\.create\("ChatChannel/, channel) + assert_match(/,\n\n speak/, channel) + assert_match(/,\n\n mute: function\(\) \{\n return this\.perform\('mute'\);\n \}\n\}\);/, channel) + end + end + + def test_channel_asset_is_not_created_when_skip_assets_is_passed + run_generator ["chat", "--skip-assets"] + + assert_file "app/channels/chat_channel.rb" do |channel| + assert_match(/class ChatChannel < ApplicationCable::Channel/, channel) + end + + assert_no_file "app/javascript/channels/chat_channel.js" + end + + def test_consumer_js_is_created_if_not_present_already + run_generator ["chat"] + FileUtils.rm("#{destination_root}/app/javascript/channels/index.js") + FileUtils.rm("#{destination_root}/app/javascript/channels/consumer.js") + run_generator ["camp"] + + assert_file "app/javascript/channels/index.js" + assert_file "app/javascript/channels/consumer.js" + end + + def test_channel_on_revoke + run_generator ["chat"] + run_generator ["chat"], behavior: :revoke + + assert_no_file "app/channels/chat_channel.rb" + assert_no_file "app/javascript/channels/chat_channel.js" + + assert_file "app/channels/application_cable/channel.rb" + assert_file "app/channels/application_cable/connection.rb" + assert_file "app/javascript/channels/index.js" + assert_file "app/javascript/channels/consumer.js" + end + + def test_channel_suffix_is_not_duplicated + run_generator ["chat_channel"] + + assert_no_file "app/channels/chat_channel_channel.rb" + assert_file "app/channels/chat_channel.rb" + + assert_no_file "app/javascript/channels/chat_channel_channel.js" + assert_file "app/javascript/channels/chat_channel.js" + end +end diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb new file mode 100644 index 0000000000..8786756c68 --- /dev/null +++ b/railties/test/generators/controller_generator_test.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/controller/controller_generator" + +class ControllerGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(Account foo bar) + + setup :copy_routes + + def test_help_does_not_show_invoked_generators_options_if_they_already_exist + content = run_generator ["--help"] + assert_no_match(/Helper options\:/, content) + end + + def test_controller_skeleton_is_created + run_generator + assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/ + end + + def test_check_class_collision + Object.send :const_set, :ObjectController, Class.new + content = capture(:stderr) { run_generator ["object"] } + assert_match(/The name 'ObjectController' is either already used in your application or reserved/, content) + ensure + Object.send :remove_const, :ObjectController + end + + def test_invokes_helper + run_generator + assert_file "app/helpers/account_helper.rb" + end + + def test_does_not_invoke_helper_if_required + run_generator ["account", "--skip-helper"] + assert_no_file "app/helpers/account_helper.rb" + end + + def test_invokes_assets + run_generator + assert_file "app/assets/stylesheets/account.css" + end + + def test_does_not_invoke_assets_if_required + run_generator ["account", "--skip-assets"] + assert_no_file "app/assets/stylesheets/account.css" + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/controllers/account_controller_test.rb" + end + + def test_does_not_invoke_test_framework_if_required + run_generator ["account", "--no-test-framework"] + assert_no_file "test/controllers/account_controller_test.rb" + end + + def test_invokes_default_template_engine + run_generator + assert_file "app/views/account/foo.html.erb", %r(app/views/account/foo\.html\.erb) + assert_file "app/views/account/bar.html.erb", %r(app/views/account/bar\.html\.erb) + end + + def test_add_routes + run_generator + assert_file "config/routes.rb", /^ get 'account\/foo'/, /^ get 'account\/bar'/ + end + + def test_skip_routes + run_generator ["account", "foo", "--skip-routes"] + assert_file "config/routes.rb" do |routes| + assert_no_match(/get 'account\/foo'/, routes) + end + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["account"] + assert_file "app/views/account" + end + + def test_template_engine_with_class_path + run_generator ["admin/account"] + assert_file "app/views/admin/account" + end + + def test_actions_are_turned_into_methods + run_generator + + assert_file "app/controllers/account_controller.rb" do |controller| + assert_instance_method :foo, controller + assert_instance_method :bar, controller + end + end + + def test_namespaced_routes_are_created_in_routes + run_generator ["admin/dashboard", "index"] + assert_file "config/routes.rb" do |route| + assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n end$/, route) + end + end + + def test_namespaced_routes_with_multiple_actions_are_created_in_routes + run_generator ["admin/dashboard", "index", "show"] + assert_file "config/routes.rb" do |route| + assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n get 'dashboard\/show'\n end$/, route) + end + end + + def test_does_not_add_routes_when_action_is_not_specified + run_generator ["admin/dashboard"] + assert_file "config/routes.rb" do |routes| + assert_no_match(/namespace :admin/, routes) + end + end + + def test_controller_suffix_is_not_duplicated + run_generator ["account_controller"] + + assert_no_file "app/controllers/account_controller_controller.rb" + assert_file "app/controllers/account_controller.rb" + + assert_no_file "app/views/account_controller/" + assert_file "app/views/account/" + + assert_no_file "test/controllers/account_controller_controller_test.rb" + assert_file "test/controllers/account_controller_test.rb" + + assert_no_file "app/helpers/account_controller_helper.rb" + assert_file "app/helpers/account_helper.rb" + + assert_no_file "app/assets/stylesheets/account_controller.css" + assert_file "app/assets/stylesheets/account.css" + end +end diff --git a/railties/test/generators/create_migration_test.rb b/railties/test/generators/create_migration_test.rb new file mode 100644 index 0000000000..2ae38045c5 --- /dev/null +++ b/railties/test/generators/create_migration_test.rb @@ -0,0 +1,136 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/migration/migration_generator" + +class CreateMigrationTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + class Migrator < Rails::Generators::MigrationGenerator + include Rails::Generators::Migration + + def self.next_migration_number(dirname) + current_migration_number(dirname) + 1 + end + end + + tests Migrator + + def default_destination_path + "db/migrate/create_articles.rb" + end + + def create_migration(destination_path = default_destination_path, config = {}, generator_options = {}, &block) + migration_name = File.basename(destination_path, ".rb") + generator([migration_name], generator_options) + generator.set_migration_assigns!(destination_path) + + dir, base = File.split(destination_path) + timestamped_destination_path = File.join(dir, ["%migration_number%", base].join("_")) + + @migration = Rails::Generators::Actions::CreateMigration.new(generator, timestamped_destination_path, block || "contents", config) + end + + def migration_exists!(*args) + @existing_migration = create_migration(*args) + invoke! + @generator = nil + end + + def invoke! + capture(:stdout) { @migration.invoke! } + end + + def revoke! + capture(:stdout) { @migration.revoke! } + end + + def test_invoke + create_migration + + assert_match(/create db\/migrate\/1_create_articles\.rb\n/, invoke!) + assert_file @migration.destination + end + + def test_invoke_pretended + create_migration(default_destination_path, {}, { pretend: true }) + + assert_no_file @migration.destination + end + + def test_invoke_when_exists + migration_exists! + create_migration + + assert_equal @existing_migration.destination, @migration.existing_migration + end + + def test_invoke_when_exists_identical + migration_exists! + create_migration + + assert_match(/identical db\/migrate\/1_create_articles\.rb\n/, invoke!) + assert_predicate @migration, :identical? + end + + def test_invoke_when_exists_not_identical + migration_exists! + create_migration { "different content" } + + assert_raise(Rails::Generators::Error) { invoke! } + end + + def test_invoke_forced_when_exists_not_identical + dest = "db/migrate/migration.rb" + migration_exists!(dest) + create_migration(dest, force: true) { "different content" } + + stdout = invoke! + assert_match(/remove db\/migrate\/1_migration\.rb\n/, stdout) + assert_match(/create db\/migrate\/2_migration\.rb\n/, stdout) + assert_file @migration.destination + assert_no_file @existing_migration.destination + end + + def test_invoke_forced_pretended_when_exists_not_identical + migration_exists! + create_migration(default_destination_path, { force: true }, { pretend: true }) do + "different content" + end + + stdout = invoke! + assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, stdout) + assert_match(/create db\/migrate\/2_create_articles\.rb\n/, stdout) + assert_no_file @migration.destination + end + + def test_invoke_skipped_when_exists_not_identical + migration_exists! + create_migration(default_destination_path, {}, { skip: true }) { "different content" } + + assert_match(/skip db\/migrate\/2_create_articles\.rb\n/, invoke!) + assert_no_file @migration.destination + end + + def test_revoke + migration_exists! + create_migration + + assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!) + assert_no_file @existing_migration.destination + end + + def test_revoke_pretended + migration_exists! + create_migration(default_destination_path, {}, { pretend: true }) + + assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!) + assert_file @existing_migration.destination + end + + def test_revoke_when_no_exists + create_migration + + assert_match(/remove db\/migrate\/1_create_articles\.rb\n/, revoke!) + end +end diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb new file mode 100644 index 0000000000..772b4f6f0d --- /dev/null +++ b/railties/test/generators/generated_attribute_test.rb @@ -0,0 +1,154 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/generated_attribute" + +class GeneratedAttributeTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_field_type_returns_number_field + assert_field_type :integer, :number_field + end + + def test_field_type_returns_text_field + %w(float decimal string).each do |attribute_type| + assert_field_type attribute_type, :text_field + end + end + + def test_field_type_returns_datetime_select + %w(datetime timestamp).each do |attribute_type| + assert_field_type attribute_type, :datetime_select + end + end + + def test_field_type_returns_time_select + assert_field_type :time, :time_select + end + + def test_field_type_returns_date_select + assert_field_type :date, :date_select + end + + def test_field_type_returns_text_area + assert_field_type :text, :text_area + end + + def test_field_type_returns_check_box + assert_field_type :boolean, :check_box + end + + def test_field_type_with_unknown_type_returns_text_field + %w(foo bar baz).each do |attribute_type| + assert_field_type attribute_type, :text_field + end + end + + def test_default_value_is_integer + assert_field_default_value :integer, 1 + end + + def test_default_value_is_float + assert_field_default_value :float, 1.5 + end + + def test_default_value_is_decimal + assert_field_default_value :decimal, "9.99" + end + + def test_default_value_is_datetime + %w(datetime timestamp time).each do |attribute_type| + assert_field_default_value attribute_type, Time.now.to_s(:db) + end + end + + def test_default_value_is_date + assert_field_default_value :date, Date.today.to_s(:db) + end + + def test_default_value_is_string + assert_field_default_value :string, "MyString" + end + + def test_default_value_for_type + att = Rails::Generators::GeneratedAttribute.parse("type:string") + assert_equal("", att.default) + end + + def test_default_value_is_text + assert_field_default_value :text, "MyText" + end + + def test_default_value_is_boolean + assert_field_default_value :boolean, false + end + + def test_default_value_is_nil + %w(references belongs_to).each do |attribute_type| + assert_field_default_value attribute_type, nil + end + end + + def test_default_value_is_empty_string + %w(foo bar baz).each do |attribute_type| + assert_field_default_value attribute_type, "" + end + end + + def test_human_name + assert_equal( + "Full name", + create_generated_attribute(:string, "full_name").human_name + ) + end + + def test_reference_is_true + %w(references belongs_to).each do |attribute_type| + assert_predicate create_generated_attribute(attribute_type), :reference? + end + end + + def test_reference_is_false + %w(foo bar baz).each do |attribute_type| + assert_not_predicate create_generated_attribute(attribute_type), :reference? + end + end + + def test_polymorphic_reference_is_true + %w(references belongs_to).each do |attribute_type| + assert_predicate create_generated_attribute("#{attribute_type}{polymorphic}"), :polymorphic? + end + end + + def test_polymorphic_reference_is_false + %w(foo bar baz).each do |attribute_type| + assert_not_predicate create_generated_attribute("#{attribute_type}{polymorphic}"), :polymorphic? + end + end + + def test_blank_type_defaults_to_string_raises_exception + assert_equal :string, create_generated_attribute(nil, "title").type + assert_equal :string, create_generated_attribute("", "title").type + end + + def test_handles_index_names_for_references + assert_equal "post", create_generated_attribute("string", "post").index_name + assert_equal "post_id", create_generated_attribute("references", "post").index_name + assert_equal "post_id", create_generated_attribute("belongs_to", "post").index_name + assert_equal ["post_id", "post_type"], create_generated_attribute("references{polymorphic}", "post").index_name + end + + def test_handles_column_names_for_references + assert_equal "post", create_generated_attribute("string", "post").column_name + assert_equal "post_id", create_generated_attribute("references", "post").column_name + assert_equal "post_id", create_generated_attribute("belongs_to", "post").column_name + end + + def test_parse_required_attribute_with_index + att = Rails::Generators::GeneratedAttribute.parse("supplier:references{required}:index") + assert_equal "supplier", att.name + assert_equal :references, att.type + assert_predicate att, :has_index? + assert_predicate att, :required? + end +end diff --git a/railties/test/generators/generator_generator_test.rb b/railties/test/generators/generator_generator_test.rb new file mode 100644 index 0000000000..eaa964cabc --- /dev/null +++ b/railties/test/generators/generator_generator_test.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/generator/generator_generator" + +class GeneratorGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(awesome) + + def test_generator_skeleton_is_created + run_generator + + %w( + lib/generators/awesome + lib/generators/awesome/USAGE + lib/generators/awesome/templates + ).each { |path| assert_file path } + + assert_file "lib/generators/awesome/awesome_generator.rb", + /class AwesomeGenerator < Rails::Generators::NamedBase/ + assert_file "test/lib/generators/awesome_generator_test.rb", + /class AwesomeGeneratorTest < Rails::Generators::TestCase/, + /require 'generators\/awesome\/awesome_generator'/ + end + + def test_namespaced_generator_skeleton + run_generator ["rails/awesome"] + + %w( + lib/generators/rails/awesome + lib/generators/rails/awesome/USAGE + lib/generators/rails/awesome/templates + ).each { |path| assert_file path } + + assert_file "lib/generators/rails/awesome/awesome_generator.rb", + /class Rails::AwesomeGenerator < Rails::Generators::NamedBase/ + assert_file "test/lib/generators/rails/awesome_generator_test.rb", + /class Rails::AwesomeGeneratorTest < Rails::Generators::TestCase/, + /require 'generators\/rails\/awesome\/awesome_generator'/ + end + + def test_generator_skeleton_is_created_without_file_name_namespace + run_generator ["awesome", "--namespace", "false"] + + %w( + lib/generators/ + lib/generators/USAGE + lib/generators/templates + ).each { |path| assert_file path } + + assert_file "lib/generators/awesome_generator.rb", + /class AwesomeGenerator < Rails::Generators::NamedBase/ + assert_file "test/lib/generators/awesome_generator_test.rb", + /class AwesomeGeneratorTest < Rails::Generators::TestCase/, + /require 'generators\/awesome_generator'/ + end + + def test_namespaced_generator_skeleton_without_file_name_namespace + run_generator ["rails/awesome", "--namespace", "false"] + + %w( + lib/generators/rails + lib/generators/rails/USAGE + lib/generators/rails/templates + ).each { |path| assert_file path } + + assert_file "lib/generators/rails/awesome_generator.rb", + /class Rails::AwesomeGenerator < Rails::Generators::NamedBase/ + assert_file "test/lib/generators/rails/awesome_generator_test.rb", + /class Rails::AwesomeGeneratorTest < Rails::Generators::TestCase/, + /require 'generators\/rails\/awesome_generator'/ + end +end diff --git a/railties/test/generators/generator_test.rb b/railties/test/generators/generator_test.rb new file mode 100644 index 0000000000..5f7daf5ac3 --- /dev/null +++ b/railties/test/generators/generator_test.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +require "active_support/test_case" +require "active_support/testing/autorun" +require "rails/generators/app_base" + +module Rails + module Generators + class GeneratorTest < ActiveSupport::TestCase + def make_builder_class + Class.new(AppBase) do + add_shared_options_for "application" + + # include a module to get around thor's method_added hook + include(Module.new { + def gemfile_entries; super; end + def invoke_all; super; self; end + def add_gem_entry_filter; super; end + def gemfile_entry(*args); super; end + }) + end + end + + def test_construction + klass = make_builder_class + assert klass.start(["new", "blah"]) + end + + def test_add_gem + klass = make_builder_class + generator = klass.start(["new", "blah"]) + generator.gemfile_entry "tenderlove" + assert_includes generator.gemfile_entries.map(&:name), "tenderlove" + end + + def test_add_gem_with_version + klass = make_builder_class + generator = klass.start(["new", "blah"]) + generator.gemfile_entry "tenderlove", "2.0.0" + assert generator.gemfile_entries.find { |gfe| + gfe.name == "tenderlove" && gfe.version == "2.0.0" + } + end + + def test_add_github_gem + klass = make_builder_class + generator = klass.start(["new", "blah"]) + generator.gemfile_entry "tenderlove", github: "hello world" + assert generator.gemfile_entries.find { |gfe| + gfe.name == "tenderlove" && gfe.options[:github] == "hello world" + } + end + + def test_add_path_gem + klass = make_builder_class + generator = klass.start(["new", "blah"]) + generator.gemfile_entry "tenderlove", path: "hello world" + assert generator.gemfile_entries.find { |gfe| + gfe.name == "tenderlove" && gfe.options[:path] == "hello world" + } + end + + def test_filter + klass = make_builder_class + generator = klass.start(["new", "blah"]) + gems = generator.gemfile_entries + generator.add_gem_entry_filter { |gem| + gem.name != gems.first.name + } + assert_equal gems.drop(1), generator.gemfile_entries + end + + def test_two_filters + klass = make_builder_class + generator = klass.start(["new", "blah"]) + gems = generator.gemfile_entries + generator.add_gem_entry_filter { |gem| + gem.name != gems.first.name + } + generator.add_gem_entry_filter { |gem| + gem.name != gems[1].name + } + assert_equal gems.drop(2), generator.gemfile_entries + end + + def test_recommended_rails_versions + klass = make_builder_class + generator = klass.start(["new", "blah"]) + + specifier_for = -> v { generator.send(:rails_version_specifier, Gem::Version.new(v)) } + + assert_equal "~> 4.1.13", specifier_for["4.1.13"] + assert_equal "~> 4.1.6.rc1", specifier_for["4.1.6.rc1"] + assert_equal ["~> 4.1.7", ">= 4.1.7.1"], specifier_for["4.1.7.1"] + assert_equal ["~> 4.1.7", ">= 4.1.7.1.2"], specifier_for["4.1.7.1.2"] + assert_equal ["~> 4.1.7", ">= 4.1.7.1.rc2"], specifier_for["4.1.7.1.rc2"] + assert_equal "~> 4.2.0.beta1", specifier_for["4.2.0.beta1"] + assert_equal "~> 5.0.0.beta1", specifier_for["5.0.0.beta1"] + end + end + end +end diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb new file mode 100644 index 0000000000..25d5dba1d8 --- /dev/null +++ b/railties/test/generators/generators_test_helper.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require "abstract_unit" +require "active_support/core_ext/module/remove_method" +require "active_support/testing/stream" +require "active_support/testing/method_call_assertions" +require "rails/generators" +require "rails/generators/test_case" + +module Rails + class << self + remove_possible_method :root + def root + @root ||= Pathname.new(File.expand_path("../fixtures", __dir__)) + end + end +end +Rails.application.config.root = Rails.root +Rails.application.config.generators.templates = [File.join(Rails.root, "lib", "templates")] + +# Call configure to load the settings from +# Rails.application.config.generators to Rails::Generators +Rails.application.load_generators + +require "active_record" +require "action_dispatch" +require "action_view" + +module GeneratorsTestHelper + include ActiveSupport::Testing::Stream + include ActiveSupport::Testing::MethodCallAssertions + + def self.included(base) + base.class_eval do + destination File.join(Rails.root, "tmp") + setup :prepare_destination + + begin + base.tests Rails::Generators.const_get(base.name.sub(/Test$/, "")) + rescue + end + end + end + + def with_secondary_database_configuration + original_configurations = ActiveRecord::Base.configurations + ActiveRecord::Base.configurations = { + test: { + secondary: { + database: "db/secondary.sqlite3", + migrations_paths: "db/secondary_migrate", + }, + }, + } + yield + ensure + ActiveRecord::Base.configurations = original_configurations + end + + def copy_routes + routes = File.expand_path("../../lib/rails/generators/rails/app/templates/config/routes.rb.tt", __dir__) + destination = File.join(destination_root, "config") + FileUtils.mkdir_p(destination) + FileUtils.cp routes, File.join(destination, "routes.rb") + end +end diff --git a/railties/test/generators/helper_generator_test.rb b/railties/test/generators/helper_generator_test.rb new file mode 100644 index 0000000000..192839799e --- /dev/null +++ b/railties/test/generators/helper_generator_test.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/helper/helper_generator" + +ObjectHelper = Class.new +AnotherObjectHelperTest = Class.new + +class HelperGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(admin) + + def test_helper_skeleton_is_created + run_generator + assert_file "app/helpers/admin_helper.rb", /module AdminHelper/ + end + + def test_check_class_collision + content = capture(:stderr) { run_generator ["object"] } + assert_match(/The name 'ObjectHelper' is either already used in your application or reserved/, content) + end + + def test_namespaced_and_not_namespaced_helpers + run_generator ["products"] + + # We have to require the generated helper to show the problem because + # the test helpers just check for generated files and contents but + # do not actually load them. But they have to be loaded (as in a real environment) + # to make the second generator run fail + require "#{destination_root}/app/helpers/products_helper" + + assert_nothing_raised do + run_generator ["admin::products"] + ensure + # cleanup + Object.send(:remove_const, :ProductsHelper) + end + end + + def test_helper_suffix_is_not_duplicated + run_generator %w(products_helper) + + assert_no_file "app/helpers/products_helper_helper.rb" + assert_file "app/helpers/products_helper.rb" + end +end diff --git a/railties/test/generators/integration_test_generator_test.rb b/railties/test/generators/integration_test_generator_test.rb new file mode 100644 index 0000000000..2ec4895096 --- /dev/null +++ b/railties/test/generators/integration_test_generator_test.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/integration_test/integration_test_generator" + +class IntegrationTestGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_integration_test_skeleton_is_created + run_generator %w(integration) + assert_file "test/integration/integration_test.rb", /class IntegrationTest < ActionDispatch::IntegrationTest/ + end + + def test_namespaced_integration_test_skeleton_is_created + run_generator %w(iguchi/integration) + assert_file "test/integration/iguchi/integration_test.rb", /class Iguchi::IntegrationTest < ActionDispatch::IntegrationTest/ + end + + def test_test_suffix_is_not_duplicated + run_generator %w(integration_test) + + assert_no_file "test/integration/integration_test_test.rb" + assert_file "test/integration/integration_test.rb" + end +end diff --git a/railties/test/generators/job_generator_test.rb b/railties/test/generators/job_generator_test.rb new file mode 100644 index 0000000000..234ba6dad7 --- /dev/null +++ b/railties/test/generators/job_generator_test.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/job/job_generator" + +class JobGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_job_skeleton_is_created + run_generator ["refresh_counters"] + assert_file "app/jobs/refresh_counters_job.rb" do |job| + assert_match(/class RefreshCountersJob < ApplicationJob/, job) + end + end + + def test_job_queue_param + run_generator ["refresh_counters", "--queue", "important"] + assert_file "app/jobs/refresh_counters_job.rb" do |job| + assert_match(/class RefreshCountersJob < ApplicationJob/, job) + assert_match(/queue_as :important/, job) + end + end + + def test_job_namespace + run_generator ["admin/refresh_counters", "--queue", "admin"] + assert_file "app/jobs/admin/refresh_counters_job.rb" do |job| + assert_match(/class Admin::RefreshCountersJob < ApplicationJob/, job) + assert_match(/queue_as :admin/, job) + end + end + + def test_application_job_skeleton_is_created + run_generator ["refresh_counters"] + assert_file "app/jobs/application_job.rb" do |job| + assert_match(/class ApplicationJob < ActiveJob::Base/, job) + end + end + + def test_job_suffix_is_not_duplicated + run_generator ["notifier_job"] + + assert_no_file "app/jobs/notifier_job_job.rb" + assert_file "app/jobs/notifier_job.rb" + + assert_no_file "test/jobs/notifier_job_job_test.rb" + assert_file "test/jobs/notifier_job_test.rb" + end +end diff --git a/railties/test/generators/mailer_generator_test.rb b/railties/test/generators/mailer_generator_test.rb new file mode 100644 index 0000000000..ddac6e1a1e --- /dev/null +++ b/railties/test/generators/mailer_generator_test.rb @@ -0,0 +1,179 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/mailer/mailer_generator" + +class MailerGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(notifier foo bar) + + def test_mailer_skeleton_is_created + run_generator + assert_file "app/mailers/notifier_mailer.rb" do |mailer| + assert_match(/class NotifierMailer < ApplicationMailer/, mailer) + assert_no_match(/default from: "from@example\.com"/, mailer) + assert_no_match(/layout :mailer_notifier/, mailer) + end + + assert_file "app/mailers/application_mailer.rb" do |mailer| + assert_match(/class ApplicationMailer < ActionMailer::Base/, mailer) + assert_match(/default from: 'from@example\.com'/, mailer) + assert_match(/layout 'mailer'/, mailer) + end + end + + def test_mailer_with_i18n_helper + run_generator + assert_file "app/mailers/notifier_mailer.rb" do |mailer| + assert_match(/en\.notifier_mailer\.foo\.subject/, mailer) + assert_match(/en\.notifier_mailer\.bar\.subject/, mailer) + end + end + + def test_check_class_collision + Object.send :const_set, :NotifierMailer, Class.new + content = capture(:stderr) { run_generator } + assert_match(/The name 'NotifierMailer' is either already used in your application or reserved/, content) + ensure + Object.send :remove_const, :NotifierMailer + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/mailers/notifier_mailer_test.rb" do |test| + assert_match(/class NotifierMailerTest < ActionMailer::TestCase/, test) + assert_match(/test "foo"/, test) + assert_match(/test "bar"/, test) + end + assert_file "test/mailers/previews/notifier_mailer_preview.rb" do |preview| + assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer/, preview) + assert_match(/class NotifierMailerPreview < ActionMailer::Preview/, preview) + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/foo/, preview) + assert_instance_method :foo, preview do |foo| + assert_match(/NotifierMailer\.foo/, foo) + end + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/notifier_mailer\/bar/, preview) + assert_instance_method :bar, preview do |bar| + assert_match(/NotifierMailer\.bar/, bar) + end + end + end + + def test_check_test_class_collision + Object.send :const_set, :NotifierMailerTest, Class.new + content = capture(:stderr) { run_generator } + assert_match(/The name 'NotifierMailerTest' is either already used in your application or reserved/, content) + ensure + Object.send :remove_const, :NotifierMailerTest + end + + def test_check_preview_class_collision + Object.send :const_set, :NotifierMailerPreview, Class.new + content = capture(:stderr) { run_generator } + assert_match(/The name 'NotifierMailerPreview' is either already used in your application or reserved/, content) + ensure + Object.send :remove_const, :NotifierMailerPreview + end + + def test_invokes_default_text_template_engine + run_generator + assert_file "app/views/notifier_mailer/foo.text.erb" do |view| + assert_match(%r(\sapp/views/notifier_mailer/foo\.text\.erb), view) + assert_match(/<%= @greeting %>/, view) + end + + assert_file "app/views/notifier_mailer/bar.text.erb" do |view| + assert_match(%r(\sapp/views/notifier_mailer/bar\.text\.erb), view) + assert_match(/<%= @greeting %>/, view) + end + + assert_file "app/views/layouts/mailer.text.erb" do |view| + assert_match(/<%= yield %>/, view) + end + end + + def test_invokes_default_html_template_engine + run_generator + assert_file "app/views/notifier_mailer/foo.html.erb" do |view| + assert_match(%r(\sapp/views/notifier_mailer/foo\.html\.erb), view) + assert_match(/<%= @greeting %>/, view) + end + + assert_file "app/views/notifier_mailer/bar.html.erb" do |view| + assert_match(%r(\sapp/views/notifier_mailer/bar\.html\.erb), view) + assert_match(/<%= @greeting %>/, view) + end + + assert_file "app/views/layouts/mailer.html.erb" do |view| + assert_match(%r{<body>\n <%= yield %>\n </body>}, view) + end + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["notifier"] + assert_file "app/views/notifier_mailer" + end + + def test_logs_if_the_template_engine_cannot_be_found + content = run_generator ["notifier", "foo", "bar", "--template-engine=haml"] + assert_match(/haml \[not found\]/, content) + end + + def test_mailer_with_namedspaced_mailer + run_generator ["Farm::Animal", "moos"] + assert_file "app/mailers/farm/animal_mailer.rb" do |mailer| + assert_match(/class Farm::AnimalMailer < ApplicationMailer/, mailer) + assert_match(/en\.farm\.animal_mailer\.moos\.subject/, mailer) + end + assert_file "test/mailers/previews/farm/animal_mailer_preview.rb" do |preview| + assert_match(/\# Preview all emails at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer/, preview) + assert_match(/class Farm::AnimalMailerPreview < ActionMailer::Preview/, preview) + assert_match(/\# Preview this email at http:\/\/localhost\:3000\/rails\/mailers\/farm\/animal_mailer\/moos/, preview) + end + assert_file "app/views/farm/animal_mailer/moos.text.erb" + assert_file "app/views/farm/animal_mailer/moos.html.erb" + end + + def test_actions_are_turned_into_methods + run_generator + + assert_file "app/mailers/notifier_mailer.rb" do |mailer| + assert_instance_method :foo, mailer do |foo| + assert_match(/mail to: "to@example\.org"/, foo) + assert_match(/@greeting = "Hi"/, foo) + end + + assert_instance_method :bar, mailer do |bar| + assert_match(/mail to: "to@example\.org"/, bar) + assert_match(/@greeting = "Hi"/, bar) + end + end + end + + def test_mailer_on_revoke + run_generator + run_generator ["notifier"], behavior: :revoke + + assert_no_file "app/mailers/notifier.rb" + assert_no_file "app/views/notifier/foo.text.erb" + assert_no_file "app/views/notifier/bar.text.erb" + assert_no_file "app/views/notifier/foo.html.erb" + assert_no_file "app/views/notifier/bar.html.erb" + end + + def test_mailer_suffix_is_not_duplicated + run_generator ["notifier_mailer"] + + assert_no_file "app/mailers/notifier_mailer_mailer.rb" + assert_file "app/mailers/notifier_mailer.rb" + + assert_no_file "app/views/notifier_mailer_mailer/" + assert_file "app/views/notifier_mailer/" + + assert_no_file "test/mailers/notifier_mailer_mailer_test.rb" + assert_file "test/mailers/notifier_mailer_test.rb" + + assert_no_file "test/mailers/previews/notifier_mailer_mailer_preview.rb" + assert_file "test/mailers/previews/notifier_mailer_preview.rb" + end +end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb new file mode 100644 index 0000000000..5812cbdfc9 --- /dev/null +++ b/railties/test/generators/migration_generator_test.rb @@ -0,0 +1,367 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/migration/migration_generator" + +class MigrationGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_migration + migration = "change_title_body_from_posts" + run_generator [migration] + assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration\[[0-9.]+\]/ + end + + def test_migrations_generated_simultaneously + migrations = ["change_title_body_from_posts", "change_email_from_comments"] + + first_migration_number, second_migration_number = migrations.collect do |migration| + run_generator [migration] + file_name = migration_file_name "db/migrate/#{migration}.rb" + + File.basename(file_name).split("_").first + end + + assert_not_equal first_migration_number, second_migration_number + end + + def test_migration_with_class_name + migration = "ChangeTitleBodyFromPosts" + run_generator [migration] + assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration\[[0-9.]+\]/ + end + + def test_migration_with_invalid_file_name + migration = "add_something:datetime" + assert_raise ActiveRecord::IllegalMigrationNameError do + run_generator [migration] + end + end + + def test_add_migration_with_attributes + migration = "add_title_body_to_posts" + run_generator [migration, "title:string", "body:text"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + end + end + end + + def test_add_migration_with_table_having_from_in_title + migration = "add_email_address_to_excluded_from_campaign" + run_generator [migration, "email_address:string"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :excluded_from_campaigns, :email_address, :string/, change) + end + end + end + + def test_remove_migration_with_indexed_attribute + migration = "remove_title_body_from_posts" + run_generator [migration, "title:string:index", "body:text"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) + assert_match(/remove_index :posts, :title/, change) + end + end + end + + def test_remove_migration_with_attributes + migration = "remove_title_body_from_posts" + run_generator [migration, "title:string", "body:text"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/remove_column :posts, :title, :string/, change) + assert_match(/remove_column :posts, :body, :text/, change) + end + end + end + + def test_remove_migration_with_table_having_to_in_title + migration = "remove_email_address_from_sent_to_user" + run_generator [migration, "email_address:string"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/remove_column :sent_to_users, :email_address, :string/, change) + end + end + end + + def test_remove_migration_with_references_options + migration = "remove_references_from_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/remove_reference :books, :author/, change) + assert_match(/remove_reference :books, :distributor, polymorphic: true/, change) + end + end + end + + def test_remove_migration_with_references_removes_foreign_keys + migration = "remove_references_from_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/remove_reference :books, :author,.*\sforeign_key: true/, change) + assert_match(/remove_reference :books, :distributor/, change) # sanity check + assert_no_match(/remove_reference :books, :distributor,.*\sforeign_key: true/, change) + end + end + end + + def test_add_migration_with_attributes_and_indices + migration = "add_title_with_index_and_body_to_posts" + run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_id, :integer/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_id, unique: true/, change) + end + end + end + + def test_add_migration_with_attributes_and_wrong_index_declaration + migration = "add_title_and_content_to_books" + run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string/, change) + assert_match(/add_column :books, :content, :text/, change) + assert_match(/add_column :books, :user_id, :integer/, change) + end + assert_no_match(/add_index :books, :title/, content) + assert_no_match(/add_index :books, :user_id/, content) + end + end + + def test_add_migration_with_attributes_without_type_and_index + migration = "add_title_with_index_and_body_to_posts" + run_generator [migration, "title:index", "body:text", "user_uuid:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :posts, :title, :string/, change) + assert_match(/add_column :posts, :body, :text/, change) + assert_match(/add_column :posts, :user_uuid, :string/, change) + assert_match(/add_index :posts, :title/, change) + assert_match(/add_index :posts, :user_uuid, unique: true/, change) + end + end + end + + def test_add_migration_with_attributes_index_declaration_and_attribute_options + migration = "add_title_and_content_to_books" + run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :books, :title, :string, limit: 40/, change) + assert_match(/add_column :books, :content, :string, limit: 255/, change) + assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, change) + assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, change) + end + assert_match(/add_index :books, :title/, content) + assert_match(/add_index :books, :price/, content) + assert_match(/add_index :books, :discount, unique: true/, content) + end + end + + def test_add_migration_with_references_options + migration = "add_references_to_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_reference :books, :author/, change) + assert_match(/add_reference :books, :distributor, polymorphic: true/, change) + end + end + end + + def test_add_migration_with_required_references + migration = "add_references_to_books" + run_generator [migration, "author:belongs_to{required}", "distributor:references{polymorphic,required}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_reference :books, :author, null: false/, change) + assert_match(/add_reference :books, :distributor, polymorphic: true, null: false/, change) + end + end + end + + def test_add_migration_with_references_adds_foreign_keys + migration = "add_references_to_books" + run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_reference :books, :author,.*\sforeign_key: true/, change) + assert_match(/add_reference :books, :distributor/, change) # sanity check + assert_no_match(/add_reference :books, :distributor,.*\sforeign_key: true/, change) + end + end + end + + def test_create_join_table_migration + migration = "add_media_join_table" + run_generator [migration, "artist_id", "musics:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_join_table :artists, :musics/, change) + assert_match(/# t\.index \[:artist_id, :music_id\]/, change) + assert_match(/ t\.index \[:music_id, :artist_id\], unique: true/, change) + end + end + end + + def test_create_table_migration + run_generator ["create_books", "title:string", "content:text"] + assert_migration "db/migrate/create_books.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :books/, change) + assert_match(/ t\.string :title/, change) + assert_match(/ t\.text :content/, change) + end + end + end + + def test_add_uuid_to_create_table_migration + run_generator ["create_books", "--primary_key_type=uuid"] + assert_migration "db/migrate/create_books.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :books, id: :uuid/, change) + end + end + end + + def test_database_puts_migrations_in_configured_folder + with_secondary_database_configuration do + run_generator ["create_books", "--database=secondary"] + assert_migration "db/secondary_migrate/create_books.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :books/, change) + end + end + end + end + + def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create + migration = "delete_books" + run_generator [migration, "title:string", "content:text"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/^\s*$/, change) + end + end + end + + def test_properly_identifies_usage_file + assert generator_class.send(:usage_path) + end + + def test_migration_with_singular_table_name + with_singular_table_name do + migration = "add_title_body_to_post" + run_generator [migration, "title:string"] + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :post, :title, :string/, change) + end + end + end + end + + def test_create_join_table_migration_with_singular_table_name + with_singular_table_name do + migration = "add_media_join_table" + run_generator [migration, "artist_id", "music:uniq"] + + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_join_table :artist, :music/, change) + assert_match(/# t\.index \[:artist_id, :music_id\]/, change) + assert_match(/ t\.index \[:music_id, :artist_id\], unique: true/, change) + end + end + end + end + + def test_create_table_migration_with_singular_table_name + with_singular_table_name do + run_generator ["create_book", "title:string", "content:text"] + assert_migration "db/migrate/create_book.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :book/, change) + assert_match(/ t\.string :title/, change) + assert_match(/ t\.text :content/, change) + end + end + end + end + + def test_create_table_migration_with_token_option + run_generator ["create_users", "token:token", "auth_token:token"] + assert_migration "db/migrate/create_users.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :users/, change) + assert_match(/ t\.string :token/, change) + assert_match(/ t\.string :auth_token/, change) + assert_match(/add_index :users, :token, unique: true/, change) + assert_match(/add_index :users, :auth_token, unique: true/, change) + end + end + end + + def test_add_migration_with_token_option + migration = "add_token_to_users" + run_generator [migration, "auth_token:token"] + assert_migration "db/migrate/#{migration}.rb" do |content| + assert_method :change, content do |change| + assert_match(/add_column :users, :auth_token, :string/, change) + assert_match(/add_index :users, :auth_token, unique: true/, change) + end + end + end + + def test_add_migration_to_configured_path + old_paths = Rails.application.config.paths["db/migrate"] + Rails.application.config.paths.add "db/migrate", with: "db2/migrate" + + migration = "migration_in_custom_path" + run_generator [migration] + assert_migration "db2/migrate/#{migration}.rb", /.*/ + ensure + Rails.application.config.paths["db/migrate"] = old_paths + end + + private + + def with_singular_table_name + old_state = ActiveRecord::Base.pluralize_table_names + ActiveRecord::Base.pluralize_table_names = false + yield + ensure + ActiveRecord::Base.pluralize_table_names = old_state + end +end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb new file mode 100644 index 0000000000..b06db6dd8a --- /dev/null +++ b/railties/test/generators/model_generator_test.rb @@ -0,0 +1,496 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/model/model_generator" + +class ModelGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(Account name:string age:integer) + + def setup + super + Rails::Generators::ModelHelpers.skip_warn = false + end + + def test_help_shows_invoked_generators_options + content = run_generator ["--help"] + assert_match(/ActiveRecord options:/, content) + assert_match(/TestUnit options:/, content) + end + + def test_model_with_missing_attribute_type + run_generator ["post", "title", "body:text", "author"] + + assert_migration "db/migrate/create_posts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :title/, up) + assert_match(/t\.text :body/, up) + assert_match(/t\.string :author/, up) + end + end + end + + def test_invokes_default_orm + run_generator + assert_file "app/models/account.rb", /class Account < ApplicationRecord/ + end + + def test_model_with_parent_option + run_generator ["account", "--parent", "Admin::Account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_plural_names_are_singularized + content = run_generator ["accounts"] + assert_file "app/models/account.rb", /class Account < ApplicationRecord/ + assert_file "test/models/account_test.rb", /class AccountTest/ + assert_match(/\[WARNING\] The model name 'accounts' was recognized as a plural, using the singular 'account' instead\. Override with --force-plural or setup custom inflection rules for this noun before running the generator\./, content) + end + + def test_unknown_inflection_rule_are_warned + content = run_generator ["porsche"] + assert_match("[WARNING] Rails cannot recover singular form from its plural form 'porsches'.\nPlease setup custom inflection rules for this noun before running the generator in config/initializers/inflections.rb.", content) + assert_file "app/models/porsche.rb", /class Porsche < ApplicationRecord/ + + uncountable_content = run_generator ["sheep"] + assert_no_match("[WARNING] Rails cannot recover singular form from its plural form", uncountable_content) + + regular_content = run_generator ["account"] + assert_no_match("[WARNING] Rails cannot recover singular form from its plural form", regular_content) + end + + def test_model_with_underscored_parent_option + run_generator ["account", "--parent", "admin/account"] + assert_file "app/models/account.rb", /class Account < Admin::Account/ + end + + def test_model_with_namespace + run_generator ["admin/account"] + assert_file "app/models/admin.rb", /module Admin/ + assert_file "app/models/admin.rb", /def self\.table_name_prefix/ + assert_file "app/models/admin.rb", /'admin_'/ + assert_file "app/models/admin/account.rb", /class Admin::Account < ApplicationRecord/ + end + + def test_migration + run_generator + assert_migration "db/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/ + end + + def test_migration_with_namespace + run_generator ["Gallery::Image"] + assert_migration "db/migrate/create_gallery_images", /class CreateGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/ + assert_no_migration "db/migrate/create_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_admin_gallery_images", /class CreateAdminGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/ + assert_migration "db/migrate/create_admin_gallery_images", /create_table :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_admin_gallery_images" + assert_migration "db/migrate/create_admin_gallery_image", /class CreateAdminGalleryImage < ActiveRecord::Migration\[[0-9.]+\]/ + assert_migration "db/migrate/create_admin_gallery_image", /create_table :admin_gallery_image/ + ensure + ActiveRecord::Base.pluralize_table_names = true + end + + def test_migration_with_namespaces_in_model_name_without_plurization + ActiveRecord::Base.pluralize_table_names = false + run_generator ["Gallery::Image"] + assert_migration "db/migrate/create_gallery_image", /class CreateGalleryImage < ActiveRecord::Migration\[[0-9.]+\]/ + assert_no_migration "db/migrate/create_gallery_images" + ensure + ActiveRecord::Base.pluralize_table_names = true + end + + def test_migration_without_pluralization + ActiveRecord::Base.pluralize_table_names = false + run_generator + assert_migration "db/migrate/create_account", /class CreateAccount < ActiveRecord::Migration\[[0-9.]+\]/ + assert_no_migration "db/migrate/create_accounts" + ensure + ActiveRecord::Base.pluralize_table_names = true + end + + def test_migration_is_skipped + run_generator ["account", "--no-migration"] + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_migration_with_attributes + run_generator ["product", "name:string", "supplier_id:integer"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_method :change, m do |up| + assert_match(/create_table :products/, up) + assert_match(/t\.string :name/, up) + assert_match(/t\.integer :supplier_id/, up) + end + end + end + + def test_migration_with_attributes_and_with_index + run_generator ["product", "name:string:index", "supplier_id:integer:index", "user_id:integer:uniq", "order_id:uniq"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_method :change, m do |up| + assert_match(/create_table :products/, up) + assert_match(/t\.string :name/, up) + assert_match(/t\.integer :supplier_id/, up) + assert_match(/t\.integer :user_id/, up) + assert_match(/t\.string :order_id/, up) + + assert_match(/add_index :products, :name/, up) + assert_match(/add_index :products, :supplier_id/, up) + assert_match(/add_index :products, :user_id, unique: true/, up) + assert_match(/add_index :products, :order_id, unique: true/, up) + end + end + end + + def test_migration_with_attributes_and_with_wrong_index_declaration + run_generator ["product", "name:string", "supplier_id:integer:inex", "user_id:integer:unqu"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_method :change, m do |up| + assert_match(/create_table :products/, up) + assert_match(/t\.string :name/, up) + assert_match(/t\.integer :supplier_id/, up) + assert_match(/t\.integer :user_id/, up) + + assert_no_match(/add_index :products, :name/, up) + assert_no_match(/add_index :products, :supplier_id/, up) + assert_no_match(/add_index :products, :user_id/, up) + end + end + end + + def test_migration_with_missing_attribute_type_and_with_index + run_generator ["product", "name:index", "supplier_id:integer:index", "year:integer"] + + assert_migration "db/migrate/create_products.rb" do |m| + assert_method :change, m do |up| + assert_match(/create_table :products/, up) + assert_match(/t\.string :name/, up) + assert_match(/t\.integer :supplier_id/, up) + + assert_match(/add_index :products, :name/, up) + assert_match(/add_index :products, :supplier_id/, up) + assert_no_match(/add_index :products, :year/, up) + end + end + end + + def test_add_migration_with_attributes_index_declaration_and_attribute_options + run_generator ["product", "title:string{40}:index", "content:string{255}", "price:decimal{5,2}:index", "discount:decimal{5,2}:uniq", "supplier:references{polymorphic}"] + + assert_migration "db/migrate/create_products.rb" do |content| + assert_method :change, content do |up| + assert_match(/create_table :products/, up) + assert_match(/t.string :title, limit: 40/, up) + assert_match(/t.string :content, limit: 255/, up) + assert_match(/t.decimal :price, precision: 5, scale: 2/, up) + assert_match(/t.references :supplier, polymorphic: true/, up) + end + assert_match(/add_index :products, :title/, content) + assert_match(/add_index :products, :price/, content) + assert_match(/add_index :products, :discount, unique: true/, content) + end + end + + def test_migration_without_timestamps + ActiveRecord::Base.timestamped_migrations = false + run_generator ["account"] + assert_file "db/migrate/001_create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/ + + run_generator ["project"] + assert_file "db/migrate/002_create_projects.rb", /class CreateProjects < ActiveRecord::Migration\[[0-9.]+\]/ + ensure + ActiveRecord::Base.timestamped_migrations = true + end + + def test_migration_with_configured_path + old_paths = Rails.application.config.paths["db/migrate"] + Rails.application.config.paths.add "db/migrate", with: "db2/migrate" + + run_generator + + assert_migration "db2/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/ + ensure + Rails.application.config.paths["db/migrate"] = old_paths + end + + def test_model_with_references_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:references"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_model_with_belongs_to_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:belongs_to"] + assert_file "app/models/product.rb", /belongs_to :supplier/ + end + + def test_model_with_polymorphic_references_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:references{polymorphic}"] + assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/ + end + + def test_model_with_polymorphic_belongs_to_attribute_generates_belongs_to_associations + run_generator ["product", "name:string", "supplier:belongs_to{polymorphic}"] + assert_file "app/models/product.rb", /belongs_to :supplier, polymorphic: true/ + end + + def test_migration_with_timestamps + run_generator + assert_migration "db/migrate/create_accounts.rb", /t\.timestamps/ + end + + def test_migration_timestamps_are_skipped + run_generator ["account", "--no-timestamps"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_no_match(/t\.timestamps/, up) + end + end + end + + def test_migration_is_skipped_with_skip_option + run_generator + output = run_generator ["Account", "--skip"] + assert_match %r{skip\s+db/migrate/\d+_create_accounts\.rb}, output + end + + def test_migration_is_ignored_as_identical_with_skip_option + run_generator ["Account"] + output = run_generator ["Account", "--skip"] + assert_match %r{identical\s+db/migrate/\d+_create_accounts\.rb}, output + end + + def test_migration_is_skipped_on_skip_behavior + run_generator + output = run_generator ["Account"], behavior: :skip + assert_match %r{skip\s+db/migrate/\d+_create_accounts\.rb}, output + end + + def test_migration_error_is_not_shown_on_revoke + run_generator + error = capture(:stderr) { run_generator ["Account"], behavior: :revoke } + assert_no_match(/Another migration is already named create_accounts/, error) + end + + def test_migration_is_removed_on_revoke + run_generator + run_generator ["Account"], behavior: :revoke + assert_no_migration "db/migrate/create_accounts.rb" + end + + def test_existing_migration_is_removed_on_force + run_generator + old_migration = Dir["#{destination_root}/db/migrate/*_create_accounts.rb"].first + error = capture(:stderr) { run_generator ["Account", "--force"] } + assert_no_match(/Another migration is already named create_accounts/, error) + assert_no_file old_migration + assert_migration "db/migrate/create_accounts.rb" + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/models/account_test.rb", /class AccountTest < ActiveSupport::TestCase/ + + assert_file "test/fixtures/accounts.yml", /name: MyString/, /age: 1/ + assert_generated_fixture("test/fixtures/accounts.yml", + "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 }) + end + + def test_fixtures_use_the_references_ids + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "test/fixtures/line_items.yml", /product: one\n cart: one/ + assert_generated_fixture("test/fixtures/line_items.yml", + "one" => { "product" => "one", "cart" => "one" }, "two" => { "product" => "two", "cart" => "two" }) + end + + def test_fixtures_use_the_references_ids_and_type + run_generator ["LineItem", "product:references{polymorphic}", "cart:belongs_to"] + + assert_file "test/fixtures/line_items.yml", /product: one\n product_type: Product\n cart: one/ + assert_generated_fixture("test/fixtures/line_items.yml", + "one" => { "product" => "one", "product_type" => "Product", "cart" => "one" }, + "two" => { "product" => "two", "product_type" => "Product", "cart" => "two" }) + end + + def test_fixtures_respect_reserved_yml_keywords + run_generator ["LineItem", "no:integer", "Off:boolean", "ON:boolean"] + + assert_generated_fixture("test/fixtures/line_items.yml", + "one" => { "no" => 1, "Off" => false, "ON" => false }, "two" => { "no" => 1, "Off" => false, "ON" => false }) + end + + def test_fixture_is_skipped + run_generator ["account", "--skip-fixture"] + assert_no_file "test/fixtures/accounts.yml" + end + + def test_fixture_is_skipped_if_fixture_replacement_is_given + content = run_generator ["account", "-r", "factory_girl"] + assert_match(/factory_girl \[not found\]/, content) + assert_no_file "test/fixtures/accounts.yml" + end + + def test_fixture_without_pluralization + original_pluralize_table_name = ActiveRecord::Base.pluralize_table_names + ActiveRecord::Base.pluralize_table_names = false + run_generator + assert_generated_fixture("test/fixtures/account.yml", + "one" => { "name" => "MyString", "age" => 1 }, "two" => { "name" => "MyString", "age" => 1 }) + ensure + ActiveRecord::Base.pluralize_table_names = original_pluralize_table_name + end + + def test_check_class_collision + content = capture(:stderr) { run_generator ["object"] } + assert_match(/The name 'Object' is either already used in your application or reserved/, content) + end + + def test_index_is_skipped_for_belongs_to_association + run_generator ["account", "supplier:belongs_to", "--no-indexes"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_no_match(/index: true/, up) + end + end + end + + def test_index_is_skipped_for_references_association + run_generator ["account", "supplier:references", "--no-indexes"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_no_match(/index: true/, up) + end + end + end + + def test_add_uuid_to_create_table_migration + run_generator ["account", "--primary_key_type=uuid"] + assert_migration "db/migrate/create_accounts.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :accounts, id: :uuid/, change) + end + end + end + + def test_database_puts_migrations_in_configured_folder + with_secondary_database_configuration do + run_generator ["account", "--database=secondary"] + assert_migration "db/secondary_migrate/create_accounts.rb" do |content| + assert_method :change, content do |change| + assert_match(/create_table :accounts/, change) + end + end + end + end + + def test_required_belongs_to_adds_required_association + run_generator ["account", "supplier:references{required}"] + + expected_file = <<~FILE + class Account < ApplicationRecord + belongs_to :supplier, required: true + end + FILE + assert_file "app/models/account.rb", expected_file + end + + def test_required_polymorphic_belongs_to_generages_correct_model + run_generator ["account", "supplier:references{required,polymorphic}"] + + expected_file = <<~FILE + class Account < ApplicationRecord + belongs_to :supplier, polymorphic: true, required: true + end + FILE + assert_file "app/models/account.rb", expected_file + end + + def test_required_and_polymorphic_are_order_independent + run_generator ["account", "supplier:references{polymorphic.required}"] + + expected_file = <<~FILE + class Account < ApplicationRecord + belongs_to :supplier, polymorphic: true, required: true + end + FILE + assert_file "app/models/account.rb", expected_file + end + + def test_required_adds_null_false_to_column + run_generator ["account", "supplier:references{required}"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.references :supplier,.*\snull: false/, up) + end + end + end + + def test_foreign_key_is_not_added_for_non_references + run_generator ["account", "supplier:string"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_no_match(/foreign_key/, up) + end + end + end + + def test_foreign_key_is_added_for_references + run_generator ["account", "supplier:belongs_to", "user:references"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.belongs_to :supplier,.*\sforeign_key: true/, up) + assert_match(/t\.references :user,.*\sforeign_key: true/, up) + end + end + end + + def test_foreign_key_is_skipped_for_polymorphic_references + run_generator ["account", "supplier:belongs_to{polymorphic}"] + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_no_match(/foreign_key/, up) + end + end + end + + def test_token_option_adds_has_secure_token + run_generator ["user", "token:token", "auth_token:token"] + expected_file = <<~FILE + class User < ApplicationRecord + has_secure_token + has_secure_token :auth_token + end + FILE + assert_file "app/models/user.rb", expected_file + end + + private + def assert_generated_fixture(path, parsed_contents) + fixture_file = File.new File.expand_path(path, destination_root) + assert_equal(parsed_contents, YAML.load(fixture_file)) + end +end diff --git a/railties/test/generators/named_base_test.rb b/railties/test/generators/named_base_test.rb new file mode 100644 index 0000000000..4e61b660d7 --- /dev/null +++ b/railties/test/generators/named_base_test.rb @@ -0,0 +1,176 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/scaffold_controller/scaffold_controller_generator" + +class NamedBaseTest < Rails::Generators::TestCase + include GeneratorsTestHelper + tests Rails::Generators::ScaffoldControllerGenerator + + def test_named_generator_with_underscore + g = generator ["line_item"] + assert_name g, "line_item", :name + assert_name g, %w(), :class_path + assert_name g, "LineItem", :class_name + assert_name g, "line_item", :file_path + assert_name g, "line_item", :file_name + assert_name g, "Line item", :human_name + assert_name g, "line_item", :singular_name + assert_name g, "line_items", :plural_name + assert_name g, "line_item", :i18n_scope + assert_name g, "line_items", :table_name + end + + def test_named_generator_attributes + g = generator ["admin/foo"] + assert_name g, "admin/foo", :name + assert_name g, %w(admin), :class_path + assert_name g, "Admin::Foo", :class_name + assert_name g, "admin/foo", :file_path + assert_name g, "foo", :file_name + assert_name g, "Foo", :human_name + assert_name g, "foo", :singular_name + assert_name g, "foos", :plural_name + assert_name g, "admin.foo", :i18n_scope + assert_name g, "admin_foos", :table_name + assert_name g, "admin/foos", :controller_name + assert_name g, %w(admin), :controller_class_path + assert_name g, "Admin::Foos", :controller_class_name + assert_name g, "admin/foos", :controller_file_path + assert_name g, "foos", :controller_file_name + assert_name g, "admin.foos", :controller_i18n_scope + assert_name g, "admin_foo", :singular_route_name + assert_name g, "admin_foos", :plural_route_name + assert_name g, "@admin_foo", :redirect_resource_name + assert_name g, "admin_foo", :model_resource_name + assert_name g, "admin_foos", :index_helper + end + + def test_named_generator_attributes_as_ruby + g = generator ["Admin::Foo"] + assert_name g, "Admin::Foo", :name + assert_name g, %w(admin), :class_path + assert_name g, "Admin::Foo", :class_name + assert_name g, "admin/foo", :file_path + assert_name g, "foo", :file_name + assert_name g, "foo", :singular_name + assert_name g, "Foo", :human_name + assert_name g, "foos", :plural_name + assert_name g, "admin.foo", :i18n_scope + assert_name g, "admin_foos", :table_name + assert_name g, "Admin::Foos", :controller_name + assert_name g, %w(admin), :controller_class_path + assert_name g, "Admin::Foos", :controller_class_name + assert_name g, "admin/foos", :controller_file_path + assert_name g, "foos", :controller_file_name + assert_name g, "admin.foos", :controller_i18n_scope + assert_name g, "admin_foo", :singular_route_name + assert_name g, "admin_foos", :plural_route_name + assert_name g, "@admin_foo", :redirect_resource_name + assert_name g, "admin_foo", :model_resource_name + assert_name g, "admin_foos", :index_helper + end + + def test_named_generator_attributes_without_pluralized + original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names + ActiveRecord::Base.pluralize_table_names = false + + g = generator ["admin/foo"] + assert_name g, "admin_foo", :table_name + ensure + ActiveRecord::Base.pluralize_table_names = original_pluralize_table_names + end + + def test_namespaced_scaffold_plural_names + g = generator ["admin/foo"] + assert_name g, "admin/foos", :controller_name + assert_name g, %w(admin), :controller_class_path + assert_name g, "Admin::Foos", :controller_class_name + assert_name g, "admin/foos", :controller_file_path + assert_name g, "foos", :controller_file_name + assert_name g, "admin.foos", :controller_i18n_scope + end + + def test_namespaced_scaffold_plural_names_as_ruby + g = generator ["Admin::Foo"] + assert_name g, "Admin::Foos", :controller_name + assert_name g, %w(admin), :controller_class_path + assert_name g, "Admin::Foos", :controller_class_name + assert_name g, "admin/foos", :controller_file_path + assert_name g, "foos", :controller_file_name + assert_name g, "admin.foos", :controller_i18n_scope + end + + def test_application_name + g = generator ["Admin::Foo"] + Rails.stub(:application, Object.new) do + assert_name g, "object", :application_name + end + + Rails.stub(:application, nil) do + assert_name g, "application", :application_name + end + end + + def test_index_helper + g = generator ["Post"] + assert_name g, "posts", :index_helper + end + + def test_index_helper_to_pluralize_once + g = generator ["Stadium"] + assert_name g, "stadia", :index_helper + end + + def test_index_helper_with_uncountable + g = generator ["Sheep"] + assert_name g, "sheep_index", :index_helper + end + + def test_hide_namespace + g = generator ["Hidden"] + g.class.stub(:namespace, "hidden") do + assert_not_includes Rails::Generators.hidden_namespaces, "hidden" + g.class.hide! + assert_includes Rails::Generators.hidden_namespaces, "hidden" + end + end + + def test_scaffold_plural_names_with_model_name_option + g = generator ["Admin::Foo"], model_name: "User" + assert_name g, "user", :singular_name + assert_name g, "User", :name + assert_name g, "user", :file_path + assert_name g, "User", :class_name + assert_name g, "user", :file_name + assert_name g, "User", :human_name + assert_name g, "users", :plural_name + assert_name g, "user", :i18n_scope + assert_name g, "users", :table_name + assert_name g, "Admin::Foos", :controller_name + assert_name g, %w(admin), :controller_class_path + assert_name g, "Admin::Foos", :controller_class_name + assert_name g, "admin/foos", :controller_file_path + assert_name g, "foos", :controller_file_name + assert_name g, "admin.foos", :controller_i18n_scope + assert_name g, "admin_user", :singular_route_name + assert_name g, "admin_users", :plural_route_name + assert_name g, "[:admin, @user]", :redirect_resource_name + assert_name g, "[:admin, user]", :model_resource_name + assert_name g, "admin_users", :index_helper + end + + def test_scaffold_plural_names + g = generator ["User"] + assert_name g, "@user", :redirect_resource_name + assert_name g, "user", :model_resource_name + assert_name g, "user", :singular_route_name + assert_name g, "users", :plural_route_name + end + + private + + def assert_name(generator, value, method) + assert_equal value, generator.send(method) + end +end diff --git a/railties/test/generators/namespaced_generators_test.rb b/railties/test/generators/namespaced_generators_test.rb new file mode 100644 index 0000000000..4b75a31f17 --- /dev/null +++ b/railties/test/generators/namespaced_generators_test.rb @@ -0,0 +1,436 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/controller/controller_generator" +require "rails/generators/rails/model/model_generator" +require "rails/generators/mailer/mailer_generator" +require "rails/generators/rails/scaffold/scaffold_generator" +require "rails/generators/rails/application_record/application_record_generator" + +class NamespacedGeneratorTestCase < Rails::Generators::TestCase + include GeneratorsTestHelper + + def setup + super + Rails::Generators.namespace = TestApp + 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 + assert_file "app/controllers/test_app/account_controller.rb", + /require_dependency "test_app\/application_controller"/, + /module TestApp/, + / class AccountController < ApplicationController/ + + assert_file "test/controllers/test_app/account_controller_test.rb", + /module TestApp/, + / class AccountControllerTest/ + 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"] + 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 + end + + def test_helper_is_also_namespaced + run_generator + assert_file "app/helpers/test_app/account_helper.rb", /module TestApp/, / module AccountHelper/ + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/controllers/test_app/account_controller_test.rb" + 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 + assert_file "config/routes.rb", /get 'account\/foo'/, /get 'account\/bar'/ + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["account"] + assert_file "app/views/test_app/account" + end + + def test_namespaced_controller_dont_indent_blank_lines + run_generator + assert_file "app/controllers/test_app/account_controller.rb" do |content| + content.split("\n").each do |line| + assert_no_match(/^\s+$/, line, "Don't indent blank lines") + end + end + end +end + +class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase + arguments %w(Account name:string age:integer) + tests Rails::Generators::ModelGenerator + + def test_module_file_is_not_created + run_generator + assert_no_file "app/models/test_app.rb" + end + + def test_adds_namespace_to_model + run_generator + assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ApplicationRecord/ + 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/ + assert_file "app/models/test_app/admin.rb", /'test_app_admin_'/ + assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ApplicationRecord/ + end + + def test_migration + run_generator + assert_migration "db/migrate/create_test_app_accounts.rb", /create_table :test_app_accounts/, /class CreateTestAppAccounts < ActiveRecord::Migration\[[0-9.]+\]/ + end + + def test_migration_with_namespace + run_generator ["Gallery::Image"] + assert_migration "db/migrate/create_test_app_gallery_images", /class CreateTestAppGalleryImages < ActiveRecord::Migration\[[0-9.]+\]/ + 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\[[0-9.]+\]/ + 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\[[0-9.]+\]/ + 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 + assert_file "test/models/test_app/account_test.rb", /module TestApp/, /class AccountTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/test_app/accounts.yml", /name: MyString/, /age: 1/ + end +end + +class NamespacedMailerGeneratorTest < NamespacedGeneratorTestCase + arguments %w(notifier foo bar) + tests Rails::Generators::MailerGenerator + + def test_mailer_skeleton_is_created + run_generator + assert_file "app/mailers/test_app/notifier_mailer.rb" do |mailer| + assert_match(/module TestApp/, mailer) + assert_match(/class NotifierMailer < ApplicationMailer/, mailer) + assert_no_match(/default from: "from@example\.com"/, mailer) + end + end + + def test_mailer_with_i18n_helper + run_generator + assert_file "app/mailers/test_app/notifier_mailer.rb" do |mailer| + assert_match(/en\.notifier_mailer\.foo\.subject/, mailer) + assert_match(/en\.notifier_mailer\.bar\.subject/, mailer) + end + end + + def test_invokes_default_test_framework + run_generator + assert_file "test/mailers/test_app/notifier_mailer_test.rb" do |test| + assert_match(/module TestApp/, test) + assert_match(/class NotifierMailerTest < ActionMailer::TestCase/, test) + assert_match(/test "foo"/, test) + assert_match(/test "bar"/, test) + end + end + + def test_invokes_default_template_engine + run_generator + 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) + assert_match(/<%= @greeting %>/, view) + end + + 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) + assert_match(/<%= @greeting %>/, view) + end + end + + def test_invokes_default_template_engine_even_with_no_action + run_generator ["notifier"] + assert_file "app/views/test_app/notifier_mailer" + end +end + +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 < ApplicationRecord/ + assert_file "test/models/test_app/product_line_test.rb", /module TestApp\n class ProductLineTest < ActiveSupport::TestCase/ + 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 + assert_file "app/controllers/test_app/product_lines_controller.rb", + /require_dependency "test_app\/application_controller"/, + /module TestApp/, + /class ProductLinesController < ApplicationController/ + + assert_file "test/controllers/test_app/product_lines_controller_test.rb", + /module TestApp\n class ProductLinesControllerTest < ActionDispatch::IntegrationTest/ + + # Views + %w(index edit new show _form).each do |view| + assert_file "app/views/test_app/product_lines/#{view}.html.erb" + end + assert_no_file "app/views/layouts/test_app/product_lines.html.erb" + + # Helpers + assert_file "app/helpers/test_app/product_lines_helper.rb" + + # Stylesheets + assert_file "app/assets/stylesheets/scaffold.css" + end + + def test_scaffold_on_revoke + run_generator + run_generator ["product_line"], behavior: :revoke + + # Model + assert_no_file "app/models/test_app/product_line.rb" + assert_no_file "test/models/test_app/product_line_test.rb" + 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" + assert_no_file "test/controllers/test_app/product_lines_controller_test.rb" + + # 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) + assert_file "app/assets/stylesheets/scaffold.css" + 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 < ApplicationRecord/ + 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 < ActionDispatch::IntegrationTest/ + + # Views + %w(index edit new show _form).each do |view| + assert_file "app/views/test_app/admin/roles/#{view}.html.erb" + end + assert_no_file "app/views/layouts/admin/roles.html.erb" + + # Helpers + assert_file "app/helpers/test_app/admin/roles_helper.rb" + + # Stylesheets + assert_file "app/assets/stylesheets/scaffold.css" + end + + def test_scaffold_with_namespace_on_revoke + run_generator [ "admin/role", "name:string", "description:string" ] + run_generator [ "admin/role" ], behavior: :revoke + + # Model + assert_file "app/models/test_app/admin.rb" # ( should not be remove ) + assert_no_file "app/models/test_app/admin/role.rb" + assert_no_file "test/models/test_app/admin/role_test.rb" + 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| + assert_no_match(/^ namespace :admin do\n resources :roles\n end$$/, route) + end + + # Controller + assert_no_file "app/controllers/test_app/admin/roles_controller.rb" + assert_no_file "test/controllers/test_app/admin/roles_controller_test.rb" + + # 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) + assert_file "app/assets/stylesheets/scaffold.css" + end + + 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 < ApplicationRecord/ + assert_file "test/models/test_app/admin/user/special/role_test.rb", /module TestApp\n class Admin::User::Special::RoleTest < ActiveSupport::TestCase/ + 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 + + assert_file "test/controllers/test_app/admin/user/special/roles_controller_test.rb", + /module TestApp\n class Admin::User::Special::RolesControllerTest < ActionDispatch::IntegrationTest/ + + # Views + %w(index edit new show _form).each do |view| + assert_file "app/views/test_app/admin/user/special/roles/#{view}.html.erb" + end + 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" ] + run_generator [ "admin/user/special/role" ], behavior: :revoke + + # 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" + assert_no_file "test/models/test_app/admin/user/special/role_test.rb" + 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" + assert_no_file "test/controllers/test_app/admin/user/special/roles_controller_test.rb" + + # 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 + + 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 < ApplicationRecord/ + 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 < ActionDispatch::IntegrationTest/ + end +end + +class NamespacedApplicationRecordGeneratorTest < NamespacedGeneratorTestCase + include GeneratorsTestHelper + tests Rails::Generators::ApplicationRecordGenerator + + def test_adds_namespace_to_application_record + run_generator + assert_file "app/models/test_app/application_record.rb", /module TestApp/, / class ApplicationRecord < ActiveRecord::Base/ + end +end diff --git a/railties/test/generators/orm_test.rb b/railties/test/generators/orm_test.rb new file mode 100644 index 0000000000..6eaf2fbfd3 --- /dev/null +++ b/railties/test/generators/orm_test.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/scaffold_controller/scaffold_controller_generator" + +# Mock out two ORMs +module ORMWithGenerators + module Generators + class ActiveModel + def initialize(name) + end + end + end +end + +module ORMWithoutGenerators + # No generators +end + +class OrmTest < Rails::Generators::TestCase + include GeneratorsTestHelper + tests Rails::Generators::ScaffoldControllerGenerator + + def test_orm_class_returns_custom_generator_if_supported_custom_orm_set + g = generator ["Foo"], orm: "ORMWithGenerators" + assert_equal ORMWithGenerators::Generators::ActiveModel, g.send(:orm_class) + end + + def test_orm_class_returns_rails_generator_if_unsupported_custom_orm_set + g = generator ["Foo"], orm: "ORMWithoutGenerators" + assert_equal Rails::Generators::ActiveModel, g.send(:orm_class) + end + + def test_orm_instance_returns_orm_class_instance_with_name + g = generator ["Foo"] + orm_instance = g.send(:orm_instance) + assert g.send(:orm_class) === orm_instance + assert_equal "foo", orm_instance.name + end +end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb new file mode 100644 index 0000000000..66286fc554 --- /dev/null +++ b/railties/test/generators/plugin_generator_test.rb @@ -0,0 +1,781 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/plugin/plugin_generator" +require "generators/shared_generator_tests" +require "rails/engine/updater" + +DEFAULT_PLUGIN_FILES = %w( + .gitignore + Gemfile + Rakefile + README.md + bukkits.gemspec + MIT-LICENSE + lib + lib/bukkits.rb + lib/tasks/bukkits_tasks.rake + lib/bukkits/version.rb + test/bukkits_test.rb + test/test_helper.rb + test/dummy +) + +class PluginGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + destination File.join(Rails.root, "tmp/bukkits") + arguments [destination_root] + + def application_path + "#{destination_root}/test/dummy" + end + + # brings setup, teardown, and some tests + include SharedGeneratorTests + + def test_invalid_plugin_name_raises_an_error + content = capture(:stderr) { run_generator [File.join(destination_root, "my_plugin-31fr-extension")] } + assert_equal "Invalid plugin name my_plugin-31fr-extension. Please give a name which does not contain a namespace starting with numeric characters.\n", content + + content = capture(:stderr) { run_generator [File.join(destination_root, "things4.3")] } + assert_equal "Invalid plugin name things4.3. Please give a name which uses only alphabetic, numeric, \"_\" or \"-\" characters.\n", content + + content = capture(:stderr) { run_generator [File.join(destination_root, "43things")] } + assert_equal "Invalid plugin name 43things. Please give a name which does not start with numbers.\n", content + + content = capture(:stderr) { run_generator [File.join(destination_root, "plugin")] } + assert_equal "Invalid plugin name plugin. Please give a name which does not match one of the reserved rails words: application, destroy, plugin, runner, test\n", content + + content = capture(:stderr) { run_generator [File.join(destination_root, "Digest")] } + assert_equal "Invalid plugin name Digest, constant Digest is already in use. Please choose another plugin name.\n", content + end + + def test_correct_file_in_lib_folder_of_hyphenated_plugin_name + run_generator [File.join(destination_root, "hyphenated-name")] + assert_no_file "hyphenated-name/lib/hyphenated-name.rb" + assert_no_file "hyphenated-name/lib/hyphenated_name.rb" + assert_file "hyphenated-name/lib/hyphenated/name.rb", /module Hyphenated\n module Name\n # Your code goes here\.\.\.\n end\nend/ + end + + def test_correct_file_in_lib_folder_of_camelcase_plugin_name + run_generator [File.join(destination_root, "CamelCasedName")] + assert_no_file "CamelCasedName/lib/CamelCasedName.rb" + assert_file "CamelCasedName/lib/camel_cased_name.rb", /module CamelCasedName/ + end + + def test_generating_without_options + run_generator + assert_file "README.md", /Bukkits/ + assert_no_file "config/routes.rb" + assert_no_file "app/assets/config/bukkits_manifest.js" + assert_file "test/test_helper.rb" do |content| + assert_match(/require_relative.+test\/dummy\/config\/environment/, content) + assert_match(/ActiveRecord::Migrator\.migrations_paths.+test\/dummy\/db\/migrate/, content) + assert_match(/Minitest\.backtrace_filter = Minitest::BacktraceFilter\.new/, content) + assert_match(/Rails::TestUnitReporter\.executable = 'bin\/test'/, content) + end + assert_file "lib/bukkits/railtie.rb", /module Bukkits\n class Railtie < ::Rails::Railtie\n end\nend/ + assert_file "lib/bukkits.rb", /require "bukkits\/railtie"/ + assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/ + assert_file "bin/test" + assert_no_file "bin/rails" + end + + def test_generating_in_full_mode_with_almost_of_all_skip_options + run_generator [destination_root, "--full", "-M", "-O", "-C", "-S", "-T", "--skip-active-storage"] + assert_file "bin/rails" do |content| + assert_no_match(/\s+require\s+["']rails\/all["']/, content) + end + assert_file "bin/rails", /#\s+require\s+["']active_record\/railtie["']/ + assert_file "bin/rails", /#\s+require\s+["']active_storage\/engine["']/ + assert_file "bin/rails", /#\s+require\s+["']action_mailer\/railtie["']/ + assert_file "bin/rails", /#\s+require\s+["']action_cable\/engine["']/ + assert_file "bin/rails", /#\s+require\s+["']sprockets\/railtie["']/ + assert_file "bin/rails", /#\s+require\s+["']rails\/test_unit\/railtie["']/ + end + + def test_generating_test_files_in_full_mode + run_generator [destination_root, "--full"] + assert_directory "test/integration/" + + assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/ + end + + def test_inclusion_of_git_source + run_generator [destination_root] + assert_file "Gemfile", /git_source/ + end + + def test_inclusion_of_a_debugger + run_generator [destination_root, "--full"] + if defined?(JRUBY_VERSION) || RUBY_ENGINE == "rbx" + assert_file "Gemfile" do |content| + assert_no_match(/byebug/, content) + end + else + assert_file "Gemfile", /# gem 'byebug'/ + end + end + + def test_generating_test_files_in_full_mode_without_unit_test_files + run_generator [destination_root, "-T", "--full"] + + assert_no_directory "test/integration/" + assert_no_directory "test" + assert_file "Rakefile" do |contents| + assert_no_match(/APP_RAKEFILE/, contents) + end + assert_file "bin/rails" do |contents| + assert_no_match(/APP_PATH/, contents) + end + end + + def test_generating_adds_dummy_app_rake_tasks_without_unit_test_files + run_generator [destination_root, "-T", "--mountable", "--dummy-path", "my_dummy_app"] + assert_file "Rakefile", /APP_RAKEFILE/ + assert_file "bin/rails", /APP_PATH/ + end + + def test_generating_adds_dummy_app_without_javascript_and_assets_deps + run_generator + + assert_file "test/dummy/app/assets/stylesheets/application.css" + end + + def test_ensure_that_plugin_options_are_not_passed_to_app_generator + FileUtils.cd(Rails.root) + assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])) + end + + def test_ensure_that_test_dummy_can_be_generated_from_a_template + FileUtils.cd(Rails.root) + run_generator([destination_root, "-m", "lib/create_test_dummy_template.rb", "--skip-test"]) + assert_directory "spec/dummy" + assert_no_directory "test" + end + + def test_database_entry_is_generated_for_sqlite3_by_default_in_full_mode + run_generator([destination_root, "--full"]) + assert_file "test/dummy/config/database.yml", /sqlite/ + assert_file "bukkits.gemspec", /sqlite3/ + end + + def test_config_another_database + run_generator([destination_root, "-d", "mysql", "--full"]) + assert_file "test/dummy/config/database.yml", /mysql/ + assert_file "bukkits.gemspec", /mysql/ + end + + def test_dont_generate_development_dependency + run_generator [destination_root, "--skip-active-record"] + + assert_file "bukkits.gemspec" do |contents| + assert_no_match(/s\.add_development_dependency "sqlite3"/, contents) + end + end + + def test_ensure_that_skip_active_record_option_is_passed_to_app_generator + run_generator [destination_root, "--skip_active_record"] + assert_file "test/test_helper.rb" do |contents| + assert_no_match(/ActiveRecord/, contents) + end + end + + def test_ensure_that_database_option_is_passed_to_app_generator + run_generator [destination_root, "--database", "postgresql"] + assert_file "test/dummy/config/database.yml", /postgres/ + end + + def test_generation_runs_bundle_install + assert_generates_without_bundler + end + + def test_dev_option + assert_generates_without_bundler(dev: true) + rails_path = File.expand_path("../../..", Rails.root) + assert_file "Gemfile", /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/ + end + + def test_edge_option + assert_generates_without_bundler(edge: true) + assert_file "Gemfile", %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$} + end + + def test_generation_does_not_run_bundle_install_with_full_and_mountable + assert_generates_without_bundler(mountable: true, full: true, dev: true) + assert_no_file "#{destination_root}/Gemfile.lock" + end + + def test_skip_javascript + run_generator [destination_root, "--skip-javascript", "--mountable"] + assert_file "app/views/layouts/bukkits/application.html.erb" do |content| + assert_no_match "javascript_pack_tag", content + end + end + + def test_template_from_dir_pwd + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) + end + + def test_ensure_that_tests_work + run_generator + FileUtils.cd destination_root + quietly { system "bundle install" } + assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bin/test 2>&1`) + end + + def test_ensure_that_tests_works_in_full_mode + run_generator [destination_root, "--full", "--skip_active_record"] + FileUtils.cd destination_root + quietly { system "bundle install" } + assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test 2>&1`) + end + + def test_ensure_that_migration_tasks_work_with_mountable_option + run_generator [destination_root, "--mountable"] + FileUtils.cd destination_root + quietly { system "bundle install" } + output = `bin/rails db:migrate 2>&1` + assert $?.success?, "Command failed: #{output}" + end + + def test_creating_engine_in_full_mode + run_generator [destination_root, "--full"] + assert_file "app/assets/stylesheets/bukkits" + assert_file "app/assets/images/bukkits" + assert_file "app/models" + assert_file "app/controllers" + assert_file "app/views" + assert_file "app/helpers" + assert_file "app/mailers" + assert_file "bin/rails", /\s+require\s+["']rails\/all["']/ + assert_file "config/routes.rb", /Rails.application.routes.draw do/ + assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < ::Rails::Engine\n end\nend/ + assert_file "lib/bukkits.rb", /require "bukkits\/engine"/ + end + + def test_creating_engine_with_hyphenated_name_in_full_mode + run_generator [File.join(destination_root, "hyphenated-name"), "--full"] + assert_no_file "hyphenated-name/app/assets/javascripts/hyphenated/name" + assert_file "hyphenated-name/app/assets/stylesheets/hyphenated/name" + assert_file "hyphenated-name/app/assets/images/hyphenated/name" + assert_file "hyphenated-name/app/models" + assert_file "hyphenated-name/app/controllers" + assert_file "hyphenated-name/app/views" + assert_file "hyphenated-name/app/helpers" + assert_file "hyphenated-name/app/mailers" + assert_file "hyphenated-name/bin/rails" + assert_file "hyphenated-name/config/routes.rb", /Rails.application.routes.draw do/ + assert_file "hyphenated-name/lib/hyphenated/name/engine.rb", /module Hyphenated\n module Name\n class Engine < ::Rails::Engine\n end\n end\nend/ + assert_file "hyphenated-name/lib/hyphenated/name.rb", /require "hyphenated\/name\/engine"/ + assert_file "hyphenated-name/bin/rails", /\.\.\/lib\/hyphenated\/name\/engine/ + end + + def test_creating_engine_with_hyphenated_and_underscored_name_in_full_mode + run_generator [File.join(destination_root, "my_hyphenated-name"), "--full"] + assert_no_file "my_hyphenated-name/app/assets/javascripts/my_hyphenated/name" + assert_file "my_hyphenated-name/app/assets/stylesheets/my_hyphenated/name" + assert_file "my_hyphenated-name/app/assets/images/my_hyphenated/name" + assert_file "my_hyphenated-name/app/models" + assert_file "my_hyphenated-name/app/controllers" + assert_file "my_hyphenated-name/app/views" + assert_file "my_hyphenated-name/app/helpers" + assert_file "my_hyphenated-name/app/mailers" + assert_file "my_hyphenated-name/bin/rails" + assert_file "my_hyphenated-name/config/routes.rb", /Rails\.application\.routes\.draw do/ + assert_file "my_hyphenated-name/lib/my_hyphenated/name/engine.rb", /module MyHyphenated\n module Name\n class Engine < ::Rails::Engine\n end\n end\nend/ + assert_file "my_hyphenated-name/lib/my_hyphenated/name.rb", /require "my_hyphenated\/name\/engine"/ + assert_file "my_hyphenated-name/bin/rails", /\.\.\/lib\/my_hyphenated\/name\/engine/ + end + + def test_being_quiet_while_creating_dummy_application + assert_no_match(/create\s+config\/application\.rb/, run_generator) + end + + def test_create_mountable_application_with_mountable_option + run_generator [destination_root, "--mountable"] + assert_no_file "app/assets/javascripts/bukkits" + assert_file "app/assets/stylesheets/bukkits" + assert_file "app/assets/images/bukkits" + assert_file "config/routes.rb", /Bukkits::Engine\.routes\.draw do/ + assert_file "lib/bukkits/engine.rb", /isolate_namespace Bukkits/ + assert_file "test/dummy/config/routes.rb", /mount Bukkits::Engine => "\/bukkits"/ + assert_file "app/controllers/bukkits/application_controller.rb", /module Bukkits\n class ApplicationController < ActionController::Base/ + assert_file "app/models/bukkits/application_record.rb", /module Bukkits\n class ApplicationRecord < ActiveRecord::Base/ + assert_file "app/jobs/bukkits/application_job.rb", /module Bukkits\n class ApplicationJob < ActiveJob::Base/ + assert_file "app/mailers/bukkits/application_mailer.rb", /module Bukkits\n class ApplicationMailer < ActionMailer::Base\n default from: 'from@example\.com'\n layout 'mailer'\n/ + assert_file "app/helpers/bukkits/application_helper.rb", /module Bukkits\n module ApplicationHelper/ + assert_file "app/views/layouts/bukkits/application.html.erb" do |contents| + assert_match "<title>Bukkits</title>", contents + assert_match "<%= csrf_meta_tags %>", contents + assert_match "<%= csp_meta_tag %>", contents + assert_match(/stylesheet_link_tag\s+['"]bukkits\/application['"]/, contents) + assert_no_match(/javascript_include_tag\s+['"]bukkits\/application['"]/, contents) + assert_match "<%= yield %>", contents + end + assert_file "test/test_helper.rb" do |content| + assert_match(/ActiveRecord::Migrator\.migrations_paths.+\.\.\/test\/dummy\/db\/migrate/, content) + assert_match(/ActiveRecord::Migrator\.migrations_paths.+<<.+\.\.\/db\/migrate/, content) + assert_match(/ActionDispatch::IntegrationTest\.fixture_path = ActiveSupport::TestCase\.fixture_pat/, content) + assert_no_match(/Rails::TestUnitReporter\.executable = 'bin\/test'/, content) + end + assert_no_file "bin/test" + end + + def test_create_mountable_application_with_mountable_option_and_hypenated_name + run_generator [File.join(destination_root, "hyphenated-name"), "--mountable"] + assert_no_file "hyphenated-name/app/assets/javascripts/hyphenated/name" + assert_file "hyphenated-name/app/assets/stylesheets/hyphenated/name" + assert_file "hyphenated-name/app/assets/images/hyphenated/name" + assert_file "hyphenated-name/config/routes.rb", /Hyphenated::Name::Engine\.routes\.draw do/ + assert_file "hyphenated-name/lib/hyphenated/name/version.rb", /module Hyphenated\n module Name\n VERSION = '0\.1\.0'\n end\nend/ + assert_file "hyphenated-name/lib/hyphenated/name/engine.rb", /module Hyphenated\n module Name\n class Engine < ::Rails::Engine\n isolate_namespace Hyphenated::Name\n end\n end\nend/ + assert_file "hyphenated-name/lib/hyphenated/name.rb", /require "hyphenated\/name\/engine"/ + assert_file "hyphenated-name/test/dummy/config/routes.rb", /mount Hyphenated::Name::Engine => "\/hyphenated-name"/ + assert_file "hyphenated-name/app/controllers/hyphenated/name/application_controller.rb", /module Hyphenated\n module Name\n class ApplicationController < ActionController::Base\n protect_from_forgery with: :exception\n end\n end\nend\n/ + assert_file "hyphenated-name/app/models/hyphenated/name/application_record.rb", /module Hyphenated\n module Name\n class ApplicationRecord < ActiveRecord::Base\n self\.abstract_class = true\n end\n end\nend/ + assert_file "hyphenated-name/app/jobs/hyphenated/name/application_job.rb", /module Hyphenated\n module Name\n class ApplicationJob < ActiveJob::Base/ + assert_file "hyphenated-name/app/mailers/hyphenated/name/application_mailer.rb", /module Hyphenated\n module Name\n class ApplicationMailer < ActionMailer::Base\n default from: 'from@example\.com'\n layout 'mailer'\n end\n end\nend/ + assert_file "hyphenated-name/app/helpers/hyphenated/name/application_helper.rb", /module Hyphenated\n module Name\n module ApplicationHelper\n end\n end\nend/ + assert_file "hyphenated-name/app/views/layouts/hyphenated/name/application.html.erb" do |contents| + assert_match "<title>Hyphenated name</title>", contents + assert_match(/stylesheet_link_tag\s+['"]hyphenated\/name\/application['"]/, contents) + assert_no_match(/javascript_include_tag\s+['"]hyphenated\/name\/application['"]/, contents) + end + end + + def test_create_mountable_application_with_mountable_option_and_hypenated_and_underscored_name + run_generator [File.join(destination_root, "my_hyphenated-name"), "--mountable"] + assert_no_file "my_hyphenated-name/app/assets/javascripts/my_hyphenated/name" + assert_file "my_hyphenated-name/app/assets/stylesheets/my_hyphenated/name" + assert_file "my_hyphenated-name/app/assets/images/my_hyphenated/name" + assert_file "my_hyphenated-name/config/routes.rb", /MyHyphenated::Name::Engine\.routes\.draw do/ + assert_file "my_hyphenated-name/lib/my_hyphenated/name/version.rb", /module MyHyphenated\n module Name\n VERSION = '0\.1\.0'\n end\nend/ + assert_file "my_hyphenated-name/lib/my_hyphenated/name/engine.rb", /module MyHyphenated\n module Name\n class Engine < ::Rails::Engine\n isolate_namespace MyHyphenated::Name\n end\n end\nend/ + assert_file "my_hyphenated-name/lib/my_hyphenated/name.rb", /require "my_hyphenated\/name\/engine"/ + assert_file "my_hyphenated-name/test/dummy/config/routes.rb", /mount MyHyphenated::Name::Engine => "\/my_hyphenated-name"/ + assert_file "my_hyphenated-name/app/controllers/my_hyphenated/name/application_controller.rb", /module MyHyphenated\n module Name\n class ApplicationController < ActionController::Base\n protect_from_forgery with: :exception\n end\n end\nend\n/ + assert_file "my_hyphenated-name/app/models/my_hyphenated/name/application_record.rb", /module MyHyphenated\n module Name\n class ApplicationRecord < ActiveRecord::Base\n self\.abstract_class = true\n end\n end\nend/ + assert_file "my_hyphenated-name/app/jobs/my_hyphenated/name/application_job.rb", /module MyHyphenated\n module Name\n class ApplicationJob < ActiveJob::Base/ + assert_file "my_hyphenated-name/app/mailers/my_hyphenated/name/application_mailer.rb", /module MyHyphenated\n module Name\n class ApplicationMailer < ActionMailer::Base\n default from: 'from@example\.com'\n layout 'mailer'\n end\n end\nend/ + assert_file "my_hyphenated-name/app/helpers/my_hyphenated/name/application_helper.rb", /module MyHyphenated\n module Name\n module ApplicationHelper\n end\n end\nend/ + assert_file "my_hyphenated-name/app/views/layouts/my_hyphenated/name/application.html.erb" do |contents| + assert_match "<title>My hyphenated name</title>", contents + assert_match(/stylesheet_link_tag\s+['"]my_hyphenated\/name\/application['"]/, contents) + assert_no_match(/javascript_include_tag\s+['"]my_hyphenated\/name\/application['"]/, contents) + end + end + + def test_create_mountable_application_with_mountable_option_and_multiple_hypenates_in_name + run_generator [File.join(destination_root, "deep-hyphenated-name"), "--mountable"] + assert_no_file "deep-hyphenated-name/app/assets/javascripts/deep/hyphenated/name" + assert_file "deep-hyphenated-name/app/assets/stylesheets/deep/hyphenated/name" + assert_file "deep-hyphenated-name/app/assets/images/deep/hyphenated/name" + assert_file "deep-hyphenated-name/config/routes.rb", /Deep::Hyphenated::Name::Engine\.routes\.draw do/ + assert_file "deep-hyphenated-name/lib/deep/hyphenated/name/version.rb", /module Deep\n module Hyphenated\n module Name\n VERSION = '0\.1\.0'\n end\n end\nend/ + assert_file "deep-hyphenated-name/lib/deep/hyphenated/name/engine.rb", /module Deep\n module Hyphenated\n module Name\n class Engine < ::Rails::Engine\n isolate_namespace Deep::Hyphenated::Name\n end\n end\n end\nend/ + assert_file "deep-hyphenated-name/lib/deep/hyphenated/name.rb", /require "deep\/hyphenated\/name\/engine"/ + assert_file "deep-hyphenated-name/test/dummy/config/routes.rb", /mount Deep::Hyphenated::Name::Engine => "\/deep-hyphenated-name"/ + assert_file "deep-hyphenated-name/app/controllers/deep/hyphenated/name/application_controller.rb", /module Deep\n module Hyphenated\n module Name\n class ApplicationController < ActionController::Base\n protect_from_forgery with: :exception\n end\n end\n end\nend\n/ + assert_file "deep-hyphenated-name/app/models/deep/hyphenated/name/application_record.rb", /module Deep\n module Hyphenated\n module Name\n class ApplicationRecord < ActiveRecord::Base\n self\.abstract_class = true\n end\n end\n end\nend/ + assert_file "deep-hyphenated-name/app/jobs/deep/hyphenated/name/application_job.rb", /module Deep\n module Hyphenated\n module Name\n class ApplicationJob < ActiveJob::Base/ + assert_file "deep-hyphenated-name/app/mailers/deep/hyphenated/name/application_mailer.rb", /module Deep\n module Hyphenated\n module Name\n class ApplicationMailer < ActionMailer::Base\n default from: 'from@example\.com'\n layout 'mailer'\n end\n end\n end\nend/ + assert_file "deep-hyphenated-name/app/helpers/deep/hyphenated/name/application_helper.rb", /module Deep\n module Hyphenated\n module Name\n module ApplicationHelper\n end\n end\n end\nend/ + assert_file "deep-hyphenated-name/app/views/layouts/deep/hyphenated/name/application.html.erb" do |contents| + assert_match "<title>Deep hyphenated name</title>", contents + assert_match(/stylesheet_link_tag\s+['"]deep\/hyphenated\/name\/application['"]/, contents) + assert_no_match(/javascript_include_tag\s+['"]deep\/hyphenated\/name\/application['"]/, contents) + end + end + + def test_creating_gemspec + run_generator + assert_file "bukkits.gemspec", /spec\.name\s+= "bukkits"/ + assert_file "bukkits.gemspec", /spec\.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*", "MIT-LICENSE", "Rakefile", "README\.md"\]/ + assert_file "bukkits.gemspec", /spec\.version\s+ = Bukkits::VERSION/ + end + + def test_usage_of_engine_commands + run_generator [destination_root, "--full"] + assert_file "bin/rails", /ENGINE_PATH = File\.expand_path\('\.\.\/lib\/bukkits\/engine', __dir__\)/ + assert_file "bin/rails", /ENGINE_ROOT = File\.expand_path\('\.\.', __dir__\)/ + assert_file "bin/rails", %r|APP_PATH = File\.expand_path\('\.\./test/dummy/config/application', __dir__\)| + assert_file "bin/rails", /require 'rails\/all'/ + assert_file "bin/rails", /require 'rails\/engine\/commands'/ + end + + def test_shebang + run_generator [destination_root, "--full"] + assert_file "bin/rails", /#!\/usr\/bin\/env ruby/ + end + + def test_passing_dummy_path_as_a_parameter + run_generator [destination_root, "--dummy_path", "spec/dummy"] + assert_file "spec/dummy" + assert_file "spec/dummy/config/application.rb" + assert_no_file "test/dummy" + assert_file "test/test_helper.rb" do |content| + assert_match(/require_relative.+spec\/dummy\/config\/environment/, content) + assert_match(/ActiveRecord::Migrator\.migrations_paths.+spec\/dummy\/db\/migrate/, content) + end + end + + def test_creating_dummy_application_with_different_name + run_generator [destination_root, "--dummy_path", "spec/fake"] + assert_file "spec/fake" + assert_file "spec/fake/config/application.rb" + assert_no_file "test/dummy" + assert_file "test/test_helper.rb" do |content| + assert_match(/require_relative.+spec\/fake\/config\/environment/, content) + assert_match(/ActiveRecord::Migrator\.migrations_paths.+spec\/fake\/db\/migrate/, content) + end + end + + def test_creating_dummy_without_tests_but_with_dummy_path + run_generator [destination_root, "--dummy_path", "spec/dummy", "--skip-test"] + assert_directory "spec/dummy" + assert_file "spec/dummy/config/application.rb", /#\s+require\s+["']rails\/test_unit\/railtie["']/ + assert_no_directory "test" + assert_file ".gitignore" do |contents| + assert_match(/spec\/dummy/, contents) + end + end + + def test_dummy_appplication_skip_listen_by_default + run_generator + + assert_file "test/dummy/config/environments/development.rb" do |contents| + assert_match(/^\s*# config\.file_watcher = ActiveSupport::EventedFileUpdateChecker/, contents) + end + end + + def test_ensure_that_gitignore_can_be_generated_from_a_template_for_dummy_path + FileUtils.cd(Rails.root) + run_generator([destination_root, "--dummy_path", "spec/dummy", "--skip-test"]) + assert_file ".gitignore" do |contents| + assert_match(/spec\/dummy/, contents) + end + end + + def test_unnecessary_files_are_not_generated_in_dummy_application + run_generator + assert_no_file "test/dummy/.gitignore" + assert_no_file "test/dummy/db/seeds.rb" + assert_no_file "test/dummy/Gemfile" + assert_no_file "test/dummy/public/robots.txt" + assert_no_file "test/dummy/README.md" + assert_no_file "test/dummy/config/master.key" + assert_no_file "test/dummy/config/credentials.yml.enc" + assert_no_directory "test/dummy/lib/tasks" + assert_no_directory "test/dummy/test" + assert_no_directory "test/dummy/vendor" + assert_no_directory "test/dummy/.git" + end + + def test_skipping_test_files + run_generator [destination_root, "--skip-test"] + assert_no_directory "test" + assert_file ".gitignore" do |contents| + assert_no_match(/test\/dummy/, contents) + end + end + + def test_skipping_gemspec + run_generator [destination_root, "--skip-gemspec"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match("gemspec", contents) + assert_match(/gem 'rails'/, contents) + assert_match_sqlite3(contents) + end + end + + def test_skipping_gemspec_in_full_mode + run_generator [destination_root, "--skip-gemspec", "--full"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match("gemspec", contents) + assert_match(/gem 'rails'/, contents) + assert_match_sqlite3(contents) + end + end + + def test_creating_plugin_in_app_directory_adds_gemfile_entry + # simulate application existence + gemfile_path = "#{Rails.root}/Gemfile" + Object.const_set("APP_PATH", Rails.root) + FileUtils.touch gemfile_path + File.write(gemfile_path, "#foo") + + run_generator + + assert_file gemfile_path, /^gem 'bukkits', path: 'tmp\/bukkits'/ + ensure + Object.send(:remove_const, "APP_PATH") + FileUtils.rm gemfile_path + end + + def test_creating_plugin_only_specify_plugin_name_in_app_directory_adds_gemfile_entry + # simulate application existence + gemfile_path = "#{Rails.root}/Gemfile" + Object.const_set("APP_PATH", Rails.root) + FileUtils.touch gemfile_path + + FileUtils.cd(destination_root) + run_generator ["bukkits"] + + assert_file gemfile_path, /gem 'bukkits', path: 'bukkits'/ + ensure + Object.send(:remove_const, "APP_PATH") + FileUtils.rm gemfile_path + end + + def test_skipping_gemfile_entry + # simulate application existence + gemfile_path = "#{Rails.root}/Gemfile" + Object.const_set("APP_PATH", Rails.root) + FileUtils.touch gemfile_path + + run_generator [destination_root, "--skip-gemfile-entry"] + + assert_file gemfile_path do |contents| + assert_no_match(/gem 'bukkits', path: 'tmp\/bukkits'/, contents) + end + ensure + Object.send(:remove_const, "APP_PATH") + FileUtils.rm gemfile_path + end + + def test_generating_controller_inside_mountable_engine + run_generator [destination_root, "--mountable"] + + capture(:stdout) do + `#{destination_root}/bin/rails g controller admin/dashboard foo` + end + + assert_file "config/routes.rb" do |contents| + assert_match(/namespace :admin/, contents) + assert_no_match(/namespace :bukkit/, contents) + end + end + + def test_git_name_and_email_in_gemspec_file + name = `git config user.name`.chomp rescue "TODO: Write your name" + email = `git config user.email`.chomp rescue "TODO: Write your email address" + + run_generator + assert_file "bukkits.gemspec" do |contents| + assert_match name, contents + assert_match email, contents + end + end + + def test_git_name_in_license_file + name = `git config user.name`.chomp rescue "TODO: Write your name" + + run_generator + assert_file "MIT-LICENSE" do |contents| + assert_match name, contents + end + end + + def test_no_details_from_git_when_skip_git + name = "TODO: Write your name" + email = "TODO: Write your email address" + + run_generator [destination_root, "--skip-git"] + assert_file "MIT-LICENSE" do |contents| + assert_match name, contents + end + assert_file "bukkits.gemspec" do |contents| + assert_match name, contents + assert_match email, contents + end + end + + def test_skipping_useless_folders_generation_for_api_engines + ["--full", "--mountable"].each do |option| + run_generator [destination_root, option, "--api"] + + assert_no_directory "app/assets" + assert_no_directory "app/helpers" + assert_no_directory "app/views" + + FileUtils.rm_rf destination_root + end + end + + def test_application_controller_parent_for_mountable_api_plugins + run_generator [destination_root, "--mountable", "--api"] + + assert_file "app/controllers/bukkits/application_controller.rb" do |content| + assert_match "ApplicationController < ActionController::API", content + end + end + + def test_dummy_api_application_for_api_plugins + run_generator [destination_root, "--api"] + + assert_file "test/dummy/config/application.rb" do |content| + assert_match "config.api_only = true", content + end + end + + def test_api_generators_configuration_for_api_engines + run_generator [destination_root, "--full", "--api"] + + assert_file "lib/bukkits/engine.rb" do |content| + assert_match "config.generators.api_only = true", content + end + end + + def test_scaffold_generator_for_mountable_api_plugins + run_generator [destination_root, "--mountable", "--api"] + + capture(:stdout) do + `#{destination_root}/bin/rails g scaffold article` + end + + assert_file "app/models/bukkits/article.rb" + assert_file "app/controllers/bukkits/articles_controller.rb" do |content| + assert_match "only: [:show, :update, :destroy]", content + end + + assert_no_directory "app/assets" + assert_no_directory "app/helpers" + assert_no_directory "app/views" + end + + def test_model_with_existent_application_record_in_mountable_engine + run_generator [destination_root, "--mountable"] + capture(:stdout) do + `#{destination_root}/bin/rails g model article` + end + + assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/ + end + + def test_generate_application_mailer_when_does_not_exist_in_mountable_engine + run_generator [destination_root, "--mountable"] + FileUtils.rm "#{destination_root}/app/mailers/bukkits/application_mailer.rb" + capture(:stdout) do + `#{destination_root}/bin/rails g mailer User` + end + + assert_file "#{destination_root}/app/mailers/bukkits/application_mailer.rb" do |mailer| + assert_match(/module Bukkits/, mailer) + assert_match(/class ApplicationMailer < ActionMailer::Base/, mailer) + end + end + + def test_generate_mailer_layouts_when_does_not_exist_in_mountable_engine + run_generator [destination_root, "--mountable"] + capture(:stdout) do + `#{destination_root}/bin/rails g mailer User` + end + + assert_file "#{destination_root}/app/views/layouts/bukkits/mailer.text.erb" do |view| + assert_match(/<%= yield %>/, view) + end + + assert_file "#{destination_root}/app/views/layouts/bukkits/mailer.html.erb" do |view| + assert_match(%r{<body>\n <%= yield %>\n </body>}, view) + end + end + + def test_generate_application_job_when_does_not_exist_in_mountable_engine + run_generator [destination_root, "--mountable"] + FileUtils.rm "#{destination_root}/app/jobs/bukkits/application_job.rb" + capture(:stdout) do + `#{destination_root}/bin/rails g job refresh_counters` + end + + assert_file "#{destination_root}/app/jobs/bukkits/application_job.rb" do |record| + assert_match(/module Bukkits/, record) + assert_match(/class ApplicationJob < ActiveJob::Base/, record) + end + end + + def test_app_update_generates_bin_file + run_generator [destination_root, "--mountable"] + + Object.const_set("ENGINE_ROOT", destination_root) + FileUtils.rm("#{destination_root}/bin/rails") + + quietly { Rails::Engine::Updater.run(:create_bin_files) } + + assert_file "#{destination_root}/bin/rails" do |content| + assert_match(%r|APP_PATH = File\.expand_path\('\.\./test/dummy/config/application', __dir__\)|, content) + end + ensure + Object.send(:remove_const, "ENGINE_ROOT") + end + + def test_after_bundle_callback + path = "http://example.org/rails_template" + template = +%{ after_bundle { run "echo ran after_bundle" } } + template.instance_eval "def read; self; end" # Make the string respond to read + + check_open = -> *args do + assert_equal [ path, "Accept" => "application/x-thor-template" ], args + template + end + + sequence = ["echo ran after_bundle"] + @sequence_step ||= 0 + ensure_bundler_first = -> command do + assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}" + @sequence_step += 1 + end + + content = nil + generator([destination_root], template: path).stub(:open, check_open, template) do + generator.stub(:bundle_command, ensure_bundler_first) do + generator.stub(:run, ensure_bundler_first) do + silence_stream($stdout) do + content = capture(:stderr) { generator.invoke_all } + end + end + end + end + + assert_equal 1, @sequence_step + assert_match(/DEPRECATION WARNING: `after_bundle` is deprecated/, content) + end + + private + + def action(*args, &block) + silence(:stdout) { generator.send(*args, &block) } + end + + def default_files + ::DEFAULT_PLUGIN_FILES + end + + def assert_match_sqlite3(contents) + if defined?(JRUBY_VERSION) + assert_match(/group :development do\n gem 'activerecord-jdbcsqlite3-adapter'\nend/, contents) + else + assert_match(/group :development do\n gem 'sqlite3'\nend/, contents) + end + end + + def assert_generates_without_bundler(options = {}) + generator([destination_root], options) + + command_check = -> command do + case command + when "install" + flunk "install expected to not be called" + when "exec spring binstub --all" + # Called when running tests with spring, let through unscathed. + end + end + + generator.stub :bundle_command, command_check do + quietly { generator.invoke_all } + end + end +end diff --git a/railties/test/generators/plugin_test_helper.rb b/railties/test/generators/plugin_test_helper.rb new file mode 100644 index 0000000000..528f8d88f9 --- /dev/null +++ b/railties/test/generators/plugin_test_helper.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "abstract_unit" +require "tmpdir" + +module PluginTestHelper + def create_test_file(name, pass: true) + plugin_file "test/#{name}_test.rb", <<-RUBY + require 'test_helper' + + class #{name.camelize}Test < ActiveSupport::TestCase + def test_truth + puts "#{name.camelize}Test" + assert #{pass}, 'wups!' + end + end + RUBY + end + + def plugin_file(path, contents, mode: "w") + FileUtils.mkdir_p File.dirname("#{plugin_path}/#{path}") + File.open("#{plugin_path}/#{path}", mode) do |f| + f.puts contents + end + end +end diff --git a/railties/test/generators/plugin_test_runner_test.rb b/railties/test/generators/plugin_test_runner_test.rb new file mode 100644 index 0000000000..89c3f1e496 --- /dev/null +++ b/railties/test/generators/plugin_test_runner_test.rb @@ -0,0 +1,122 @@ +# frozen_string_literal: true + +require "generators/plugin_test_helper" + +class PluginTestRunnerTest < ActiveSupport::TestCase + include PluginTestHelper + + def setup + @destination_root = Dir.mktmpdir("bukkits") + Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --skip-bundle` } + plugin_file "test/dummy/db/schema.rb", "" + end + + def teardown + FileUtils.rm_rf(@destination_root) + end + + def test_run_single_file + create_test_file "foo" + create_test_file "bar" + assert_match "1 runs, 1 assertions, 0 failures", run_test_command("test/foo_test.rb") + end + + def test_run_multiple_files + create_test_file "foo" + create_test_file "bar" + assert_match "2 runs, 2 assertions, 0 failures", run_test_command("test/foo_test.rb test/bar_test.rb") + end + + def test_mix_files_and_line_filters + create_test_file "account" + plugin_file "test/post_test.rb", <<-RUBY + require 'test_helper' + + class PostTest < ActiveSupport::TestCase + def test_post + puts 'PostTest' + assert true + end + + def test_line_filter_does_not_run_this + assert true + end + end + RUBY + + run_test_command("test/account_test.rb test/post_test.rb:4").tap do |output| + assert_match "AccountTest", output + assert_match "PostTest", output + assert_match "2 runs, 2 assertions", output + end + end + + def test_multiple_line_filters + create_test_file "account" + create_test_file "post" + + run_test_command("test/account_test.rb:4 test/post_test.rb:4").tap do |output| + assert_match "AccountTest", output + assert_match "PostTest", output + end + end + + def test_output_inline_by_default + create_test_file "post", pass: false + + output = run_test_command("test/post_test.rb") + expect = %r{Running:\n\nPostTest\nF\n\nFailure:\nPostTest#test_truth \[[^\]]+test/post_test.rb:6\]:\nwups!\n\nbin/test (/private)?#{plugin_path}/test/post_test.rb:4} + assert_match expect, output + end + + def test_only_inline_failure_output + create_test_file "post", pass: false + + output = run_test_command("test/post_test.rb") + assert_match %r{Finished in.*\n1 runs, 1 assertions}, output + end + + def test_fail_fast + create_test_file "post", pass: false + + assert_match(/Interrupt/, + capture(:stderr) { run_test_command("test/post_test.rb --fail-fast") }) + end + + def test_raise_error_when_specified_file_does_not_exist + error = capture(:stderr) { run_test_command("test/not_exists.rb") } + assert_match(%r{cannot load such file.+test/not_exists\.rb}, error) + end + + def test_executed_only_once + create_test_file "foo" + result = run_test_command("test/foo_test.rb") + assert_equal 1, result.scan(/1 runs, 1 assertions, 0 failures/).length + end + + def test_warnings_option + plugin_file "test/models/warnings_test.rb", <<-RUBY + require 'test_helper' + def test_warnings + a = 1 + end + RUBY + assert_match(/warning: assigned but unused variable/, + capture(:stderr) { run_test_command("test/models/warnings_test.rb -w") }) + end + + def test_run_rake_test + create_test_file "foo" + result = Dir.chdir(plugin_path) { `rake test TEST=test/foo_test.rb` } + assert_match "1 runs, 1 assertions, 0 failures", result + end + + private + def plugin_path + "#{@destination_root}/bukkits" + end + + def run_test_command(arguments) + Dir.chdir(plugin_path) { `bin/test #{arguments}` } + end +end diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb new file mode 100644 index 0000000000..b99b4baf6b --- /dev/null +++ b/railties/test/generators/resource_generator_test.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/resource/resource_generator" + +class ResourceGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(account) + + def setup + super + copy_routes + Rails::Generators::ModelHelpers.skip_warn = false + end + + def test_help_with_inherited_options + content = run_generator ["--help"] + assert_match(/ActiveRecord options:/, content) + assert_match(/TestUnit options:/, content) + end + + def test_files_from_inherited_invocation + run_generator + + %w( + app/models/account.rb + test/models/account_test.rb + test/fixtures/accounts.yml + ).each { |path| assert_file path } + + assert_migration "db/migrate/create_accounts.rb" + end + + def test_inherited_invocations_with_attributes + run_generator ["account", "name:string"] + assert_migration "db/migrate/create_accounts.rb", /t.string :name/ + end + + def test_resource_controller_with_pluralized_class_name + run_generator + assert_file "app/controllers/accounts_controller.rb", /class AccountsController < ApplicationController/ + assert_file "test/controllers/accounts_controller_test.rb", /class AccountsControllerTest < ActionDispatch::IntegrationTest/ + + assert_file "app/helpers/accounts_helper.rb", /module AccountsHelper/ + end + + def test_resource_controller_with_actions + run_generator ["account", "--actions", "index", "new"] + + assert_file "app/controllers/accounts_controller.rb" do |controller| + assert_instance_method :index, controller + assert_instance_method :new, controller + end + + assert_file "app/views/accounts/index.html.erb" + assert_file "app/views/accounts/new.html.erb" + end + + def test_resource_routes_are_added + run_generator + + assert_file "config/routes.rb" do |route| + assert_match(/resources :accounts$/, route) + end + end + + def test_plural_names_are_singularized + content = run_generator ["accounts"] + assert_file "app/models/account.rb", /class Account < ApplicationRecord/ + assert_file "test/models/account_test.rb", /class AccountTest/ + assert_match(/\[WARNING\] The model name 'accounts' was recognized as a plural, using the singular 'account' instead\. Override with --force-plural or setup custom inflection rules for this noun before running the generator\./, content) + end + + def test_plural_names_can_be_forced + content = run_generator ["accounts", "--force-plural"] + assert_file "app/models/accounts.rb", /class Accounts < ApplicationRecord/ + assert_file "test/models/accounts_test.rb", /class AccountsTest/ + assert_no_match(/\[WARNING\]/, content) + end + + def test_mass_nouns_do_not_throw_warnings + content = run_generator ["sheep"] + assert_no_match(/\[WARNING\]/, content) + end + + def test_route_is_removed_on_revoke + run_generator + run_generator ["account"], behavior: :revoke + + assert_file "config/routes.rb" do |route| + assert_no_match(/resources :accounts$/, route) + end + end +end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb new file mode 100644 index 0000000000..fd5aa817b4 --- /dev/null +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -0,0 +1,279 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/scaffold_controller/scaffold_controller_generator" + +module Unknown + module Generators + end +end + +class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(User name:string age:integer) + + def test_controller_skeleton_is_created + run_generator + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/class UsersController < ApplicationController/, content) + + assert_instance_method :index, content do |m| + assert_match(/@users = User\.all/, m) + end + + assert_instance_method :show, content + + assert_instance_method :new, content do |m| + assert_match(/@user = User\.new/, m) + end + + assert_instance_method :edit, content + + assert_instance_method :create, content do |m| + assert_match(/@user = User\.new\(user_params\)/, m) + assert_match(/@user\.save/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@user\.update\(user_params\)/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@user\.destroy/, m) + assert_match(/User was successfully destroyed/, m) + end + + assert_instance_method :set_user, content do |m| + assert_match(/@user = User\.find\(params\[:id\]\)/, m) + end + + assert_match(/def user_params/, content) + assert_match(/params\.require\(:user\)\.permit\(:name, :age\)/, content) + end + end + + def test_dont_use_require_or_permit_if_there_are_no_attributes + run_generator ["User"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/def user_params/, content) + assert_match(/params\.fetch\(:user, \{\}\)/, content) + end + end + + def test_controller_permit_references_attributes + run_generator ["LineItem", "product:references", "cart:belongs_to"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :cart_id\)/, content) + end + end + + def test_controller_permit_polymorphic_references_attributes + run_generator ["LineItem", "product:references{polymorphic}"] + + assert_file "app/controllers/line_items_controller.rb" do |content| + assert_match(/def line_item_params/, content) + assert_match(/params\.require\(:line_item\)\.permit\(:product_id, :product_type\)/, content) + end + end + + def test_helper_are_invoked_with_a_pluralized_name + run_generator + assert_file "app/helpers/users_helper.rb", /module UsersHelper/ + end + + def test_views_are_generated + run_generator + + %w(index edit new show).each do |view| + assert_file "app/views/users/#{view}.html.erb" + end + assert_no_file "app/views/layouts/users.html.erb" + end + + def test_index_page_have_notice + run_generator + + %w(index show).each do |view| + assert_file "app/views/users/#{view}.html.erb", /notice/ + end + end + + def test_functional_tests + run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}"] + + assert_file "test/controllers/users_controller_test.rb" do |content| + assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content) + assert_match(/test "should get index"/, content) + assert_match(/post users_url, params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}/, content) + assert_match(/patch user_url\(@user\), params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}/, content) + end + end + + def test_functional_tests_without_attributes + run_generator ["User"] + + assert_file "test/controllers/users_controller_test.rb" do |content| + assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content) + assert_match(/test "should get index"/, content) + assert_match(/post users_url, params: \{ user: \{ \} \}/, content) + assert_match(/patch user_url\(@user\), params: \{ user: \{ \} \}/, content) + end + end + + def test_skip_helper_if_required + run_generator ["User", "name:string", "age:integer", "--no-helper"] + assert_no_file "app/helpers/users_helper.rb" + end + + def test_skip_layout_if_required + run_generator ["User", "name:string", "age:integer", "--no-layout"] + assert_no_file "app/views/layouts/users.html.erb" + end + + def test_default_orm_is_used + run_generator ["User", "--orm=unknown"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/class UsersController < ApplicationController/, content) + + assert_instance_method :index, content do |m| + assert_match(/@users = User\.all/, m) + end + end + end + + def test_customized_orm_is_used + klass = Class.new(Rails::Generators::ActiveModel) do + def self.all(klass) + "#{klass}.find(:all)" + end + end + + Unknown::Generators.const_set(:ActiveModel, klass) + run_generator ["User", "--orm=unknown"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/class UsersController < ApplicationController/, content) + + assert_instance_method :index, content do |m| + assert_match(/@users = User\.find\(:all\)/, m) + assert_no_match(/@users = User\.all/, m) + end + end + ensure + Unknown::Generators.send :remove_const, :ActiveModel + end + + def test_model_name_option + run_generator ["Admin::User", "--model-name=User"] + assert_file "app/controllers/admin/users_controller.rb" do |content| + assert_instance_method :index, content do |m| + assert_match("@users = User.all", m) + end + + assert_instance_method :create, content do |m| + assert_match("redirect_to [:admin, @user]", m) + end + + assert_instance_method :update, content do |m| + assert_match("redirect_to [:admin, @user]", m) + end + end + + assert_file "app/views/admin/users/index.html.erb" do |content| + assert_match("'Show', [:admin, user]", content) + assert_match("'Edit', edit_admin_user_path(user)", content) + assert_match("'Destroy', [:admin, user]", content) + assert_match("'New User', new_admin_user_path", content) + end + + assert_file "app/views/admin/users/new.html.erb" do |content| + assert_match("'Back', admin_users_path", content) + end + + assert_file "app/views/admin/users/_form.html.erb" do |content| + assert_match("model: [:admin, user]", content) + end + end + + def test_controller_tests_pass_by_default_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails g controller dashboard foo` } + quietly { `bin/rails db:migrate RAILS_ENV=test` } + assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_controller_tests_pass_by_default_inside_full_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails g controller dashboard foo` } + quietly { `bin/rails db:migrate RAILS_ENV=test` } + assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_api_only_generates_a_proper_api_controller + run_generator ["User", "--api"] + + assert_file "app/controllers/users_controller.rb" do |content| + assert_match(/class UsersController < ApplicationController/, content) + assert_no_match(/respond_to/, content) + + assert_match(/before_action :set_user, only: \[:show, :update, :destroy\]/, content) + + assert_instance_method :index, content do |m| + assert_match(/@users = User\.all/, m) + assert_match(/render json: @users/, m) + end + + assert_instance_method :show, content do |m| + assert_match(/render json: @user/, m) + end + + assert_instance_method :create, content do |m| + assert_match(/@user = User\.new\(user_params\)/, m) + assert_match(/@user\.save/, m) + assert_match(/@user\.errors/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@user\.update\(user_params\)/, m) + assert_match(/@user\.errors/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@user\.destroy/, m) + end + end + + assert_no_file "app/views/users/index.html.erb" + assert_no_file "app/views/users/edit.html.erb" + assert_no_file "app/views/users/show.html.erb" + assert_no_file "app/views/users/new.html.erb" + assert_no_file "app/views/users/_form.html.erb" + end + + def test_api_controller_tests + run_generator ["User", "name:string", "age:integer", "organization:references{polymorphic}", "--api"] + + assert_file "test/controllers/users_controller_test.rb" do |content| + assert_match(/class UsersControllerTest < ActionDispatch::IntegrationTest/, content) + assert_match(/test "should get index"/, content) + assert_match(/post users_url, params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}, as: :json/, content) + assert_match(/patch user_url\(@user\), params: \{ user: \{ age: @user\.age, name: @user\.name, organization_id: @user\.organization_id, organization_type: @user\.organization_type \} \}, as: :json/, content) + assert_no_match(/assert_redirected_to/, content) + end + end +end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb new file mode 100644 index 0000000000..f672e301a7 --- /dev/null +++ b/railties/test/generators/scaffold_generator_test.rb @@ -0,0 +1,660 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/scaffold/scaffold_generator" + +class ScaffoldGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(product_line title:string approved:boolean product:belongs_to user:references) + + setup :copy_routes + + def test_scaffold_on_invoke + run_generator + + # Model + assert_file "app/models/product_line.rb", /class ProductLine < ApplicationRecord/ + assert_file "test/models/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/product_lines.yml" + assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/ + assert_migration "db/migrate/create_product_lines.rb", /boolean :approved/ + assert_migration "db/migrate/create_product_lines.rb", /references :user/ + + # Route + assert_file "config/routes.rb" do |route| + assert_match(/resources :product_lines$/, route) + end + + # Controller + assert_file "app/controllers/product_lines_controller.rb" do |content| + assert_match(/class ProductLinesController < ApplicationController/, content) + + assert_instance_method :index, content do |m| + assert_match(/@product_lines = ProductLine\.all/, m) + end + + assert_instance_method :show, content + + assert_instance_method :new, content do |m| + assert_match(/@product_line = ProductLine\.new/, m) + end + + assert_instance_method :edit, content + + assert_instance_method :create, content do |m| + assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) + assert_match(/@product_line\.save/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@product_line\.update\(product_line_params\)/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@product_line\.destroy/, m) + end + + assert_instance_method :set_product_line, content do |m| + assert_match(/@product_line = ProductLine\.find\(params\[:id\]\)/, m) + end + end + + assert_file "test/controllers/product_lines_controller_test.rb" do |test| + assert_match(/class ProductLinesControllerTest < ActionDispatch::IntegrationTest/, test) + assert_match(/post product_lines_url, params: \{ product_line: \{ approved: @product_line\.approved, product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test) + assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ approved: @product_line\.approved, product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test) + end + + # System tests + assert_file "test/system/product_lines_test.rb" do |test| + assert_match(/class ProductLinesTest < ApplicationSystemTestCase/, test) + assert_match(/visit product_lines_url/, test) + assert_match(/fill_in "Title", with: @product_line\.title/, test) + assert_match(/check "Approved" if @product_line\.approved/, test) + assert_match(/assert_text "Product line was successfully updated"/, test) + end + + # Views + assert_no_file "app/views/layouts/product_lines.html.erb" + + %w(index show).each do |view| + assert_file "app/views/product_lines/#{view}.html.erb" + end + + %w(edit new).each do |view| + assert_file "app/views/product_lines/#{view}.html.erb", /render 'form', product_line: @product_line/ + end + + assert_file "app/views/product_lines/_form.html.erb" do |test| + assert_match "product_line", test + assert_no_match "@product_line", test + end + + # Helpers + assert_file "app/helpers/product_lines_helper.rb" + + # Assets + assert_file "app/assets/stylesheets/scaffold.css" + assert_file "app/assets/stylesheets/product_lines.css" + end + + def test_api_scaffold_on_invoke + run_generator %w(product_line title:string product:belongs_to user:references --api --no-template-engine --no-helper --no-assets) + + # Model + assert_file "app/models/product_line.rb", /class ProductLine < ApplicationRecord/ + assert_file "test/models/product_line_test.rb", /class ProductLineTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/product_lines.yml" + assert_migration "db/migrate/create_product_lines.rb", /belongs_to :product/ + assert_migration "db/migrate/create_product_lines.rb", /references :user/ + + # Route + assert_file "config/routes.rb" do |route| + assert_match(/resources :product_lines$/, route) + end + + # Controller + assert_file "app/controllers/product_lines_controller.rb" do |content| + assert_match(/class ProductLinesController < ApplicationController/, content) + assert_no_match(/respond_to/, content) + + assert_match(/before_action :set_product_line, only: \[:show, :update, :destroy\]/, content) + + assert_instance_method :index, content do |m| + assert_match(/@product_lines = ProductLine\.all/, m) + assert_match(/render json: @product_lines/, m) + end + + assert_instance_method :show, content do |m| + assert_match(/render json: @product_line/, m) + end + + assert_instance_method :create, content do |m| + assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) + assert_match(/@product_line\.save/, m) + assert_match(/@product_line\.errors/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@product_line\.update\(product_line_params\)/, m) + assert_match(/@product_line\.errors/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@product_line\.destroy/, m) + end + end + + assert_file "test/controllers/product_lines_controller_test.rb" do |test| + assert_match(/class ProductLinesControllerTest < ActionDispatch::IntegrationTest/, test) + assert_match(/post product_lines_url, params: \{ product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test) + assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ product_id: @product_line\.product_id, title: @product_line\.title, user_id: @product_line\.user_id \} \}/, test) + assert_no_match(/assert_redirected_to/, test) + end + + # System tests + assert_no_file "test/system/product_lines_test.rb" + + # Views + assert_no_file "app/views/layouts/product_lines.html.erb" + + %w(index show new edit _form).each do |view| + assert_no_file "app/views/product_lines/#{view}.html.erb" + end + + # Helpers + assert_no_file "app/helpers/product_lines_helper.rb" + + # Assets + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_no_file "app/assets/stylesheets/product_lines.css" + end + + def test_functional_tests_without_attributes + run_generator ["product_line"] + + assert_file "test/controllers/product_lines_controller_test.rb" do |content| + assert_match(/class ProductLinesControllerTest < ActionDispatch::IntegrationTest/, content) + assert_match(/test "should get index"/, content) + assert_match(/post product_lines_url, params: \{ product_line: \{ \} \}/, content) + assert_match(/patch product_line_url\(@product_line\), params: \{ product_line: \{ \} \}/, content) + end + end + + def test_system_tests_without_attributes + run_generator ["product_line"] + + assert_file "test/system/product_lines_test.rb" do |content| + assert_match(/class ProductLinesTest < ApplicationSystemTestCase/, content) + assert_match(/test "visiting the index"/, content) + assert_no_match(/fill_in/, content) + end + end + + def test_scaffold_on_revoke + run_generator + run_generator ["product_line"], behavior: :revoke + + # Model + assert_no_file "app/models/product_line.rb" + assert_no_file "test/models/product_line_test.rb" + assert_no_file "test/fixtures/product_lines.yml" + assert_no_migration "db/migrate/create_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/product_lines_controller.rb" + assert_no_file "test/controllers/product_lines_controller_test.rb" + + # System tests + assert_no_file "test/system/product_lines_test.rb" + + # Views + assert_no_file "app/views/product_lines" + assert_no_file "app/views/layouts/product_lines.html.erb" + + # Helpers + assert_no_file "app/helpers/product_lines_helper.rb" + + # Assets + assert_file "app/assets/stylesheets/scaffold.css", /:visited/ + assert_no_file "app/assets/stylesheets/product_lines.css" + end + + def test_scaffold_with_namespace_on_invoke + run_generator [ "admin/role", "name:string", "description:string" ] + + # Model + assert_file "app/models/admin.rb", /module Admin/ + assert_file "app/models/admin/role.rb", /class Admin::Role < ApplicationRecord/ + assert_file "test/models/admin/role_test.rb", /class Admin::RoleTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/admin/roles.yml" + assert_migration "db/migrate/create_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/admin/roles_controller.rb" do |content| + assert_match(/class Admin::RolesController < ApplicationController/, content) + + assert_instance_method :index, content do |m| + assert_match(/@admin_roles = Admin::Role\.all/, m) + end + + assert_instance_method :show, content + + assert_instance_method :new, content do |m| + assert_match(/@admin_role = Admin::Role\.new/, m) + end + + assert_instance_method :edit, content + + assert_instance_method :create, content do |m| + assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, m) + assert_match(/@admin_role\.save/, m) + end + + assert_instance_method :update, content do |m| + assert_match(/@admin_role\.update\(admin_role_params\)/, m) + end + + assert_instance_method :destroy, content do |m| + assert_match(/@admin_role\.destroy/, m) + end + + assert_instance_method :set_admin_role, content do |m| + assert_match(/@admin_role = Admin::Role\.find\(params\[:id\]\)/, m) + end + end + + assert_file "test/controllers/admin/roles_controller_test.rb", + /class Admin::RolesControllerTest < ActionDispatch::IntegrationTest/ + + assert_file "test/system/admin/roles_test.rb", + /class Admin::RolesTest < ApplicationSystemTestCase/ + + # Views + assert_file "app/views/admin/roles/index.html.erb" do |content| + assert_match("'Show', admin_role", content) + assert_match("'Edit', edit_admin_role_path(admin_role)", content) + assert_match("'Destroy', admin_role", content) + assert_match("'New Admin Role', new_admin_role_path", content) + end + + %w(edit new show _form).each do |view| + assert_file "app/views/admin/roles/#{view}.html.erb" + end + assert_no_file "app/views/layouts/admin/roles.html.erb" + + # Helpers + assert_file "app/helpers/admin/roles_helper.rb" + + # Assets + assert_file "app/assets/stylesheets/scaffold.css", /:visited/ + assert_file "app/assets/stylesheets/admin/roles.css" + end + + def test_scaffold_with_namespace_on_revoke + run_generator [ "admin/role", "name:string", "description:string" ] + run_generator [ "admin/role" ], behavior: :revoke + + # Model + assert_file "app/models/admin.rb" # ( should not be remove ) + assert_no_file "app/models/admin/role.rb" + assert_no_file "test/models/admin/role_test.rb" + assert_no_file "test/fixtures/admin/roles.yml" + assert_no_migration "db/migrate/create_admin_roles.rb" + + # Route + assert_file "config/routes.rb" do |route| + assert_no_match(/namespace :admin do resources :roles end$/, route) + end + + # Controller + assert_no_file "app/controllers/admin/roles_controller.rb" + assert_no_file "test/controllers/admin/roles_controller_test.rb" + + # System tests + assert_no_file "test/system/admin/roles_test.rb" + + # Views + assert_no_file "app/views/admin/roles" + assert_no_file "app/views/layouts/admin/roles.html.erb" + + # Helpers + assert_no_file "app/helpers/admin/roles_helper.rb" + + # Assets + assert_file "app/assets/stylesheets/scaffold.css" + assert_no_file "app/assets/stylesheets/admin/roles.css" + end + + def test_scaffold_generator_on_revoke_does_not_mutilate_legacy_map_parameter + run_generator + + # Add a |map| parameter to the routes block manually + route_path = File.expand_path("config/routes.rb", destination_root) + content = File.read(route_path).gsub(/\.routes\.draw do/) do |match| + "#{match} |map|" + end + File.write(route_path, content) + + run_generator ["product_line"], behavior: :revoke + + assert_file "config/routes.rb", /\.routes\.draw do\s*\|map\|\s*$/ + end + + def test_scaffold_generator_on_revoke_does_not_mutilate_routes + run_generator + + 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) + assert_file "config/routes.rb", /\.routes\.draw do\n resources :product_lines\nend\n\z/ + + run_generator ["product_line"], behavior: :revoke + + assert_file "config/routes.rb", /\.routes\.draw do\nend\n\z/ + end + + def test_scaffold_generator_ignores_commented_routes + run_generator ["product"] + assert_file "config/routes.rb", /\.routes\.draw do\n resources :products\n/ + end + + def test_scaffold_generator_no_assets_with_switch_no_assets + run_generator [ "posts", "--no-assets" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_no_file "app/assets/stylesheets/posts.css" + end + + def test_scaffold_generator_no_assets_with_switch_assets_false + run_generator [ "posts", "--assets=false" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_no_file "app/assets/stylesheets/posts.css" + end + + def test_scaffold_generator_no_scaffold_stylesheet_with_switch_no_scaffold_stylesheet + run_generator [ "posts", "--no-scaffold-stylesheet" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_file "app/assets/stylesheets/posts.css" + end + + def test_scaffold_generator_no_scaffold_stylesheet_with_switch_scaffold_stylesheet_false + run_generator [ "posts", "--scaffold-stylesheet=false" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_file "app/assets/stylesheets/posts.css" + end + + def test_scaffold_generator_with_switch_resource_route_false + run_generator [ "posts", "--resource-route=false" ] + assert_file "config/routes.rb" do |route| + assert_no_match(/resources :posts$/, route) + end + end + + def test_scaffold_generator_no_helper_with_switch_no_helper + output = run_generator [ "posts", "--no-helper" ] + + assert_no_match(/error/, output) + assert_no_file "app/helpers/posts_helper.rb" + end + + def test_scaffold_generator_no_helper_with_switch_helper_false + output = run_generator [ "posts", "--helper=false" ] + + assert_no_match(/error/, output) + assert_no_file "app/helpers/posts_helper.rb" + end + + def test_scaffold_generator_no_stylesheets + run_generator [ "posts", "--no-stylesheets" ] + assert_no_file "app/assets/stylesheets/scaffold.css" + assert_no_file "app/assets/stylesheets/posts.css" + end + + def test_scaffold_generator_outputs_error_message_on_missing_attribute_type + run_generator ["post", "title", "body:text", "author"] + + assert_migration "db/migrate/create_posts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :title/, up) + assert_match(/t\.text :body/, up) + assert_match(/t\.string :author/, up) + end + end + end + + def test_scaffold_generator_belongs_to_and_references + run_generator ["account", "name", "currency:belongs_to", "user:references"] + + assert_file "app/models/account.rb", /belongs_to :currency/ + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :name/, up) + assert_match(/t\.belongs_to :currency/, up) + end + end + + assert_file "app/controllers/accounts_controller.rb" do |content| + assert_instance_method :account_params, content do |m| + assert_match(/permit\(:name, :currency_id, :user_id\)/, m) + end + end + + assert_file "app/views/accounts/_form.html.erb" do |content| + assert_match(/^\W{4}<%= form\.text_field :name %>/, content) + assert_match(/^\W{4}<%= form\.text_field :currency_id %>/, content) + end + + assert_file "app/views/accounts/index.html.erb" do |content| + assert_match(/^\W{8}<td><%= account\.name %><\/td>/, content) + assert_match(/^\W{8}<td><%= account\.user_id %><\/td>/, content) + end + + assert_file "app/views/accounts/show.html.erb" do |content| + assert_match(/^\W{2}<%= @account\.name %>/, content) + assert_match(/^\W{2}<%= @account\.user_id %>/, content) + end + end + + def test_scaffold_generator_database + with_secondary_database_configuration do + run_generator ["posts", "--database=secondary"] + + assert_migration "db/secondary_migrate/create_posts.rb" + end + end + + def test_scaffold_generator_password_digest + run_generator ["user", "name", "password:digest"] + + assert_file "app/models/user.rb", /has_secure_password/ + + assert_migration "db/migrate/create_users.rb" do |m| + assert_method :change, m do |up| + assert_match(/t\.string :name/, up) + assert_match(/t\.string :password_digest/, up) + end + end + + assert_file "app/controllers/users_controller.rb" do |content| + assert_instance_method :user_params, content do |m| + assert_match(/permit\(:name, :password, :password_confirmation\)/, m) + end + end + + assert_file "app/views/users/_form.html.erb" do |content| + assert_match(/<%= form\.password_field :password %>/, content) + assert_match(/<%= form\.password_field :password_confirmation %>/, content) + end + + assert_file "app/views/users/index.html.erb" do |content| + assert_no_match(/password/, content) + end + + assert_file "app/views/users/show.html.erb" do |content| + assert_no_match(/password/, content) + end + + assert_file "test/controllers/users_controller_test.rb" do |content| + assert_match(/password: 'secret'/, content) + assert_match(/password_confirmation: 'secret'/, content) + end + + assert_file "test/system/users_test.rb" do |content| + assert_match(/fill_in "Password", with: 'secret'/, content) + assert_match(/fill_in "Password confirmation", with: 'secret'/, content) + end + + assert_file "test/fixtures/users.yml" do |content| + assert_match(/password_digest: <%= BCrypt::Password.create\('secret'\) %>/, content) + end + end + + def test_scaffold_tests_pass_by_default_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits-admin --mountable` } + + engine_path = File.join(destination_root, "bukkits-admin") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + + assert_file "bukkits-admin/app/controllers/bukkits/admin/users_controller.rb" do |content| + assert_match(/module Bukkits::Admin/, content) + assert_match(/class UsersController < ApplicationController/, content) + end + + assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_scaffold_tests_pass_by_default_inside_full_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + assert_match(/8 runs, 10 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_scaffold_tests_pass_by_default_inside_api_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable --api` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + assert_match(/6 runs, 8 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_scaffold_tests_pass_by_default_inside_api_full_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full --api` } + + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly do + `bin/rails g scaffold User name:string age:integer; + bin/rails db:migrate` + end + assert_match(/6 runs, 8 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`) + end + end + + def test_scaffold_on_invoke_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails generate scaffold User name:string age:integer` } + + assert File.exist?("app/models/bukkits/user.rb") + assert File.exist?("test/models/bukkits/user_test.rb") + assert File.exist?("test/fixtures/bukkits/users.yml") + + assert File.exist?("app/controllers/bukkits/users_controller.rb") + assert File.exist?("test/controllers/bukkits/users_controller_test.rb") + + assert File.exist?("test/system/bukkits/users_test.rb") + + assert File.exist?("app/views/bukkits/users/index.html.erb") + assert File.exist?("app/views/bukkits/users/edit.html.erb") + assert File.exist?("app/views/bukkits/users/show.html.erb") + assert File.exist?("app/views/bukkits/users/new.html.erb") + assert File.exist?("app/views/bukkits/users/_form.html.erb") + + assert File.exist?("app/helpers/bukkits/users_helper.rb") + + assert File.exist?("app/assets/stylesheets/bukkits/users.css") + end + end + + def test_scaffold_on_revoke_inside_mountable_engine + Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` } + engine_path = File.join(destination_root, "bukkits") + + Dir.chdir(engine_path) do + quietly { `bin/rails generate scaffold User name:string age:integer` } + quietly { `bin/rails destroy scaffold User` } + + assert_not File.exist?("app/models/bukkits/user.rb") + assert_not File.exist?("test/models/bukkits/user_test.rb") + assert_not File.exist?("test/fixtures/bukkits/users.yml") + + assert_not File.exist?("app/controllers/bukkits/users_controller.rb") + assert_not File.exist?("test/controllers/bukkits/users_controller_test.rb") + + assert_not File.exist?("test/system/bukkits/users_test.rb") + + assert_not File.exist?("app/views/bukkits/users/index.html.erb") + assert_not File.exist?("app/views/bukkits/users/edit.html.erb") + assert_not File.exist?("app/views/bukkits/users/show.html.erb") + assert_not File.exist?("app/views/bukkits/users/new.html.erb") + assert_not File.exist?("app/views/bukkits/users/_form.html.erb") + + assert_not File.exist?("app/helpers/bukkits/users_helper.rb") + + assert_not File.exist?("app/assets/stylesheets/bukkits/users.css") + end + end +end diff --git a/railties/test/generators/shared_generator_tests.rb b/railties/test/generators/shared_generator_tests.rb new file mode 100644 index 0000000000..7441ab0603 --- /dev/null +++ b/railties/test/generators/shared_generator_tests.rb @@ -0,0 +1,350 @@ +# frozen_string_literal: true + +# +# Tests, setup, and teardown common to the application and plugin generator suites. +# +module SharedGeneratorTests + def setup + Rails.application = TestApp::Application + super + Rails::Generators::AppGenerator.instance_variable_set("@desc", nil) + + Kernel.silence_warnings do + Thor::Base.shell.attr_accessor :always_force + @shell = Thor::Base.shell.new + @shell.always_force = true + end + end + + def teardown + super + Rails::Generators::AppGenerator.instance_variable_set("@desc", nil) + Rails.application = TestApp::Application.instance + end + + def application_path + destination_root + end + + def test_skeleton_is_created + run_generator + + default_files.each { |path| assert_file path } + end + + def test_plugin_new_generate_pretend + run_generator ["testapp", "--pretend"] + default_files.each { |path| assert_no_file File.join("testapp", path) } + end + + def test_invalid_database_option_raises_an_error + content = capture(:stderr) { run_generator([destination_root, "-d", "unknown"]) } + assert_match(/Invalid value for \-\-database option/, content) + end + + def test_test_files_are_skipped_if_required + run_generator [destination_root, "--skip-test"] + assert_no_file "test" + end + + def test_name_collision_raises_an_error + reserved_words = %w[application destroy plugin runner test] + reserved_words.each do |reserved| + content = capture(:stderr) { run_generator [File.join(destination_root, reserved)] } + assert_match(/Invalid \w+ name #{reserved}\. Please give a name which does not match one of the reserved rails words: application, destroy, plugin, runner, test\n/, content) + end + end + + def test_name_raises_an_error_if_name_already_used_constant + %w{ String Hash Class Module Set Symbol }.each do |ruby_class| + content = capture(:stderr) { run_generator [File.join(destination_root, ruby_class)] } + assert_match(/Invalid \w+ name #{ruby_class}, constant #{ruby_class} is already in use\. Please choose another \w+ name\.\n/, content) + end + end + + def test_shebang_is_added_to_rails_file + run_generator [destination_root, "--ruby", "foo/bar/baz", "--full"] + assert_file "bin/rails", /#!foo\/bar\/baz/ + end + + def test_shebang_when_is_the_same_as_default_use_env + run_generator [destination_root, "--ruby", Thor::Util.ruby_command, "--full"] + assert_file "bin/rails", /#!\/usr\/bin\/env/ + end + + def test_template_raises_an_error_with_invalid_path + quietly do + content = capture(:stderr) { run_generator([destination_root, "-m", "non/existent/path"]) } + + assert_match(/The template \[.*\] could not be loaded/, content) + assert_match(/non\/existent\/path/, content) + end + end + + def test_template_is_executed_when_supplied_an_https_path + path = "https://gist.github.com/josevalim/103208/raw/" + template = +%{ say "It works!" } + template.instance_eval "def read; self; end" # Make the string respond to read + + check_open = -> *args do + assert_equal [ path, "Accept" => "application/x-thor-template" ], args + template + end + + generator([destination_root], template: path, skip_webpack_install: true).stub(:open, check_open, template) do + generator.stub :bundle_command, nil do + quietly { assert_match(/It works!/, capture(:stdout) { generator.invoke_all }) } + end + end + end + + def test_skip_gemfile + assert_not_called(generator([destination_root], skip_gemfile: true, skip_webpack_install: true), :bundle_command) do + quietly { generator.invoke_all } + assert_no_file "Gemfile" + end + end + + def test_skip_git + run_generator [destination_root, "--skip-git", "--full"] + assert_no_file(".gitignore") + assert_no_directory(".git") + end + + def test_skip_keeps + run_generator [destination_root, "--skip-keeps", "--full"] + + assert_file ".gitignore" do |content| + assert_no_match(/\.keep/, content) + end + + assert_no_file("app/models/concerns/.keep") + end + + def test_default_frameworks_are_required_when_others_are_removed + run_generator [ + destination_root, + "--skip-active-record", + "--skip-active-storage", + "--skip-action-mailer", + "--skip-action-cable", + "--skip-sprockets" + ] + + assert_file "#{application_path}/config/application.rb", /^require\s+["']rails["']/ + assert_file "#{application_path}/config/application.rb", /^require\s+["']active_model\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^require\s+["']active_job\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^# require\s+["']active_record\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^# require\s+["']active_storage\/engine["']/ + assert_file "#{application_path}/config/application.rb", /^require\s+["']action_controller\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^# require\s+["']action_mailer\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^require\s+["']action_view\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^# require\s+["']action_cable\/engine["']/ + assert_file "#{application_path}/config/application.rb", /^# require\s+["']sprockets\/railtie["']/ + assert_file "#{application_path}/config/application.rb", /^require\s+["']rails\/test_unit\/railtie["']/ + end + + def test_generator_without_skips + run_generator + assert_file "#{application_path}/config/application.rb", /\s+require\s+["']rails\/all["']/ + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_match(/config\.action_mailer\.raise_delivery_errors = false/, content) + end + assert_file "#{application_path}/config/environments/test.rb" do |content| + assert_match(/config\.action_mailer\.delivery_method = :test/, content) + end + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content) + assert_match(/^ # config\.require_master_key = true/, content) + end + end + + def test_gitignore_when_sqlite3 + run_generator + + assert_file ".gitignore" do |content| + assert_match(/sqlite3/, content) + end + end + + def test_gitignore_when_non_sqlite3_db + run_generator([destination_root, "-d", "mysql"]) + + assert_file ".gitignore" do |content| + assert_no_match(/sqlite/i, content) + end + end + + def test_generator_if_skip_active_record_is_given + run_generator [destination_root, "--skip-active-record"] + assert_no_directory "#{application_path}/db/" + assert_no_file "#{application_path}/config/database.yml" + assert_no_file "#{application_path}/app/models/application_record.rb" + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ + assert_file "test/test_helper.rb" do |helper_content| + assert_no_match(/fixtures :all/, helper_content) + end + assert_file "#{application_path}/bin/setup" do |setup_content| + assert_no_match(/db:setup/, setup_content) + end + assert_file "#{application_path}/bin/update" do |update_content| + assert_no_match(/db:migrate/, update_content) + end + assert_file ".gitignore" do |content| + assert_no_match(/sqlite/i, content) + end + end + + def test_generator_for_active_storage + run_generator + + unless generator_class.name == "Rails::Generators::PluginGenerator" + assert_file "#{application_path}/app/javascript/packs/application.js" do |content| + assert_match(/^import \* as ActiveStorage from "activestorage"\nActiveStorage.start\(\)/, content) + end + end + + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/test.rb" do |content| + assert_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/storage.yml" + assert_directory "#{application_path}/storage" + assert_directory "#{application_path}/tmp/storage" + + assert_file ".gitignore" do |content| + assert_match(/\/storage\//, content) + end + end + + def test_generator_if_skip_active_storage_is_given + run_generator [destination_root, "--skip-active-storage"] + + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/ + + assert_file "#{application_path}/app/javascript/packs/application.js" do |content| + assert_no_match(/activestorage/, content) + end + + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/test.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_no_file "#{application_path}/config/storage.yml" + assert_no_directory "#{application_path}/storage" + assert_no_directory "#{application_path}/tmp/storage" + + assert_file ".gitignore" do |content| + assert_no_match(/\/storage\//, content) + end + end + + def test_generator_does_not_generate_active_storage_contents_if_skip_active_record_is_given + run_generator [destination_root, "--skip-active-record"] + + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/ + + assert_file "#{application_path}/app/javascript/packs/application.js" do |content| + assert_no_match(/^import * as ActiveStorage from "activestorage"\nActiveStorage.start\(\)/, content) + end + + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_file "#{application_path}/config/environments/test.rb" do |content| + assert_no_match(/config\.active_storage/, content) + end + + assert_no_file "#{application_path}/config/storage.yml" + assert_no_directory "#{application_path}/storage" + assert_no_directory "#{application_path}/tmp/storage" + + assert_file ".gitignore" do |content| + assert_no_match(/\/storage\//, content) + end + end + + def test_generator_if_skip_action_mailer_is_given + run_generator [destination_root, "--skip-action-mailer"] + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailer\/railtie["']/ + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_file "#{application_path}/config/environments/test.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_no_match(/config\.action_mailer/, content) + end + assert_no_directory "#{application_path}/app/mailers" + assert_no_directory "#{application_path}/test/mailers" + end + + def test_generator_if_skip_action_cable_is_given + run_generator [destination_root, "--skip-action-cable"] + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_cable\/engine["']/ + assert_no_file "#{application_path}/config/cable.yml" + assert_no_file "#{application_path}/app/javascript/consumer.js" + assert_no_directory "#{application_path}/app/javascript/channels" + assert_no_directory "#{application_path}/app/channels" + assert_file "Gemfile" do |content| + assert_no_match(/redis/, content) + end + end + + def test_generator_if_skip_sprockets_is_given + run_generator [destination_root, "--skip-sprockets"] + + assert_no_file "#{application_path}/config/initializers/assets.rb" + + assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']sprockets\/railtie["']/ + + assert_file "Gemfile" do |content| + assert_no_match(/sass-rails/, content) + end + + assert_file "#{application_path}/config/environments/development.rb" do |content| + assert_no_match(/config\.assets\.debug/, content) + end + + assert_file "#{application_path}/config/environments/production.rb" do |content| + assert_no_match(/config\.assets\.digest/, content) + assert_no_match(/config\.assets\.css_compressor/, content) + assert_no_match(/config\.assets\.compile/, content) + end + end + + def test_generator_for_yarn + skip "#34009 disabled JS by default for plugins" if generator_class.name == "Rails::Generators::PluginGenerator" + run_generator + assert_file "#{application_path}/package.json", /dependencies/ + assert_file "#{application_path}/bin/yarn" + end + + def test_generator_for_yarn_skipped + run_generator([destination_root, "--skip-javascript"]) + assert_no_file "#{application_path}/package.json" + assert_no_file "#{application_path}/bin/yarn" + end +end diff --git a/railties/test/generators/system_test_generator_test.rb b/railties/test/generators/system_test_generator_test.rb new file mode 100644 index 0000000000..5742ba444d --- /dev/null +++ b/railties/test/generators/system_test_generator_test.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/system_test/system_test_generator" + +class SystemTestGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(user) + + def test_system_test_skeleton_is_created + run_generator + assert_file "test/system/users_test.rb", /class UsersTest < ApplicationSystemTestCase/ + end + + def test_namespaced_system_test_skeleton_is_created + run_generator %w(admin/user) + assert_file "test/system/admin/users_test.rb", /class Admin::UsersTest < ApplicationSystemTestCase/ + end + + def test_test_name_is_pluralized + run_generator %w(user) + + assert_no_file "test/system/user_test.rb" + assert_file "test/system/users_test.rb" + end + + def test_test_suffix_is_not_duplicated + run_generator %w(users_test) + + assert_no_file "test/system/users_test_test.rb" + assert_file "test/system/users_test.rb" + end +end diff --git a/railties/test/generators/task_generator_test.rb b/railties/test/generators/task_generator_test.rb new file mode 100644 index 0000000000..5f162919d8 --- /dev/null +++ b/railties/test/generators/task_generator_test.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "generators/generators_test_helper" +require "rails/generators/rails/task/task_generator" + +class TaskGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + arguments %w(feeds foo bar) + + def test_task_is_created + run_generator + assert_file "lib/tasks/feeds.rake" do |content| + assert_match(/namespace :feeds/, content) + assert_match(/task foo:/, content) + assert_match(/task bar:/, content) + end + end + + def test_task_on_revoke + task_path = "lib/tasks/feeds.rake" + run_generator + assert_file task_path + run_generator ["feeds"], behavior: :revoke + assert_no_file task_path + end +end diff --git a/railties/test/generators/test_runner_in_engine_test.rb b/railties/test/generators/test_runner_in_engine_test.rb new file mode 100644 index 0000000000..bd102a32b5 --- /dev/null +++ b/railties/test/generators/test_runner_in_engine_test.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +require "generators/plugin_test_helper" + +class TestRunnerInEngineTest < ActiveSupport::TestCase + include PluginTestHelper + + def setup + @destination_root = Dir.mktmpdir("bukkits") + Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --full --skip-bundle` } + plugin_file "test/dummy/db/schema.rb", "" + end + + def teardown + FileUtils.rm_rf(@destination_root) + end + + def test_rerun_snippet_is_relative_path + create_test_file "post", pass: false + + output = run_test_command("test/post_test.rb") + expect = %r{Running:\n\nPostTest\nF\n\nFailure:\nPostTest#test_truth \[[^\]]+test/post_test\.rb:6\]:\nwups!\n\nrails test test/post_test\.rb:4} + assert_match expect, output + end + + private + def plugin_path + "#{@destination_root}/bukkits" + end + + def run_test_command(arguments) + Dir.chdir(plugin_path) { `bin/rails test #{arguments}` } + end +end |