fix process#alive? to not raise on no such file
[god.git] / test / helper.rb
blob465d81de1a308f25a652f55176bc9ddb3584a4ad
1 require File.join(File.dirname(__FILE__), *%w[.. lib god])
3 require 'test/unit'
4 require 'set'
6 include God
8 if RUBY_PLATFORM =~ /linux/i && Process.uid != 0
9   abort <<-EOF
10 *********************************************************************
11 *                                                                   *
12 *    You need to run these tests as root (netlink requires it)      *
13 *                                                                   *
14 *********************************************************************
15 EOF
16 end
18 begin
19   require 'mocha'
20 rescue LoadError
21   unless gems ||= false
22     require 'rubygems'
23     gems = true
24     retry
25   else
26     abort "=> You need the Mocha gem to run these tests."
27   end
28 end
30 module God
31   module Conditions
32     class FakeCondition < Condition
33       def test
34         true
35       end
36     end
37   
38     class FakePollCondition < PollCondition
39       def test
40         true
41       end
42     end
43   
44     class FakeEventCondition < EventCondition
45       def test
46         true
47       end
48     end
49   end
50   
51   module Behaviors
52     class FakeBehavior < Behavior
53     end
54   end
55   
56   class << self
57     alias :at_exit_orig :at_exit
58   end
59   
60   def self.at_exit
61     # disable at_exit
62   end
63   
64   def self.reset
65     self.watches = nil
66     self.groups = nil
67     self.server = nil
68     self.inited = nil
69     self.host = nil
70     self.port = nil
71     self.pid_file_directory = nil
72     self.registry.reset
73   end
74 end
76 def silence_warnings
77   old_verbose, $VERBOSE = $VERBOSE, nil
78   yield
79 ensure
80   $VERBOSE = old_verbose
81 end
83 def no_stdout
84   old_stdout = $stdout.dup
85   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
86   yield
87   $stdout.reopen(old_stdout)
88 end
90 def no_stderr
91   old_stderr = $stderr.dup
92   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
93   yield
94   $stderr.reopen(old_stderr)
95 end
97 module Kernel
98   def abort(text)
99     raise SystemExit
100   end
103 module Test::Unit::Assertions
104   def assert_abort
105     assert_raise SystemExit do
106       no_stderr do
107         yield
108       end
109     end
110   end
113 # This allows you to be a good OOP citizen and honor encapsulation, but
114 # still make calls to private methods (for testing) by doing
116 #   obj.bypass.private_thingie(arg1, arg2)
118 # Which is easier on the eye than
120 #   obj.send(:private_thingie, arg1, arg2)
122 class Object
123   class Bypass
124     instance_methods.each do |m|
125       undef_method m unless m =~ /^__/
126     end
128     def initialize(ref)
129       @ref = ref
130     end
131   
132     def method_missing(sym, *args)
133       @ref.__send__(sym, *args)
134     end
135   end
137   def bypass
138     Bypass.new(self)
139   end