From 5fbf14b92f08fe8d36c066beef85b73b26a6b732 Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Thu, 9 Jan 2020 15:17:04 -0600 Subject: [PATCH] Put dev dependencies in generated plugin Gemfile The Gemfile offers more flexibility than the gemspec in terms of gem groups and platforms. Putting the default development dependencies in the Gemfile encourages users to add their own development dependencies to the Gemfile. This is similar to the current behavior of the `bundle gem` command (see bundler/bundler#7222). This change also fixes a corner case where using the "--skip-gemspec" and "--skip-active-record" options together would incorrectly generate a "sqlite3" dependency in the Gemfile. --- .../rails/plugin/templates/%name%.gemspec.tt | 4 -- .../rails/plugin/templates/Gemfile.tt | 11 +---- .../test/generators/plugin_generator_test.rb | 42 +++++++++++-------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt index 28355e6a51..fde7e348bf 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec.tt @@ -21,8 +21,4 @@ Gem::Specification.new do |spec| spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] <%= '# ' if options.dev? || options.edge? -%>spec.add_dependency "rails", "<%= Array(rails_version_specifier).join('", "') %>" -<% unless options[:skip_active_record] -%> - - spec.add_development_dependency "<%= gem_for_database[0] %>" -<% end -%> end diff --git a/railties/lib/rails/generators/rails/plugin/templates/Gemfile.tt b/railties/lib/rails/generators/rails/plugin/templates/Gemfile.tt index 290259b4db..20cf6509c9 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/Gemfile.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/Gemfile.tt @@ -4,21 +4,14 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } <% if options[:skip_gemspec] -%> <%= '# ' if options.dev? || options.edge? -%>gem 'rails', '<%= Array(rails_version_specifier).join("', '") %>' <% else -%> -# Declare your gem's dependencies in <%= name %>.gemspec. -# Bundler will treat runtime dependencies like base dependencies, and -# development dependencies will be added by default to the :development group. +# Specify your gem's dependencies in <%= name %>.gemspec. gemspec <% end -%> +<% unless options[:skip_active_record] -%> -<% if options[:skip_gemspec] -%> group :development do gem '<%= gem_for_database[0] %>' end -<% else -%> -# Declare any dependencies that are still in development here instead of in -# your gemspec. These might include edge Rails or gems from your path or -# Git. Remember to move these dependencies to your gemspec before releasing -# your gem to rubygems.org. <% end -%> <% if options.dev? || options.edge? -%> diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 5d6a26e183..dd2ffb6967 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -154,23 +154,31 @@ class PluginGeneratorTest < Rails::Generators::TestCase 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"] - + def test_no_development_dependencies_in_gemspec + run_generator assert_file "bukkits.gemspec" do |contents| - assert_no_match(/s\.add_development_dependency "sqlite3"/, contents) + assert_no_match(/add_development_dependency/, contents) + end + end + + def test_default_database_dependency_is_sqlite + run_generator + assert_file "test/dummy/config/database.yml", /sqlite/ + assert_file "Gemfile" do |contents| + assert_match_sqlite3(contents) + end + end + + def test_custom_database_dependency + run_generator [destination_root, "-d", "mysql"] + assert_file "test/dummy/config/database.yml", /mysql/ + assert_file "Gemfile", /mysql/ + end + + def test_skip_database_dependency + run_generator [destination_root, "--skip-active-record"] + assert_file "Gemfile" do |contents| + assert_no_match(/sqlite/, contents) end end @@ -495,7 +503,6 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile" do |contents| assert_no_match("gemspec", contents) assert_match(/gem 'rails'/, contents) - assert_match_sqlite3(contents) end end @@ -505,7 +512,6 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_file "Gemfile" do |contents| assert_no_match("gemspec", contents) assert_match(/gem 'rails'/, contents) - assert_match_sqlite3(contents) end end