2
2
3
3
var expect = require ( 'expect' ) ;
4
4
var miss = require ( 'mississippi' ) ;
5
+ var sinon = require ( 'sinon' ) ;
6
+
7
+ // Need to wrap this to cause walker to emit an error
8
+ var fs = require ( 'fs' ) ;
5
9
6
10
var globStream = require ( '../' ) ;
7
11
@@ -119,7 +123,7 @@ describe('glob-stream', function () {
119
123
120
124
pipe (
121
125
[
122
- globStream ( './fixtures/has (parens)/*.dmc' , { cwd : dir } ) ,
126
+ globStream ( './fixtures/has \\ (parens\\ )/*.dmc' , { cwd : dir } ) ,
123
127
concat ( assert ) ,
124
128
] ,
125
129
done
@@ -213,7 +217,7 @@ describe('glob-stream', function () {
213
217
) ;
214
218
} ) ;
215
219
216
- it ( 'properly orders objects when given multiple paths and specified base' , function ( done ) {
220
+ it ( 'emits all objects (unordered) when given multiple paths and specified base' , function ( done ) {
217
221
var base = dir + '/fixtures' ;
218
222
219
223
var expected = [
@@ -242,13 +246,15 @@ describe('glob-stream', function () {
242
246
243
247
function assert ( pathObjs ) {
244
248
expect ( pathObjs . length ) . toEqual ( 3 ) ;
245
- expect ( pathObjs ) . toEqual ( expected ) ;
249
+ expect ( pathObjs ) . toContainEqual ( expected [ 0 ] ) ;
250
+ expect ( pathObjs ) . toContainEqual ( expected [ 1 ] ) ;
251
+ expect ( pathObjs ) . toContainEqual ( expected [ 2 ] ) ;
246
252
}
247
253
248
254
pipe ( [ globStream ( paths , { cwd : base , base : base } ) , concat ( assert ) ] , done ) ;
249
255
} ) ;
250
256
251
- it ( 'properly orders objects when given multiple paths and cwdbase' , function ( done ) {
257
+ it ( 'emits all objects (unordered) when given multiple paths and cwdbase' , function ( done ) {
252
258
var base = dir + '/fixtures' ;
253
259
254
260
var expected = [
@@ -277,7 +283,9 @@ describe('glob-stream', function () {
277
283
278
284
function assert ( pathObjs ) {
279
285
expect ( pathObjs . length ) . toEqual ( 3 ) ;
280
- expect ( pathObjs ) . toEqual ( expected ) ;
286
+ expect ( pathObjs ) . toContainEqual ( expected [ 0 ] ) ;
287
+ expect ( pathObjs ) . toContainEqual ( expected [ 1 ] ) ;
288
+ expect ( pathObjs ) . toContainEqual ( expected [ 2 ] ) ;
281
289
}
282
290
283
291
pipe (
@@ -286,7 +294,7 @@ describe('glob-stream', function () {
286
294
) ;
287
295
} ) ;
288
296
289
- it ( 'properly orders objects when given multiple globs with globstars' , function ( done ) {
297
+ it ( 'emits all objects (unordered) when given multiple globs with globstars' , function ( done ) {
290
298
var expected = [
291
299
{
292
300
cwd : dir ,
@@ -324,13 +332,17 @@ describe('glob-stream', function () {
324
332
325
333
function assert ( pathObjs ) {
326
334
expect ( pathObjs . length ) . toEqual ( 5 ) ;
327
- expect ( pathObjs ) . toEqual ( expected ) ;
335
+ expect ( pathObjs ) . toContainEqual ( expected [ 0 ] ) ;
336
+ expect ( pathObjs ) . toContainEqual ( expected [ 1 ] ) ;
337
+ expect ( pathObjs ) . toContainEqual ( expected [ 2 ] ) ;
338
+ expect ( pathObjs ) . toContainEqual ( expected [ 3 ] ) ;
339
+ expect ( pathObjs ) . toContainEqual ( expected [ 4 ] ) ;
328
340
}
329
341
330
342
pipe ( [ globStream ( globs , { cwd : dir } ) , concat ( assert ) ] , done ) ;
331
343
} ) ;
332
344
333
- it ( 'properly orders objects when given multiple absolute paths and no cwd' , function ( done ) {
345
+ it ( 'emits all objects (unordered) when given multiple absolute paths and no cwd' , function ( done ) {
334
346
var expected = [
335
347
{
336
348
cwd : process . cwd ( ) ,
@@ -357,7 +369,9 @@ describe('glob-stream', function () {
357
369
358
370
function assert ( pathObjs ) {
359
371
expect ( pathObjs . length ) . toEqual ( 3 ) ;
360
- expect ( pathObjs ) . toEqual ( expected ) ;
372
+ expect ( pathObjs ) . toContainEqual ( expected [ 0 ] ) ;
373
+ expect ( pathObjs ) . toContainEqual ( expected [ 1 ] ) ;
374
+ expect ( pathObjs ) . toContainEqual ( expected [ 2 ] ) ;
361
375
}
362
376
363
377
pipe ( [ globStream ( paths ) , concat ( assert ) ] , done ) ;
@@ -640,6 +654,7 @@ describe('glob-stream', function () {
640
654
641
655
it ( 'emits an error when a singular path in multiple paths not found' , function ( done ) {
642
656
function assert ( err ) {
657
+ expect ( err ) . toEqual ( expect . anything ( ) ) ;
643
658
expect ( err . toString ( ) ) . toMatch ( / F i l e n o t f o u n d w i t h s i n g u l a r g l o b / ) ;
644
659
done ( ) ;
645
660
}
@@ -689,8 +704,6 @@ describe('options', function () {
689
704
var defaultedOpts = {
690
705
cwd : process . cwd ( ) ,
691
706
dot : false ,
692
- silent : true ,
693
- nonull : false ,
694
707
cwdbase : false ,
695
708
} ;
696
709
@@ -703,33 +716,30 @@ describe('options', function () {
703
716
pipe ( [ stream , concat ( ) ] , done ) ;
704
717
} ) ;
705
718
706
- describe ( 'silent' , function ( ) {
707
- it ( 'accepts a boolean' , function ( done ) {
708
- pipe (
709
- [
710
- globStream ( dir + '/fixtures/stuff/run.dmc' , { silent : false } ) ,
711
- concat ( ) ,
712
- ] ,
713
- done
714
- ) ;
715
- } ) ;
716
- } ) ;
717
-
718
- describe ( 'nonull' , function ( ) {
719
- it ( 'accepts a boolean' , function ( done ) {
720
- pipe ( [ globStream ( 'notfound{a,b}' , { nonull : true } ) , concat ( ) ] , done ) ;
721
- } ) ;
722
-
723
- it ( 'does not have any effect on our results' , function ( done ) {
724
- function assert ( pathObjs ) {
725
- expect ( pathObjs . length ) . toEqual ( 0 ) ;
726
- }
719
+ it ( 'throws on invalid options' , function ( done ) {
720
+ expect ( function ( ) {
721
+ globStream ( './fixtures/stuff/*.dmc' , { cwd : 1234 } ) ;
722
+ } ) . toThrow ( 'must be a string' ) ;
723
+ expect ( function ( ) {
724
+ globStream ( './fixtures/stuff/*.dmc' , { dot : 1234 } ) ;
725
+ } ) . toThrow ( 'must be a boolean' ) ;
726
+ expect ( function ( ) {
727
+ globStream ( './fixtures/stuff/*.dmc' , { cwdbase : 1234 } ) ;
728
+ } ) . toThrow ( 'must be a boolean' ) ;
729
+ expect ( function ( ) {
730
+ globStream ( './fixtures/stuff/*.dmc' , { uniqueBy : 1234 } ) ;
731
+ } ) . toThrow ( 'must be a string or function' ) ;
732
+ expect ( function ( ) {
733
+ globStream ( './fixtures/stuff/*.dmc' , { allowEmpty : 1234 } ) ;
734
+ } ) . toThrow ( 'must be a boolean' ) ;
735
+ expect ( function ( ) {
736
+ globStream ( './fixtures/stuff/*.dmc' , { base : 1234 } ) ;
737
+ } ) . toThrow ( 'must be a string if specified' ) ;
738
+ expect ( function ( ) {
739
+ globStream ( './fixtures/stuff/*.dmc' , { ignore : 1234 } ) ;
740
+ } ) . toThrow ( 'must be a string or array' ) ;
727
741
728
- pipe (
729
- [ globStream ( 'notfound{a,b}' , { nonull : true } ) , concat ( assert ) ] ,
730
- done
731
- ) ;
732
- } ) ;
742
+ done ( ) ;
733
743
} ) ;
734
744
735
745
describe ( 'ignore' , function ( ) {
@@ -799,7 +809,7 @@ describe('options', function () {
799
809
) ;
800
810
} ) ;
801
811
802
- it ( 'merges ignore option and negative globs' , function ( done ) {
812
+ it ( 'can use both ignore option and negative globs' , function ( done ) {
803
813
var globs = [ './fixtures/stuff/*.dmc' , '!./fixtures/stuff/test.dmc' ] ;
804
814
805
815
function assert ( pathObjs ) {
@@ -815,4 +825,166 @@ describe('options', function () {
815
825
) ;
816
826
} ) ;
817
827
} ) ;
828
+
829
+ it ( 'emits an error if there are no matches' , function ( done ) {
830
+ function assert ( err ) {
831
+ expect ( err . message ) . toMatch ( / ^ F i l e n o t f o u n d w i t h s i n g u l a r g l o b / g) ;
832
+ done ( ) ;
833
+ }
834
+
835
+ pipe ( [ globStream ( 'notfound' , { cwd : dir } ) , concat ( ) ] , assert ) ;
836
+ } ) ;
837
+
838
+ it ( 'throws an error if you try to write to it' , function ( done ) {
839
+ var gs = globStream ( 'notfound' , { cwd : dir } ) ;
840
+ gs . on ( 'error' , function ( ) { } ) ;
841
+
842
+ try {
843
+ gs . write ( { } ) ;
844
+ } catch ( err ) {
845
+ expect ( err ) . toEqual ( expect . anything ( ) ) ;
846
+ done ( ) ;
847
+ }
848
+ } ) ;
849
+
850
+ it ( 'does not throw an error if you push to it' , function ( done ) {
851
+ var stub = {
852
+ cwd : dir ,
853
+ base : dir ,
854
+ path : dir ,
855
+ } ;
856
+
857
+ var gs = globStream ( './fixtures/test.coffee' , { cwd : dir } ) ;
858
+
859
+ gs . push ( stub ) ;
860
+
861
+ function assert ( pathObjs ) {
862
+ expect ( pathObjs . length ) . toEqual ( 2 ) ;
863
+ expect ( pathObjs [ 0 ] ) . toEqual ( stub ) ;
864
+ }
865
+
866
+ pipe ( [ gs , concat ( assert ) ] , done ) ;
867
+ } ) ;
868
+
869
+ it ( 'accepts a file path' , function ( done ) {
870
+ var expected = {
871
+ cwd : dir ,
872
+ base : dir + '/fixtures' ,
873
+ path : dir + '/fixtures/test.coffee' ,
874
+ } ;
875
+
876
+ function assert ( pathObjs ) {
877
+ expect ( pathObjs . length ) . toBe ( 1 ) ;
878
+ expect ( pathObjs [ 0 ] ) . toMatchObject ( expected ) ;
879
+ }
880
+
881
+ pipe (
882
+ [ globStream ( './fixtures/test.coffee' , { cwd : dir } ) , concat ( assert ) ] ,
883
+ done
884
+ ) ;
885
+ } ) ;
886
+
887
+ it ( 'accepts a glob' , function ( done ) {
888
+ var expected = [
889
+ {
890
+ cwd : dir ,
891
+ base : dir + '/fixtures' ,
892
+ path : dir + '/fixtures/has (parens)/test.dmc' ,
893
+ } ,
894
+ {
895
+ cwd : dir ,
896
+ base : dir + '/fixtures' ,
897
+ path : dir + '/fixtures/stuff/run.dmc' ,
898
+ } ,
899
+ {
900
+ cwd : dir ,
901
+ base : dir + '/fixtures' ,
902
+ path : dir + '/fixtures/stuff/test.dmc' ,
903
+ } ,
904
+ ] ;
905
+
906
+ function assert ( pathObjs ) {
907
+ expect ( pathObjs . length ) . toBe ( 3 ) ;
908
+ expect ( pathObjs ) . toContainEqual ( expected [ 0 ] ) ;
909
+ expect ( pathObjs ) . toContainEqual ( expected [ 1 ] ) ;
910
+ expect ( pathObjs ) . toContainEqual ( expected [ 2 ] ) ;
911
+ }
912
+
913
+ pipe (
914
+ [ globStream ( './fixtures/**/*.dmc' , { cwd : dir } ) , concat ( assert ) ] ,
915
+ done
916
+ ) ;
917
+ } ) ;
918
+
919
+ it ( 'pauses the globber upon backpressure' , function ( done ) {
920
+ var gs = globStream ( './fixtures/**/*.dmc' , { cwd : dir , highWaterMark : 1 } ) ;
921
+
922
+ function waiter ( pathObj , _ , cb ) {
923
+ setTimeout ( function ( ) {
924
+ cb ( null , pathObj ) ;
925
+ } , 500 ) ;
926
+ }
927
+
928
+ function assert ( pathObjs ) {
929
+ expect ( pathObjs . length ) . toEqual ( 3 ) ;
930
+ }
931
+
932
+ pipe (
933
+ [ gs , through2 . obj ( { highWaterMark : 1 } , waiter ) , concat ( assert ) ] ,
934
+ done
935
+ ) ;
936
+ } ) ;
937
+
938
+ it ( 'destroys the stream with an error if no match is found' , function ( done ) {
939
+ var gs = globStream ( 'notfound' , { cwd : dir } ) ;
940
+
941
+ var spy = sinon . spy ( gs , 'destroy' ) ;
942
+
943
+ function assert ( err ) {
944
+ sinon . restore ( ) ;
945
+ expect ( spy . getCall ( 0 ) . args [ 0 ] ) . toBe ( err ) ;
946
+ expect ( err . toString ( ) ) . toMatch ( / F i l e n o t f o u n d w i t h s i n g u l a r g l o b / ) ;
947
+ done ( ) ;
948
+ }
949
+
950
+ pipe ( [ gs , concat ( ) ] , assert ) ;
951
+ } ) ;
952
+
953
+ it ( 'destroys the stream if walker errors' , function ( done ) {
954
+ var expectedError = new Error ( 'Stubbed error' ) ;
955
+
956
+ var gs = globStream ( './fixtures/**/*.dmc' , { cwd : dir } ) ;
957
+
958
+ function stubError ( dirpath , opts , cb ) {
959
+ cb ( expectedError ) ;
960
+ }
961
+
962
+ var spy = sinon . spy ( gs , 'destroy' ) ;
963
+ sinon . stub ( fs , 'readdir' ) . callsFake ( stubError ) ;
964
+
965
+ function assert ( err ) {
966
+ sinon . restore ( ) ;
967
+ expect ( spy . called ) . toEqual ( true ) ;
968
+ expect ( err ) . toBe ( expectedError ) ;
969
+ done ( ) ;
970
+ }
971
+
972
+ pipe ( [ gs , concat ( ) ] , assert ) ;
973
+ } ) ;
974
+
975
+ it ( 'does not emit an error if stream is destroyed without an error' , function ( done ) {
976
+ var gs = globStream ( './fixtures/**/*.dmc' , { cwd : dir } ) ;
977
+
978
+ var spy = sinon . spy ( ) ;
979
+
980
+ gs . on ( 'error' , spy ) ;
981
+
982
+ gs . on ( 'close' , function ( ) {
983
+ sinon . restore ( ) ;
984
+ expect ( spy . called ) . toEqual ( false ) ;
985
+ done ( ) ;
986
+ } ) ;
987
+
988
+ gs . destroy ( ) ;
989
+ } ) ;
818
990
} ) ;
0 commit comments