Skip to content

Commit

Permalink
Put dev dependencies in generated plugin Gemfile
Browse files Browse the repository at this point in the history
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 rubygems/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.
  • Loading branch information
jonathanhefner committed Jan 9, 2020
1 parent de853a2 commit 5fbf14b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 2 additions & 9 deletions railties/lib/rails/generators/rails/plugin/templates/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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? -%>
Expand Down
30 changes: 18 additions & 12 deletions railties/test/generators/plugin_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,31 @@ def test_ensure_that_test_dummy_can_be_generated_from_a_template
assert_no_directory "test"
end

def test_database_entry_is_generated_for_sqlite3_by_default_in_full_mode
run_generator([destination_root, "--full"])
def test_no_development_dependencies_in_gemspec
run_generator
assert_file "bukkits.gemspec" do |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 "bukkits.gemspec", /sqlite3/
assert_file "Gemfile" do |contents|
assert_match_sqlite3(contents)
end
end

def test_config_another_database
run_generator([destination_root, "-d", "mysql", "--full"])
def test_custom_database_dependency
run_generator [destination_root, "-d", "mysql"]
assert_file "test/dummy/config/database.yml", /mysql/
assert_file "bukkits.gemspec", /mysql/
assert_file "Gemfile", /mysql/
end

def test_dont_generate_development_dependency
def test_skip_database_dependency
run_generator [destination_root, "--skip-active-record"]

assert_file "bukkits.gemspec" do |contents|
assert_no_match(/s\.add_development_dependency "sqlite3"/, contents)
assert_file "Gemfile" do |contents|
assert_no_match(/sqlite/, contents)
end
end

Expand Down Expand Up @@ -495,7 +503,6 @@ def test_skipping_gemspec
assert_file "Gemfile" do |contents|
assert_no_match("gemspec", contents)
assert_match(/gem 'rails'/, contents)
assert_match_sqlite3(contents)
end
end

Expand All @@ -505,7 +512,6 @@ def test_skipping_gemspec_in_full_mode
assert_file "Gemfile" do |contents|
assert_no_match("gemspec", contents)
assert_match(/gem 'rails'/, contents)
assert_match_sqlite3(contents)
end
end

Expand Down

0 comments on commit 5fbf14b

Please sign in to comment.