-
-
Notifications
You must be signed in to change notification settings - Fork 230
/
run-instrument.test.js
209 lines (193 loc) · 7.44 KB
/
run-instrument.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* globals describe, it, beforeEach, afterEach */
var assert = require('chai').assert,
path = require('path'),
fs = require('fs'),
mkdirp = require('mkdirp'),
rimraf = require('rimraf'),
codeRoot = path.resolve(__dirname, 'sample-code'),
outputDir = path.resolve(__dirname, 'coverage'),
configuration = require('../lib/config'),
instrument = require('../lib/run-instrument'),
ms = require('memory-streams'),
vm = require('vm'),
hijack = require('./hijack-streams'),
wrap = hijack.wrap;
describe('run instrument', function () {
function getConfig(overrides) {
var cfg = configuration.loadObject({
verbose: false,
instrumentation: {
root: codeRoot
},
reporting: {
dir: outputDir
}
}, overrides);
return cfg;
}
function assertValidCode(code) {
assert.doesNotThrow(function () {
vm.createScript(code, 'foo.js');
}, "Invalid code generated; logging interference perhaps?");
}
function outFile(f) {
return path.resolve(outputDir, f);
}
beforeEach(function () {
mkdirp.sync(outputDir);
});
afterEach(function () {
rimraf.sync(outputDir);
});
describe('single file', function () {
var memStream,
origWrite;
beforeEach(function () {
memStream = new ms.WritableStream();
origWrite = process.stdout.write;
process.stdout.write = function () {
memStream.write.apply(memStream, Array.prototype.slice.call(arguments));
};
});
function reset(cb) {
process.stdout.write = origWrite;
cb();
}
afterEach(reset);
it('works with default options for a single file', function (cb) {
instrument.run(getConfig(), {input: path.resolve(codeRoot, 'foo.js')}, function (err) {
assert.ok(!err);
assertValidCode(memStream.toString());
reset(cb);
});
});
it('preserves comments in output', function (cb) {
instrument.run(getConfig({instrumentation: {'preserve-comments': true}}),
{input: path.resolve(codeRoot, 'foo.js')},
function (err) {
assert.ok(!err);
assert.ok(memStream.toString().match(/call bind in mainline/));
assertValidCode(memStream.toString());
reset(cb);
});
});
it('works with compact as default', function (cb) {
instrument.run(getConfig(),
{input: path.resolve(codeRoot, 'foo.js')},
function (err) {
assert.ok(!err);
var pass1 = memStream.toString();
memStream = new ms.WritableStream();
instrument.run(getConfig({instrumentation: {compact: false}}),
{input: path.resolve(codeRoot, 'foo.js')},
function (err) {
assert.ok(!err);
var pass2 = memStream.toString();
assert.ok(pass2.length > pass1.length);
reset(cb);
});
});
});
it('works with explicit output options', function (cb) {
var outFile = path.resolve(outputDir, 'foo.js');
instrument.run(getConfig(),
{
input: path.resolve(codeRoot, 'foo.js'),
output: outFile
},
function (err) {
assert.ok(!err);
assert.ok(fs.existsSync(outFile));
assertValidCode(fs.readFileSync(outFile,'utf8'));
reset(cb);
});
});
});
describe('multiple files', function () {
beforeEach(hijack.silent);
afterEach(hijack.reset);
it('instruments multiple files', function (cb) {
cb = wrap(cb);
instrument.run(getConfig({ verbose: true }), {input: codeRoot, output: outputDir },
function (err) {
assert.ok(!err);
assert.ok(fs.existsSync(outFile('foo.js')));
assert.ok(fs.existsSync(outFile('context.js')));
assert.ok(!fs.existsSync(outFile('node_modules/adder.js')));
cb();
});
});
it('saves baseline coverage when requested', function (cb) {
cb = wrap(cb);
instrument.run(getConfig({
instrumentation: {
'save-baseline': true,
'baseline-file': outFile('baseline.raw.json'),
'default-excludes': false,
'excludes': [ '**/bad.js' ]
}
}),{input: codeRoot, output: outputDir },
function (err) {
assert.ok(!err);
assert.ok(fs.existsSync(outFile('foo.js')));
assert.ok(!fs.existsSync(outFile('node_modules/adder.js')));
assert.ok(fs.existsSync(outFile('baseline.raw.json')));
cb();
});
});
});
describe('negative tests', function () {
beforeEach(hijack.silent);
afterEach(hijack.reset);
it('barfs on no inputs', function (cb) {
cb = wrap(cb);
instrument.run(getConfig(), null,
function (err) {
assert.ok(err);
assert.equal(err.message, 'No input specified');
cb();
});
});
it('barfs on directory coverage when output option not provided', function (cb) {
cb = wrap(cb);
instrument.run(getConfig(), {input: codeRoot },
function (err) {
assert.ok(err);
assert.equal(err.message, 'Need an output directory when input is a directory!');
cb();
});
});
it('barfs on directory coverage when output == input', function (cb) {
cb = wrap(cb);
instrument.run(getConfig(), {input: codeRoot, output: codeRoot },
function (err) {
assert.ok(err);
assert.equal(err.message, 'Cannot instrument into the same directory/ file as input!');
cb();
});
});
});
describe('complete copy', function () {
beforeEach(hijack.silent);
afterEach(hijack.reset);
it('does not copy non-JS files by default', function (cb) {
cb = wrap(cb);
instrument.run(getConfig(), {input: codeRoot, output: outputDir },
function (err) {
assert.ok(!err);
assert.ok(!fs.existsSync(outFile('styles.css')));
cb();
});
});
it('copies non-JS files when requested', function (cb) {
cb = wrap(cb);
instrument.run(getConfig({ instrumentation: { 'complete-copy': true }}),
{input: codeRoot, output: outputDir },
function (err) {
assert.ok(!err);
assert.ok(fs.existsSync(outFile('styles.css')));
cb();
});
});
});
});