Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

Expand Down
19 changes: 14 additions & 5 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ describe('node-lambda', function () {
});

describe('_postInstallScript', function () {
const postInstallScriptPath = path.join(codeDirectory, 'post_install.sh');
var hook;
/**
* Capture console output
Expand All @@ -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();
});
});
Expand Down