minor test fixes
[god.git] / test / helper.rb
blobffffe40c2f69e02b4a74d36ea9b1725f49386455
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 register
46       end
47       def deregister
48       end
49     end
50   end
51   
52   module Behaviors
53     class FakeBehavior < Behavior
54       def before_start
55         'foo'
56       end
57       def after_start
58         'bar'
59       end
60     end
61   end
62   
63   module Contacts
64     class FakeContact < Contact
65     end
66     
67     class InvalidContact
68     end
69   end
70   
71   class << self
72     alias :at_exit_orig :at_exit
73   end
74   
75   def self.at_exit
76     # disable at_exit
77   end
78   
79   def self.reset
80     self.watches = nil
81     self.groups = nil
82     self.server = nil
83     self.inited = nil
84     self.host = nil
85     self.port = nil
86     self.pid_file_directory = nil
87     self.registry.reset
88   end
89 end
91 def silence_warnings
92   old_verbose, $VERBOSE = $VERBOSE, nil
93   yield
94 ensure
95   $VERBOSE = old_verbose
96 end
98 def no_stdout
99   old_stdout = $stdout.dup
100   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
101   yield
102   $stdout.reopen(old_stdout)
105 def no_stderr
106   old_stderr = $stderr.dup
107   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
108   yield
109   $stderr.reopen(old_stderr)
112 module Kernel
113   def abort(text)
114     raise SystemExit
115   end
118 module Test::Unit::Assertions
119   def assert_abort
120     assert_raise SystemExit do
121       no_stderr do
122         yield
123       end
124     end
125   end
128 # This allows you to be a good OOP citizen and honor encapsulation, but
129 # still make calls to private methods (for testing) by doing
131 #   obj.bypass.private_thingie(arg1, arg2)
133 # Which is easier on the eye than
135 #   obj.send(:private_thingie, arg1, arg2)
137 class Object
138   class Bypass
139     instance_methods.each do |m|
140       undef_method m unless m =~ /^__/
141     end
143     def initialize(ref)
144       @ref = ref
145     end
146   
147     def method_missing(sym, *args)
148       @ref.__send__(sym, *args)
149     end
150   end
152   def bypass
153     Bypass.new(self)
154   end