Skip to content

Delay loading system test integration #1903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 77 additions & 94 deletions lib/rspec/rails/example/system_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,113 +1,96 @@
can_load_system_tests = false
begin
require 'puma'
require 'capybara'
if ActionPack::VERSION::STRING >= "5.1"
require 'action_dispatch/system_test_case'
can_load_system_tests = true
end
# rubocop:disable Lint/HandleExceptions
rescue LoadError
# rubocop:enable Lint/HandleExceptions
end
module RSpec
module Rails
# @api public
# Container class for system tests
module SystemExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include ActionDispatch::Integration::Runner
include ActionDispatch::Assertions
include ActionController::TemplateAssertions

if !can_load_system_tests
module RSpec
module Rails
module SystemExampleGroup
extend ActiveSupport::Concern

included do
abort """
System test integration requires Rails >= 5.1 and has a hard
dependency on `puma` and `capybara`, please add these to your
Gemfile before attempting to use system tests.
"""
# @private
module BlowAwayAfterTeardownHook
# @private
def after_teardown
end
end
end
end
else
module RSpec
module Rails
# @api public
# Container class for system tests
module SystemExampleGroup
extend ActiveSupport::Concern
include RSpec::Rails::RailsExampleGroup
include ActionDispatch::Integration::Runner
include ActionDispatch::Assertions
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
include ActionController::TemplateAssertions

include ActionDispatch::IntegrationTest::Behavior

# @private
module BlowAwayAfterTeardownHook
# @private
def after_teardown
end
end
# for the SystemTesting Screenshot situation
def passed?
RSpec.current_example.exception.nil?
end

original_after_teardown = ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)
# @private
def method_name
@method_name ||= [
self.class.name.underscore,
RSpec.current_example.description.underscore,
rand(1000)
].join("_").gsub(/[\/\.:, ]/, "_")
end

include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
include BlowAwayAfterTeardownHook
# Delegates to `Rails.application`.
def app
::Rails.application
end

# for the SystemTesting Screenshot situation
def passed?
RSpec.current_example.exception.nil?
included do |other|
begin
require 'capybara'
require 'action_dispatch/system_test_case'
# rubocop:disable Lint/HandleExceptions
rescue LoadError
# rubocop:enable Lint/HandleExceptions
abort """
System test integration requires Rails >= 5.1 and has a hard
dependency on a webserver and `capybara`, please add capybara to
your Gemfile and configure a webserver (e.g. `Capybara.server =
:webrick`) before attempting to use system tests.
""".gsub(/[\n\s]+/,' ').strip
end

# @private
def method_name
@method_name ||= [
self.class.name.underscore,
RSpec.current_example.description.underscore,
rand(1000)
].join("_").gsub(/[\/\.:, ]/, "_")
end
original_after_teardown =
::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown.instance_method(:after_teardown)

# Delegates to `Rails.application`.
def app
::Rails.application
end
other.include ActionDispatch::IntegrationTest::Behavior
other.include ::ActionDispatch::SystemTesting::TestHelpers::SetupAndTeardown
other.include ::ActionDispatch::SystemTesting::TestHelpers::ScreenshotHelper
other.include BlowAwayAfterTeardownHook

included do
attr_reader :driver
attr_reader :driver

if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
ActionDispatch::SystemTesting::Server.silence_puma = true
end
if ActionDispatch::SystemTesting::Server.respond_to?(:silence_puma=)
ActionDispatch::SystemTesting::Server.silence_puma = true
end

def initialize(*args, &blk)
super(*args, &blk)
@driver = nil
end
def initialize(*args, &blk)
super(*args, &blk)
@driver = nil
end

def driven_by(*args, &blk)
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
end
def driven_by(*args, &blk)
@driver = ::ActionDispatch::SystemTestCase.driven_by(*args, &blk).tap(&:use)
end

before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
@routes = ::Rails.application.routes
end
before do
# A user may have already set the driver, so only default if driver
# is not set
driven_by(:selenium) unless @driver
@routes = ::Rails.application.routes
end

after do
orig_stdout = $stdout
$stdout = StringIO.new
begin
original_after_teardown.bind(self).call
ensure
myio = $stdout
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
$stdout = orig_stdout
end
after do
orig_stdout = $stdout
$stdout = StringIO.new
begin
original_after_teardown.bind(self).call
ensure
myio = $stdout
RSpec.current_example.metadata[:extra_failure_lines] = myio.string
$stdout = orig_stdout
end
end
end
Expand Down