/
guard_spec.rb
166 lines (136 loc) · 4.66 KB
/
guard_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
require 'spec_helper'
require 'guard/guard'
describe Guard do
describe ".setup" do
subject { ::Guard.setup }
it "returns itself for chaining" do
subject.should be ::Guard
end
it "initializes the Guards" do
::Guard.guards.should be_kind_of(Array)
end
it "initializes the options" do
opts = { :my_opts => true }
::Guard.setup(opts).options.should include(:my_opts)
end
it "initializes the listener" do
::Guard.listener.should be_kind_of(Guard::Listener)
end
it "turns on the notifier by default" do
ENV["GUARD_NOTIFY"] = nil
::Guard::Notifier.should_receive(:turn_on)
::Guard.setup(:notify => true)
end
it "turns off the notifier if the notify option is false" do
::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => false)
end
it "turns off the notifier if environment variable GUARD_NOTIFY is false" do
ENV["GUARD_NOTIFY"] = 'false'
::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => true)
end
end
describe ".get_guard_class" do
after do
[:Classname, :DashedClassName, :Inline].each do |const|
Guard.send(:remove_const, const) rescue nil
end
end
it "reports an error if the class is not found" do
::Guard::UI.should_receive(:error)
Guard.get_guard_class('notAGuardClass')
end
context 'with a nested Guard class' do
it "resolves the Guard class from string" do
Guard.should_receive(:require) { |classname|
classname.should == 'guard/classname'
class Guard::Classname
end
}
Guard.get_guard_class('classname').should == Guard::Classname
end
it "resolves the Guard class from symbol" do
Guard.should_receive(:require) { |classname|
classname.should == 'guard/classname'
class Guard::Classname
end
}
Guard.get_guard_class(:classname).should == Guard::Classname
end
end
context 'with a name with dashes' do
it "returns the Guard class" do
Guard.should_receive(:require) { |classname|
classname.should == 'guard/dashed-class-name'
class Guard::DashedClassName
end
}
Guard.get_guard_class('dashed-class-name').should == Guard::DashedClassName
end
end
context 'with an inline Guard class' do
it 'returns the Guard class' do
module Guard
class Inline < Guard
end
end
Guard.should_not_receive(:require)
Guard.get_guard_class('inline').should == Guard::Inline
end
end
end
describe ".locate_guard" do
it "returns the path of a Guard gem" do
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
gem_location = Gem::Specification.find_by_name("guard-rspec").full_gem_path
else
gem_location = Gem.source_index.find_name("guard-rspec").last.full_gem_path
end
Guard.locate_guard('rspec').should == gem_location
end
end
describe ".supervised_task" do
subject { ::Guard.setup }
before(:each) do
@g = mock(Guard::Guard).as_null_object
subject.guards.push(@g)
end
context "with a task that succeed" do
context 'without any arguments' do
before(:each) do
@g.stub!(:regular) { true }
end
it "doesn't fire the Guard" do
lambda { subject.supervised_task(@g, :regular) }.should_not change(subject.guards, :size)
end
it "returns the result of the task" do
::Guard.supervised_task(@g, :regular).should be_true
end
end
context 'with arguments' do
before(:each) do
@g.stub!(:regular_with_arg).with("given_path") { "I'm a success" }
end
it "doesn't fire the Guard" do
lambda { subject.supervised_task(@g, :regular_with_arg, "given_path") }.should_not change(subject.guards, :size)
end
it "returns the result of the task" do
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "I'm a success"
end
end
end
context "with a task that raises an exception" do
before(:each) { @g.stub!(:failing) { raise "I break your system" } }
it "fires the Guard" do
lambda { subject.supervised_task(@g, :failing) }.should change(subject.guards, :size).by(-1)
subject.guards.should_not include(@g)
end
it "returns the exception" do
failing_result = ::Guard.supervised_task(@g, :failing)
failing_result.should be_kind_of(Exception)
failing_result.message.should == 'I break your system'
end
end
end
end