Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created August 10, 2017 20:11
Show Gist options
  • Select an option

  • Save jturkel/20d12d47ad4cf3e8e111ff318dd5a09a to your computer and use it in GitHub Desktop.

Select an option

Save jturkel/20d12d47ad4cf3e8e111ff318dd5a09a to your computer and use it in GitHub Desktop.
module KnapsackRSpecConfigurationPatch
def before(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #before(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_before_suite_hooks << block
end
def prepend_before(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #prepend_before(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_before_suite_hooks.unshift(block)
end
def append_before(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #append_before(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_before_suite_hooks << block
end
def after(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #after(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_after_suite_hooks << block
end
def prepend_after(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #prepend_after(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_after_suite_hooks.unshift(block)
end
def append_after(scope = nil, *, &block)
return super unless scope == :suite
puts("Registering #append_after(:suite): #{caller.first}")
KnapsackRSpecConfigurationPatch.knapsack_after_suite_hooks << block
end
def self.knapsack_before_suite_hooks
@knapsack_before_suite_hooks ||= []
end
def self.knapsack_after_suite_hooks
@knapsack_after_suite_hooks ||= []
end
cattr_accessor :before_suite_hooks_run
end
RSpec.configure do |config|
config.before(:suite) do
unless KnapsackRSpecConfigurationPatch.before_suite_hooks_run
KnapsackRSpecConfigurationPatch.before_suite_hooks_run = true
puts 'Running before(:suite) hooks'
KnapsackRSpecConfigurationPatch.knapsack_before_suite_hooks.each(&:call)
end
end
at_exit do
puts 'Running after(:suite) hooks'
KnapsackRSpecConfigurationPatch.knapsack_after_suite_hooks.each(&:call)
end
end
RSpec::Core::Configuration.prepend(KnapsackRSpecConfigurationPatch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment