1 | var test = require('tap').test;
|
2 | var spawn = require('child_process').spawn;
|
3 | var browserify = require('../');
|
4 | var path = require('path');
|
5 | var concat = require('concat-stream');
|
6 | var vm = require('vm');
|
7 | var fs = require('fs');
|
8 | var through = require('through2');
|
9 | var temp = require('temp');
|
10 | temp.track();
|
11 | var tmpdir = temp.mkdirSync({prefix: 'browserify-test'});
|
12 |
|
13 | test('bare', function (t) {
|
14 | t.plan(4);
|
15 |
|
16 | var cwd = process.cwd();
|
17 | process.chdir(__dirname);
|
18 |
|
19 | var ps = spawn(process.execPath, [
|
20 | path.resolve(__dirname, '../bin/cmd.js'),
|
21 | '-', '--bare'
|
22 | ]);
|
23 | ps.stdout.pipe(concat(function (body) {
|
24 | vm.runInNewContext(body, {
|
25 | Buffer: function (s) { return s.toLowerCase() },
|
26 | console: {
|
27 | log: function (msg) { t.equal(msg, 'abc') }
|
28 | }
|
29 | });
|
30 | vm.runInNewContext(body, {
|
31 | Buffer: Buffer,
|
32 | console: {
|
33 | log: function (msg) {
|
34 | t.ok(Buffer.isBuffer(msg));
|
35 | t.equal(msg.toString('utf8'), 'ABC')
|
36 | }
|
37 | }
|
38 | });
|
39 | }));
|
40 | ps.stdin.end('console.log(Buffer("ABC"))');
|
41 |
|
42 | ps.on('exit', function (code) {
|
43 | t.equal(code, 0);
|
44 | });
|
45 | });
|
46 |
|
47 | test('bare api', function (t) {
|
48 | t.plan(3);
|
49 |
|
50 | var input = through();
|
51 | var b = browserify(input, { bare: true });
|
52 | b.bundle().pipe(concat(function (body) {
|
53 | vm.runInNewContext(body, {
|
54 | Buffer: function (s) { return s.toLowerCase() },
|
55 | console: {
|
56 | log: function (msg) { t.equal(msg, 'abc') }
|
57 | }
|
58 | });
|
59 | vm.runInNewContext(body, {
|
60 | Buffer: Buffer,
|
61 | console: {
|
62 | log: function (msg) {
|
63 | t.ok(Buffer.isBuffer(msg));
|
64 | t.equal(msg.toString('utf8'), 'ABC')
|
65 | }
|
66 | }
|
67 | });
|
68 | }));
|
69 | input.end('console.log(Buffer("ABC"))');
|
70 | });
|
71 |
|
72 | test('bare inserts __filename,__dirname but not process,global,Buffer', function (t) {
|
73 | t.plan(2);
|
74 |
|
75 | var file = path.resolve(__dirname, 'bare/main.js');
|
76 | var ps = spawn(process.execPath, [
|
77 | path.resolve(__dirname, '../bin/cmd.js'),
|
78 | file,
|
79 | '--bare'
|
80 | ]);
|
81 |
|
82 | ps.stdout.pipe(concat(function (body) {
|
83 | vm.runInNewContext(body, {
|
84 | require: require,
|
85 | __dirname: process.cwd(),
|
86 | console: {
|
87 | log: function (msg) {
|
88 | t.same(msg, [
|
89 | path.join(__dirname, 'bare'),
|
90 | path.join(__dirname, 'bare/main.js'),
|
91 | 'undefined',
|
92 | 'undefined',
|
93 | 'undefined'
|
94 | ]);
|
95 | }
|
96 | }
|
97 | });
|
98 | }));
|
99 | ps.stdin.end();
|
100 |
|
101 | ps.on('exit', function (code) {
|
102 | t.equal(code, 0);
|
103 | });
|
104 | });
|
105 |
|
106 | test('bare inserts dynamic __filename,__dirname', function (t) {
|
107 | t.plan(2);
|
108 |
|
109 | var file = path.join(tmpdir, 'dirname-filename.js');
|
110 |
|
111 | fs.writeFileSync(
|
112 | file,
|
113 | fs.readFileSync(path.resolve(__dirname, 'bare/dirname-filename.js'))
|
114 | );
|
115 |
|
116 | var ps = spawn(process.execPath, [
|
117 | path.resolve(__dirname, '../bin/cmd.js'),
|
118 | file,
|
119 | '--bare'
|
120 | ]);
|
121 |
|
122 | ps.stdout.pipe(concat(function (body) {
|
123 | vm.runInNewContext(body, {
|
124 | require: require,
|
125 | __dirname: process.cwd(),
|
126 | console: {
|
127 | log: function (msg) {
|
128 | t.same(msg, [
|
129 | path.dirname(file),
|
130 | file
|
131 | ]);
|
132 | }
|
133 | }
|
134 | });
|
135 | }));
|
136 | ps.stdin.end();
|
137 |
|
138 | ps.on('exit', function (code) {
|
139 | t.equal(code, 0);
|
140 | });
|
141 | });
|
142 |
|
143 | test('bare inserts dynamic __filename,__dirname with basedir', function (t) {
|
144 | t.plan(2);
|
145 |
|
146 | var file = 'dirname-filename.js';
|
147 | var ps = spawn(process.execPath, [
|
148 | path.resolve(__dirname, '../bin/cmd.js'),
|
149 | file,
|
150 | '--bare',
|
151 | '--basedir=' + path.join(__dirname, 'bare')
|
152 | ]);
|
153 |
|
154 | ps.stdout.pipe(concat(function (body) {
|
155 | vm.runInNewContext(body, {
|
156 | require: require,
|
157 | __dirname: process.cwd(),
|
158 | console: {
|
159 | log: function (msg) {
|
160 | t.same(msg, [
|
161 | __dirname,
|
162 | path.join(__dirname, file)
|
163 | ]);
|
164 | }
|
165 | }
|
166 | });
|
167 | }));
|
168 | ps.stdin.end();
|
169 |
|
170 | ps.on('exit', function (code) {
|
171 | t.equal(code, 0);
|
172 | });
|
173 | });
|