-
Notifications
You must be signed in to change notification settings - Fork 535
/
protect-fail.test.js
56 lines (52 loc) · 1.96 KB
/
protect-fail.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
var applyPatch = require('../lib/protect/apply-patch');
var path = require('path');
var fs = require('fs');
var thenfs = require('then-fs');
var test = require('tape');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var snyk = require('../lib');
test('bad patch file does not apply', function (t) {
// check the target file first
var root = path.resolve(__dirname, './fixtures/semver-patch-fail/');
var dir = path.resolve(root, './node_modules/semver');
var semver = fs.readFileSync(dir + '/semver.js', 'utf8');
t.ok('original semver loaded');
var old = snyk.config.get('disable-analytics');
snyk.config.set('disable-analytics', '1');
applyPatch(root + '/363ce409-2d19-46da-878a-e059df2d39bb.snyk-patch', {
source: dir,
name: 'semver',
version: '4.3.1',
id: 'npm:semver:20150403',
from: ['semver@4.3.1'],
}, true).then(function () {
t.fail('patch successfully applied');
fs.writeFileSync(dir + '/semver.js', semver);
}).catch(function (error) {
var semver2 = fs.readFileSync(dir + '/semver.js', 'utf8');
t.equal(semver, semver2, 'target was untouched');
t.equal(error.code, 'FAIL_PATCH', 'patch failed, task exited correctly');
}).then(function () {
// clean up
var noop = function () {};
var promises = [];
promises.push(thenfs.unlink(dir + '/semver.js.orig').catch(noop));
promises.push(thenfs.unlink(dir + '/semver.js.rej').catch(noop));
promises.push(thenfs.unlink(dir + '/test/big-numbers.js.orig').catch(noop));
promises.push(thenfs.unlink(dir + '/test/big-numbers.js.rej').catch(noop));
fs.writeFileSync(dir + '/semver.js', semver);
return Promise.all(promises);
}).then(function () {
t.ok('clean up done');
}).catch(function (e) {
console.log(e);
t.fail('clean up failed');
}).then(function () {
if (old === undefined) {
snyk.config.del('disable-analytics');
} else {
snyk.config.set('disable-analytics', old);
}
t.end();
});
});