require 'simplecov'
SimpleCov.start

ENV["RAILS_ENV"] = "test"

require "stringio"
require File.expand_path("../dummy/config/environment.rb",  __FILE__)
require "rspec/rails"
require "rspec/expectations"
require "rails/generators"

Rails.backtrace_cleaner.remove_silencers!

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

# Load fixtures from the engine
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
end

# Define custom RSpec matcher
RSpec::Matchers.define :contain do |count|
  match do |actual|
    actual.scan(@substr).count == count
  end

  chain :occurrences_of do |substr|
    @substr = substr
  end

  description do
    "contain #{count} occurrences of #{@substr}"
  end

  failure_message_for_should do |actual|
    "expected that '#{actual}' would contain #{count} occurrences of '#{@substr}'"
  end
end

# Define helpers
module Helpers
  # Capture stdout output: http://stackoverflow.com/a/16507680
  def capture_stdout(&block)
    old = $stdout
    $stdout = fake = StringIO.new
    block.call
    fake.string
  ensure
    $stdout = old
  end
end

# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus
  config.include Helpers

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = 'random'
  config.after(:all) do
    if File.exists?(SeedMigration::Migrator::SEEDS_FILE_PATH)
      File.delete(SeedMigration::Migrator::SEEDS_FILE_PATH)
    end
  end
end
