summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/utils/script_spec.rb
blob: 20b5d293b07b2ec36a21f510a2326f73883a05b4 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
require 'spec_helper'
require 'mspec/utils/script'
require 'mspec/runner/mspec'
require 'mspec/runner/filters'
require 'mspec/runner/actions/filter'

describe MSpecScript, ".config" do
  it "returns a Hash" do
    MSpecScript.config.should be_kind_of(Hash)
  end
end

describe MSpecScript, ".set" do
  it "sets the config hash key, value" do
    MSpecScript.set :a, 10
    MSpecScript.config[:a].should == 10
  end
end

describe MSpecScript, ".get" do
  it "gets the config hash value for a key" do
    MSpecScript.set :a, 10
    MSpecScript.get(:a).should == 10
  end
end

describe MSpecScript, "#config" do
  it "returns the MSpecScript config hash" do
    MSpecScript.set :b, 5
    MSpecScript.new.config[:b].should == 5
  end

  it "returns the MSpecScript config hash from subclasses" do
    class MSSClass < MSpecScript; end
    MSpecScript.set :b, 5
    MSSClass.new.config[:b].should == 5
  end
end

describe MSpecScript, "#load_default" do
  before :all do
    @verbose = $VERBOSE
    $VERBOSE = nil
  end

  after :all do
    $VERBOSE = @verbose
  end

  before :each do
    @version = RUBY_VERSION
    if Object.const_defined? :RUBY_ENGINE
      @engine = Object.const_get :RUBY_ENGINE
    end
    @script = MSpecScript.new
    MSpecScript.stub(:new).and_return(@script)
  end

  after :each do
    Object.const_set :RUBY_VERSION, @version
    Object.const_set :RUBY_ENGINE, @engine if @engine
  end

  it "attempts to load 'default.mspec'" do
    @script.stub(:try_load)
    @script.should_receive(:try_load).with('default.mspec').and_return(true)
    @script.load_default
  end

  it "attempts to load a config file based on RUBY_ENGINE and RUBY_VERSION" do
    Object.const_set :RUBY_ENGINE, "ybur"
    Object.const_set :RUBY_VERSION, "1.8.9"
    default = "ybur.1.8.mspec"
    @script.should_receive(:try_load).with('default.mspec').and_return(false)
    @script.should_receive(:try_load).with(default)
    @script.should_receive(:try_load).with('ybur.mspec')
    @script.load_default
  end
end

describe MSpecScript, ".main" do
  before :each do
    @script = double("MSpecScript").as_null_object
    MSpecScript.stub(:new).and_return(@script)
    # Do not require full mspec as it would conflict with RSpec
    MSpecScript.should_receive(:require).with('mspec')
  end

  it "creates an instance of MSpecScript" do
    MSpecScript.should_receive(:new).and_return(@script)
    MSpecScript.main
  end

  it "attempts to load the default config" do
    @script.should_receive(:load_default)
    MSpecScript.main
  end

  it "attempts to load the '~/.mspecrc' script" do
    @script.should_receive(:try_load).with('~/.mspecrc')
    MSpecScript.main
  end

  it "calls the #options method on the script" do
    @script.should_receive(:options)
    MSpecScript.main
  end

  it "calls the #signals method on the script" do
    @script.should_receive(:signals)
    MSpecScript.main
  end

  it "calls the #register method on the script" do
    @script.should_receive(:register)
    MSpecScript.main
  end

  it "calls the #setup_env method on the script" do
    @script.should_receive(:setup_env)
    MSpecScript.main
  end

  it "calls the #run method on the script" do
    @script.should_receive(:run)
    MSpecScript.main
  end
end

describe MSpecScript, "#initialize" do
  before :each do
    @config = MSpecScript.new.config
  end

  it "sets the default config values" do
    @config[:formatter].should  == nil
    @config[:includes].should   == []
    @config[:excludes].should   == []
    @config[:patterns].should   == []
    @config[:xpatterns].should  == []
    @config[:tags].should       == []
    @config[:xtags].should      == []
    @config[:atags].should      == []
    @config[:astrings].should   == []
    @config[:abort].should      == true
    @config[:config_ext].should == '.mspec'
  end
end

