@stdlib/random-streams-mt19937-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish_cli.yml +165 -0
- package/CONTRIBUTORS +39 -0
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +256 -0
- package/bin/cli +186 -0
- package/docs/usage.txt +16 -0
- package/etc/cli_opts.json +25 -0
- package/package.json +86 -0
- package/test/fixtures/bad_state.txt +0 -0
- package/test/fixtures/state.txt +0 -0
- package/test/fixtures/write_error.js.txt +39 -0
- package/test/test.cli.js +499 -0
package/test/test.cli.js
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var resolve = require( 'path' ).resolve;
|
|
24
|
+
var exec = require( 'child_process' ).exec;
|
|
25
|
+
var tape = require( 'tape' );
|
|
26
|
+
var IS_BROWSER = require( '@stdlib/assert-is-browser' );
|
|
27
|
+
var IS_WINDOWS = require( '@stdlib/assert-is-windows' );
|
|
28
|
+
var EXEC_PATH = require( '@stdlib/process-exec-path' );
|
|
29
|
+
var isnan = require( '@stdlib/math-base-assert-is-nan' );
|
|
30
|
+
var isInteger = require( '@stdlib/math-base-assert-is-integer' );
|
|
31
|
+
var mt19937 = require( '@stdlib/random-base-mt19937' ).factory;
|
|
32
|
+
var readFileSync = require( '@stdlib/fs-read-file' ).sync;
|
|
33
|
+
var existsSync = require( '@stdlib/fs-exists' ).sync;
|
|
34
|
+
var unlinkSync = require( '@stdlib/fs-unlink' ).sync;
|
|
35
|
+
var Uint32Array = require( '@stdlib/array-uint32' );
|
|
36
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
37
|
+
var gcopy = require( '@stdlib/blas-base-gcopy' );
|
|
38
|
+
var replace = require( '@stdlib/string-replace' );
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// VARIABLES //
|
|
42
|
+
|
|
43
|
+
var fpath = resolve( __dirname, '..', 'bin', 'cli' );
|
|
44
|
+
var opts = {
|
|
45
|
+
'skip': IS_BROWSER || IS_WINDOWS
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
// FIXTURES //
|
|
50
|
+
|
|
51
|
+
var PKG_VERSION = require( './../package.json' ).version;
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// TESTS //
|
|
55
|
+
|
|
56
|
+
tape( 'command-line interface', function test( t ) {
|
|
57
|
+
t.ok( true, __filename );
|
|
58
|
+
t.end();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
tape( 'when invoked with a `--help` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
|
|
62
|
+
var expected;
|
|
63
|
+
var cmd;
|
|
64
|
+
|
|
65
|
+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
|
|
66
|
+
'encoding': 'utf8'
|
|
67
|
+
});
|
|
68
|
+
cmd = [
|
|
69
|
+
EXEC_PATH,
|
|
70
|
+
fpath,
|
|
71
|
+
'--help'
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
exec( cmd.join( ' ' ), done );
|
|
75
|
+
|
|
76
|
+
function done( error, stdout, stderr ) {
|
|
77
|
+
if ( error ) {
|
|
78
|
+
t.fail( error.message );
|
|
79
|
+
} else {
|
|
80
|
+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
|
|
81
|
+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
|
|
82
|
+
}
|
|
83
|
+
t.end();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
tape( 'when invoked with a `-h` flag, the command-line interface prints the help text to `stderr`', opts, function test( t ) {
|
|
88
|
+
var expected;
|
|
89
|
+
var cmd;
|
|
90
|
+
|
|
91
|
+
expected = readFileSync( resolve( __dirname, '..', 'docs', 'usage.txt' ), {
|
|
92
|
+
'encoding': 'utf8'
|
|
93
|
+
});
|
|
94
|
+
cmd = [
|
|
95
|
+
EXEC_PATH,
|
|
96
|
+
fpath,
|
|
97
|
+
'-h'
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
exec( cmd.join( ' ' ), done );
|
|
101
|
+
|
|
102
|
+
function done( error, stdout, stderr ) {
|
|
103
|
+
if ( error ) {
|
|
104
|
+
t.fail( error.message );
|
|
105
|
+
} else {
|
|
106
|
+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
|
|
107
|
+
t.strictEqual( stderr.toString(), expected+'\n', 'expected value' );
|
|
108
|
+
}
|
|
109
|
+
t.end();
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
tape( 'when invoked with a `--version` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
|
|
114
|
+
var cmd = [
|
|
115
|
+
EXEC_PATH,
|
|
116
|
+
fpath,
|
|
117
|
+
'--version'
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
exec( cmd.join( ' ' ), done );
|
|
121
|
+
|
|
122
|
+
function done( error, stdout, stderr ) {
|
|
123
|
+
if ( error ) {
|
|
124
|
+
t.fail( error.message );
|
|
125
|
+
} else {
|
|
126
|
+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
|
|
127
|
+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
|
|
128
|
+
}
|
|
129
|
+
t.end();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
tape( 'when invoked with a `-V` flag, the command-line interface prints the version to `stderr`', opts, function test( t ) {
|
|
134
|
+
var cmd = [
|
|
135
|
+
EXEC_PATH,
|
|
136
|
+
fpath,
|
|
137
|
+
'-V'
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
exec( cmd.join( ' ' ), done );
|
|
141
|
+
|
|
142
|
+
function done( error, stdout, stderr ) {
|
|
143
|
+
if ( error ) {
|
|
144
|
+
t.fail( error.message );
|
|
145
|
+
} else {
|
|
146
|
+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
|
|
147
|
+
t.strictEqual( stderr.toString(), PKG_VERSION+'\n', 'expected value' );
|
|
148
|
+
}
|
|
149
|
+
t.end();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
tape( 'the command-line interface supports specifying the number of generated pseudorandom numbers', opts, function test( t ) {
|
|
154
|
+
var cmd = [
|
|
155
|
+
EXEC_PATH,
|
|
156
|
+
fpath,
|
|
157
|
+
'--iter 10'
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
exec( cmd.join( ' ' ), done );
|
|
161
|
+
|
|
162
|
+
function done( error, stdout, stderr ) {
|
|
163
|
+
var results;
|
|
164
|
+
var v;
|
|
165
|
+
var i;
|
|
166
|
+
if ( error ) {
|
|
167
|
+
t.fail( error.message );
|
|
168
|
+
} else {
|
|
169
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
170
|
+
|
|
171
|
+
results = stdout.toString();
|
|
172
|
+
results = results.split( '\n' );
|
|
173
|
+
|
|
174
|
+
// 10 numbers + trailing newline:
|
|
175
|
+
t.strictEqual( results.length, 11, 'has expected length' );
|
|
176
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
177
|
+
v = parseFloat( results[ i ] );
|
|
178
|
+
t.strictEqual( typeof v, 'number', 'has expected type' );
|
|
179
|
+
t.strictEqual( isnan( v ), false, 'is not NaN' );
|
|
180
|
+
t.strictEqual( isInteger( v ), true, 'is an integer' );
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
t.end();
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
tape( 'the command-line interface supports specifying whether to generate normalized pseudorandom numbers', opts, function test( t ) {
|
|
188
|
+
var cmd = [
|
|
189
|
+
EXEC_PATH,
|
|
190
|
+
fpath,
|
|
191
|
+
'--iter 10',
|
|
192
|
+
'--normalized'
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
exec( cmd.join( ' ' ), done );
|
|
196
|
+
|
|
197
|
+
function done( error, stdout, stderr ) {
|
|
198
|
+
var results;
|
|
199
|
+
var v;
|
|
200
|
+
var i;
|
|
201
|
+
if ( error ) {
|
|
202
|
+
t.fail( error.message );
|
|
203
|
+
} else {
|
|
204
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
205
|
+
|
|
206
|
+
results = stdout.toString();
|
|
207
|
+
results = results.split( '\n' );
|
|
208
|
+
|
|
209
|
+
// 10 numbers + trailing newline:
|
|
210
|
+
t.strictEqual( results.length, 11, 'has expected length' );
|
|
211
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
212
|
+
v = parseFloat( results[ i ] );
|
|
213
|
+
t.strictEqual( typeof v, 'number', 'has expected type' );
|
|
214
|
+
t.strictEqual( isnan( v ), false, 'is not NaN' );
|
|
215
|
+
t.strictEqual( v >= 0.0 && v < 1.0, true, 'has expected range' );
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
t.end();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
tape( 'the command-line interface supports specifying the pseudorandom number generator seed', opts, function test( t ) {
|
|
223
|
+
var rand;
|
|
224
|
+
var cmd;
|
|
225
|
+
|
|
226
|
+
cmd = [
|
|
227
|
+
EXEC_PATH,
|
|
228
|
+
fpath,
|
|
229
|
+
'--iter 10',
|
|
230
|
+
'--seed 12345'
|
|
231
|
+
];
|
|
232
|
+
|
|
233
|
+
// Note: we assume that the underlying generator is the following PRNG...
|
|
234
|
+
rand = mt19937({
|
|
235
|
+
'seed': 12345
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
exec( cmd.join( ' ' ), done );
|
|
239
|
+
|
|
240
|
+
function done( error, stdout, stderr ) {
|
|
241
|
+
var results;
|
|
242
|
+
var v;
|
|
243
|
+
var i;
|
|
244
|
+
if ( error ) {
|
|
245
|
+
t.fail( error.message );
|
|
246
|
+
} else {
|
|
247
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
248
|
+
|
|
249
|
+
results = stdout.toString();
|
|
250
|
+
results = results.split( '\n' );
|
|
251
|
+
|
|
252
|
+
// 10 numbers + trailing newline:
|
|
253
|
+
t.strictEqual( results.length, 11, 'has expected length' );
|
|
254
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
255
|
+
v = parseFloat( results[ i ] );
|
|
256
|
+
t.strictEqual( v, rand(), 'prints expected value' );
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
t.end();
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
tape( 'the command-line interface supports specifying a custom delimiter', opts, function test( t ) {
|
|
264
|
+
var cmd = [
|
|
265
|
+
EXEC_PATH,
|
|
266
|
+
fpath,
|
|
267
|
+
'--iter 10',
|
|
268
|
+
'--sep aba'
|
|
269
|
+
];
|
|
270
|
+
|
|
271
|
+
exec( cmd.join( ' ' ), done );
|
|
272
|
+
|
|
273
|
+
function done( error, stdout, stderr ) {
|
|
274
|
+
var results;
|
|
275
|
+
var v;
|
|
276
|
+
var i;
|
|
277
|
+
if ( error ) {
|
|
278
|
+
t.fail( error.message );
|
|
279
|
+
} else {
|
|
280
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
281
|
+
|
|
282
|
+
results = stdout.toString();
|
|
283
|
+
results = results.split( '\n' );
|
|
284
|
+
|
|
285
|
+
// Only trailing newline:
|
|
286
|
+
t.strictEqual( results.length, 2, 'has expected length' );
|
|
287
|
+
|
|
288
|
+
results = results[ 0 ].split( 'aba' );
|
|
289
|
+
t.strictEqual( results.length, 10, 'has expected length' );
|
|
290
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
291
|
+
v = parseFloat( results[ i ] );
|
|
292
|
+
t.strictEqual( typeof v, 'number', 'has expected type' );
|
|
293
|
+
t.strictEqual( isnan( v ), false, 'is not NaN' );
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
t.end();
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
tape( 'the command-line interface supports specifying a pseudorandom number generator state', opts, function test( t ) {
|
|
301
|
+
var state;
|
|
302
|
+
var spath;
|
|
303
|
+
var rand;
|
|
304
|
+
var cmd;
|
|
305
|
+
var len;
|
|
306
|
+
|
|
307
|
+
spath = resolve( __dirname, 'fixtures', 'state.txt' );
|
|
308
|
+
state = readFileSync( spath );
|
|
309
|
+
|
|
310
|
+
cmd = [
|
|
311
|
+
EXEC_PATH,
|
|
312
|
+
fpath,
|
|
313
|
+
'--iter 10',
|
|
314
|
+
'--state "'+spath+'"'
|
|
315
|
+
];
|
|
316
|
+
|
|
317
|
+
// Explicitly copy the `Buffer`, as, in older Node.js versions, `Buffer` is not a typed array:
|
|
318
|
+
len = state.length;
|
|
319
|
+
state = gcopy( len, state, 1, new Uint8Array( len ), 1 );
|
|
320
|
+
state = new Uint32Array( state.buffer, state.byteOffset, len/Uint32Array.BYTES_PER_ELEMENT ); // eslint-disable-line max-len
|
|
321
|
+
|
|
322
|
+
// Note: we assume that the underlying generator is the following PRNG...
|
|
323
|
+
rand = mt19937({
|
|
324
|
+
'state': state
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
exec( cmd.join( ' ' ), done );
|
|
328
|
+
|
|
329
|
+
function done( error, stdout, stderr ) {
|
|
330
|
+
var results;
|
|
331
|
+
var v;
|
|
332
|
+
var i;
|
|
333
|
+
if ( error ) {
|
|
334
|
+
t.fail( error.message );
|
|
335
|
+
} else {
|
|
336
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
337
|
+
|
|
338
|
+
results = stdout.toString();
|
|
339
|
+
results = results.split( '\n' );
|
|
340
|
+
|
|
341
|
+
// 10 numbers + trailing newline:
|
|
342
|
+
t.strictEqual( results.length, 11, 'has expected length' );
|
|
343
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
344
|
+
v = parseFloat( results[ i ] );
|
|
345
|
+
t.strictEqual( v, rand(), 'prints expected value' );
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
t.end();
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
tape( 'the command-line interface supports specifying an output file path for saving the pseudorandom number generator state upon exit', opts, function test( t ) {
|
|
353
|
+
var state;
|
|
354
|
+
var spath;
|
|
355
|
+
var rand;
|
|
356
|
+
var cmd;
|
|
357
|
+
var arr;
|
|
358
|
+
var i;
|
|
359
|
+
|
|
360
|
+
// Define an output path for a temporary file:
|
|
361
|
+
spath = resolve( __dirname, 'fixtures', 'tmp_state_snapshot.txt' );
|
|
362
|
+
|
|
363
|
+
// Ensure that the temporary file does not already exist:
|
|
364
|
+
if ( existsSync( spath ) ) {
|
|
365
|
+
t.fail( 'temporary file should not exist: '+spath );
|
|
366
|
+
return t.end();
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
cmd = [
|
|
370
|
+
EXEC_PATH,
|
|
371
|
+
fpath,
|
|
372
|
+
'--seed 12345',
|
|
373
|
+
'--iter 10',
|
|
374
|
+
'--snapshot "'+spath+'"'
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
// Note: we assume that the underlying generator is the following PRNG...
|
|
378
|
+
rand = mt19937({
|
|
379
|
+
'seed': 12345
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// Advance the PRNG state:
|
|
383
|
+
arr = [];
|
|
384
|
+
for ( i = 0; i < 10; i++ ) {
|
|
385
|
+
arr.push( rand() );
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Get the current PRNG state:
|
|
389
|
+
state = rand.state;
|
|
390
|
+
|
|
391
|
+
exec( cmd.join( ' ' ), done );
|
|
392
|
+
|
|
393
|
+
function done( error, stdout, stderr ) {
|
|
394
|
+
var snapshot;
|
|
395
|
+
var results;
|
|
396
|
+
var len;
|
|
397
|
+
var err;
|
|
398
|
+
var v;
|
|
399
|
+
var i;
|
|
400
|
+
if ( error ) {
|
|
401
|
+
t.fail( error.message );
|
|
402
|
+
} else {
|
|
403
|
+
t.strictEqual( stderr.toString(), '', 'does not print to `stderr`' );
|
|
404
|
+
|
|
405
|
+
results = stdout.toString();
|
|
406
|
+
results = results.split( '\n' );
|
|
407
|
+
|
|
408
|
+
// 10 numbers + trailing newline:
|
|
409
|
+
t.strictEqual( results.length, 11, 'has expected length' );
|
|
410
|
+
for ( i = 0; i < results.length-1; i++ ) {
|
|
411
|
+
v = parseFloat( results[ i ] );
|
|
412
|
+
t.strictEqual( v, arr[ i ], 'prints expected value' );
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Check that the PRNG state was written to a file:
|
|
416
|
+
t.strictEqual( existsSync( spath ), true, 'temporary snapshot file exists' );
|
|
417
|
+
|
|
418
|
+
// Verify the file contents (note: we need to explicitly copy the `Buffer`, as, in older Node.js versions, `Buffer` is not a typed array):
|
|
419
|
+
snapshot = readFileSync( spath );
|
|
420
|
+
len = snapshot.length;
|
|
421
|
+
snapshot = gcopy( len, snapshot, 1, new Uint8Array( len ), 1 );
|
|
422
|
+
snapshot = new Uint32Array( snapshot.buffer, snapshot.byteOffset, len/Uint32Array.BYTES_PER_ELEMENT ); // eslint-disable-line max-len
|
|
423
|
+
|
|
424
|
+
t.deepEqual( snapshot, state, 'file contains expected contents' );
|
|
425
|
+
|
|
426
|
+
// Remove the temporary file:
|
|
427
|
+
err = unlinkSync( spath );
|
|
428
|
+
if ( err ) {
|
|
429
|
+
t.fail( err.message );
|
|
430
|
+
}
|
|
431
|
+
t.strictEqual( existsSync( spath ), false, 'temporary snapshot file does not exist' );
|
|
432
|
+
}
|
|
433
|
+
t.end();
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
tape( 'if provided an invalid pseudorandom number generator state, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) {
|
|
438
|
+
var spath;
|
|
439
|
+
var cmd;
|
|
440
|
+
|
|
441
|
+
spath = resolve( __dirname, 'fixtures', 'bad_state.txt' );
|
|
442
|
+
|
|
443
|
+
cmd = [
|
|
444
|
+
EXEC_PATH,
|
|
445
|
+
fpath,
|
|
446
|
+
'--iter 10',
|
|
447
|
+
'--state "'+spath+'"'
|
|
448
|
+
];
|
|
449
|
+
|
|
450
|
+
exec( cmd.join( ' ' ), done );
|
|
451
|
+
|
|
452
|
+
function done( error, stdout, stderr ) {
|
|
453
|
+
if ( error ) {
|
|
454
|
+
t.pass( error.message );
|
|
455
|
+
t.strictEqual( error.code, 1, 'expected exit code' );
|
|
456
|
+
} else {
|
|
457
|
+
t.fail( 'should error' );
|
|
458
|
+
}
|
|
459
|
+
t.strictEqual( stdout.toString(), '', 'does not print to `stdout`' );
|
|
460
|
+
t.strictEqual( stderr.toString().length > 0, true, 'prints to `stderr`' );
|
|
461
|
+
t.end();
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
tape( 'if an error is encountered when writing pseudorandom number generator state to file, the command-line interface prints an error and sets a non-zero exit code', opts, function test( t ) {
|
|
466
|
+
var script;
|
|
467
|
+
var opts;
|
|
468
|
+
var cmd;
|
|
469
|
+
|
|
470
|
+
script = readFileSync( resolve( __dirname, 'fixtures', 'write_error.js.txt' ), {
|
|
471
|
+
'encoding': 'utf8'
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
// Replace single quotes with double quotes:
|
|
475
|
+
script = replace( script, '\'', '"' );
|
|
476
|
+
|
|
477
|
+
cmd = [
|
|
478
|
+
EXEC_PATH,
|
|
479
|
+
'-e',
|
|
480
|
+
'\''+script+'\''
|
|
481
|
+
];
|
|
482
|
+
|
|
483
|
+
opts = {
|
|
484
|
+
'cwd': __dirname
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
exec( cmd.join( ' ' ), opts, done );
|
|
488
|
+
|
|
489
|
+
function done( error, stdout, stderr ) {
|
|
490
|
+
if ( error ) {
|
|
491
|
+
t.pass( error.message );
|
|
492
|
+
t.strictEqual( error.code, 1, 'expected exit code' );
|
|
493
|
+
}
|
|
494
|
+
// Pseudorandom numbers have already been written to `stdout` by the time the error is encountered...
|
|
495
|
+
t.strictEqual( stdout.toString().length > 0, true, 'prints to `stdout`' );
|
|
496
|
+
t.strictEqual( stderr.toString(), 'Error: beep\n', 'expected value' );
|
|
497
|
+
t.end();
|
|
498
|
+
}
|
|
499
|
+
});
|