2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class GeneratorsTest < ActiveSupport::TestCase
|
2009-10-08 21:12:28 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2009-11-30 18:58:38 -05:00
|
|
|
def app_const
|
|
|
|
@app_const ||= Class.new(Rails::Application)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_config
|
2009-12-31 15:28:48 -05:00
|
|
|
require "rails/all"
|
2009-12-22 20:03:23 -05:00
|
|
|
require "rails/generators"
|
2009-11-30 18:58:38 -05:00
|
|
|
yield app_const.config
|
|
|
|
end
|
|
|
|
|
2010-01-27 20:39:11 -05:00
|
|
|
def with_bare_config
|
|
|
|
require "rails"
|
|
|
|
require "rails/generators"
|
|
|
|
yield app_const.config
|
|
|
|
end
|
|
|
|
|
2010-11-15 14:09:05 -05:00
|
|
|
test "allow running plugin new generator inside Rails app directory" do
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "plugin", "new", "vendor/plugins/bukkits"
|
2010-11-17 17:31:29 -05:00
|
|
|
assert File.exist?(File.join(rails_root, "vendor/plugins/bukkits/test/dummy/config/application.rb"))
|
Do not allow to use plugin_new generator directly, you should use Usage:
rails new APP_PATH [options]
Options:
-G, [--skip-git] # Skip Git ignores and keeps
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /Users/drogus/.rvm/rubies/ruby-1.8.7-p302/bin/ruby
-b, [--builder=BUILDER] # Path to an application builder (can be a filesystem path or URL)
[--edge] # Setup the application with Gemfile pointing to Rails repository
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--skip-gemfile] # Don't create a Gemfile
-d, [--database=DATABASE] # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db)
# Default: sqlite3
-O, [--skip-active-record] # Skip Active Record files
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
-J, [--skip-prototype] # Skip Prototype files
-T, [--skip-test-unit] # Skip Test::Unit files
Runtime options:
-s, [--skip] # Skip files that already exist
-p, [--pretend] # Run but do not make any changes
-f, [--force] # Overwrite files that already exist
-q, [--quiet] # Supress status output
Rails options:
-v, [--version] # Show Rails version number and quit
-h, [--help] # Show this help message and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going. command
2010-11-15 14:09:39 -05:00
|
|
|
end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
test "generators default values" do
|
2010-01-27 20:39:11 -05:00
|
|
|
with_bare_config do |c|
|
2009-10-08 21:12:28 -04:00
|
|
|
assert_equal(true, c.generators.colorize_logging)
|
|
|
|
assert_equal({}, c.generators.aliases)
|
|
|
|
assert_equal({}, c.generators.options)
|
2010-02-01 04:48:59 -05:00
|
|
|
assert_equal({}, c.generators.fallbacks)
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators set rails options" do
|
2010-01-28 13:45:25 -05:00
|
|
|
with_bare_config do |c|
|
2011-10-18 09:29:16 -04:00
|
|
|
c.generators.orm = :data_mapper
|
2009-10-08 21:12:28 -04:00
|
|
|
c.generators.test_framework = :rspec
|
2009-11-03 21:14:44 -05:00
|
|
|
c.generators.helper = false
|
2012-10-14 06:03:39 -04:00
|
|
|
expected = { rails: { orm: :data_mapper, test_framework: :rspec, helper: false } }
|
2009-10-08 21:12:28 -04:00
|
|
|
assert_equal(expected, c.generators.options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators set rails aliases" do
|
2009-11-30 18:58:38 -05:00
|
|
|
with_config do |c|
|
2012-10-14 06:03:39 -04:00
|
|
|
c.generators.aliases = { rails: { test_framework: "-w" } }
|
|
|
|
expected = { rails: { test_framework: "-w" } }
|
2009-10-08 21:12:28 -04:00
|
|
|
assert_equal expected, c.generators.aliases
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-06 11:32:06 -05:00
|
|
|
test "generators aliases, options, templates and fallbacks on initialization" do
|
2009-12-22 20:03:23 -05:00
|
|
|
add_to_config <<-RUBY
|
2012-10-14 06:03:39 -04:00
|
|
|
config.generators.rails aliases: { test_framework: "-w" }
|
2011-10-18 09:29:16 -04:00
|
|
|
config.generators.orm :data_mapper
|
2009-12-22 20:03:23 -05:00
|
|
|
config.generators.test_framework :rspec
|
2010-02-01 04:48:59 -05:00
|
|
|
config.generators.fallbacks[:shoulda] = :test_unit
|
2010-02-06 11:32:06 -05:00
|
|
|
config.generators.templates << "some/where"
|
2009-12-22 20:03:23 -05:00
|
|
|
RUBY
|
|
|
|
|
2009-10-15 17:50:02 -04:00
|
|
|
# Initialize the application
|
2010-01-12 11:48:09 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2011-05-25 03:30:41 -04:00
|
|
|
Rails.application.load_generators
|
2009-10-08 21:12:28 -04:00
|
|
|
|
|
|
|
assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
|
|
|
|
assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
|
2012-10-14 06:03:39 -04:00
|
|
|
assert_equal Hash[shoulda: :test_unit], Rails::Generators.fallbacks
|
2010-10-06 11:18:59 -04:00
|
|
|
assert_equal ["some/where"], Rails::Generators.templates_path
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "generators no color on initialization" do
|
2009-12-22 20:03:23 -05:00
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.generators.colorize_logging = false
|
|
|
|
RUBY
|
|
|
|
|
2009-10-15 17:50:02 -04:00
|
|
|
# Initialize the application
|
2009-12-22 20:03:23 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2011-05-25 03:30:41 -04:00
|
|
|
Rails.application.load_generators
|
2009-10-08 21:12:28 -04:00
|
|
|
|
|
|
|
assert_equal Thor::Base.shell, Thor::Shell::Basic
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators with hashes for options and aliases" do
|
2010-01-27 20:39:11 -05:00
|
|
|
with_bare_config do |c|
|
2009-10-08 21:12:28 -04:00
|
|
|
c.generators do |g|
|
2012-10-14 06:03:39 -04:00
|
|
|
g.orm :data_mapper, migration: false
|
|
|
|
g.plugin aliases: { generator: "-g" },
|
|
|
|
generator: true
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expected = {
|
2012-10-14 06:03:39 -04:00
|
|
|
rails: { orm: :data_mapper },
|
|
|
|
plugin: { generator: true },
|
|
|
|
data_mapper: { migration: false }
|
2009-10-08 21:12:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, c.generators.options
|
2012-10-14 06:03:39 -04:00
|
|
|
assert_equal({ plugin: { generator: "-g" } }, c.generators.aliases)
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
|
|
|
end
|
2010-07-21 10:37:03 -04:00
|
|
|
|
|
|
|
test "generators with string and hash for options should generate symbol keys" do
|
|
|
|
with_bare_config do |c|
|
|
|
|
c.generators do |g|
|
2016-08-06 13:16:09 -04:00
|
|
|
g.orm "data_mapper", migration: false
|
2010-07-21 10:37:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
expected = {
|
2012-10-14 06:03:39 -04:00
|
|
|
rails: { orm: :data_mapper },
|
|
|
|
data_mapper: { migration: false }
|
2010-07-21 10:37:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, c.generators.options
|
|
|
|
end
|
|
|
|
end
|
2015-04-17 19:34:21 -04:00
|
|
|
|
2015-04-18 19:53:40 -04:00
|
|
|
test "api only generators hide assets, helper, js and css namespaces and set api option" do
|
2015-04-17 19:34:21 -04:00
|
|
|
add_to_config <<-RUBY
|
2015-04-20 12:28:23 -04:00
|
|
|
config.api_only = true
|
2015-04-17 19:34:21 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
# Initialize the application
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Rails.application.load_generators
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "assets"
|
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "helper"
|
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "js"
|
|
|
|
assert_includes Rails::Generators.hidden_namespaces, "css"
|
2015-04-18 19:53:40 -04:00
|
|
|
assert Rails::Generators.options[:rails][:api]
|
2015-04-19 21:15:39 -04:00
|
|
|
assert_equal false, Rails::Generators.options[:rails][:assets]
|
2015-04-19 21:17:24 -04:00
|
|
|
assert_equal false, Rails::Generators.options[:rails][:helper]
|
2015-04-19 20:23:19 -04:00
|
|
|
assert_nil Rails::Generators.options[:rails][:template_engine]
|
2015-04-17 19:34:21 -04:00
|
|
|
end
|
2015-04-20 12:45:46 -04:00
|
|
|
|
|
|
|
test "api only generators allow overriding generator options" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.generators.helper = true
|
|
|
|
config.api_only = true
|
|
|
|
config.generators.template_engine = :my_template
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
# Initialize the application
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Rails.application.load_generators
|
|
|
|
|
|
|
|
assert Rails::Generators.options[:rails][:api]
|
|
|
|
assert Rails::Generators.options[:rails][:helper]
|
|
|
|
assert_equal :my_template, Rails::Generators.options[:rails][:template_engine]
|
|
|
|
end
|
2016-02-12 07:13:40 -05:00
|
|
|
|
|
|
|
test "api only generator generate mailer views" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.api_only = true
|
|
|
|
RUBY
|
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
rails "generate", "mailer", "notifier", "foo"
|
2016-02-12 07:13:40 -05:00
|
|
|
assert File.exist?(File.join(rails_root, "app/views/notifier_mailer/foo.text.erb"))
|
|
|
|
assert File.exist?(File.join(rails_root, "app/views/notifier_mailer/foo.html.erb"))
|
|
|
|
end
|
2016-12-16 20:10:21 -05:00
|
|
|
|
|
|
|
test "ARGV is mutated as expected" do
|
|
|
|
require "#{app_path}/config/environment"
|
2017-09-02 14:45:25 -04:00
|
|
|
require "rails/command"
|
2016-12-16 20:10:21 -05:00
|
|
|
Rails::Command.const_set("APP_PATH", "rails/all")
|
|
|
|
|
|
|
|
FileUtils.cd(rails_root) do
|
|
|
|
ARGV = ["mailer", "notifier", "foo"]
|
|
|
|
Rails::Command.const_set("ARGV", ARGV)
|
2016-12-21 21:01:15 -05:00
|
|
|
quietly { Rails::Command.invoke :generate, ARGV }
|
2016-12-16 20:10:21 -05:00
|
|
|
|
|
|
|
assert_equal ["notifier", "foo"], ARGV
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails::Command.send(:remove_const, "APP_PATH")
|
|
|
|
end
|
2017-02-24 09:45:05 -05:00
|
|
|
|
2017-11-09 06:54:24 -05:00
|
|
|
test "help does not show hidden namespaces and hidden commands" do
|
2017-02-24 09:45:05 -05:00
|
|
|
FileUtils.cd(rails_root) do
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("generate", "--help")
|
2017-02-24 09:45:05 -05:00
|
|
|
assert_no_match "active_record:migration", output
|
2017-11-09 06:54:24 -05:00
|
|
|
assert_no_match "credentials", output
|
2017-03-26 19:29:20 -04:00
|
|
|
|
2017-09-03 12:55:26 -04:00
|
|
|
output = rails("destroy", "--help")
|
2017-03-26 19:29:20 -04:00
|
|
|
assert_no_match "active_record:migration", output
|
2017-02-24 09:45:05 -05:00
|
|
|
end
|
|
|
|
end
|
2019-06-28 19:43:16 -04:00
|
|
|
|
|
|
|
test "skip collision check" do
|
|
|
|
rails("generate", "model", "post", "title:string")
|
|
|
|
|
|
|
|
output = rails("generate", "model", "post", "title:string", "body:string")
|
|
|
|
assert_match(/The name 'Post' is either already used in your application or reserved/, output)
|
|
|
|
|
|
|
|
output = rails("generate", "model", "post", "title:string", "body:string", "--skip-collision-check")
|
|
|
|
assert_no_match(/The name 'Post' is either already used in your application or reserved/, output)
|
|
|
|
end
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2009-11-03 21:14:44 -05:00
|
|
|
end
|