describe MSpecScript, "#load" do
  before :each do
    File.stub(:exist?).and_return(false)
    @script = MSpecScript.new
    @file = "default.mspec"
    @base = "default"
  end

  it "attempts to locate the file through the expanded path name" do
    File.should_receive(:expand_path).with(@file, ".").and_return(@file)
    File.should_receive(:exist?).with(@file).and_return(true)
    Kernel.should_receive(:load).with(@file).and_return(:loaded)
    @script.load(@file).should == :loaded
  end

  it "appends config[:config_ext] to the name and attempts to locate the file through the expanded path name" do
    File.should_receive(:expand_path).with(@base, ".").and_return(@base)
    File.should_receive(:expand_path).with(@base, "spec").and_return(@base)
    File.should_receive(:expand_path).with(@file, ".").and_return(@file)
    File.should_receive(:exist?).with(@base).and_return(false)
    File.should_receive(:exist?).with(@file).and_return(true)
    Kernel.should_receive(:load).with(@file).and_return(:loaded)
    @script.load(@base).should == :loaded
  end

  it "attemps to locate the file in '.'" do
    path = File.expand_path @file, "."
    File.should_receive(:exist?).with(path).and_return(true)
    Kernel.should_receive(:load).with(path).and_return(:loaded)
    @script.load(@file).should == :loaded
  end

  it "appends config[:config_ext] to the name and attempts to locate the file in '.'" do
    path = File.expand_path @file, "."
    File.should_receive(:exist?).with(path).and_return(true)
    Kernel.should_receive(:load).with(path).and_return(:loaded)
    @script.load(@base).should == :loaded
  end

  it "attemps to locate the file in 'spec'" do
    path = File.expand_path @file, "spec"
    File.should_receive(:exist?).with(path).and_return(true)
    Kernel.should_receive(:load).with(path).and_return(:loaded)
    @script.load(@file).should == :loaded
  end

  it "appends config[:config_ext] to the name and attempts to locate the file in 'spec'" do
    path = File.expand_path @file, "spec"
    File.should_receive(:exist?).with(path).and_return(true)
    Kernel.should_receive(:load).with(path).and_return(:loaded)
    @script.load(@base).should == :loaded
  end

  it "loads a given file only once" do
    path = File.expand_path @file, "spec"
    File.should_receive(:exist?).with(path).and_return(true)
    Kernel.should_receive(:load).once.with(path).and_return(:loaded)
    @script.load(@base).should == :loaded
    @script.load(@base).should == true
  end
end

describe MSpecScript, "#custom_options" do
  before :each do
    @script = MSpecScript.new
  end

  after :each do
  end

  it "prints 'None'" do
    options = double("options")
    options.should_receive(:doc).with("   No custom options registered")
    @script.custom_options options
  end
end

describe MSpecScript, "#register" do
  before :each do
    @script = MSpecScript.new

    @formatter = double("formatter").as_null_object
    @script.config[:formatter] = @formatter
  end

  it "creates and registers the formatter" do
    @formatter.should_receive(:new).and_return(@formatter)
    @formatter.should_receive(:register)
    @script.register
  end

  it "does not register the formatter if config[:formatter] is false" do
    @script.config[:formatter] = false
    @script.register
  end

  it "calls #custom_register" do
    @script.should_receive(:custom_register)
    @script.register
  end

  it "registers :formatter with the formatter instance" do
    @formatter.stub(:new).and_return(@formatter)
    MSpec.should_receive(:store).with(:formatter, @formatter)
    @script.register
  end

  it "does not register :formatter if config[:formatter] is false" do
    @script.config[:formatter] = false
    MSpec.should_not_receive(:store)
    @script.register
  end
end

describe MSpecScript, "#register" do
  before :each do
    @script = MSpecScript.new

    @formatter = double("formatter").as_null_object
    @script.config[:formatter] = @formatter

    @filter = double("filter")
    @filter.should_receive(:register)

    @ary = ["some", "spec"]
  end

  it "creates and registers a MatchFilter for include specs" do
    MatchFilter.should_receive(:new).with(:include, *@ary).and_return(@filter)
    @script.config[:includes] = @ary
    @script.register
  end

  it "creates and registers a MatchFilter for excluded specs" do
    MatchFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter)
    @script.config[:excludes] = @ary
    @script.register
  end

  it "creates and registers a RegexpFilter for include specs" do
    RegexpFilter.should_receive(:new).with(:include, *@ary).and_return(@filter)
    @script.config[:patterns] = @ary
    @script.register
  end

  it "creates and registers a RegexpFilter for excluded specs" do
    RegexpFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter)
    @script.config[:xpatterns] = @ary
    @script.register
  end

  it "creates and registers a TagFilter for include specs" do
    TagFilter.should_receive(:new).with(:include, *@ary).and_return(@filter)
    @script.config[:tags] = @ary
    @script.register
  end

  it "creates and registers a TagFilter for excluded specs" do
    TagFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter)
    @script.config[:xtags] = @ary
    @script.register
  end

  it "creates and registers a ProfileFilter for include specs" do
    ProfileFilter.should_receive(:new).with(:include, *@ary).and_return(@filter)
    @script.config[:profiles] = @ary
    @script.register
  end

  it "creates and registers a ProfileFilter for excluded specs" do
    ProfileFilter.should_receive(:new).with(:exclude, *@ary).and_return(@filter)
    @script.config[:xprofiles] = @ary
    @script.register
  end
