Clean up acceptance tests a bit

This commit is contained in:
Elliot Winkler 2014-11-05 14:29:19 -07:00
parent d2a198e4dc
commit 0da09b2132
11 changed files with 68 additions and 60 deletions

View File

@ -4,7 +4,15 @@ describe 'shoulda-matchers has independent matchers' do
context 'specifically delegate_method' do
specify 'and integrates with a Ruby application that uses Minitest' do
create_generic_bundler_project
add_minitest_to_project
updating_bundle do
add_minitest_to_project
add_shoulda_context_to_project(manually: true)
add_shoulda_matchers_to_project(
test_frameworks: [:n_unit],
manually: true
)
end
write_file 'lib/post_office.rb', <<-FILE
class PostOffice

View File

@ -46,7 +46,7 @@ describe 'shoulda-matchers integrates with Rails' do
specify 'in a project that uses RSpec' do
updating_bundle do
add_gems_for_rspec
add_shoulda_matchers_to_project
add_shoulda_matchers_to_project(test_frameworks: [:rspec])
end
run_tests_for_rspec
@ -60,7 +60,10 @@ describe 'shoulda-matchers integrates with Rails' do
updating_bundle do
add_spring_to_project
add_gems_for_rspec
add_shoulda_matchers_to_project(manually: true)
add_shoulda_matchers_to_project(
test_frameworks: [:rspec],
manually: true
)
end
run_command_within_bundle!('spring stop')
@ -116,13 +119,13 @@ describe 'shoulda-matchers integrates with Rails' do
end
def run_tests_for_rspec
add_spec 'spec/models/user_spec.rb', <<-FILE
add_rspec_file 'spec/models/user_spec.rb', <<-FILE
describe User do
it { should validate_presence_of(:name) }
end
FILE
add_spec 'spec/controllers/examples_controller_spec.rb', <<-FILE
add_rspec_file 'spec/controllers/examples_controller_spec.rb', <<-FILE
describe ExamplesController, "show" do
before { get :show }

View File

@ -15,10 +15,6 @@ module AcceptanceTests
example_group.before do
fs.clean
end
# example_group.around do |example|
# Bundler.with_clean_env { example.run }
# end
end
include ActiveModelHelpers

View File

@ -1,7 +1,11 @@
require_relative 'gem_helpers'
module AcceptanceTests
module ActiveModelHelpers
include GemHelpers
def active_model_version
Bundler.definition.specs['activemodel'][0].version
bundle_version_of('activemodel')
end
end
end

View File

@ -8,10 +8,6 @@ module AcceptanceTests
fs.append_to_file(path, content, options)
end
def append_to_file_following(path, content_to_add, insertion_point)
fs.append_to_file_following(path, content_to_add, insertion_point)
end
def remove_from_file(path, pattern)
fs.remove_from_file(path, pattern)
end

View File

@ -1,21 +1,19 @@
require_relative 'gem_helpers'
module AcceptanceTests
module MinitestHelpers
include GemHelpers
def minitest_test_case_superclass
if minitest_gte_5?
if minitest_version >= 5
'Minitest::Test'
else
'MiniTest::Unit::TestCase'
end
end
def minitest_gte_5?
if minitest_version
Gem::Requirement.new('>= 5').satisfied_by?(minitest_version)
end
end
def minitest_version
Bundler.definition.specs['minitest'][0].version
bundle_version_of('minitest')
end
end
end

View File

@ -1,3 +1,5 @@
require_relative 'gem_helpers'
module AcceptanceTests
module RailsVersionHelpers
include GemHelpers

View File

@ -6,7 +6,7 @@ module AcceptanceTests
bundle_version_of('rspec-rails')
end
def add_spec(path, content)
def add_rspec_file(path, content)
content = "require '#{spec_helper_require_path}'\n#{content}"
write_file path, content
end

View File

@ -30,27 +30,39 @@ module AcceptanceTests
add_gem 'shoulda-matchers', gem_options
if options[:manually]
append_to_file spec_helper_file_path,
"require 'shoulda/matchers'",
following: "require 'rspec/rails'"
if options[:test_frameworks].include?(:rspec)
append_to_file spec_helper_file_path, "require 'shoulda/matchers'"
end
if options[:test_frameworks].include?(:n_unit)
append_to_file 'test/test_helper.rb', "require 'shoulda/matchers'"
end
end
end
def add_minitest_to_project
add_gem 'shoulda-context'
add_gem 'minitest-reporters'
write_file 'test/test_helper.rb', <<-FILE
append_to_file 'test/test_helper.rb', <<-FILE
require 'minitest/autorun'
require 'minitest/reporters'
require 'shoulda/context'
require 'shoulda/matchers'
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
FILE
end
def write_minitest_test(path, &block)
contents = block.call(minitest_test_case_superclass)
def add_shoulda_context_to_project(options = {})
add_gem 'shoulda-context'
if options[:manually]
append_to_file 'test/test_helper.rb', <<-FILE
require 'shoulda/context'
FILE
end
end
def write_minitest_test(path)
contents = yield minitest_test_case_superclass
write_file(path, contents)
end
@ -73,11 +85,6 @@ module AcceptanceTests
bundle.remove_gem 'turn'
bundle.remove_gem 'coffee-rails'
bundle.remove_gem 'uglifier'
# if ruby_version >= '1.9.3'
# bundle.add_gem 'rake', '~> 0.9'
# run_command! 'bundle update rake --local'
# end
end
end

View File

@ -143,7 +143,8 @@ Output:
def run
Dir.chdir(directory) do
system(env, *command, options)
pid = spawn(env, *command, options)
Process.waitpid(pid)
end
@status = $?

View File

@ -19,6 +19,14 @@ module Tests
PROJECT_DIRECTORY
end
def wrap(path)
if path.is_a?(Pathname)
path
else
find_in_project(path)
end
end
def within_project(&block)
Dir.chdir(project_directory, &block)
end
@ -46,33 +54,18 @@ module Tests
end
def write(path, content)
pathname = find_in_project(path)
pathname.dirname.mkpath
pathname = wrap(path)
create_parents_of(pathname)
pathname.open('w') { |f| f.write(content) }
end
def append_to_file(path, content, options = {})
if options[:following]
append_to_file_following(path, content, options[:following])
else
open(path, 'a') { |f| f.puts(content + "\n") }
end
def create_parents_of(path)
wrap(path).dirname.mkpath
end
def append_to_file_following(path, content_to_add, insertion_point)
content_to_add = content_to_add + "\n"
file_content = read(path)
file_lines = file_content.split("\n")
insertion_index = file_lines.find_index(insertion_point)
if insertion_index.nil?
raise "Cannot find #{insertion_point.inspect} in #{path}"
end
file_lines.insert(insertion_index + 1, content_to_add)
new_file_content = file_lines.join("\n")
write(path, new_file_content)
def append_to_file(path, content, options = {})
create_parents_of(path)
open(path, 'a') { |f| f.puts(content + "\n") }
end
def remove_from_file(path, pattern)