Skip to content

Commit

Permalink
fix: Fix for writing a flag after each patch application
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Feb 2, 2018
1 parent 28019b2 commit 4e20b64
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
28 changes: 22 additions & 6 deletions lib/protect/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ function patch(vulns, live, cwd) {
if (!exists) {
return patch;
}

debug("Previous flag found =", exists);
// else revert the patch
debug("Restroring file back to .orig");
return new Promise(function (resolve, reject) {
recursive(vuln.source, function (error, files) {
if (error) {
Expand All @@ -112,13 +113,28 @@ function patch(vulns, live, cwd) {
}

debug('applying patch file for %s: \n%s\n%s', vuln.id, url, patch);

return applyPatch(patch, vuln, live).then(function () {

// return applyPatch(patch, vuln, live)
// // now write the flag
// .then(function () {
// return true;
// }, function (e) {
// errorList.push(e);
// return false;
// }).then(function (ok) {
// return ok ? vuln : false;
// });

return applyPatch(patch, vuln, live)
// now write the flag
.then(writePatchFlag(now, vuln))
.then(function () {
return true;
}, function (e) {
errorList.push(e);
return false;
}).then(function (ok) {
})
.then(function (ok) {
return ok ? vuln : false;
});
});
Expand All @@ -138,8 +154,8 @@ function patch(vulns, live, cwd) {
debug('[skipping - dry run]');
return patched;
}

return Promise.all(patched.map(writePatchFlag.bind(null, now)));
return Promise.all(patched);
// return Promise.all(patched.map(writePatchFlag.bind(null, now)));
}).then(function (patched) {
patched = patched.reduce(function (acc, curr) {
var key = curr.id + ':' + curr.__filename;
Expand Down
5 changes: 2 additions & 3 deletions lib/protect/write-patch-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@ function writePatchFlag(now, vuln) {
if (vuln.grouped && vuln.grouped.includes) {
debug('found addition vulns to write flag files for');
var writePromises = [fs.writeFile(flag, now.toJSON(), 'utf8')];
debug(flag);
vuln.grouped.includes.forEach(function (id) {
var fileSafeId = vuln.id.replace(/:/g, '-');
var flag = path.resolve(vuln.source, '.snyk-' + fileSafeId + '.flag');
debug(flag);
debug('Writing flag for grouped vulns', flag);
writePromises.push(fs.writeFile(flag, now.toJSON(), 'utf8'));
});
promise = Promise.all(writePromises);
} else {
debug('Writing flag for single vuln', flag);
promise = fs.writeFile(flag, now.toJSON(), 'utf8');
}

return promise.then(function () {
return vuln;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2018-01-26T15:53:51.804Z
2018-01-30T11:59:09.401Z
27 changes: 9 additions & 18 deletions test/protect-apply-same-patch-again.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const sinon = require('sinon');

// fixtures
const fixturesFolder = `${__dirname}/fixtures/protect-apply-same-patch-again/`;
const wizardAnswers = require(fixturesFolder + '/answers.json');
const wizardAnswers = require(fixturesFolder + 'answers.json');

const noop = function () {};

Expand All @@ -18,27 +18,20 @@ const writeSpy = sinon.spy();

//main proxy
const patch = proxyquire('../lib/protect/patch', {
'./get-vuln-source': function () {
'./get-vuln-source': () => {
console.info(fixturesFolder);
return fixturesFolder;
},
'./write-patch-flag': proxyquire('../lib/protect/write-patch-flag', {
'writePatchFlag': {
writePatchFlag: (now, vuln) => {
writePatchFlagSpy(now, vuln);
}
}
}),
// './write-patch-flag': function (now, vuln) {
// writePatchFlagSpy(now, vuln);
// return Promise.resolve(vuln);
// },
'then-fs': {
rename: function (filename) {
rename: (filename) => {
return Promise.resolve();
},
writeFile: function (filename, body) {
writeSpy(filename, body);
writeFile: (filename, body) => {
return Promise.resolve();
},
createWriteStream: function () {
Expand All @@ -52,7 +45,7 @@ const patch = proxyquire('../lib/protect/patch', {
},
'./apply-patch': proxyquire('../lib/protect/apply-patch', {
'child_process': {
exec: function (a, b, callback) {
exec: (a, b, callback) => {
// ignore dry run
if (a.indexOf('--dry-run') === -1) {
execSpy(a);
Expand All @@ -71,19 +64,17 @@ test('same patch is not applied again to the same package', (t) => {

return patch(tasks, true)
.then((res) => {
console.info(`===> Write flag`);
t.equal(writePatchFlagSpy.callCount, 1, 'Flag is written for first patch application');
console.log(`writePatchFlagSpy.callCount ${writePatchFlagSpy.callCount}`);
t.equal(writePatchFlagSpy.calledOnce, 'Flag is written only once still');
})
.then(() => {
console.info(`**************************\n Apply patch again \n**************************\n `);
// 2nd test
// try to apply patch again
// make sure it is skipped
// make sure write flag did not run this time
return patch(tasks, true).then((res) => {
console.info(`===> Write flag`);
t.equal(writePatchFlagSpy.callCount, 1, 'Same patch is not applied again');
t.equal(writePatchFlagSpy.callCount, 1, 'Flag is not written on second application of same patch');
console.log(`writePatchFlagSpy.callCount ${writePatchFlagSpy.callCount}`);
t.equal(writePatchFlagSpy.calledOnce, 'Flag is not written again');
})
})
.catch();
Expand Down

0 comments on commit 4e20b64

Please sign in to comment.