2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "generators/generators_test_helper"
|
|
|
|
require "rails/generators/rails/app/app_generator"
|
|
|
|
require "env_helpers"
|
2009-06-19 07:13:46 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class ActionsTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
2012-12-06 07:05:45 -05:00
|
|
|
include EnvHelpers
|
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
tests Rails::Generators::AppGenerator
|
|
|
|
arguments [destination_root]
|
|
|
|
|
2009-06-19 07:13:46 -04:00
|
|
|
def setup
|
2010-09-25 07:11:07 -04:00
|
|
|
Rails.application = TestApp::Application
|
2009-06-19 07:13:46 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-09-25 07:11:07 -04:00
|
|
|
def teardown
|
|
|
|
Rails.application = TestApp::Application.instance
|
|
|
|
end
|
|
|
|
|
2010-01-18 04:43:10 -05:00
|
|
|
def test_invoke_other_generator_with_shortcut
|
2016-08-06 13:16:09 -04:00
|
|
|
action :invoke, "model", ["my_model"]
|
|
|
|
assert_file "app/models/my_model.rb", /MyModel/
|
2010-01-18 04:43:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invoke_other_generator_with_full_namespace
|
2016-08-06 13:16:09 -04:00
|
|
|
action :invoke, "rails:model", ["my_model"]
|
|
|
|
assert_file "app/models/my_model.rb", /MyModel/
|
2010-01-18 04:43:10 -05:00
|
|
|
end
|
|
|
|
|
2009-06-28 06:00:13 -04:00
|
|
|
def test_create_file_should_write_data_to_file_path
|
2016-08-06 13:16:09 -04:00
|
|
|
action :create_file, "lib/test_file.rb", "heres test data"
|
|
|
|
assert_file "lib/test_file.rb", "heres test data"
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2009-06-28 06:00:13 -04:00
|
|
|
def test_create_file_should_write_block_contents_to_file_path
|
2016-08-16 03:30:11 -04:00
|
|
|
action(:create_file, "lib/test_file.rb") { "heres block data" }
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "lib/test_file.rb", "heres block data"
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2009-11-03 19:02:24 -05:00
|
|
|
def test_add_source_adds_source_to_gemfile
|
2009-06-19 10:08:03 -04:00
|
|
|
run_generator
|
2016-08-06 13:16:09 -04:00
|
|
|
action :add_source, "http://gems.github.com"
|
2019-02-18 10:56:39 -05:00
|
|
|
assert_file "Gemfile", /source 'http:\/\/gems\.github\.com'\n/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2015-06-25 02:25:49 -04:00
|
|
|
def test_add_source_with_block_adds_source_to_gemfile_with_gem
|
|
|
|
run_generator
|
2016-08-06 13:16:09 -04:00
|
|
|
action :add_source, "http://gems.github.com" do
|
|
|
|
gem "rspec-rails"
|
2015-06-25 02:25:49 -04:00
|
|
|
end
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\z/
|
2015-12-18 21:54:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_source_with_block_adds_source_to_gemfile_after_gem
|
|
|
|
run_generator
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "will-paginate"
|
|
|
|
action :add_source, "http://gems.github.com" do
|
|
|
|
gem "rspec-rails"
|
2015-12-18 21:54:54 -05:00
|
|
|
end
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\ngem 'will-paginate'\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_source_should_create_newline_between_blocks
|
|
|
|
run_generator
|
|
|
|
action :add_source, "http://gems.github.com" do
|
|
|
|
gem "rspec-rails"
|
|
|
|
end
|
|
|
|
|
|
|
|
action :add_source, "http://gems2.github.com" do
|
|
|
|
gem "fakeweb"
|
|
|
|
end
|
|
|
|
assert_file "Gemfile", /\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'rspec-rails'\nend\n\nsource 'http:\/\/gems2\.github\.com' do\n gem 'fakeweb'\nend\n\z/
|
2015-06-25 02:25:49 -04:00
|
|
|
end
|
|
|
|
|
2009-11-03 19:02:24 -05:00
|
|
|
def test_gem_should_put_gem_dependency_in_gemfile
|
2009-06-19 10:08:03 -04:00
|
|
|
run_generator
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "will-paginate"
|
2019-02-18 10:56:39 -05:00
|
|
|
assert_file "Gemfile", /gem 'will\-paginate'\n\z/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2010-02-08 14:58:32 -05:00
|
|
|
def test_gem_with_version_should_include_version_in_gemfile
|
|
|
|
run_generator
|
2017-08-19 01:13:23 -04:00
|
|
|
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
|
2010-02-08 14:58:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gem_should_insert_on_separate_lines
|
|
|
|
run_generator
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
File.open("Gemfile", "a") { |f| f.write("# Some content...") }
|
2011-11-25 12:06:14 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "rspec"
|
|
|
|
action :gem, "rspec-rails"
|
2010-02-08 14:58:32 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "Gemfile", /^gem 'rspec'$/
|
|
|
|
assert_file "Gemfile", /^gem 'rspec-rails'$/
|
2010-02-08 14:58:32 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 05:26:06 -04:00
|
|
|
def test_gem_should_include_options
|
|
|
|
run_generator
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "rspec", github: "dchelimsky/rspec", tag: "1.2.9.rc1"
|
2012-05-22 05:26:06 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "Gemfile", /gem 'rspec', github: 'dchelimsky\/rspec', tag: '1\.2\.9\.rc1'/
|
2012-05-22 05:26:06 -04:00
|
|
|
end
|
|
|
|
|
2014-08-27 06:30:35 -04:00
|
|
|
def test_gem_with_non_string_options
|
|
|
|
run_generator
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "rspec", require: false
|
|
|
|
action :gem, "rspec-rails", group: [:development, :test]
|
2014-08-27 06:30:35 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "Gemfile", /^gem 'rspec', require: false$/
|
|
|
|
assert_file "Gemfile", /^gem 'rspec-rails', group: \[:development, :test\]$/
|
2014-08-27 06:30:35 -04:00
|
|
|
end
|
|
|
|
|
2014-05-27 03:21:16 -04:00
|
|
|
def test_gem_falls_back_to_inspect_if_string_contains_single_quote
|
|
|
|
run_generator
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
action :gem, "rspec", ">=2.0'0"
|
2014-05-27 03:21:16 -04:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "Gemfile", /^gem 'rspec', ">=2\.0'0"$/
|
2012-05-22 05:26:06 -04:00
|
|
|
end
|
|
|
|
|
2016-01-24 00:34:52 -05:00
|
|
|
def test_gem_works_even_if_frozen_string_is_passed_as_argument
|
|
|
|
run_generator
|
|
|
|
|
2018-02-27 23:33:37 -05:00
|
|
|
action :gem, -"frozen_gem", -"1.0.0"
|
2016-01-24 00:34:52 -05:00
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "Gemfile", /^gem 'frozen_gem', '1.0.0'$/
|
2016-01-24 00:34:52 -05:00
|
|
|
end
|
|
|
|
|
2011-09-04 04:14:53 -04:00
|
|
|
def test_gem_group_should_wrap_gems_in_a_group
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :gem_group, :development, :test do
|
2016-08-06 13:16:09 -04:00
|
|
|
gem "rspec-rails"
|
2011-09-04 04:14:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
action :gem_group, :test do
|
2016-08-06 13:16:09 -04:00
|
|
|
gem "fakeweb"
|
2011-09-04 04:14:53 -04:00
|
|
|
end
|
2011-12-09 16:49:29 -05:00
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\n\ngroup :development, :test do\n gem 'rspec-rails'\nend\n\ngroup :test do\n gem 'fakeweb'\nend\n\z/
|
2011-09-04 04:14:53 -04:00
|
|
|
end
|
|
|
|
|
2018-08-28 23:20:29 -04:00
|
|
|
def test_github_should_create_an_indented_block
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :github, "user/repo" do
|
|
|
|
gem "foo"
|
|
|
|
gem "bar"
|
|
|
|
gem "baz"
|
|
|
|
end
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\n\ngithub 'user\/repo' do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
|
2018-08-28 23:20:29 -04:00
|
|
|
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
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\n\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
|
2018-08-28 23:20:29 -04:00
|
|
|
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
|
2019-07-22 00:04:01 -04:00
|
|
|
github "user/repo2", a: "correct", other: true do
|
|
|
|
gem "foo"
|
|
|
|
gem "bar"
|
|
|
|
gem "baz"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "Gemfile", /\n\ngroup :magic do\n github 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\n github 'user\/repo2', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\n end\nend\n\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_github_should_create_newline_between_blocks
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :github, "user/repo", a: "correct", other: true do
|
|
|
|
gem "foo"
|
|
|
|
gem "bar"
|
|
|
|
gem "baz"
|
|
|
|
end
|
|
|
|
|
|
|
|
action :github, "user/repo2", a: "correct", other: true do
|
|
|
|
gem "foo"
|
|
|
|
gem "bar"
|
|
|
|
gem "baz"
|
2018-08-28 23:20:29 -04:00
|
|
|
end
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /\n\ngithub 'user\/repo', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\ngithub 'user\/repo2', a: 'correct', other: true do\n gem 'foo'\n gem 'bar'\n gem 'baz'\nend\n\z/
|
2019-02-18 10:56:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gem_with_gemfile_without_newline_at_the_end
|
|
|
|
run_generator
|
|
|
|
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
|
|
|
|
|
|
|
|
action :gem, "will-paginate"
|
|
|
|
assert_file "Gemfile", /gem 'rspec-rails'\ngem 'will-paginate'\n\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_gem_group_with_gemfile_without_newline_at_the_end
|
|
|
|
run_generator
|
|
|
|
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
|
|
|
|
|
|
|
|
action :gem_group, :test do
|
|
|
|
gem "fakeweb"
|
|
|
|
end
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /gem 'rspec-rails'\n\ngroup :test do\n gem 'fakeweb'\nend\n\z/
|
2019-02-18 10:56:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_source_with_gemfile_without_newline_at_the_end
|
|
|
|
run_generator
|
|
|
|
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
|
|
|
|
|
|
|
|
action :add_source, "http://gems.github.com" do
|
|
|
|
gem "fakeweb"
|
|
|
|
end
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /gem 'rspec-rails'\n\nsource 'http:\/\/gems\.github\.com' do\n gem 'fakeweb'\nend\n\z/
|
2019-02-18 10:56:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_github_with_gemfile_without_newline_at_the_end
|
|
|
|
run_generator
|
|
|
|
File.open("Gemfile", "a") { |f| f.write("gem 'rspec-rails'") }
|
|
|
|
|
|
|
|
action :github, "user/repo" do
|
|
|
|
gem "fakeweb"
|
|
|
|
end
|
|
|
|
|
2019-07-22 00:04:01 -04:00
|
|
|
assert_file "Gemfile", /gem 'rspec-rails'\n\ngithub 'user\/repo' do\n gem 'fakeweb'\nend\n\z/
|
2018-08-28 23:20:29 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_environment_should_include_data_in_environment_initializer_block
|
|
|
|
run_generator
|
2010-06-22 17:17:20 -04:00
|
|
|
autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
|
|
|
|
action :environment, autoload_paths
|
2017-08-09 12:26:21 -04:00
|
|
|
assert_file "config/application.rb", / class Application < Rails::Application\n #{Regexp.escape(autoload_paths)}\n/
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2011-07-17 02:47:21 -04:00
|
|
|
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"]'
|
2016-08-06 13:16:09 -04:00
|
|
|
action :environment, autoload_paths, env: "development"
|
2017-08-09 12:26:21 -04:00
|
|
|
assert_file "config/environments/development.rb", /Rails\.application\.configure do\n #{Regexp.escape(autoload_paths)}\n/
|
2011-07-17 02:47:21 -04:00
|
|
|
end
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_environment_with_block_should_include_block_contents_in_environment_initializer_block
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
action :environment do
|
2019-04-11 16:50:51 -04:00
|
|
|
_ = "# This wont be added" # assignment to silence parse-time warning "unused literal ignored"
|
2016-08-06 13:16:09 -04:00
|
|
|
"# This will be added"
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
assert_file "config/application.rb" do |content|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/# This will be added/, content)
|
|
|
|
assert_no_match(/# This wont be added/, content)
|
2009-07-06 12:31:28 -04:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2017-08-09 12:26:21 -04:00
|
|
|
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
|
|
|
|
|
2009-06-19 10:08:03 -04:00
|
|
|
def test_git_with_symbol_should_run_command_using_git_scm
|
2019-12-15 13:58:07 -05:00
|
|
|
assert_runs "git init", nil do
|
2015-05-29 02:02:44 -04:00
|
|
|
action :git, :init
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_git_with_hash_should_run_each_command_using_git_scm
|
2019-12-15 13:58:07 -05:00
|
|
|
assert_runs ["git rm README", "git add ."], nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :git, rm: "README", add: "."
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_vendor_should_write_data_to_file_in_vendor
|
2016-08-06 13:16:09 -04:00
|
|
|
action :vendor, "vendor_file.rb", "# vendor data"
|
2017-08-09 12:26:21 -04:00
|
|
|
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
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_lib_should_write_data_to_file_in_lib
|
2016-08-06 13:16:09 -04:00
|
|
|
action :lib, "my_library.rb", "class MyLibrary"
|
2017-08-09 12:26:21 -04:00
|
|
|
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
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rakefile_should_write_date_to_file_in_lib_tasks
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rakefile, "myapp.rake", "task run: [:environment]"
|
2017-08-09 12:26:21 -04:00
|
|
|
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
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_initializer_should_write_date_to_file_in_config_initializers
|
2016-08-06 13:16:09 -04:00
|
|
|
action :initializer, "constants.rb", "MY_CONSTANT = 42"
|
2017-08-09 12:26:21 -04:00
|
|
|
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
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2019-10-20 19:17:26 -04:00
|
|
|
test "generate" do
|
2018-12-07 01:01:32 -05:00
|
|
|
run_generator
|
|
|
|
action :generate, "model", "MyModel"
|
|
|
|
assert_file "app/models/my_model.rb", /MyModel/
|
|
|
|
end
|
|
|
|
|
2020-01-09 16:49:58 -05:00
|
|
|
test "generate should raise on failure" do
|
|
|
|
run_generator
|
|
|
|
message = capture(:stderr) do
|
|
|
|
assert_raises SystemExit do
|
|
|
|
action :generate, "model", "1234567890"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_match(/1234567890/, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "generate with inline option" do
|
2018-12-07 01:01:32 -05:00
|
|
|
run_generator
|
2020-01-09 16:49:58 -05:00
|
|
|
assert_not_called(generator, :run) do
|
|
|
|
action :generate, "model", "MyModel", inline: true
|
|
|
|
end
|
2019-10-20 19:17:26 -04:00
|
|
|
assert_file "app/models/my_model.rb", /MyModel/
|
|
|
|
end
|
|
|
|
|
2020-01-09 16:49:58 -05:00
|
|
|
test "generate with inline option should raise on failure" do
|
2019-10-20 19:17:26 -04:00
|
|
|
run_generator
|
|
|
|
error = assert_raises do
|
2020-01-09 16:49:58 -05:00
|
|
|
action :generate, "model", "1234567890", inline: true
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2019-10-20 19:17:26 -04:00
|
|
|
assert_match(/1234567890/, error.message)
|
2009-06-19 07:13:46 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake should run rake with the default environment" do
|
|
|
|
assert_runs "rake log:clear", env: { "RAILS_ENV" => "development" } do
|
2015-05-29 02:02:44 -04:00
|
|
|
with_rails_env nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rake, "log:clear"
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake with env option should run rake with the env environment" do
|
|
|
|
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rake, "log:clear", env: "production"
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake with RAILS_ENV set should run rake with the RAILS_ENV environment" do
|
|
|
|
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
|
2015-05-29 02:02:44 -04:00
|
|
|
with_rails_env "production" do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rake, "log:clear"
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2011-09-29 08:35:25 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake with env option and RAILS_ENV set should run rake with the env environment" do
|
|
|
|
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
|
2015-05-29 02:02:44 -04:00
|
|
|
with_rails_env "staging" do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rake, "log:clear", env: "production"
|
2015-05-29 02:02:44 -04:00
|
|
|
end
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2011-09-29 08:35:25 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake with sudo option should run rake with sudo" do
|
|
|
|
assert_runs "sudo rake log:clear" do
|
|
|
|
action :rake, "log:clear", sudo: true
|
2012-12-06 07:05:45 -05:00
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rake with capture option should run rake with capture" do
|
|
|
|
assert_runs "rake log:clear", capture: true do
|
|
|
|
action :rake, "log:clear", capture: true
|
2017-10-11 03:08:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-20 19:17:26 -04:00
|
|
|
test "rake with abort_on_failure option should raise on failure" do
|
|
|
|
capture(:stderr) do
|
|
|
|
assert_raises SystemExit do
|
|
|
|
action :rake, "invalid", abort_on_failure: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command should run rails with the default environment" do
|
|
|
|
assert_runs "rails log:clear", env: { "RAILS_ENV" => "development" } do
|
2016-02-20 12:56:35 -05:00
|
|
|
with_rails_env nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rails_command, "log:clear"
|
2016-02-20 12:56:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command with env option should run rails with the env environment" do
|
|
|
|
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rails_command, "log:clear", env: "production"
|
2016-02-20 12:56:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command with RAILS_ENV set should run rails with the RAILS_ENV environment" do
|
|
|
|
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
|
2016-02-20 12:56:35 -05:00
|
|
|
with_rails_env "production" do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rails_command, "log:clear"
|
2016-02-20 12:56:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command with env option and RAILS_ENV set should run rails with the env environment" do
|
|
|
|
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
|
2016-02-20 12:56:35 -05:00
|
|
|
with_rails_env "staging" do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rails_command, "log:clear", env: "production"
|
2016-02-20 12:56:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command with sudo option should run rails with sudo" do
|
|
|
|
assert_runs "sudo rails log:clear" do
|
2016-02-20 12:56:35 -05:00
|
|
|
with_rails_env nil do
|
2016-08-06 13:16:09 -04:00
|
|
|
action :rails_command, "log:clear", sudo: true
|
2016-02-20 12:56:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
test "rails_command with capture option should run rails with capture" do
|
|
|
|
assert_runs "rails log:clear", capture: true do
|
2017-10-11 03:08:10 -04:00
|
|
|
with_rails_env nil do
|
|
|
|
action :rails_command, "log:clear", capture: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-20 19:17:26 -04:00
|
|
|
test "rails_command with abort_on_failure option should raise on failure" do
|
|
|
|
run_generator
|
|
|
|
capture(:stderr) do
|
|
|
|
assert_raises SystemExit do
|
|
|
|
action :rails_command, "invalid", abort_on_failure: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-09 16:49:58 -05:00
|
|
|
test "rails_command with inline option" do
|
|
|
|
run_generator
|
|
|
|
assert_not_called(generator, :run) do
|
|
|
|
action :rails_command, "generate model MyModel", inline: true
|
|
|
|
end
|
|
|
|
assert_file "app/models/my_model.rb", /MyModel/
|
|
|
|
end
|
|
|
|
|
|
|
|
test "rails_command with inline option should raise on failure" do
|
|
|
|
run_generator
|
|
|
|
error = assert_raises do
|
|
|
|
action :rails_command, "generate model 1234567890", inline: true
|
|
|
|
end
|
|
|
|
assert_match(/1234567890/, error.message)
|
|
|
|
end
|
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
test "route should add route" do
|
2009-06-19 10:08:03 -04:00
|
|
|
run_generator
|
2019-11-22 21:16:41 -05:00
|
|
|
route_commands = ["get 'foo'", "get 'bar'", "get 'baz'"]
|
|
|
|
route_commands.each do |route_command|
|
|
|
|
action :route, route_command
|
|
|
|
end
|
|
|
|
assert_routes route_commands
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
test "route should indent routing code" do
|
2015-10-27 01:34:07 -04:00
|
|
|
run_generator
|
2019-11-22 21:16:41 -05:00
|
|
|
route_commands = ["get 'foo'", "get 'bar'", "get 'baz'"]
|
|
|
|
action :route, route_commands.join("\n")
|
|
|
|
assert_routes route_commands
|
2015-10-27 01:34:07 -04:00
|
|
|
end
|
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
test "route should be idempotent" do
|
2015-03-18 10:27:55 -04:00
|
|
|
run_generator
|
2019-11-22 21:16:41 -05:00
|
|
|
route_command = "root 'welcome#index'"
|
2015-03-18 10:27:55 -04:00
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
# runs first time
|
|
|
|
action :route, route_command
|
|
|
|
assert_routes route_command
|
2015-12-16 16:59:22 -05:00
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
content = File.read(File.expand_path("config/routes.rb", destination_root))
|
2015-12-16 16:59:22 -05:00
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
# runs second time
|
|
|
|
action :route, route_command
|
|
|
|
assert_file "config/routes.rb", content
|
|
|
|
end
|
2015-03-18 10:27:55 -04:00
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
test "route with namespace option should nest route" do
|
|
|
|
run_generator
|
|
|
|
action :route, "get 'foo'\nget 'bar'", namespace: :baz
|
|
|
|
assert_routes <<~ROUTING_CODE.chomp
|
|
|
|
namespace :baz do
|
|
|
|
get 'foo'
|
|
|
|
get 'bar'
|
|
|
|
end
|
|
|
|
ROUTING_CODE
|
|
|
|
end
|
2015-03-18 10:27:55 -04:00
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
test "route with namespace option array should deeply nest route" do
|
|
|
|
run_generator
|
|
|
|
action :route, "get 'foo'\nget 'bar'", namespace: %w[baz qux]
|
|
|
|
assert_routes <<~ROUTING_CODE.chomp
|
|
|
|
namespace :baz do
|
|
|
|
namespace :qux do
|
|
|
|
get 'foo'
|
|
|
|
get 'bar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ROUTING_CODE
|
2015-03-18 10:27:55 -04:00
|
|
|
end
|
|
|
|
|
2010-02-17 12:09:13 -05:00
|
|
|
def test_readme
|
|
|
|
run_generator
|
2015-08-15 08:08:18 -04:00
|
|
|
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
|
2015-05-29 02:02:44 -04:00
|
|
|
assert_match "application up and running", action(:readme, "README.md")
|
|
|
|
end
|
2010-02-17 12:09:13 -05:00
|
|
|
end
|
|
|
|
|
2011-03-04 07:05:27 -05:00
|
|
|
def test_readme_with_quiet
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
run_generator
|
2015-08-15 08:08:18 -04:00
|
|
|
assert_called(Rails::Generators::AppGenerator, :source_root, times: 2, returns: destination_root) do
|
2015-05-29 02:02:44 -04:00
|
|
|
assert_no_match "application up and running", action(:readme, "README.md")
|
|
|
|
end
|
2011-03-04 07:05:27 -05:00
|
|
|
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
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
assert_equal("", action(:log, "YES"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_log_with_status_with_quiet
|
2012-10-14 06:03:39 -04:00
|
|
|
generator(default_arguments, quiet: true)
|
2011-03-04 07:05:27 -05:00
|
|
|
assert_equal("", action(:log, :yes, "YES"))
|
|
|
|
end
|
|
|
|
|
2016-12-23 09:45:39 -05:00
|
|
|
private
|
2019-12-27 16:05:21 -05:00
|
|
|
if RUBY_VERSION < "2.7"
|
|
|
|
def action(*args, &block)
|
|
|
|
capture(:stdout) { generator.send(*args, &block) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def action(*args, **kwargs, &block)
|
|
|
|
capture(:stdout) { generator.send(*args, **kwargs, &block) }
|
|
|
|
end
|
2009-06-19 10:08:03 -04:00
|
|
|
end
|
2019-11-22 21:16:41 -05:00
|
|
|
|
2019-12-15 13:58:07 -05:00
|
|
|
def assert_runs(commands, config = {}, &block)
|
|
|
|
config_matcher = ->(actual_config) do
|
|
|
|
assert_equal config, actual_config.slice(*config.keys)
|
|
|
|
end if config
|
|
|
|
args = Array(commands).map { |command| [command, *config_matcher] }
|
|
|
|
|
|
|
|
assert_called_with(generator, :run, args) do
|
|
|
|
block.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-22 21:16:41 -05:00
|
|
|
def assert_routes(*route_commands)
|
|
|
|
route_regexps = route_commands.flatten.map do |route_command|
|
|
|
|
%r{
|
|
|
|
^#{Regexp.escape("Rails.application.routes.draw do")}\n
|
|
|
|
(?:[ ]{2}.+\n|\n)*
|
|
|
|
#{Regexp.escape(route_command.indent(2))}\n
|
|
|
|
(?:[ ]{2}.+\n|\n)*
|
|
|
|
end\n
|
|
|
|
}x
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "config/routes.rb", *route_regexps
|
|
|
|
end
|
2009-06-19 07:13:46 -04:00
|
|
|
end
|