-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager_spec.rb
210 lines (171 loc) · 6.29 KB
/
manager_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
require_relative './spec_helper'
require 'kubot/manager'
require 'tmpdir'
require_relative Kubot::MOCK_PATH+"/foo"
require_relative Kubot::MOCK_PATH+"/bar"
require_relative Kubot::MOCK_PATH+"/multi"
require_relative Kubot::MOCK_PATH+"/exist"
describe Kubot::Manager do
before :each do
Kernel.stub(:load)
end
subject { Kubot::Manager.new(Kubot::MOCK_LOAD_PATH) }
it 'has accessor for load path' do
subject.load_path.should == Kubot::MOCK_LOAD_PATH
end
describe '.new' do
it 'accepts Array as load_path, bot list' do
expect { Kubot::Manager.new(Kubot::MOCK_LOAD_PATH) }.to_not raise_error
end
end
describe '#locate' do
it 'returns bot file name found in load_path' do
subject.locate(:foo).should == "#{Kubot::MOCK_PATH}/foo.rb"
subject.locate(:bar).should == "#{Kubot::MOCK_PATH}/bar.rb"
subject.locate(:shouldnt_exist).should be_nil
end
end
describe '#load' do
it 'accepts bot name, config, option' do
expect { subject.load(:foo) }.to_not raise_error
expect { subject.load(:foo, {the: :config}) }.to_not raise_error
expect { subject.load(:foo, {the: :config}, {db: nil}) }.to_not raise_error
end
it 'finds bot using #locate start it' do
config= {the: :config}
option= {db: nil}
dummy = "this is a dummy"
subject.should_receive(:locate).with(:foo).and_return(dummy)
FooBot.should_receive(:new).with(config, option).and_return(nil)
Kubot::Bot.stub(:bots_diff).and_yield.and_return([FooBot])
Kernel.should_receive(:load).with(dummy)
File.stub(:mtime){Time.now}
subject.load(:foo, config, option)
end
it 'detects bot using bots_diff' do
Kubot::Bot.should_receive(:bots_diff).and_return([FooBot])
bot = FooBot.new
FooBot.should_receive(:new).and_return(bot)
subject.load(:foo)
end
it 'reloads bot using #reload if it has already loaded' do
subject.should_receive(:reload).with(:foo, {the: :config}, {}).once
subject.load(:foo, {the: :config})
subject.load(:foo, {the: :config})
end
it 'raises error unless bot file is found' do
subject.stub(:locate){nil}
expect { subject.load(:this_is_not_exist) }.to raise_error(Kubot::Manager::BotNotFound)
end
it "doesn't share Bot instances" do
another = Kubot::Manager.new(Kubot::MOCK_LOAD_PATH)
bot = FooBot.new
Kubot::Bot.stub(:bots_diff) { [FooBot] }
FooBot.should_receive(:new).and_return { bot }
subject.load(:foo)
FooBot.should_receive(:new).and_return { bot }
another.load(:foo)
end
it 'starts bots both already exist and added' do
bot_a = [EaBot.new, EaBot.new]
bot_b = EbBot.new
Kubot::Bot.stub(:bots_diff) { [EaBot] }
EaBot.should_receive(:new).and_return(bot_a.shift)
subject.load(:exist)
Kubot::Bot.stub(:bots_diff) { [EbBot] }
EaBot.should_receive(:new).and_return(bot_a.shift)
EbBot.should_receive(:new).and_return(bot_b)
subject.load(:exist)
end
context 'with multiple bots in single file' do
it 'loads all bots in a file and start all of them' do
Kubot::Bot.stub(:bots_diff) { [MaBot, MbBot] }
[MaBot, MbBot].each do |klass|
bot = klass.new
klass.should_receive(:new).and_return(bot)
end
subject.load(:multi)
end
end
end
describe '#unload' do
it 'raises error unless specified bot has loaded' do
expect { subject.unload(:foo) }.to raise_error(Kubot::Manager::BotNotLoaded)
end
it 'calls Bot#finalize to specified bot' do
bot = FooBot.new
Kubot::Bot.stub(:bots_diff) { [FooBot] }
FooBot.stub(:new){bot}
subject.load(:foo)
bot.should_receive(:finalize)
subject.unload(:foo)
end
it "manages loaded bots using class variable, but doesn't share Bot instances" do
bot_a, bot_b = FooBot.new, FooBot.new
another = Kubot::Manager.new(Kubot::MOCK_LOAD_PATH)
Kubot::Bot.stub(:bots_diff) { [FooBot] }
FooBot.stub(:new){ bot_a }
subject.load(:foo)
FooBot.stub(:new){ bot_b }
another.load(:foo)
end
context 'with multiple bots in single file' do
it 'unloads all bots in a file' do
Kubot::Bot.stub(:bots_diff) { [MaBot, MbBot] }
[MaBot, MbBot].each do |klass|
bot = klass.new
bot.should_receive(:finalize)
klass.should_receive(:new).and_return(bot)
end
subject.load(:multi)
subject.unload(:multi)
end
end
end
describe '#reload' do
it 'calls #unload first then call #load' do
subject.should_receive(:unload).with(:not_exist).ordered
subject.should_receive(:load).with(:not_exist, {}, {}).ordered
subject.reload(:not_exist)
end
context 'when calling #unload' do
it 'ignores Kubot::Manager::BotNotLoaded error' do
subject.should_receive(:unload) { raise Kubot::Manager::BotNotLoaded }
subject.stub(:load) {}
expect { subject.reload(:foo) }.to_not raise_error
end
end
it 'calls #load for all plugins which newer than when loaded before' do
path_a = Kubot::MOCK_PATH+"/reload_a.rb"
path_b = Kubot::MOCK_PATH+"/reload_b.rb"
time_a = File.mtime(path_a)
time_b = File.mtime(path_b)
future_a = time_a+100
future_b = time_b+100
File.should_receive(:mtime).with(path_a).and_return(time_a, future_a, future_a)
File.should_receive(:mtime).with(path_b).and_return(time_b, future_b, future_a)
subject.load(:reload_a)
subject.load(:reload_b)
subject.should_receive(:unload).with(:reload_a).twice.ordered
subject.should_receive(:load).with(:reload_a, {}, {}).twice.ordered
subject.should_receive(:unload).with(:reload_b).twice.ordered
subject.should_receive(:load).with(:reload_b, {}, {}).twice.ordered
subject.reload
subject.reload
end
end
describe '#unload_all' do
it 'unloads all bots that has an instance using #unload' do
foo = FooBot.new
FooBot.stub(:new).and_return foo
bar = BarBot.new
BarBot.stub(:new).and_return bar
Kubot::Bot.stub(:bots_diff).and_return([FooBot, BarBot])
subject.load(:foo)
subject.load(:bar)
foo.should_receive(:finalize)
bar.should_receive(:finalize)
subject.unload_all
end
end
end