1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
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)
files <<
if bundle.includes?('rspec-rails')
'spec/rails_helper.rb'
else
'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
|