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.readFile', function (t) {
|
10 | t.plan(2);
|
11 | var sm = staticModule({
|
12 | fs: { readFile: readFile }
|
13 | }, { vars: { __dirname: __dirname + '/fs' } });
|
14 | readStream('readfile.js').pipe(sm).pipe(concat(function (body) {
|
15 | t.equal(body.toString('utf8').replace(/;/g,'').trim(),
|
16 | 'process.nextTick(function(){(function (err, src) {\n'
|
17 | + ' console.log(src)\n'
|
18 | + '})(null,"beep boop\\n")})'
|
19 | );
|
20 | Function(['console'],body)({ log: log });
|
21 | function log (msg) { t.equal(msg, 'beep boop\n') }
|
22 | }));
|
23 | });
|
24 |
|
25 | test('fs.readFileSync', function (t) {
|
26 | t.plan(2);
|
27 | var sm = staticModule({
|
28 | fs: { readFileSync: readFileSync }
|
29 | }, { vars: { __dirname: __dirname + '/fs' } });
|
30 | readStream('html.js').pipe(sm).pipe(concat(function (body) {
|
31 | t.equal(body.toString('utf8'),
|
32 | 'var html = "EXTERMINATE\\n";\n'
|
33 | + 'console.log(html);\n'
|
34 | );
|
35 | Function(['console'],body)({ log: log });
|
36 | function log (msg) { t.equal(msg, 'EXTERMINATE\n') }
|
37 | }));
|
38 | });
|
39 |
|
40 | function readStream (file) {
|
41 | return fs.createReadStream(path.join(__dirname, 'fs', file));
|
42 | }
|
43 |
|
44 | function readFile (file, cb) {
|
45 | var stream = through(write, end);
|
46 | stream.push('process.nextTick(function(){(' + cb + ')(null,');
|
47 | return fs.createReadStream(file).pipe(quote()).pipe(stream);
|
48 |
|
49 | function write (buf, enc, next) { this.push(buf); next() }
|
50 | function end (next) { this.push(')})'); this.push(null); next() }
|
51 | }
|
52 |
|
53 | function readFileSync (file, opts) {
|
54 | return fs.createReadStream(file).pipe(quote());
|
55 | }
|