Sha256: e9e83f07a131d096dbd9aa40c306d517fcb2c316073da9d63f040b2e845366a5

Contents?: true

Size: 1.83 KB

Versions: 8

Compression:

Stored size: 1.83 KB

Contents

require 'pathname'
require 'bundler'
require 'potassium/cli_options'

module PotassiumTestHelpers
  include Potassium::CliOptions

  APP_NAME = "dummy_app"

  def remove_project_directory
    FileUtils.rm_rf(project_path)
  end

  def create_tmp_directory
    FileUtils.mkdir_p(tmp_path)
  end

  def create_dummy_project(arguments = {})
    Dir.chdir(tmp_path) do
      Bundler.with_clean_env do
        add_fakes_to_path
        full_arguments = hash_to_arguments(create_arguments(true).merge(arguments))
        run_command("#{potassium_bin} create #{APP_NAME} #{full_arguments}")
        on_project { run_command("hound rules update ruby --local") }
      end
    end
  end

  def drop_dummy_database
    return unless File.exist?(project_path)
    on_project { run_command("bundle exec rails db:drop") }
  end

  def add_fakes_to_path
    ENV["PATH"] = "#{support_bin}:#{ENV['PATH']}"
  end

  def project_path
    @project_path ||= Pathname.new("#{tmp_path}/#{APP_NAME}")
  end

  def on_project(&block)
    Dir.chdir(project_path) do
      Bundler.with_clean_env do
        block.call
      end
    end
  end

  private

  def tmp_path
    @tmp_path ||= Pathname.new("#{root_path}/tmp")
  end

  def potassium_bin
    File.join(root_path, "bin", "potassium")
  end

  def hash_to_arguments(hash)
    hash.map do |key, value|
      if value == true
        "--#{key}"
      elsif value == false
        "--no-#{key}"
      elsif value
        "--#{key}=#{value}"
      end
    end.join(" ")
  end

  def support_bin
    File.join(root_path, "spec", "fakes", "bin")
  end

  def root_path
    File.expand_path("../../../", __FILE__)
  end

  def run_command(command)
    system(command)
  end

  def run_rubocop
    options, paths = RuboCop::Options.new.parse(["."])
    runner = RuboCop::Runner.new(options, RuboCop::ConfigStore.new)
    runner.run(paths)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
potassium-5.2.2 spec/support/potassium_test_helpers.rb
potassium-5.2.1 spec/support/potassium_test_helpers.rb
potassium-5.2.0 spec/support/potassium_test_helpers.rb
potassium-5.1.4 spec/support/potassium_test_helpers.rb
potassium-5.1.3 spec/support/potassium_test_helpers.rb
potassium-5.1.2 spec/support/potassium_test_helpers.rb
potassium-5.1.1 spec/support/potassium_test_helpers.rb
potassium-3.0.0 spec/support/potassium_test_helpers.rb