diff options
Diffstat (limited to 'test/helper.rb')
-rw-r--r-- | test/helper.rb | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..ab9a04f --- /dev/null +++ b/test/helper.rb @@ -0,0 +1,115 @@ +# 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 +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) + + 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 + + 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 + COVTMP.rewind + COVTMP.truncate(0) + COVTMP.write(Marshal.dump(prev)) + COVTMP.flock(File::LOCK_UN) + end + + Coverage.start + 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 +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 ] + end + path +end + +require 'tmpdir' +class Dir + require 'fileutils' + def Dir.mktmpdir + begin + d = "#{Dir.tmpdir}/#$$.#{rand}" + Dir.mkdir(d) + rescue Errno::EEXIST + end while true + begin + yield d + ensure + FileUtils.remove_entry(d) + end + end +end unless Dir.respond_to?(:mktmpdir) + +def tmpfile(*args) + tmp = Tempfile.new(*args) + tmp.sync = true + tmp.binmode + tmp +end + +require 'io/wait' +# needed for Rubinius 2.0.0, we only use IO#nread in tests +class IO + # this ignores buffers + def nread + buf = "\0" * 8 + ioctl(0x541B, buf) + buf.unpack("l_")[0] + end +end if ! IO.method_defined?(:nread) && RUBY_PLATFORM =~ /linux/ + +require 'yahns' + +# needed for parallel (MT) tests) +require 'yahns/rack' |