1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/support/acceptance/adds_shoulda_matchers_to_project.rb
Elliot Winkler e0a0200fe4 Make independent matchers really independent
Why:

* When `delegate_method` was modified a while back to add Doublespeak
  and use MatcherContext, Shoulda::Matchers::Independent became unable
  to be required independently.

To satisfy the above:

* Require Doublespeak and MatcherContext within
  `delegate_method_matcher.rb`.
* Add an acceptance test to ensure that Independent stays independent.

Secondary-Author: jc00ke <jesse@jc00ke.com>
2015-10-01 10:12:17 -06:00

133 lines
3.2 KiB
Ruby

require_relative 'helpers/base_helpers'
require_relative 'helpers/rspec_helpers'
module AcceptanceTests
class AddsShouldaMatchersToProject
def self.call(options)
new(options).call
end
include BaseHelpers
include RspecHelpers
def initialize(options)
@options = options
end
def call
add_gem 'shoulda-matchers', gem_options
unless options[:with_configuration] === false
configure_test_helper_files
end
end
protected
attr_reader :options
private
def gem_options
gem_options = { path: fs.root_directory }
if options[:manually]
gem_options[:require] = false
end
gem_options
end
def configure_test_helper_files
each_test_helper_file do |test_helper_file, test_framework, library|
add_configuration_block_to(
test_helper_file,
test_framework,
library
)
end
end
def each_test_helper_file
options[:test_frameworks].each do |test_framework|
libraries = options.fetch(:libraries, [])
test_helper_files_for(test_framework, libraries).each do |test_helper_file|
yield test_helper_file, test_framework, libraries
end
end
end
def add_configuration_block_to(test_helper_file, test_framework, libraries)
test_framework_config = test_framework_config_for(test_framework)
library_config = library_config_for(libraries)
content = <<-EOT
Shoulda::Matchers.configure do |config|
config.integrate do |with|
#{test_framework_config}
#{library_config}
end
end
EOT
if options[:manually]
content = "require 'shoulda-matchers'\n#{content}"
end
fs.append_to_file(test_helper_file, content)
end
def test_framework_config_for(test_framework)
if test_framework
"with.test_framework :#{test_framework}\n"
else
''
end
end
def library_config_for(libraries)
libraries.map { |library| "with.library :#{library}" }.join("\n")
end
def test_helper_files_for(test_framework, libraries)
files = []
if integrates_with_nunit_and_rails?(test_framework, libraries) ||
integrates_with_nunit_only?(test_framework)
files << 'test/test_helper.rb'
end
if integrates_with_rspec?(test_framework)
if bundle.includes?('rspec-rails')
files << 'spec/rails_helper.rb'
else
files << 'spec/spec_helper.rb'
end
end
files
end
def integrates_with_nunit_only?(test_framework)
nunit_frameworks = [:test_unit, :minitest, :minitest_4, :minitest_5]
nunit_frameworks.include?(test_framework)
end
def integrates_with_rspec?(test_framework)
test_framework == :rspec
end
def integrates_with_rspec_rails_3_x?(test_framework, libraries)
integrates_with_rails?(libraries) && rspec_rails_version >= 3
end
def integrates_with_nunit_and_rails?(test_framework, libraries)
test_framework.nil? && libraries.include?(:rails)
end
def integrates_with_rails?(libraries)
libraries.include?(:rails)
end
end
end