Skip to content

Commit

Permalink
Clean up acceptance tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Nov 5, 2014
1 parent d2a198e commit 0da09b2
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 60 deletions.
10 changes: 9 additions & 1 deletion spec/acceptance/independent_matchers_spec.rb
Expand Up @@ -4,7 +4,15 @@
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
Expand Down
11 changes: 7 additions & 4 deletions spec/acceptance/rails_integration_spec.rb
Expand Up @@ -46,7 +46,7 @@ def show
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
Expand All @@ -60,7 +60,10 @@ def show
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')
Expand Down Expand Up @@ -116,13 +119,13 @@ def setup
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 }
Expand Down
4 changes: 0 additions & 4 deletions spec/support/acceptance/helpers.rb
Expand Up @@ -15,10 +15,6 @@ def self.configure_example_group(example_group)
example_group.before do
fs.clean
end

# example_group.around do |example|
# Bundler.with_clean_env { example.run }
# end
end

include ActiveModelHelpers
Expand Down
6 changes: 5 additions & 1 deletion spec/support/acceptance/helpers/active_model_helpers.rb
@@ -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
4 changes: 0 additions & 4 deletions spec/support/acceptance/helpers/file_helpers.rb
Expand Up @@ -8,10 +8,6 @@ def append_to_file(path, content, options = {})
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
Expand Down
14 changes: 6 additions & 8 deletions spec/support/acceptance/helpers/minitest_helpers.rb
@@ -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
2 changes: 2 additions & 0 deletions spec/support/acceptance/helpers/rails_version_helpers.rb
@@ -1,3 +1,5 @@
require_relative 'gem_helpers'

module AcceptanceTests
module RailsVersionHelpers
include GemHelpers
Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance/helpers/rspec_helpers.rb
Expand Up @@ -6,7 +6,7 @@ def rspec_rails_version
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
Expand Down
35 changes: 21 additions & 14 deletions spec/support/acceptance/helpers/step_helpers.rb
Expand Up @@ -30,27 +30,39 @@ def add_shoulda_matchers_to_project(options = {})
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

Expand All @@ -73,11 +85,6 @@ def create_rails_application
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

Expand Down
3 changes: 2 additions & 1 deletion spec/support/tests/command_runner.rb
Expand Up @@ -143,7 +143,8 @@ def formatted_env

def run
Dir.chdir(directory) do
system(env, *command, options)
pid = spawn(env, *command, options)
Process.waitpid(pid)
end

@status = $?
Expand Down
37 changes: 15 additions & 22 deletions spec/support/tests/filesystem.rb
Expand Up @@ -19,6 +19,14 @@ def project_directory
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
Expand Down Expand Up @@ -46,33 +54,18 @@ def read(path)
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)
Expand Down

0 comments on commit 0da09b2

Please sign in to comment.