more diagnostics
[god.git] / test / helper.rb
blob0f4f43c5f9e51c275eb8160b75031b48e14d27b0
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   def self.reset
72     self.watches = nil
73     self.groups = nil
74     self.server = nil
75     self.inited = nil
76     self.host = nil
77     self.port = nil
78     self.pid_file_directory = nil
79     self.registry.reset
80   end
81 end
83 def silence_warnings
84   old_verbose, $VERBOSE = $VERBOSE, nil
85   yield
86 ensure
87   $VERBOSE = old_verbose
88 end
90 def no_stdout
91   old_stdout = $stdout.dup
92   $stdout.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
93   yield
94   $stdout.reopen(old_stdout)
95 end
97 def no_stderr
98   old_stderr = $stderr.dup
99   $stderr.reopen(File.open((PLATFORM =~ /mswin/ ? "NUL" : "/dev/null"), 'w'))
100   yield
101   $stderr.reopen(old_stderr)
104 module Kernel
105   def abort(text)
106     raise SystemExit
107   end
108   def exit(code)
109     raise SystemExit
110   end
113 module Test::Unit::Assertions
114   def assert_abort
115     assert_raise SystemExit do
116       yield
117     end
118   end
121 # This allows you to be a good OOP citizen and honor encapsulation, but
122 # still make calls to private methods (for testing) by doing
124 #   obj.bypass.private_thingie(arg1, arg2)
126 # Which is easier on the eye than
128 #   obj.send(:private_thingie, arg1, arg2)
130 class Object
131   class Bypass
132     instance_methods.each do |m|
133       undef_method m unless m =~ /^__/
134     end
136     def initialize(ref)
137       @ref = ref
138     end
139   
140     def method_missing(sym, *args)
141       @ref.__send__(sym, *args)
142     end
143   end
145   def bypass
146     Bypass.new(self)
147   end