end

describe MSpecScript, "#signals" do
  before :each do
    @script = MSpecScript.new
    @abort = @script.config[:abort]
  end

  after :each do
    @script.config[:abort] = @abort
  end

  it "traps the INT signal if config[:abort] is true" do
    Signal.should_receive(:trap).with("INT")
    @script.config[:abort] = true
    @script.signals
  end

  it "does not trap the INT signal if config[:abort] is not true" do
    Signal.should_not_receive(:trap).with("INT")
    @script.config[:abort] = false
    @script.signals
  end
end

describe MSpecScript, "#entries" do
  before :each do
    @script = MSpecScript.new

    File.stub(:expand_path).and_return("name")
    File.stub(:file?).and_return(false)
    File.stub(:directory?).and_return(false)
  end

  it "returns the pattern in an array if it is a file" do
    File.should_receive(:expand_path).with("file").and_return("file/expanded.rb")
    File.should_receive(:file?).with("file/expanded.rb").and_return(true)
    @script.entries("file").should == ["file/expanded.rb"]
  end

  it "returns Dir['pattern/**/*_spec.rb'] if pattern is a directory" do
    File.should_receive(:directory?).with("name").and_return(true)
    File.stub(:expand_path).and_return("name","name/**/*_spec.rb")
    Dir.should_receive(:[]).with("name/**/*_spec.rb").and_return(["dir1", "dir2"])
    @script.entries("name").should == ["dir1", "dir2"]
  end

  it "aborts if pattern cannot be resolved to a file nor a directory" do
    @script.should_receive(:abort)
    @script.entries("pattern")
  end

  describe "with config[:prefix] set" do
    before :each do
      prefix = "prefix/dir"
      @script.config[:prefix] = prefix
      @name = prefix + "/name"
    end

    it "returns the pattern in an array if it is a file" do
      name = "#{@name}.rb"
      File.should_receive(:expand_path).with(name).and_return(name)
      File.should_receive(:file?).with(name).and_return(true)
      @script.entries("name.rb").should == [name]
    end

    it "returns Dir['pattern/**/*_spec.rb'] if pattern is a directory" do
      File.stub(:expand_path).and_return(@name, @name+"/**/*_spec.rb")
      File.should_receive(:directory?).with(@name).and_return(true)
      Dir.should_receive(:[]).with(@name + "/**/*_spec.rb").and_return(["dir1", "dir2"])
      @script.entries("name").should == ["dir1", "dir2"]
    end

    it "aborts if pattern cannot be resolved to a file nor a directory" do
      @script.should_receive(:abort)
      @script.entries("pattern")
    end
  end
end

describe MSpecScript, "#files" do
  before :each do
    @script = MSpecScript.new
  end

  it "accumlates the values returned by #entries" do
    @script.should_receive(:entries).and_return(["file1"], ["file2"])
    @script.files(["a", "b"]).should == ["file1", "file2"]
  end

  it "strips a leading '^' and removes the values returned by #entries" do
    @script.should_receive(:entries).and_return(["file1"], ["file2"], ["file1"])
    @script.files(["a", "b", "^a"]).should == ["file2"]
  end

  it "processes the array elements in order" do
    @script.should_receive(:entries).and_return(["file1"], ["file1"], ["file2"])
    @script.files(["^a", "a", "b"]).should == ["file1", "file2"]
  end
end

describe MSpecScript, "#files" do
  before :each do
    MSpecScript.set :files, ["file1", "file2"]

    @script = MSpecScript.new
  end

  after :each do
    MSpecScript.config.delete :files
  end

  it "looks up items with leading ':' in the config object" do
    @script.should_receive(:entries).and_return(["file1"], ["file2"])
    @script.files([":files"]).should == ["file1", "file2"]
  end

  it "returns an empty list if the config key is not set" do
    @script.files([":all_files"]).should == []
  end
end

describe MSpecScript, "#setup_env" do
  before :each do
    @script = MSpecScript.new
    @options, @config = new_option
    @script.stub(:config).and_return(@config)
  end

  after :each do
  end

  it "sets MSPEC_RUNNER = '1' in the environment" do
    ENV["MSPEC_RUNNER"] = "0"
    @script.setup_env
    ENV["MSPEC_RUNNER"].should == "1"
  end

  it "sets RUBY_EXE = config[:target] in the environment" do
    ENV["RUBY_EXE"] = nil
    @script.setup_env
    ENV["RUBY_EXE"].should == @config[:target]
  end

  it "sets RUBY_FLAGS = config[:flags] in the environment" do
    ENV["RUBY_FLAGS"] = nil
    @config[:flags] = ["-w", "-Q"]
    @script.setup_env
    ENV["RUBY_FLAGS"].should == "-w -Q"
  end
end