| 1 |
var staticModule = require('../'); |
| 2 |
var test = require('tape'); |
| 3 |
var concat = require('concat-stream'); |
| 4 |
var quote = require('quote-stream'); |
| 5 |
var through = require('through2'); |
| 6 |
var fs = require('fs'); |
| 7 |
var path = require('path'); |
| 8 |
|
| 9 |
test('fs.readFileSync twice', function (t) { |
| 10 |
var expected = [ 'EXTERMINATE\n', 'beep boop\n' ]; |
| 11 |
t.plan(expected.length + 1); |
| 12 |
var sm = staticModule({ |
| 13 |
fs: { readFileSync: readFileSync } |
| 14 |
}, { vars: { __dirname: __dirname + '/fs_twice' } }); |
| 15 |
readStream('html.js').pipe(sm).pipe(concat(function (body) { |
| 16 |
t.equal(body.toString('utf8'), |
| 17 |
'var a = "EXTERMINATE\\n";\n' |
| 18 |
+ 'var b = "beep boop\\n";\n' |
| 19 |
+ 'console.log(a);\n' |
| 20 |
+ 'console.log(b);\n' |
| 21 |
); |
| 22 |
Function(['console'],body)({ log: log }); |
| 23 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 24 |
})); |
| 25 |
}); |
| 26 |
|
| 27 |
test('fs.readFileSync twice in vars', function (t) { |
| 28 |
var expected = [ 'EXTERMINATE\n', 'beep boop\n' ]; |
| 29 |
t.plan(expected.length + 1); |
| 30 |
var sm = staticModule({ |
| 31 |
fs: { readFileSync: readFileSync } |
| 32 |
}, { vars: { __dirname: __dirname + '/fs_twice' } }); |
| 33 |
readStream('vars.js').pipe(sm).pipe(concat(function (body) { |
| 34 |
t.equal(body.toString('utf8').trim(), |
| 35 |
'var a = "EXTERMINATE\\n",\n' |
| 36 |
+ ' b = "beep boop\\n";\n' |
| 37 |
+ 'console.log(a);\n' |
| 38 |
+ 'console.log(b);' |
| 39 |
); |
| 40 |
Function(['console'],body)({ log: log }); |
| 41 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 42 |
})); |
| 43 |
}); |
| 44 |
|
| 45 |
test('fs.readFileSync 4x', function (t) { |
| 46 |
var expected = [ |
| 47 |
'EXTERMINATE\n', 'beep boop\n', 'EXTERMINATE\n', 'beep boop\n' |
| 48 |
]; |
| 49 |
t.plan(expected.length + 1); |
| 50 |
var sm = staticModule({ |
| 51 |
fs: { readFileSync: readFileSync } |
| 52 |
}, { vars: { __dirname: __dirname + '/fs_twice' } }); |
| 53 |
readStream('4x.js').pipe(sm).pipe(concat(function (body) { |
| 54 |
t.equal(body.toString('utf8').trim(), |
| 55 |
'var a = "EXTERMINATE\\n";\n' |
| 56 |
+ 'var b = "beep boop\\n";\n' |
| 57 |
+ 'var c = "EXTERMINATE\\n";\n' |
| 58 |
+ 'var d = "beep boop\\n";\n' |
| 59 |
+ 'console.log(a);\n' |
| 60 |
+ 'console.log(b);\n' |
| 61 |
+ 'console.log(c);\n' |
| 62 |
+ 'console.log(d);' |
| 63 |
); |
| 64 |
Function(['console'],body)({ log: log }); |
| 65 |
function log (msg) { t.equal(msg, expected.shift()) } |
| 66 |
})); |
| 67 |
}); |
| 68 |
|
| 69 |
function readStream (file) { |
| 70 |
return fs.createReadStream(path.join(__dirname, 'fs_twice', file)); |
| 71 |
} |
| 72 |
|
| 73 |
function readFile (file, cb) { |
| 74 |
var stream = through(write, end); |
| 75 |
stream.push('process.nextTick(function(){(' + cb + ')(null,'); |
| 76 |
return fs.createReadStream(file).pipe(quote()).pipe(stream); |
| 77 |
|
| 78 |
function write (buf, enc, next) { this.push(buf); next() } |
| 79 |
function end (next) { this.push(')})'); this.push(null); next() } |
| 80 |
} |
| 81 |
|
| 82 |
function readFileSync (file, opts) { |
| 83 |
return fs.createReadStream(file).pipe(quote()); |
| 84 |
} |