From 3e09ac0c10c95bb24a08af62393b4f761e2743d0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 24 Aug 2013 09:54:45 +0000 Subject: initial commit --- test/helper.rb | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/helper.rb (limited to 'test/helper.rb') diff --git a/test/helper.rb b/test/helper.rb new file mode 100644 index 0000000..e4643ae --- /dev/null +++ b/test/helper.rb @@ -0,0 +1,76 @@ +# -*- encoding: binary -*- +# Copyright (C) 2013, Eric Wong +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +$stdout.sync = $stderr.sync = Thread.abort_on_exception = true + +# fork-aware coverage data gatherer, see also test/covshow.rb +if ENV["COVERAGE"] + require "coverage" + COVMATCH = %r{/lib/dtas\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)) + ensure + COVTMP.flock(File::LOCK_UN) + end + + Coverage.start + at_exit { __covmerge } +end + +gem 'minitest' +require 'minitest/autorun' +require "tempfile" + +FIFOS = [] +at_exit { FIFOS.each { |(pid,path)| File.unlink(path) if $$ == pid } } +def tmpfifo + tmp = Tempfile.new(%w(dtas-test .fifo)) + path = tmp.path + tmp.close! + assert system(*%W(mkfifo #{path})), "mkfifo #{path}" + FIFOS << [ $$, path ] + 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) -- cgit v1.2.3-24-ge0c7