Skip to content
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

Dev/remove monkey patching #240

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/testup/debug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Debug
module Timing

def time(title = '', &block)
start = Time.now
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = block.call
lapsed_time = Time.now - start
Log.trace :timing, "Timing #{title}: #{lapsed_time}s"
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
Log.trace :timing, "Timing #{title}: #{'%.5f' % elapsed_time}s"
result
end

Expand Down
49 changes: 0 additions & 49 deletions src/testup/debug_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,6 @@
require 'testup/minitest_setup.rb'
require 'testup/app_files.rb'


# Patching Minitest because we currently use 5.4.3 which doesn't have `prerecord`.
module Minitest
class Runnable

class << self # rubocop:disable Style/MultilineIfModifier
puts "Alias old_run_one_method..."
alias_method :old_run_one_method, :run_one_method
end unless respond_to?(:old_run_one_method)

def self.run_one_method(klass, method_name, reporter)
reporter.prerecord(klass, method_name) if reporter.respond_to?(:prerecord)
self.old_run_one_method(klass, method_name, reporter)
end

##
# About to start running a test. This allows a reporter to show
# that it is starting or that we are in the middle of a test run.
def prerecord(klass, method_name)
end

end

class CompositeReporter < AbstractReporter
def prerecord(klass, method_name)
self.reporters.each do |reporter|
reporter.prerecord(klass, method_name) if reporter.respond_to?(:prerecord)
end
end
end
end


# Patch minitest-reporter to support `prerecord`.
module Minitest
module Reporters
class DelegateReporter < Minitest::AbstractReporter

def prerecord(klass, method_name)
all_reporters.each do |reporter|
reporter.prerecord(klass, method_name) if reporter.respond_to?(:prerecord)
end
end

end
end
end


module TestUp
# Based on Minitest::SummaryReporter
class DebugReporter < Minitest::StatisticsReporter
Expand Down
6 changes: 3 additions & 3 deletions src/testup/debugger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def self.debugger_output=(value)

# Time the given block and output it to the system debugger.
def self.time(title, &block)
start = Time.now
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
block.call
ensure
lapsed_time = Time.now - start
self.output("TestUp::Debugger.time: #{title} #{lapsed_time}s")
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
self.output("TestUp::Debugger.time: #{title} #{'%.5f' % elapsed_time}s")
nil
end

Expand Down
104 changes: 43 additions & 61 deletions src/testup/minitest_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,55 @@
# run the tests when SketchUp exits because Minitest.autoload uses at_exit {}.

require "rubygems"
gem "minitest"
require "minitest"
require "minitest/spec"
require "minitest/mock"


# TestUp modifications of Minitest.
module Minitest

class << self

# In case some tests cause crashes it's nice if the name of the test is
# output to the debugger before the test is run.
# TODO(thomthom): Review if this can be done without overriding the method.
unless method_defined?(:testup_run_one_method)
alias :testup_run_one_method :run_one_method
def run_one_method(*args)
klass, method_name, reporter = args
TestUp::Debugger.output("Running: #{klass.name}.#{method_name}")
start_time = Time.now
result = self.testup_run_one_method(*args)
lapsed_time = Time.now - start_time
TestUp::Debugger.output("> Elapsed time: #{lapsed_time}s")
result
end
module TestUp
module MinitestPrepend
def run_one_method(klass, method_name)
TestUp::Debugger.output("Running: #{klass.name}.#{method_name}")
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = super
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
TestUp::Debugger.output("> Elapsed time: #{'%.5f' % elapsed_time}s")
result
end

end # class << self

class Runnable
class << self
unless method_defined?(:testup_run)
alias :testup_run :run
# This is mostly a copy+paste of Minitest::Runnable.run method from v5.4.3.
# It adds hooks called before and after a testcase is run.
# It's to allow setup or tests that might be slow to setup.
def run(reporter, options = {})
filter = options[:filter] || '/./'
filter = Regexp.new $1 if filter =~ /\/(.*)\//

filtered_methods = self.runnable_methods.find_all { |m|
filter === m || filter === "#{self}##{m}"
}

with_info_handler reporter do
# Custom TestUp hook
if !filtered_methods.empty? && self.respond_to?(:setup_testcase)
TestUp::Debugger.output("setup_testcase: #{self.name}")
self.setup_testcase
end

filtered_methods.each do |method_name|
run_one_method self, method_name, reporter
end

# Custom TestUp hook
if !filtered_methods.empty? && self.respond_to?(:teardown_testcase)
TestUp::Debugger.output("teardown_testcase: #{self.name}")
self.teardown_testcase
end
end
end # def run
end

module RunnablePrepend
def run(reporter, options = {})
filter = options[:filter] || '/./'
filter = Regexp.new $1 if filter =~ /\/(.*)\//

filtered_methods = runnable_methods.find_all { |m|
filter === m || filter === "#{self}##{m}"
}

with_info_handler reporter do
# Custom TestUp hook
if !filtered_methods.empty? && self.respond_to?(:setup_testcase)
TestUp::Debugger.output("setup_testcase: #{self.name}")
setup_testcase
end

filtered_methods.each do |method_name|
run_one_method self, method_name, reporter
end

# Custom TestUp hook
if !filtered_methods.empty? && self.respond_to?(:teardown_testcase)
TestUp::Debugger.output("teardown_testcase: #{self.name}")
teardown_testcase
end
end
end # class << self
end # class Runnable
end # def run
end
end

# TestUp modifications of Minitest
Minitest.singleton_class.prepend TestUp::MinitestPrepend

end # module Minitest
Minitest::Runnable.prepend TestUp::RunnablePrepend


# TODO(thomthom): Not sure if this is needed.
Expand Down
Loading