/
dsl_spec.rb
375 lines (297 loc) · 14 KB
/
dsl_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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
require 'spec_helper'
require 'guard/plugin'
describe Guard::Dsl do
let(:local_guardfile) { File.join(Dir.pwd, 'Guardfile') }
let(:home_guardfile) { File.expand_path(File.join('~', '.Guardfile')) }
let(:home_config) { File.expand_path(File.join('~', '.guard.rb')) }
let(:guardfile_evaluator) { double('Guard::Guardfile::Evaluator') }
before do
stub_const 'Guard::Foo', Class.new(Guard::Plugin)
stub_const 'Guard::Bar', Class.new(Guard::Plugin)
stub_const 'Guard::Baz', Class.new(Guard::Plugin)
::Guard.stub(:setup_interactor)
::Guard.setup
end
def self.disable_user_config
before { File.stub(:exist?).with(home_config) { false } }
end
describe '.evaluate_guardfile' do
it 'displays a deprecation warning to the user' do
::Guard::UI.should_receive(:deprecation).with(::Guard::Deprecator::EVALUATE_GUARDFILE_DEPRECATION)
described_class.evaluate_guardfile
end
it 'delegates to Guard::Guardfile::Generator' do
Guard::Guardfile::Evaluator.should_receive(:new).with(:foo => 'bar') { guardfile_evaluator }
guardfile_evaluator.should_receive(:evaluate_guardfile)
described_class.evaluate_guardfile(:foo => 'bar')
end
end
describe '#ignore_paths' do
disable_user_config
it 'adds the paths to the listener\'s ignore_paths' do
::Guard::UI.should_receive(:deprecation).with(Guard::Deprecator::DSL_METHOD_IGNORE_PATHS_DEPRECATION)
described_class.evaluate_guardfile(:guardfile_contents => 'ignore_paths \'foo\', \'bar\'')
end
end
describe '#ignore' do
disable_user_config
let(:listener) { stub }
it 'add ignored regexps to the listener' do
::Guard.stub(:listener) { listener }
::Guard.listener.should_receive(:ignore).with(/^foo/,/bar/) { listener }
::Guard.should_receive(:listener=).with(listener)
described_class.evaluate_guardfile(:guardfile_contents => 'ignore %r{^foo}, /bar/')
end
end
describe '#ignore!' do
disable_user_config
let(:listener) { stub }
it 'replace ignored regexps in the listener' do
::Guard.stub(:listener) { listener }
::Guard.listener.should_receive(:ignore!).with(/^foo/,/bar/) { listener }
::Guard.should_receive(:listener=).with(listener)
described_class.evaluate_guardfile(:guardfile_contents => 'ignore! %r{^foo}, /bar/')
end
end
describe '#filter' do
disable_user_config
let(:listener) { stub }
it 'add ignored regexps to the listener' do
::Guard.stub(:listener) { listener }
::Guard.listener.should_receive(:filter).with(/.txt$/, /.*.zip/) { listener }
::Guard.should_receive(:listener=).with(listener)
described_class.evaluate_guardfile(:guardfile_contents => 'filter %r{.txt$}, /.*.zip/')
end
end
describe '#filter!' do
disable_user_config
let(:listener) { stub }
it 'replace ignored regexps in the listener' do
::Guard.stub(:listener) { listener }
::Guard.listener.should_receive(:filter!).with(/.txt$/, /.*.zip/) { listener }
::Guard.should_receive(:listener=).with(listener)
described_class.evaluate_guardfile(:guardfile_contents => 'filter! %r{.txt$}, /.*.zip/')
end
end
describe '#notification' do
disable_user_config
it 'adds a notification to the notifier' do
::Guard::Notifier.should_receive(:add_notifier).with(:growl, { :silent => false })
described_class.evaluate_guardfile(:guardfile_contents => 'notification :growl')
end
it 'adds multiple notification to the notifier' do
::Guard::Notifier.should_receive(:add_notifier).with(:growl, { :silent => false })
::Guard::Notifier.should_receive(:add_notifier).with(:ruby_gntp, { :host => '192.168.1.5', :silent => false })
described_class.evaluate_guardfile(:guardfile_contents => "notification :growl\nnotification :ruby_gntp, :host => '192.168.1.5'")
end
end
describe '#interactor' do
disable_user_config
it 'disables the interactions with :off' do
::Guard::UI.should_not_receive(:deprecation).with(Guard::Deprecator::DSL_METHOD_INTERACTOR_DEPRECATION)
described_class.evaluate_guardfile(:guardfile_contents => 'interactor :off')
Guard::Interactor.enabled.should be_false
end
it 'shows a deprecation for symbols other than :off' do
::Guard::UI.should_receive(:deprecation).with(Guard::Deprecator::DSL_METHOD_INTERACTOR_DEPRECATION)
described_class.evaluate_guardfile(:guardfile_contents => 'interactor :coolline')
end
it 'passes the options to the interactor' do
::Guard::UI.should_not_receive(:deprecation).with(Guard::Deprecator::DSL_METHOD_INTERACTOR_DEPRECATION)
described_class.evaluate_guardfile(:guardfile_contents => 'interactor :option1 => \'a\', :option2 => 123')
Guard::Interactor.options.should include({ :option1 => 'a', :option2 => 123 })
end
end
describe '#group' do
disable_user_config
context 'no plugins in group' do
it 'displays an error' do
::Guard::UI.should_receive(:error).with("No Guard plugins found in the group 'w', please add at least one.")
described_class.evaluate_guardfile(:guardfile_contents => guardfile_string_with_empty_group)
end
end
it 'evaluates all groups' do
::Guard.should_receive(:add_plugin).with(:pow, { :watchers => [], :callbacks => [], :group => :default })
::Guard.should_receive(:add_plugin).with(:test, { :watchers => [], :callbacks => [], :group => :w })
::Guard.should_receive(:add_plugin).with(:rspec, { :watchers => [], :callbacks => [], :group => :x })
::Guard.should_receive(:add_plugin).with(:ronn, { :watchers => [], :callbacks => [], :group => :x })
::Guard.should_receive(:add_plugin).with(:less, { :watchers => [], :callbacks => [], :group => :y })
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
end
end
describe '#guard' do
disable_user_config
it 'loads a guard specified as a quoted string from the DSL' do
::Guard.should_receive(:add_plugin).with('test', { :watchers => [], :callbacks => [], :group => :default })
described_class.evaluate_guardfile(:guardfile_contents => 'guard \'test\'')
end
it 'loads a guard specified as a double quoted string from the DSL' do
::Guard.should_receive(:add_plugin).with('test', { :watchers => [], :callbacks => [], :group => :default })
described_class.evaluate_guardfile(:guardfile_contents => 'guard "test"')
end
it 'loads a guard specified as a symbol from the DSL' do
::Guard.should_receive(:add_plugin).with(:test, { :watchers => [], :callbacks => [], :group => :default })
described_class.evaluate_guardfile(:guardfile_contents => 'guard :test')
end
it 'loads a guard specified as a symbol and called with parens from the DSL' do
::Guard.should_receive(:add_plugin).with(:test, { :watchers => [], :callbacks => [], :group => :default })
described_class.evaluate_guardfile(:guardfile_contents => 'guard(:test)')
end
it 'receives options when specified, from normal arg' do
::Guard.should_receive(:add_plugin).with('test', { :watchers => [], :callbacks => [], :opt_a => 1, :opt_b => 'fancy', :group => :default })
described_class.evaluate_guardfile(:guardfile_contents => 'guard \'test\', :opt_a => 1, :opt_b => \'fancy\'')
end
end
describe '#watch' do
disable_user_config
it 'should receive watchers when specified' do
::Guard.should_receive(:add_plugin).with(:dummy, { :watchers => [anything, anything], :callbacks => [], :group => :default }) do |_, options|
options[:watchers].size.should eq 2
options[:watchers][0].pattern.should eq 'a'
options[:watchers][0].action.call.should eq proc { 'b' }.call
options[:watchers][1].pattern.should eq 'c'
options[:watchers][1].action.should be_nil
end
described_class.evaluate_guardfile(:guardfile_contents => '
guard :dummy do
watch(\'a\') { \'b\' }
watch(\'c\')
end')
end
end
describe '#callback' do
it 'creates callbacks for the guard' do
class MyCustomCallback
def self.call(plugin, event, args)
# do nothing
end
end
::Guard.should_receive(:add_plugin).with(:dummy, { :watchers => [], :callbacks => [anything, anything], :group => :default }) do |_, options|
options[:callbacks].should have(2).items
options[:callbacks][0][:events].should eq :start_end
options[:callbacks][0][:listener].call(Guard::Dummy, :start_end, 'foo').should eq 'Guard::Dummy executed \'start_end\' hook with foo!'
options[:callbacks][1][:events].should eq [:start_begin, :run_all_begin]
options[:callbacks][1][:listener].should eq MyCustomCallback
end
described_class.evaluate_guardfile(:guardfile_contents => '
guard :dummy do
callback(:start_end) { |plugin, event, args| "#{plugin} executed \'#{event}\' hook with #{args}!" }
callback(MyCustomCallback, [:start_begin, :run_all_begin])
end')
end
end
describe '#logger' do
after { Guard::UI.options = { level: :info, template: ':time - :severity - :message', time_format: '%H:%M:%S' } }
context 'with valid options' do
it 'sets the logger log level' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :level => :error')
Guard::UI.options.level.should eq :error
end
it 'sets the logger log level and convert to a symbol' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :level => \'error\'')
Guard::UI.options.level.should eq :error
end
it 'sets the logger template' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :template => \':message - :severity\'')
Guard::UI.options.template.should eq ':message - :severity'
end
it 'sets the logger time format' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :time_format => \'%Y\'')
Guard::UI.options.time_format.should eq '%Y'
end
it 'sets the logger only filter from a symbol' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :only => :cucumber')
Guard::UI.options.only.should eq(/cucumber/i)
end
it 'sets the logger only filter from a string' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :only => \'jasmine\'')
Guard::UI.options.only.should eq(/jasmine/i)
end
it 'sets the logger only filter from an array of symbols and string' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :only => [:rspec, \'cucumber\']')
Guard::UI.options.only.should eq(/rspec|cucumber/i)
end
it 'sets the logger except filter from a symbol' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :except => :jasmine')
Guard::UI.options.except.should eq(/jasmine/i)
end
it 'sets the logger except filter from a string' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :except => \'jasmine\'')
Guard::UI.options.except.should eq(/jasmine/i)
end
it 'sets the logger except filter from an array of symbols and string' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :except => [:rspec, \'cucumber\', :jasmine]')
Guard::UI.options.except.should eq(/rspec|cucumber|jasmine/i)
end
end
context 'with invalid options' do
context 'for the log level' do
it 'shows a warning' do
Guard::UI.should_receive(:warning).with 'Invalid log level `baz` ignored. Please use either :debug, :info, :warn or :error.'
described_class.evaluate_guardfile(:guardfile_contents => 'logger :level => :baz')
end
it 'does not set the invalid value' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :level => :baz')
Guard::UI.options.level.should eq :info
end
end
context 'when having both the :only and :except options' do
it 'shows a warning' do
Guard::UI.should_receive(:warning).with 'You cannot specify the logger options :only and :except at the same time.'
described_class.evaluate_guardfile(:guardfile_contents => 'logger :only => :jasmine, :except => :rspec')
end
it 'removes the options' do
described_class.evaluate_guardfile(:guardfile_contents => 'logger :only => :jasmine, :except => :rspec')
Guard::UI.options.only.should be_nil
Guard::UI.options.except.should be_nil
end
end
end
end
describe '#scope' do
before do
::Guard.add_plugin(:foo)
::Guard.add_plugin(:bar)
::Guard.add_plugin(:baz)
::Guard.setup_scope(plugins: nil, groups: nil)
end
it 'does use the DSL scope plugin' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugin => :baz')
::Guard.scope[:plugins].should eq [::Guard.plugins(:baz)]
::Guard.setup_scope(plugins: [], groups: [])
::Guard.scope[:plugins].should eq [::Guard.plugins(:baz)]
end
it 'does use the DSL scope plugins' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :plugins => [:foo, :bar]')
::Guard.scope[:plugins].should eq [::Guard.plugins(:foo), ::Guard.plugins(:bar)]
end
it 'does use the DSL scope group' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :group => :baz')
::Guard.scope[:groups].should eq [::Guard.groups(:baz)]
end
it 'does use the DSL scope groups' do
described_class.evaluate_guardfile(:guardfile_contents => 'scope :groups => [:foo, :bar]')
::Guard.scope[:groups].should eq [::Guard.groups(:foo), ::Guard.groups(:bar)]
end
end
private
def valid_guardfile_string
'
notification :growl
guard :pow
group :w do
guard :test
end
group :x, :halt_on_fail => true do
guard :rspec
guard :ronn
end
group :y do
guard :less
end
'
end
def guardfile_string_with_empty_group
'group :w'
end
end