Created
September 14, 2017 16:49
-
-
Save kaspth/08253d0988bfa6858156efd9709bbd11 to your computer and use it in GitHub Desktop.
`Minitest.after_runnable` callbacks to execute after every test class method has been run… it might even work!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# minitest uses Gem.find_files, so this should be somewhere on the load path: | |
# $LOAD_PATH/minitest/after_runnable_plugin.rb | |
class Minitest | |
class AfterRunnableReporter < AbstractReporter | |
def initialize(after_runnable, methods) | |
@after_runnable, @methods = after_runnable, methods | |
end | |
def prerecord(klass, name) | |
@methods[klass].delete(name) | |
end | |
def report(result) | |
@after_runnables.reverse_each(&:call) if @methods[result.class].empty? | |
end | |
end | |
class << self | |
@@after_runnable = [] | |
# Minitest.after_suite { eileen.rejoice! } | |
def after_runnable(&block) | |
@@after_runnable << block | |
end | |
# Called from within the at_exit autorun, every class/runnable has been loaded. | |
# Collects every runnable method within a class, so the reporter can slowly whittle 'em down. | |
def plugin_after_suite_init(*) | |
reporter << AfterRunnableReporter.new \ | |
@@after_runnable, | |
runnables.map { |runnable| [ runnable, runnable.runnable_methods ] }.to_h | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment