# frozen_string_literal: true

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
    Pathname.new(destination_root).join("config/environments").mkpath
    %w[README.md Rakefile config/environments/development.rb].each do |path|
      FileUtils.touch Pathname.new(destination_root).join(path)
    end
    Pathname.new(destination_root).join("Gemfile").write(<<~GEMFILE)
      source "https://rubygems.org"
    GEMFILE
  end

  test "adds rubocop gems in development group" do
    apply_generator

    assert_file "Gemfile", /#{Regexp.quote(<<~GEMFILE)}/
      group :development do
        gem "rubocop", require: false
        gem "rubocop-performance", require: false
        gem "rubocop-rails", require: false
      end
    GEMFILE
  end

  test "generates a .rubocop.yml file that requires additional plugins" do
    apply_generator

    assert_file ".rubocop.yml", /#{Regexp.quote(<<~YML)}/
      plugins:
        - rubocop-performance
        - rubocop-rails
    YML
  end

  test "enables the rubocop generator setting" do
    Pathname.new(destination_root).join("config/environments/development.rb").write(<<~RB)
      Rails.application.configure do
        # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
        # config.generators.apply_rubocop_autocorrect_after_generate!
      end
    RB

    apply_generator

    assert_file "config/environments/development.rb", <<~RB
      Rails.application.configure do
        # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
        config.generators.apply_rubocop_autocorrect_after_generate!
      end
    RB
  end
end
