Module: RailsTestServing::Utilities
- Included in:
- Server
- Defined in:
- lib/rails_test_serving/utilities.rb
Instance Method Summary collapse
- #capture_standard_stream(name) ⇒ Object
- #capture_testrunner_result ⇒ Object
- #find_index_by_pattern(enumerable, pattern) ⇒ Object
-
#fix_objectspace_collector ⇒ Object
The stock ObjectSpace collector collects every single class that inherits from Test::Unit, including those which have just been unassigned from their constant and not yet garbage collected.
- #log(message, stream = $stdout) ⇒ Object
-
#set_default_testrunner_stream(io) ⇒ Object
The default output stream of TestRunner is STDOUT which cannot be captured and, as a consequence, neither can TestRunner output when not instantiated explicitely.
- #shorten_path(path) ⇒ Object
Instance Method Details
#capture_standard_stream(name) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/rails_test_serving/utilities.rb', line 20 def capture_standard_stream(name) eval("old, $std#{name} = $std#{name}, StringIO.new") begin yield return eval("$std#{name}").string ensure eval("$std#{name} = old") end end |
#capture_testrunner_result ⇒ Object
30 31 32 33 |
# File 'lib/rails_test_serving/utilities.rb', line 30 def capture_testrunner_result set_default_testrunner_stream(io = StringIO.new) { yield } io.string end |
#find_index_by_pattern(enumerable, pattern) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/rails_test_serving/utilities.rb', line 95 def find_index_by_pattern(enumerable, pattern) enumerable.each_with_index do |element, index| return index if pattern === element end nil end |
#fix_objectspace_collector ⇒ Object
The stock ObjectSpace collector collects every single class that inherits from Test::Unit, including those which have just been unassigned from their constant and not yet garbage collected. This method fixes that behaviour by filtering out these soon-to-be-garbage-collected classes.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rails_test_serving/utilities.rb', line 65 def fix_objectspace_collector require 'test/unit/collector/objectspace' Test::Unit::Collector::ObjectSpace.class_eval do alias_method :old_collect, :collect def collect(name) tests = [] ConstantManagement.subclasses_of(Test::Unit::TestCase, :legit => true).each { |klass| add_suite(tests, klass.suite) } suite = Test::Unit::TestSuite.new(name) sort(tests).each { |t| suite << t } suite end end begin return yield ensure Test::Unit::Collector::ObjectSpace.class_eval do alias_method :collect, :old_collect remove_method :old_collect end end end |
#log(message, stream = $stdout) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rails_test_serving/utilities.rb', line 3 def log(, stream=$stdout) print = lambda do |str| stream.print(str) stream.flush end print[] if block_given? result = nil elapsed = Benchmark.realtime do result = yield end print[" (%d ms)\n" % (elapsed * 1000)] result end end |
#set_default_testrunner_stream(io) ⇒ Object
The default output stream of TestRunner is STDOUT which cannot be captured and, as a consequence, neither can TestRunner output when not instantiated explicitely. The following method can change the default output stream argument so that it can be set to a stream that can be captured instead.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rails_test_serving/utilities.rb', line 39 def set_default_testrunner_stream(io) require 'test/unit/ui/console/testrunner' Test::Unit::UI::Console::TestRunner.class_eval do alias_method :old_initialize, :initialize def initialize(suite, output_level, io=Thread.current["test_runner_io"]) old_initialize(suite, output_level, io) end end Thread.current["test_runner_io"] = io begin return yield ensure Thread.current["test_runner_io"] = nil Test::Unit::UI::Console::TestRunner.class_eval do alias_method :initialize, :old_initialize remove_method :old_initialize end end end |
#shorten_path(path) ⇒ Object
89 90 91 92 93 |
# File 'lib/rails_test_serving/utilities.rb', line 89 def shorten_path(path) shortenable, base = File.(path), File.(Dir.pwd) attempt = shortenable.sub(/^#{Regexp.escape base + File::SEPARATOR}/, '') attempt.length < path.length ? attempt : path end |