1. bfad
  2. lspec

Source

lspec / spec / command / lspec.spec.inc

  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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
local(path_here) = currentCapture->callsite_file->stripLastComponent
not #path_here->beginsWith('/')
    ? #path_here = io_file_getcwd + '/' + #path_here
not #path_here->endsWith('/')
    ? #path_here->append('/')

local(lspec_path) = #path_here + '../../command/lspec'
var(lspec_path)   = #lspec_path

define call_lspec(args::staticarray=(:)) => {
    local(stdout, stderr)
    local(my_process) = sys_process
    
    handle => {#my_process->close}
    #my_process->open($lspec_path, #args)
    #my_process->wait
    #stdout = #my_process->read
    #stderr = #my_process->readError
    
    return (:#stdout, #stderr)
}


var(termColor) = map(
	`normal`     = '' + decode_base64('Gw==') + '[0m',
	`boldRed`    = '' + decode_base64('Gw==') + '[1;31;49m',
	`boldGreen`  = '' + decode_base64('Gw==') + '[1;32;49m',
	`boldYellow` = '' + decode_base64('Gw==') + '[1;33;49m',
	`boldCyan`   = '' + decode_base64('Gw==') + '[1;36;49m',
)


describe('lspec command') => {
    
    describe('default lspec library options') => {
        local(the_options)
        beforeAll => {
            // Run it in a different thread and get the option values that get setup
            local(_,read) = split_thread => {
                // save stdout using ->dup
                // create a local conduit
                // close real stdout, reopen it on our write pipe using dup2 system call
                // make a file object here which we read stdout from
                local(save_stdout)     = file_stdout->fd->dup
                local(readFd, writeFd) = io_file_pipe
                #writeFd->dup2(file_stdout->fd)
                local(read_test) = file(#readFd)
                // set stdout back using our save
                handle => { #save_stdout->dup2(file_stdout->fd) }
                
                var(argv) = (:'lspec', '//tmp/lspec_test_delete_me.spec.inc')
                file('//tmp/lspec_test_delete_me.spec.inc')->openTruncate&close
                protect => { sourcefile(file(#lspec_path))->invoke }
                #1->first->writeObject(lspec->options)
            }
            #the_options = #read->readObject
        }
        afterAll => {
            file('//tmp/lspec_test_delete_me.spec.inc')->delete
        }
        it(`is set to progressive mode`) => {
            expect('p', #the_options->find(`resultsFormat`))
        }
        it(`is set to output in color`) => {
            expect(true, #the_options->find(`colorOutput`))
        }
    }
    
    context('given a file path with no test groups') => {
        local(test_file) = file('/tmp/lspec.test')
        
        beforeEach => {
            #test_file->doWithClose => { #test_file->openTruncate }
        }
        
        afterEach => { #test_file->close&delete }
        
        it('executes the file') => {
            #test_file->doWithClose => {
                #test_file->writeString("file('" + #test_file->path + "')->openTruncate&close")
            }
            
            local(stdout, stderr) = call_lspec((:#test_file->path))
            
            expect(void == #stderr)
            expect('', #test_file->readString)
        }
        
        it('displays the "no test" summary') => {
            local(expected) = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
            
            local(stdout, stderr) = call_lspec((:#test_file->path))
            
            expect(void == #stderr)
            expect(#expected, #stdout)
        }
    }
    
    context('given multiple file path with no test groups') => {
        local(test_file1) = file('/tmp/lspec.test1')
        local(test_file2) = file('/tmp/lspec.test2')
        local(test_file3) = file('/tmp/lspec.test3')
        
        beforeEach => {
            #test_file1->doWithClose => { #test_file1->openTruncate }
            #test_file2->doWithClose => { #test_file2->openTruncate }
            #test_file3->doWithClose => { #test_file3->openTruncate }
        }
        
        afterEach => {
            #test_file1->close&delete
            #test_file2->close&delete
            #test_file3->close&delete
        }

        it('executes each file') => {
            #test_file1->doWithClose => {
                #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")
            }
            #test_file2->doWithClose => {
                #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")
            }
            #test_file3->doWithClose => {
                #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")
            }
            
            local(stdout, stderr) = call_lspec((:#test_file1->path, #test_file2->path, #test_file3->path))
            
            expect(void == #stderr)
            expect('', #test_file1->readString)
            expect('', #test_file2->readString)
            expect('', #test_file3->readString)
        }

        it('displays the "no test" summary') => {
            local(expected) = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'

            local(stdout, stderr) = call_lspec((:#test_file1->path, #test_file2->path, #test_file3->path))

            expect(void == #stderr)
            expect(#expected, #stdout)
        }
    }
    
    context('given folder path(s)') => {
        beforeAll => {
            local(test_dir) = dir('/tmp/lspec_test/')
            #test_dir->create
            dir(#test_dir->path + 'spec/')->create
        }
        afterAll => {
            sys_process('/bin/rm', (:'-rf', #test_dir->path))->wait
        }
        afterEach => {
            sys_process('/bin/rm', (:'-rf', #test_dir->path + 'spec/'))->wait
            dir(#test_dir->path + 'spec/')->create
        }
        
        context('folder contains one file without the test extensions') => {
            beforeEach => {
                local(test_file) = file(#test_dir->path + 'spec/foo.lasso')
                #test_file->doWithClose => {
                    #test_file->writeString("file('" + #test_file->path + "')->openTruncate&close")
                }
            }
            
            it("doesn't execute the file") => {
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))

                expect(void == #stderr)
                expect("file('" + #test_file->path + "')->openTruncate&close", #test_file->readString)
            }
            it('displays the "no test" summary') => {
                local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect(#expected, #stdout)
            }
        }
        
        context("folder containes a file with the test extension with no tests") => {
            beforeEach => {
                local(test_file) = file(#test_dir->path + 'spec/foo.spec.inc')
                #test_file->doWithClose => {
                    #test_file->writeString("file('" + #test_file->path + "')->openTruncate&close")
                }
            }
            
            it("executes the file") => {
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect('', #test_file->readString)
            }
            it('displays the "no test" summary') => {
                local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect(#expected, #stdout)
            }
        }
        
        context("folder containes 3 files with the test extension with no tests") => {
            beforeEach => {
                local(test_file1) = file(#test_dir->path + 'spec/foo1.spec.inc')
                local(test_file2) = file(#test_dir->path + 'spec/foo2.spec.inc')
                local(test_file3) = file(#test_dir->path + 'spec/foo3.spec.inc')
                #test_file1->doWithClose => {
                    #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")
                }
                #test_file2->doWithClose => {
                    #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")
                }
                #test_file3->doWithClose => {
                    #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")
                }
            }
            
            it("executes the files") => {
                local(_, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect('', #test_file1->readString)
                expect('', #test_file2->readString)
                expect('', #test_file3->readString)
            }
            it('displays the "no test" summary') => {
                local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect(#expected, #stdout)
            }
        }
        
        context("folder contains a sub folder with an empty file that has the test extension") => {
            beforeEach => {
                dir(#test_dir->path + 'spec/sub/')->create
                local(test_file) = file(#test_dir->path + 'spec/sub/foo.spec.inc')
                #test_file->doWithClose => {
                    #test_file->writeString("file('" + #test_file->path + "')->openTruncate&close")
                }
            }
            
            it("executes the file") => {
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect('', #test_file->readString)
            }
            it('displays the "no test" summary') => {
                local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec'))
                
                expect(void == #stderr)
                expect(#expected, #stdout)
            }
        }
        
        context("three folders each with a file with the test extension with no tests") => {
            beforeEach => {
                dir(#test_dir->path + 'spec/sub1/')->create
                dir(#test_dir->path + 'spec/sub2/')->create
                dir(#test_dir->path + 'spec/sub3/')->create

                local(test_file1) = file(#test_dir->path + 'spec/sub1/foo1.spec.inc')
                local(test_file2) = file(#test_dir->path + 'spec/sub2/foo2.spec.inc')
                local(test_file3) = file(#test_dir->path + 'spec/sub3/foo3.spec.inc')
                #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
                #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
                #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
            }
            
            it("executes the files") => {
                local(_, stderr) = call_lspec((:#test_dir->path + 'spec/sub1', #test_dir->path + 'spec/sub2', #test_dir->path + 'spec/sub3'))
                
                expect(void == #stderr)
                expect('', #test_file1->readString)
                expect('', #test_file2->readString)
                expect('', #test_file3->readString)
            }
            it('displays the "no test" summary') => {
                local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec/sub1', #test_dir->path + 'spec/sub2', #test_dir->path + 'spec/sub3'))
                
                expect(void == #stderr)
                expect(#expected, #stdout)
            }
            
            context("sub-folders three levels deep in one of the folders that have files with the test extension") => {
                beforeEach => {
                    dir(#test_dir->path + 'spec/sub1/')->create
                    dir(#test_dir->path + 'spec/sub1/sub2/')->create
                    dir(#test_dir->path + 'spec/sub1/sub2/sub3/')->create

                    local(test_file1) = file(#test_dir->path + 'spec/sub1/sub2/sub3/foo1.spec.inc')
                    local(test_file2) = file(#test_dir->path + 'spec/sub1/sub2/sub3/foo2.spec.inc')
                    local(test_file3) = file(#test_dir->path + 'spec/sub1/sub2/sub3/foo3.spec.inc')
                    #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
                    #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
                    #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
                }
                
                it("executes the files") => {
                    local(_, stderr) = call_lspec((:#test_dir->path + 'spec'))
                    
                    expect(void == #stderr)
                    expect('', #test_file1->readString)
                    expect('', #test_file2->readString)
                    expect('', #test_file3->readString)
                }
                it('displays the "no test" summary') => {
                    local(expected)       = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
                    local(stdout, stderr) = call_lspec((:#test_dir->path + 'spec/sub1', #test_dir->path + 'spec/sub2', #test_dir->path + 'spec/sub3'))
                    
                    expect(void == #stderr)
                    expect(#expected, #stdout)
                }
            }
        }
    }
    
    context('called with no arguments') => {
        beforeAll => {
            local(test_dir) = dir('/tmp/lspec_test/')
            #test_dir->create
            dir(#test_dir->path + 'spec/')->create
        }
        afterAll => {
            sys_process('/bin/rm', (:'-rf', #test_dir->path))->wait
        }
        afterEach => {
            sys_process('/bin/rm', (:'-rf', #test_dir->path + 'spec/'))->wait
            dir(#test_dir->path + 'spec/')->create
        }
        
        it("throws an error if a folder named spec doesn't exist") => {
            // [dir->setCwd] can cause errors with lspec library, so
            // make sure to save off cwd first and set it back right after lspec call
            local(cwd)      = dir(io_file_getcwd)
            local(expected) = error_code_resNotFound + ':' + error_msg_resNotFound + ' - ./spec/'
            
            sys_process('/bin/rm', (:'-rf', #test_dir->path + 'spec/'))->wait
            
            #test_dir->setcwd
            handle => { #cwd->setcwd }
            local(stdout, stderr) = call_lspec
            #cwd->setcwd
            
            expect(#expected, #stderr)
            expect(void == #stdout)
        }
        it("executes all the files with the test extension in the spec folder") => {
            local(cwd)        = dir(io_file_getcwd)
            local(expected)   = '\n\nFinished in 0.000000 seconds\n' + $termColor('boldCyan') + '0' + $termColor('normal') + ' tests, ' + $termColor('boldRed') + '0' + $termColor('normal') + ' failures\n'
            local(test_file1) = file(#test_dir->path + 'spec/foo1.spec.inc')
            local(test_file2) = file(#test_dir->path + 'spec/foo2.spec.inc')
            local(test_file3) = file(#test_dir->path + 'spec/foo3.expt.inc')
            #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
            #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
            #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
            
            #test_dir->setcwd
            handle => { #cwd->setcwd }
            local(stdout, stderr) = call_lspec
            #cwd->setcwd
            
            expect(void == #stderr)
            expect(#expected, #stdout)
            expect('', #test_file1->readString)
            expect('', #test_file2->readString)
            expect("file('" + #test_file3->path + "')->openTruncate&close", #test_file3->readString)
        }
        it("executes all the files with the test extension in any of spec's sub-folders") => {
            dir(#test_dir->path + 'spec/sub1/')->create
            dir(#test_dir->path + 'spec/sub1/sub2/')->create
            dir(#test_dir->path + 'spec/sub1/sub2/sub3/')->create

            local(test_file1) = file(#test_dir->path + 'spec/sub1/foo1.spec.inc')
            local(test_file2) = file(#test_dir->path + 'spec/sub1/sub2/foo2.spec.inc')
            local(test_file3) = file(#test_dir->path + 'spec/sub1/sub2/sub3/foo3.inc')
            #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
            #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
            #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
            
            #test_dir->setcwd
            handle => { #cwd->setcwd }
            local(stdout, stderr) = call_lspec
            #cwd->setcwd
            
            expect(void == #stderr)
            expect(#expected, #stdout)
            expect('', #test_file1->readString)
            expect('', #test_file2->readString)
            expect("file('" + #test_file3->path + "')->openTruncate&close", #test_file3->readString)
        }
    }
    
    context('called with the -exts argument') => {
        it('displays the usage message when no extensions list is passed') => {
            local(stdout, stderr) = call_lspec((:'-exts'))
            
            expect(void == #stderr)
            expect(#stdout->beginsWith('usage:'))
        }
        it("executes the specified file even if it doesn't have the extension") => {
            local(f) = file('//tmp/lspec.test.rh')
            #f->doWithClose => {
                #f->writeString("file('" + #f->path + "')->openTruncate&close")
            }
            
            local(stdout, stderr) = call_lspec((:#f->path, '-exts', 'lasso,inc'))
            
            expect(void == #stderr)
            expect('', #f->readString)
        }
        it(`executes those files in a directory that end in .spec.[extension passed]`) => {
            local(test_dir) = dir('//tmp/lspec/')
            handle => { sys_process('/bin/rm', (:'-rf', #test_dir->path))->wait }
            #test_dir->create
            
            local(test_file1) = file(#test_dir->path + 'foo1.spec.inc')
            local(test_file2) = file(#test_dir->path + 'foo2.spec.lasso')
            local(test_file3) = file(#test_dir->path + 'foo3.lasso')
            #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
            #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
            #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
            
            local(stdout, stderr) = call_lspec((:#test_dir->path, '-exts', 'lasso'))
            
            expect(void == #stderr)
            expect('', #test_file2->readString)
            expect("file('" + #test_file1->path + "')->openTruncate&close", #test_file1->readString)
            expect("file('" + #test_file3->path + "')->openTruncate&close", #test_file3->readString)
        }
        it(`executes those files in a directory that end in .spec.[any of the csv extensions passed]`) => {
            local(test_dir) = dir('//tmp/lspec/')
            handle => { sys_process('/bin/rm', (:'-rf', #test_dir->path))->wait }
            #test_dir->create
            
            local(test_file1) = file(#test_dir->path + 'foo1.spec.inc')
            local(test_file2) = file(#test_dir->path + 'foo2.spec.lasso')
            local(test_file3) = file(#test_dir->path + 'foo3.spec.rh')
            local(test_file4) = file(#test_dir->path + 'foo4.rh')
            #test_file1->writeString("file('" + #test_file1->path + "')->openTruncate&close")&close
            #test_file2->writeString("file('" + #test_file2->path + "')->openTruncate&close")&close
            #test_file3->writeString("file('" + #test_file3->path + "')->openTruncate&close")&close
            #test_file4->writeString("file('" + #test_file4->path + "')->openTruncate&close")&close
            
            local(stdout, stderr) = call_lspec((:#test_dir->path, '-exts', 'lasso,rh'))
            
            expect(void == #stderr)
            expect('', #test_file2->readString)
            expect('', #test_file3->readString)
            expect("file('" + #test_file1->path + "')->openTruncate&close", #test_file1->readString)
            expect("file('" + #test_file4->path + "')->openTruncate&close", #test_file4->readString)
        }
    }

    context('called with the -nocolor argument') => {
        local(file_testPass) = file('//tmp/lspec.test.pass')
        local(file_testFail) = file('//tmp/lspec.test.fail')
        local(file_testPend) = file('//tmp/lspec.test.pend')
        beforeAll => {
            #file_testPass->doWithClose => {
                #file_testPass->openTruncate&writeString("\
                    describe('passing test') => { it('passes') => { expect(true) } }\
                ")
            }
            #file_testFail->doWithClose => {
                #file_testFail->openTruncate&writeString("\
                    describe('failing test') => { it('fails') => { expect(false) } }\
                ")
            }
            #file_testPend->doWithClose => {
                #file_testPend->openTruncate&writeString("\
                    describe('pending test') => { it('pends') => { } }\
                ")
            }
        }
        afterAll => {
            #file_testPass->delete
            #file_testFail->delete
            #file_testPend->delete
        }
        it(`doesn't colorize passing tests`) => {
            local(expected) = regexp("\\.\n\n" + `Finished in [0-9.]+ seconds` + "\n1 test, 0 failures")
            local(stdout, stderr) = call_lspec((:'-nocolor', #file_testPass->path))
            
            expect(void == #stderr)
            expect(#stdout->asString->contains(#expected))
        }
        it(`doesn't colorize failing tests`) => {
            local(expected) = regexp("\\.\n\n" + `Finished in [0-9.]+ seconds` + "\n1 test, 0 failures")

            expect(void == #stderr)
            expect(#stdout->asString->contains(#expected))
        }
        it(`doesn't colorize pending tests'`) => {
            local(expected) = regexp(
                "\\*\n\nPending:\n\n    1\\) pending test pends\n           # 1:31 //tmp/lspec.test.pend\n\n" +
                `Finished in [0-9.]+ seconds` + "\n1 test, 0 failures, 1 pending"
            )
            local(stdout, stderr) = call_lspec((:'-nocolor', #file_testPend->path))

            expect(void == #stderr)
            expect(#stdout->asString->contains(#expected))
        }
    }
}

lspec->stop