require 'singleton' require 'tmpdir' require 'yaml' module RaPT class TestObserver include Singleton class Data attr_accessor :tests, :errors, :failures def initialize(t, e, f) @tests, @errors, @failures = t, e, f end def total_failures errors + failures end end PLUGIN_RESULTS_FILENAME = 'plugin_test_results.yml' # Restores the test data from it's saved location def self.load_data YAML.load(File.open(File.join(Dir.tmpdir, PLUGIN_RESULTS_FILENAME))) end attr_accessor :result def attach_mediator(mediator) # any time something changes in the test results (i.e. an assertion is encountered, # an error detected, or an assertion fails), we'll know about it. mediator.add_listener(Test::Unit::TestResult::CHANGED, &method(:set_result)) # When the suite of tests is finished, save our test results away. mediator.add_listener(Test::Unit::UI::TestRunnerMediator::FINISHED, &method(:save)) end # Saves test results into a temporary file for later retrieval def save(*args) raise "Empty results!!" if @result.nil? data = Data.new(@result.run_count, @result.error_count, @result.failure_count) File.open(File.join(Dir.tmpdir, PLUGIN_RESULTS_FILENAME), 'w') do |f| f.puts data.to_yaml end end private def set_result(r) @result = r # the result set that is passed through is the same each time. end end end