2017-08-14 17:08:09 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-10-09 01:12:28 +00:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-06 01:30:17 +00:00
|
|
|
class GeneratorsTest < ActiveSupport::TestCase
|
2009-10-09 01:12:28 +00:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
end
|
|
|
|
|
2011-06-06 12:54:05 +00:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2009-11-30 23:58:38 +00:00
|
|
|
def app_const
|
|
|
|
@app_const ||= Class.new(Rails::Application)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_config
|
2009-12-31 20:28:48 +00:00
|
|
|
require "rails/all"
|
2009-12-23 01:03:23 +00:00
|
|
|
require "rails/generators"
|
2009-11-30 23:58:38 +00:00
|
|
|
yield app_const.config
|
|
|
|
end
|
|
|
|
|
2010-01-28 01:39:11 +00:00
|
|
|
def with_bare_config
|
|
|
|
require "rails"
|
|
|
|
require "rails/generators"
|
|
|
|
yield app_const.config
|
|
|
|
end
|
|
|
|
|
2010-11-15 19:09:05 +00:00
|
|
|
test "allow running plugin new generator inside Rails app directory" do
|
2017-09-03 16:55:26 +00:00
|
|
|
rails "plugin", "new", "vendor/plugins/bukkits"
|
2010-11-17 22:31:29 +00: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 19:09:39 +00:00
|
|
|
end
|
|
|
|
|
2009-10-09 01:12:28 +00:00
|
|
|
test "generators default values" do
|
2010-01-28 01:39:11 +00:00
|
|
|
with_bare_config do |c|
|
2009-10-09 01:12:28 +00:00
|
|
|
assert_equal(true, c.generators.colorize_logging)
|
|
|
|
assert_equal({}, c.generators.aliases)
|
|
|
|
assert_equal({}, c.generators.options)
|
2010-02-01 09:48:59 +00:00
|
|
|
assert_equal({}, c.generators.fallbacks)
|
2009-10-09 01:12:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators set rails options" do
|
2010-01-28 18:45:25 +00:00
|
|
|
with_bare_config do |c|
|
2011-10-18 13:29:16 +00:00
|
|
|
c.generators.orm = :data_mapper
|
2009-10-09 01:12:28 +00:00
|
|
|
c.generators.test_framework = :rspec
|
2009-11-04 02:14:44 +00:00
|
|
|
c.generators.helper = false
|
2012-10-14 10:03:39 +00:00
|
|
|
expected = { rails: { orm: :data_mapper, test_framework: :rspec, helper: false } }
|
2009-10-09 01:12:28 +00:00
|
|
|
assert_equal(expected, c.generators.options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators set rails aliases" do
|
2009-11-30 23:58:38 +00:00
|
|
|
with_config do |c|
|
2012-10-14 10:03:39 +00:00
|
|
|
c.generators.aliases = { rails: { test_framework: "-w" } }
|
|
|
|
expected = { rails: { test_framework: "-w" } }
|
2009-10-09 01:12:28 +00:00
|
|
|
assert_equal expected, c.generators.aliases
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-06 16:32:06 +00:00
|
|
|
test "generators aliases, options, templates and fallbacks on initialization" do
|
2009-12-23 01:03:23 +00:00
|
|
|
add_to_config <<-RUBY
|
2012-10-14 10:03:39 +00:00
|
|
|
config.generators.rails aliases: { test_framework: "-w" }
|
2011-10-18 13:29:16 +00:00
|
|
|
config.generators.orm :data_mapper
|
2009-12-23 01:03:23 +00:00
|
|
|
config.generators.test_framework :rspec
|
2010-02-01 09:48:59 +00:00
|
|
|
config.generators.fallbacks[:shoulda] = :test_unit
|
2010-02-06 16:32:06 +00:00
|
|
|
config.generators.templates << "some/where"
|
2009-12-23 01:03:23 +00:00
|
|
|
RUBY
|
|
|
|
|
2009-10-15 21:50:02 +00:00
|
|
|
# Initialize the application
|
2010-01-12 16:48:09 +00:00
|
|
|
require "#{app_path}/config/environment"
|
2011-05-25 07:30:41 +00:00
|
|
|
Rails.application.load_generators
|
2009-10-09 01:12:28 +00:00
|
|
|
|
|
|
|
assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
|
|
|
|
assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
|
2012-10-14 10:03:39 +00:00
|
|
|
assert_equal Hash[shoulda: :test_unit], Rails::Generators.fallbacks
|
2010-10-06 15:18:59 +00:00
|
|
|
assert_equal ["some/where"], Rails::Generators.templates_path
|
2009-10-09 01:12:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "generators no color on initialization" do
|
2009-12-23 01:03:23 +00:00
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.generators.colorize_logging = false
|
|
|
|
RUBY
|
|
|
|
|
2009-10-15 21:50:02 +00:00
|
|
|
# Initialize the application
|
2009-12-23 01:03:23 +00:00
|
|
|
require "#{app_path}/config/environment"
|
2011-05-25 07:30:41 +00:00
|
|
|
Rails.application.load_generators
|
2009-10-09 01:12:28 +00:00
|
|
|
|
|
|
|
assert_equal Thor::Base.shell, Thor::Shell::Basic
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generators with hashes for options and aliases" do
|
2010-01-28 01:39:11 +00:00
|
|
|
with_bare_config do |c|
|
2009-10-09 01:12:28 +00:00
|
|
|
c.generators do |g|
|
2012-10-14 10:03:39 +00:00
|
|
|
g.orm :data_mapper, migration: false
|
|
|
|
g.plugin aliases: { generator: "-g" },
|
|
|
|
generator: true
|
2009-10-09 01:12:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
expected = {
|
2012-10-14 10:03:39 +00:00
|
|
|
rails: { orm: :data_mapper },
|
|
|
|
plugin: { generator: true },
|
|
|
|
data_mapper: { migration: false }
|
2009-10-09 01:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, c.generators.options
|
2012-10-14 10:03:39 +00:00
|
|
|
assert_equal({ plugin: { generator: "-g" } }, c.generators.aliases)
|
2009-10-09 01:12:28 +00:00
|
|
|
end
|
|
|
|
end
|
2010-07-21 14:37:03 +00: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 17:16:09 +00:00
|
|
|
g.orm "data_mapper", migration: false
|
2010-07-21 14:37:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
expected = {
|
2012-10-14 10:03:39 +00:00
|
|
|
rails: { orm: :data_mapper },
|
|
|
|
data_mapper: { migration: false }
|
2010-07-21 14:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal expected, c.generators.options
|
|
|
|
end
|
|
|
|
end
|
2015-04-17 23:34:21 +00:00
|
|
|
|
2015-04-18 23:53:40 +00:00
|
|
|
test "api only generators hide assets, helper, js and css namespaces and set api option" do
|
2015-04-17 23:34:21 +00:00
|
|
|
add_to_config <<-RUBY
|
2015-04-20 16:28:23 +00:00
|
|
|
config.api_only = true
|
2015-04-17 23:34:21 +00:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
# Initialize the application
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
Rails.application.load_generators
|
|
|
|
|
2016-09-16 16:44:05 +00: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 23:53:40 +00:00
|
|
|
assert Rails::Generators.options[:rails][:api]
|
2015-04-20 01:15:39 +00:00
|
|
|
assert_equal false, Rails::Generators.options[:rails][:assets]
|
2015-04-20 01:17:24 +00:00
|
|
|
assert_equal false, Rails::Generators.options[:rails][:helper]
|
2015-04-20 00:23:19 +00:00
|
|
|
assert_nil Rails::Generators.options[:rails][:template_engine]
|
2015-04-17 23:34:21 +00:00
|
|
|
end
|
2015-04-20 16:45:46 +00: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 12:13:40 +00:00
|
|
|
|
|
|
|
test "api only generator generate mailer views" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.api_only = true
|
|
|
|
RUBY
|
|
|
|
|
2017-09-03 16:55:26 +00:00
|
|
|
rails "generate", "mailer", "notifier", "foo"
|
2016-02-12 12:13:40 +00: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-17 01:10:21 +00:00
|
|
|
|
|
|
|
test "ARGV is mutated as expected" do
|
|
|
|
require "#{app_path}/config/environment"
|
2017-09-02 18:45:25 +00:00
|
|
|
require "rails/command"
|
2016-12-17 01:10:21 +00: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-22 02:01:15 +00:00
|
|
|
quietly { Rails::Command.invoke :generate, ARGV }
|
2016-12-17 01:10:21 +00:00
|
|
|
|
|
|
|
assert_equal ["notifier", "foo"], ARGV
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails::Command.send(:remove_const, "APP_PATH")
|
|
|
|
end
|
2017-02-24 14:45:05 +00:00
|
|
|
|
2017-11-09 11:54:24 +00:00
|
|
|
test "help does not show hidden namespaces and hidden commands" do
|
2017-02-24 14:45:05 +00:00
|
|
|
FileUtils.cd(rails_root) do
|
2017-09-03 16:55:26 +00:00
|
|
|
output = rails("generate", "--help")
|
2017-02-24 14:45:05 +00:00
|
|
|
assert_no_match "active_record:migration", output
|
2017-11-09 11:54:24 +00:00
|
|
|
assert_no_match "credentials", output
|
2017-03-26 23:29:20 +00:00
|
|
|
|
2017-09-03 16:55:26 +00:00
|
|
|
output = rails("destroy", "--help")
|
2017-03-26 23:29:20 +00:00
|
|
|
assert_no_match "active_record:migration", output
|
2017-02-24 14:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
2019-06-28 23:43:16 +00: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-09 01:12:28 +00:00
|
|
|
end
|
2009-11-04 02:14:44 +00:00
|
|
|
end
|