test/helper.rb in yahns-0.0.0 vs test/helper.rb in yahns-0.0.1

- old
+ new

@@ -1,80 +1,80 @@ # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) $stdout.sync = $stderr.sync = Thread.abort_on_exception = true +$-w = true if RUBY_VERSION.to_f >= 2.0 require 'thread' # Global Test Lock, to protect: # Process.wait*, Dir.chdir, ENV, trap, require, etc... GTL = Mutex.new # fork-aware coverage data gatherer, see also test/covshow.rb if ENV["COVERAGE"] require "coverage" COVMATCH = %r{/lib/yahns\b.*rb\z} - COVTMP = File.open("coverage.dump", IO::CREAT|IO::RDWR) - COVTMP.binmode - COVTMP.sync = true def __covmerge res = Coverage.result - # we own this file (at least until somebody tries to use NFS :x) - COVTMP.flock(File::LOCK_EX) + # do not create the file, Makefile does htis before any tests run + File.open("coverage.dump", IO::RDWR) do |covtmp| + covtmp.binmode + covtmp.sync = true - COVTMP.rewind - prev = COVTMP.read - prev = prev.empty? ? {} : Marshal.load(prev) - res.each do |filename, counts| - # filter out stuff that's not in our project - COVMATCH =~ filename or next + # we own this file (at least until somebody tries to use NFS :x) + covtmp.flock(File::LOCK_EX) - merge = prev[filename] || [] - merge = merge - counts.each_with_index do |count, i| - count or next - merge[i] = (merge[i] || 0) + count + prev = covtmp.read + prev = prev.empty? ? {} : Marshal.load(prev) + res.each do |filename, counts| + # filter out stuff that's not in our project + COVMATCH =~ filename or next + + merge = prev[filename] || [] + merge = merge + counts.each_with_index do |count, i| + count or next + merge[i] = (merge[i] || 0) + count + end + prev[filename] = merge end - prev[filename] = merge + covtmp.rewind + covtmp.truncate(0) + covtmp.write(Marshal.dump(prev)) + covtmp.flock(File::LOCK_UN) end - COVTMP.rewind - COVTMP.truncate(0) - COVTMP.write(Marshal.dump(prev)) - COVTMP.flock(File::LOCK_UN) end Coverage.start + # we need to nest at_exit to fire after minitest runs at_exit { at_exit { __covmerge } } end gem 'minitest' -require 'minitest/autorun' -require "tempfile" - -Testcase = begin - Minitest::Test # minitest 5 -rescue NameError - Minitest::Unit::TestCase # minitest 4 +begin # favor minitest 5 + require 'minitest' + Testcase = Minitest::Test + mtobj = Minitest +rescue NameError, LoadError # but support minitest 4 + require 'minitest/unit' + Testcase = Minitest::Unit::TestCase + mtobj = MiniTest::Unit.new end -FIFOS = [] -def tmpfifo - tmp = Tempfile.new(%w(yahns-test .fifo)) - path = tmp.path - tmp.close! - assert system(*%W(mkfifo #{path})), "mkfifo #{path}" - - GTL.synchronize do - if FIFOS.empty? - at_exit do - FIFOS.each { |(pid,_path)| File.unlink(_path) if $$ == pid } - end - end - FIFOS << [ $$, path ] +# Not using minitest/autorun because that doesn't guard against redundant +# extra runs with fork. We cannot use exit! in the tests either +# (since users/apps hosted on yahns _should_ expect exit, not exit!). +TSTART_PID = $$ +at_exit do + # skipping @@after_run stuff in minitest since we don't need it + case $! + when nil, SystemExit + mtobj.run(ARGV) if $$ == TSTART_PID end - path end +require "tempfile" require 'tmpdir' class Dir require 'fileutils' def Dir.mktmpdir begin