From 9ab7484b1bc0823eeec3f41c0ba3fe5161b613d4 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 26 Apr 2017 20:14:46 +0900 Subject: [PATCH 1/2] Fix as fs.exists is deprecated --- lib/main.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/main.js b/lib/main.js index fd339500..59b0a09a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -288,22 +288,20 @@ Lambda.prototype._postInstallScript = function (program, codeDirectory, callback var filePath = path.join(codeDirectory, scriptFilename); - fs.exists(filePath, function (exists) { - if (exists) { - console.log('=> Running post install script ' + scriptFilename); - exec(cmd, { env: process.env, cwd: codeDirectory, maxBuffer: maxBufferSize }, - function (error, stdout, stderr) { - - if (error) { - callback(error + " stdout: " + stdout + " stderr: " + stderr); - } else { - console.log("\t\t" + stdout); - callback(null); - } - }); - } else { - callback(null); + if (!fs.existsSync(filePath)) { + return callback(null); + } + console.log('=> Running post install script ' + scriptFilename); + exec(cmd, { + env: process.env, + cwd: codeDirectory, + maxBuffer: maxBufferSize + }, function (error, stdout, stderr) { + if (error) { + return callback(error + " stdout: " + stdout + " stderr: " + stderr); } + console.log("\t\t" + stdout); + callback(null); }); }; From 1cf53a389adf08275015b5335a2c8f0cc60f3f1f Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 26 Apr 2017 20:16:18 +0900 Subject: [PATCH 2/2] Add test when 'post_install.sh failed Some code was refactored --- test/main.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/test/main.js b/test/main.js index 4c7a7315..6c3ad7dc 100644 --- a/test/main.js +++ b/test/main.js @@ -289,6 +289,7 @@ describe('node-lambda', function () { }); describe('_postInstallScript', function () { + const postInstallScriptPath = path.join(codeDirectory, 'post_install.sh'); var hook; /** * Capture console output @@ -315,23 +316,31 @@ describe('node-lambda', function () { }); afterEach(function(){ hook.unhook(); + if (fs.existsSync(postInstallScriptPath)) + fs.unlinkSync(postInstallScriptPath); }); - it('should not throw any errors if no script', function (done) { lambda._postInstallScript(program, codeDirectory, function (err) { - assert.equal(err, null); + assert.isNull(err); + done(); + }); + }); + + it('should throw any errors if script fails', function (done) { + fs.writeFileSync(postInstallScriptPath, '___fails___'); + lambda._postInstallScript(program, codeDirectory, function (err) { + assert.match(err, /^Error: Command failed:/); done(); }); }); it('running script gives expected output', function (done) { - fs.writeFileSync(path.join(codeDirectory, 'post_install.sh'), fs.readFileSync(path.join('test', 'post_install.sh'))); + fs.writeFileSync(postInstallScriptPath, fs.readFileSync(path.join('test', 'post_install.sh'))); fs.chmodSync(path.join(codeDirectory, 'post_install.sh'), '755'); lambda._postInstallScript(program, codeDirectory, function (err) { - assert.equal(err, null); + assert.isNull(err); assert.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured()); - fs.unlinkSync(path.join(codeDirectory, 'post_install.sh')); done(); }); });