unless defined?(Spree::InstallGenerator)
  require 'generators/spree/install/install_generator'
end

require 'generators/spree/dummy/dummy_generator'

desc "Generates a dummy app for testing"
namespace :common do
  task :test_app, :user_class do |_t, args|
    args.with_defaults(user_class: "Spree::LegacyUser")
    require ENV['LIB_NAME']

    ENV["RAILS_ENV"] = 'test'

    Spree::DummyGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--quiet"]
    Spree::InstallGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--auto-accept", "--migrate=false", "--seed=false", "--sample=false", "--quiet", "--user_class=#{args[:user_class]}"]

    puts "Setting up dummy database..."

    sh "bundle exec bin/rails db:environment:set RAILS_ENV=test"
    sh "bundle exec rake db:drop db:create db:migrate VERBOSE=false RAILS_ENV=test"

    begin
      require "generators/#{ENV['LIB_NAME']}/install/install_generator"
      puts 'Running extension installation generator...'
      "#{ENV['LIB_NAME'].camelize}::Generators::InstallGenerator".constantize.start(["--auto-run-migrations"])
    rescue LoadError
      # No extension generator to run
    end
  end

  task :seed do |_t, _args|
    puts "Seeding ..."

    sh "bundle exec rake db:seed RAILS_ENV=test"
  end
end

unless defined?(Spree::InstallGenerator)
  require 'generators/spree/install/install_generator'
end

require 'generators/spree/dummy/dummy_generator'

desc "Generates a dummy app for testing"
namespace :common do
  task :test_app, :user_class do |_t, args|
    args.with_defaults(user_class: "Spree::LegacyUser")
    require ENV['LIB_NAME']

    ENV["RAILS_ENV"] = 'test'

    Spree::DummyGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--quiet"]
    Spree::InstallGenerator.start ["--lib_name=#{ENV['LIB_NAME']}", "--auto-accept", "--migrate=false", "--seed=false", "--sample=false", "--quiet", "--user_class=#{args[:user_class]}"]

    puts "Setting up dummy database..."

    sh "bin/rails db:environment:set RAILS_ENV=test"
    sh "bin/rails db:drop db:create db:migrate VERBOSE=false RAILS_ENV=test"

    begin
      require "generators/#{ENV['LIB_NAME']}/install/install_generator"
      puts 'Running extension installation generator...'
      "#{ENV['LIB_NAME'].camelize}::Generators::InstallGenerator".constantize.start(["--auto-run-migrations"])
    rescue LoadError
      # No extension generator to run
    end
  end

  task :seed do |_t, _args|
    puts "Seeding ..."

    sh "bundle exec rake db:seed RAILS_ENV=test"
  end
end

Drop bundle exec from bin/rails in test_app
Drop bundle exec from bin/rails in test_app

Without this we are getting bundler errors

    bundle exec bin/rails db:environment:set RAILS_ENV=test
    bundler: failed to load command: bin/rails (bin/rails)
    Gem::LoadError: You have already activated arel 7.1.4, but your Gemfile requires arel 7.1.2. Prepending `bundle exec` to your command may solve this.
      /home/jhawthorn/.gem/ruby/2.3.1/gems/bundler-1.13.1/lib/bundler/runtime.rb:40:in `block in setup'
      /home/jhawthorn/.gem/ruby/2.3.1/gems/bundler-1.13.1/lib/bundler/runtime.rb:25:in `map'
      /home/jhawthorn/.gem/ruby/2.3.1/gems/bundler-1.13.1/lib/bundler/runtime.rb:25:in `setup'
      /home/jhawthorn/.gem/ruby/2.3.1/gems/bundler-1.13.1/lib/bundler.rb:99:in `setup'
      /home/jhawthorn/src/solidus/core/spec/dummy/config/boot.rb:6:in `<top (required)>'
      bin/rails:3:in `require_relative'
      bin/rails:3:in `<top (required)>'
    rake aborted!
    Command failed with status (1): [bundle exec bin/rails db:environment:set R...]
