/
protect-patch-same-pkg.test.js
80 lines (76 loc) · 2.19 KB
/
protect-patch-same-pkg.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
var toTasks = require('../src/cli/commands/protect/tasks');
var test = require('tap').test;
var answers = require(__dirname + '/fixtures/patch-same-package-answers.json');
var proxyquire = require('proxyquire');
var sinon = require('sinon');
var noop = function() {};
// spies
var execSpy = sinon.spy();
var renameSpy = sinon.spy();
var writeSpy = sinon.spy();
// main proxy
var patch = proxyquire('../src/lib/protect/patch', {
glob: function(pattern, options, cb) {
cb(null, ['uglify.js.orig']);
},
'./get-vuln-source': function() {
return 'foo';
},
'./write-patch-flag': function(now, vuln) {
return Promise.resolve(vuln);
},
fs: {
renameSync: function(filename) {
renameSpy(filename);
},
writeFileSync: function(filename, body) {
writeSpy(filename, body);
},
createWriteStream: function() {
// fake event emitter (sort of)
return {
on: noop,
end: noop,
removeListener: noop,
emit: noop,
};
},
},
'./apply-patch': function(patch) {
execSpy(patch);
return Promise.resolve();
},
});
test('if two patches for same package selected, only newest runs', function(t) {
var latestId = 'uglify-js-20151024';
var tasks = toTasks(answers).patch;
return patch(tasks, true)
.then(function(res) {
t.equal(
Object.keys(res.patch).length,
tasks.length,
'two vulns went in, two came out',
);
t.match(execSpy.args[0], new RegExp(latestId), 'correct patch picked');
t.equal(execSpy.callCount, 1, 'patch only applied once');
})
.then(function() {
// 2nd test
execSpy = sinon.spy();
return patch(tasks.reverse(), true).then(function() {
t.match(
execSpy.args[0],
new RegExp(latestId),
'correct patch picked (reversed)',
);
t.equal(execSpy.callCount, 1, 'patch only applied once (reversed)');
});
});
});
test('different patches are not affected', function(t) {
var answers = require(__dirname + '/fixtures/forever-answers.json');
execSpy = sinon.spy();
return patch(toTasks(answers).patch, true).then(function() {
t.equal(execSpy.callCount, 2, 'two patches applied');
});
});