crowbar/crowbar-core

View on GitHub
crowbar_framework/spec/spec_helper.rb

Summary

Maintainability
A
0 mins
Test Coverage
#
# Copyright 2013-2014, SUSE LINUX Products GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

ENV["RAILS_ENV"] ||= "test"

require File.expand_path("../../config/environment", __FILE__)
require "simplecov"

if ENV["CODECLIMATE_REPO_TOKEN"]
  require "coveralls"
  require "codeclimate-test-reporter"

  Coveralls.wear!

  SimpleCov.start do
    add_filter "/spec"

    formatter SimpleCov::Formatter::MultiFormatter[
      SimpleCov::Formatter::HTMLFormatter,
      CodeClimate::TestReporter::Formatter
    ]
  end
else
  SimpleCov.start do
    add_filter "/spec"
  end
end

require "rspec/rails"
require "webmock/rspec"

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

WebMock.disable_net_connect!(allow_localhost: false)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  config.mock_with :rspec
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # 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"

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explictly tag your specs with their type, e.g.:
  #
  #     describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/v/3-0/docs
  config.infer_spec_type_from_file_location!

  config.before do
    allow(Crowbar::Settings).to receive(:domain).and_return("crowbar.com")
    allow_any_instance_of(Proposal).to receive(:properties_template_dir).
      and_return(Rails.root.join("spec/fixtures/data_bags"))
    allow(::Crowbar::Checks::Maintenance).to(
      receive(:updates_status).
      and_return(passed: true, errors: [])
    )
    allow_any_instance_of(Crowbar::UpgradeStatus).to receive(:lock_path).and_return(
      Tempfile.open("upgrade_status_spec.lock").path
    )
  end

  config.before(:each) do
    @upgrade_state_file = File.join(
      Dir::Tmpname.tmpdir,
      Dir::Tmpname.make_tmpname("upgrade_status_spec.state.yaml", nil)
    )
    allow(Crowbar::UpgradeStatus).to receive(:new).and_return(
      Crowbar::UpgradeStatus.new(Rails.logger, @upgrade_state_file)
    )
  end

  config.after(:each) do
    File.unlink @upgrade_state_file
  end

  config.append_before(:each) do
    stub_request(:any, /localhost:4000/).to_rack(OfflineChef)
    stub_request(:any, /localhost:5984/).to_rack(OfflineCouchDB)
  end
end