23
23
var fs = require ( 'fs' ) ;
24
24
var exec = require ( 'child_process' ) . exec ;
25
25
var async = require ( 'async' ) ;
26
- var jsdiff = require ( 'diff' ) ;
27
26
28
27
var _which = require ( 'which' ) . sync ;
29
28
function which ( command ) {
@@ -38,6 +37,10 @@ function which(command) {
38
37
// which is Windows' horribly crippled grep alternative.
39
38
var grep = which ( 'grep' ) || process . platform === 'win32' && which ( 'find' ) ;
40
39
40
+ function normalizeLineEndings ( s ) {
41
+ return s . replace ( / \r ? \n / g, '\n' ) ;
42
+ }
43
+
41
44
function run ( command , options , callback ) {
42
45
if ( typeof options === 'function' ) {
43
46
callback = options ;
@@ -48,28 +51,12 @@ function run(command, options, callback) {
48
51
command += ' | ' + grep + ' "std"' ;
49
52
}
50
53
exec ( command , function ( error , stdout ) {
51
- callback ( command , error ? error . code : 0 , stdout ) ;
54
+ callback ( command , error ? error . code : 0 , normalizeLineEndings ( stdout ) ) ;
52
55
} ) ;
53
56
}
54
57
55
- function showDiff ( actual , expected ) {
56
- actual = actual . replace ( / \r \n / g, '\n' ) ;
57
- expected = expected . replace ( / \r \n / g, '\n' ) ;
58
- if ( actual === expected ) {
59
- return true ;
60
- } else {
61
- return jsdiff . diffLines ( expected , actual ) . map ( function ( d ) {
62
- if ( d . removed ) {
63
- return '**EXPECTED** ' + d . value ;
64
- } else if ( d . added ) {
65
- return '**UNEXPECTED** ' + d . value ;
66
- }
67
- } ) . filter ( Boolean ) . join ( '' ) ;
68
- }
69
- }
70
-
71
58
function fixture ( filename ) {
72
- return String ( fs . readFileSync ( filename ) ) ;
59
+ return normalizeLineEndings ( String ( fs . readFileSync ( filename ) ) ) ;
73
60
}
74
61
75
62
exports [ 'exit' ] = {
@@ -90,45 +77,30 @@ exports['exit'] = {
90
77
} ,
91
78
'stdout stderr' : function ( test ) {
92
79
var counts = [ 10 , 100 , 1000 ] ;
93
- test . expect ( counts . length ) ;
80
+ var outputs = [ 'stdout stderr' , 'stdout' , 'stderr' ] ;
81
+ test . expect ( counts . length * outputs . length * 2 ) ;
94
82
async . eachSeries ( counts , function ( n , next ) {
95
- run ( 'node log.js 0 ' + n + ' stdout stderr' , { pipe : true } , function ( command , code , actual ) {
96
- var expected = fixture ( n + '-stdout-stderr.txt' ) ;
97
- test . equal ( true , showDiff ( actual , expected ) , command ) ;
98
- next ( ) ;
99
- } ) ;
100
- } , test . done ) ;
101
- } ,
102
- 'stdout' : function ( test ) {
103
- var counts = [ 10 , 100 , 1000 ] ;
104
- test . expect ( counts . length ) ;
105
- async . eachSeries ( counts , function ( n , next ) {
106
- run ( 'node log.js 0 ' + n + ' stdout' , { pipe : true } , function ( command , code , actual ) {
107
- var expected = fixture ( n + '-stdout.txt' ) ;
108
- test . equal ( true , showDiff ( actual , expected ) , command ) ;
109
- next ( ) ;
110
- } ) ;
111
- } , test . done ) ;
112
- } ,
113
- 'stderr' : function ( test ) {
114
- var counts = [ 10 , 100 , 1000 ] ;
115
- test . expect ( counts . length ) ;
116
- async . eachSeries ( counts , function ( n , next ) {
117
- run ( 'node log.js 0 ' + n + ' stderr' , { pipe : true } , function ( command , code , actual ) {
118
- var expected = fixture ( n + '-stderr.txt' ) ;
119
- test . equal ( true , showDiff ( actual , expected ) , command ) ;
120
- next ( ) ;
121
- } ) ;
83
+ async . eachSeries ( outputs , function ( o , next ) {
84
+ run ( 'node log.js 0 ' + n + ' ' + o , { pipe : true } , function ( command , code , actual ) {
85
+ var expected = fixture ( n + '-' + o . replace ( ' ' , '-' ) + '.txt' ) ;
86
+ // Sometimes, the actual file lines are out of order on Windows.
87
+ // But since the point of this lib is to drain the buffer and not
88
+ // guarantee output order, we only test the length.
89
+ test . equal ( actual . length , expected . length , '(length) ' + command ) ;
90
+ // The "fail" lines in log.js should NOT be output!
91
+ test . ok ( actual . indexOf ( 'fail' ) === - 1 , '(no more output after exit) ' + command ) ;
92
+ next ( ) ;
93
+ } ) ;
94
+ } , next ) ;
122
95
} , test . done ) ;
123
96
} ,
124
97
'exit codes' : function ( test ) {
125
98
var codes = [ 0 , 1 , 123 ] ;
126
- test . expect ( codes . length * 2 ) ;
99
+ test . expect ( codes . length ) ;
127
100
async . eachSeries ( codes , function ( n , next ) {
128
- run ( 'node log.js ' + n + ' 10 stdout stderr' , { pipe : false } , function ( command , code , actual ) {
101
+ run ( 'node log.js ' + n + ' 10 stdout stderr' , { pipe : false } , function ( command , code ) {
102
+ // The specified exit code should be passed through.
129
103
test . equal ( code , n , 'should have exited with ' + n + ' error code.' ) ;
130
- var expected = fixture ( '10-stdout-stderr.txt' ) ;
131
- test . equal ( true , showDiff ( actual , expected ) , command ) ;
132
104
next ( ) ;
133
105
} ) ;
134
106
} , test . done ) ;
0 commit comments