From 2594def00b5336e251c82d9432a6b1018026a12b Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Fri, 27 Oct 2023 08:54:45 -0700 Subject: [PATCH] Use rubocop-rails >= 2.22.0 for better Rails 7.1 support Before, rubocop-rails treated the Rails 7.1 `Rails.env.local?` as an unrecognized environment name, requiring custom configuration of `Rails/UnknownEnv` as a workaround. Now that rubocop-rails 2.22.0 is released, this workaround is no longer needed. This commit removes the workaround and ensures that `rubocop-rails` is added to the Gemfile with the `>= 2.22.0` version requirement. --- lib/nextgen/generators/rubocop.rb | 3 ++- template/.rubocop.yml.tt | 9 ------- test/nextgen/generators/rubocop_test.rb | 35 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 test/nextgen/generators/rubocop_test.rb diff --git a/lib/nextgen/generators/rubocop.rb b/lib/nextgen/generators/rubocop.rb index cb69828..2268430 100644 --- a/lib/nextgen/generators/rubocop.rb +++ b/lib/nextgen/generators/rubocop.rb @@ -6,7 +6,8 @@ plugins << "minitest" if minitest? plugins << "performance" plugins << "rails" -install_gems "rubocop", *plugins.map { "rubocop-#{_1}" }, group: :development, require: false +install_gem("rubocop-rails", version: ">= 2.22.0", group: :development, require: false) +install_gems(*plugins.map { "rubocop-#{_1}" }, "rubocop", group: :development, require: false) binstub "rubocop" say_git "Generate .rubocop.yml" diff --git a/template/.rubocop.yml.tt b/template/.rubocop.yml.tt index 62331b4..f52cf07 100644 --- a/template/.rubocop.yml.tt +++ b/template/.rubocop.yml.tt @@ -190,15 +190,6 @@ Rails/RootPathnameMethods: Rails/SkipsModelValidations: Enabled: false -<% if Gem::Version.new(Rails.version) >= Gem::Version.new("7.1.0.beta1") -%> -Rails/UnknownEnv: - Environments: - - development - - test - - production - - local -<% end -%> - Rails/Validation: Enabled: false diff --git a/test/nextgen/generators/rubocop_test.rb b/test/nextgen/generators/rubocop_test.rb new file mode 100644 index 0000000..eb9aeb7 --- /dev/null +++ b/test/nextgen/generators/rubocop_test.rb @@ -0,0 +1,35 @@ +require_relative "test_case" + +class Nextgen::Generators::RubocopTest < Nextgen::Generators::TestCase + destination File.join(Dir.tmpdir, "test_#{SecureRandom.hex(8)}") + setup :prepare_destination + + setup do + %w[README.md Rakefile].each { FileUtils.touch Pathname.new(destination_root).join(_1) } + Pathname.new(destination_root).join("Gemfile").write(<<~GEMFILE) + source "https://rubygems.org" + GEMFILE + end + + test "installs rubocop gems in development group, with version spec for rubocop-rails" do + apply_generator + + assert_file "Gemfile", /#{Regexp.quote(<<~GEMFILE)}/ + group :development do + gem "rubocop", require: false + gem "rubocop-performance", require: false + gem "rubocop-rails", ">= 2.22.0", require: false + end + GEMFILE + end + + test "installs generates a .rubocop.yml file that requires additional plugins" do + apply_generator + + assert_file ".rubocop.yml", /#{Regexp.quote(<<~YML)}/ + require: + - rubocop-performance + - rubocop-rails + YML + end +end