diff --git a/lib/main.js b/lib/main.js index 8c6235be..fd339500 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,16 +1,16 @@ 'use strict'; +var path = require('path'); var aws = require('aws-sdk'); var exec = require('child_process').exec; var fs = require('fs-extra'); var os = require('os'); -var packageJson = require('./../package.json'); -var path = require('path'); +var packageJson = require(path.join(__dirname, '..', 'package.json')); var minimatch = require('minimatch'); var async = require('async'); var zip = new require('node-zip')(); var dotenv = require('dotenv'); -var ScheduleEvents = require('./schedule_events'); +var ScheduleEvents = require(path.join(__dirname, 'schedule_events')); var maxBufferSize = 50 * 1024 * 1024; @@ -21,8 +21,11 @@ var Lambda = function () { }; Lambda.prototype._createSampleFile = function (file, boilerplateName) { - var exampleFile = process.cwd() + '/' + file; - var boilerplateFile = __dirname + '/' + (boilerplateName || file) + '.example'; + var exampleFile = path.join(process.cwd(), file); + var boilerplateFile = path.join( + __dirname, + (boilerplateName || file) + '.example' + ); if (!fs.existsSync(exampleFile)) { fs.writeFileSync(exampleFile, fs.readFileSync(boilerplateFile)); @@ -52,9 +55,9 @@ Lambda.prototype.run = function (program) { this._setRunTimeEnvironmentVars(program); } - var handler = require(process.cwd() + '/' + filename)[handlername]; - var event = require(process.cwd() + '/' + program.eventFile); - var context = require(process.cwd() + '/' + program.contextFile); + var handler = require(path.join(process.cwd(), filename))[handlername]; + var event = require(path.join(process.cwd(), program.eventFile)); + var context = require(path.join(process.cwd(), program.contextFile)); this._runHandler(handler, event, program, context); }; @@ -280,14 +283,14 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { }; Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) { - var script_filename = 'post_install.sh'; - var cmd = './' + script_filename + ' ' + program.environment; + var scriptFilename = 'post_install.sh'; + var cmd = path.join(codeDirectory, scriptFilename) + ' ' + program.environment; - var filePath = [codeDirectory, script_filename].join('/'); + var filePath = path.join(codeDirectory, scriptFilename); fs.exists(filePath, function (exists) { if (exists) { - console.log('=> Running post install script ' + script_filename); + console.log('=> Running post install script ' + scriptFilename); exec(cmd, { env: process.env, cwd: codeDirectory, maxBuffer: maxBufferSize }, function (error, stdout, stderr) { diff --git a/test/main.js b/test/main.js index 34e27d78..4c7a7315 100644 --- a/test/main.js +++ b/test/main.js @@ -1,10 +1,12 @@ 'use strict'; +var path = require('path'); +var os = require('os'); var chai = require('chai'); var program = require('commander'); var fs = require('fs-extra'); var Hoek = require('hoek'); -var lambda = require('../lib/main'); +var lambda = require(path.join(__dirname, '..', 'lib', 'main')); var _ = require('lodash'); var zip = require('node-zip'); var rimraf = require('rimraf'); @@ -41,7 +43,7 @@ describe('node-lambda', function () { after(function () { this.timeout(30000); // give it time to remove - fs.removeSync(`/tmp/${program.functionName}-[0-9]*`); + fs.removeSync(path.join(os.tmpDir(), `${program.functionName}-[0-9]*`)); }); it('version should be set', function () { @@ -216,7 +218,7 @@ describe('node-lambda', function () { ['node-lambda.png', 'test'].forEach(function (needle) { assert.notInclude(contents, needle, `Target: "${needle}"`); }); - contents = fs.readdirSync(codeDirectory + '/lib'); + contents = fs.readdirSync(path.join(codeDirectory, 'lib')); assert.notInclude(contents, 'main.js', 'Target: "lib/main.js"'); done(); }); @@ -232,18 +234,18 @@ describe('node-lambda', function () { }); it(funcName + ' should not include package.json when --prebuiltDirectory is set', function (done) { - var path = '.build_' + Date.now(); + var buildDir = '.build_' + Date.now(); after(function() { - rimraf.sync(path, fs); + rimraf.sync(buildDir, fs); }); - fs.mkdirSync(path); - fs.writeFileSync(path + '/testa'); - fs.writeFileSync(path + '/package.json'); + fs.mkdirSync(buildDir); + fs.writeFileSync(path.join(buildDir, 'testa')); + fs.writeFileSync(path.join(buildDir, 'package.json')); program.excludeGlobs = '*.json'; - program.prebuiltDirectory = path; - lambda[funcName](program, path, codeDirectory, true, function(err, result) { + program.prebuiltDirectory = buildDir; + lambda[funcName](program, buildDir, codeDirectory, true, function(err, result) { var contents = fs.readdirSync(codeDirectory); assert.notInclude(contents, 'package.json', 'Target: "packages.json"'); assert.include(contents, 'testa', 'Target: "testa"'); @@ -324,12 +326,12 @@ describe('node-lambda', function () { }); it('running script gives expected output', function (done) { - fs.writeFileSync(codeDirectory + '/post_install.sh', fs.readFileSync('test/post_install.sh')); - fs.chmodSync(codeDirectory + '/post_install.sh', '755'); + fs.writeFileSync(path.join(codeDirectory, 'post_install.sh'), 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.equal("=> Running post install script post_install.sh\n\t\tYour environment is "+program.environment+"\n", hook.captured()); - fs.unlinkSync(codeDirectory + '/post_install.sh'); + fs.unlinkSync(path.join(codeDirectory, 'post_install.sh')); done(); }); }); @@ -384,34 +386,34 @@ describe('node-lambda', function () { }); var result = _.includes(contents, 'index.js'); assert.equal(result, true); - result = _.includes(contents, 'node_modules/async/lib/async.js'); + result = _.includes(contents, path.join('node_modules', 'async', 'lib', 'async.js')); assert.equal(result, true); done(); }); }); it('packages a prebuilt module without installing', function (done) { - var path = '.build_' + Date.now(); + var buildDir = '.build_' + Date.now(); after(function() { - rimraf.sync(path, fs); + rimraf.sync(buildDir, fs); }); - fs.mkdirSync(path); - fs.mkdirSync(path + '/d'); - fs.mkdirSync(path + '/node_modules'); - fs.writeFileSync(path + '/node_modules/a', '...'); - fs.writeFileSync(path + '/testa', '...'); - fs.writeFileSync(path + '/d/testb', '...'); + fs.mkdirSync(buildDir); + fs.mkdirSync(path.join(buildDir, 'd')); + fs.mkdirSync(path.join(buildDir, 'node_modules')); + fs.writeFileSync(path.join(buildDir, 'node_modules', 'a'), '...'); + fs.writeFileSync(path.join(buildDir, 'testa'), '...'); + fs.writeFileSync(path.join(buildDir, 'd', 'testb'), '...'); - program.prebuiltDirectory = path; + program.prebuiltDirectory = buildDir; lambda._archive(program, function (err, data) { var archive = new zip(data); var contents = _.map(archive.files, function (f) { return f.name.toString(); }); var result = _.includes(contents, 'testa') && - _.includes(contents, 'd/testb') && - _.includes(contents, 'node_modules/a'); + _.includes(contents, path.join('d', 'testb')) && + _.includes(contents, path.join('node_modules', 'a')); assert.equal(result, true); done(); }); @@ -419,7 +421,7 @@ describe('node-lambda', function () { }); describe('_readArchive', function () { - const testZipFile = '/tmp/node-lambda-test.zip'; + const testZipFile = path.join(os.tmpDir(), 'node-lambda-test.zip'); var bufferExpected = null; before(function(done) { this.timeout(30000); // give it time to zip @@ -445,11 +447,12 @@ describe('node-lambda', function () { }); it('_readArchive fails (does not exists file)', function (done) { - const _program = Object.assign({ deployZipfile: '/aaaa/bbbb' }, program); + const filePath = path.join(path.resolve('/aaaa'), 'bbbb'); + const _program = Object.assign({ deployZipfile: filePath }, program); lambda._readArchive(_program, function (err, data) { assert.isUndefined(data); assert.instanceOf(err, Error); - assert.equal(err.message, 'No such Zipfile [/aaaa/bbbb]'); + assert.equal(err.message, `No such Zipfile [${filePath}]`); done(); }); }); @@ -465,7 +468,8 @@ describe('node-lambda', function () { describe('If value is set in `deployZipfile`, _readArchive is executed in _archive', function () { it('`deployZipfile` is a invalid value. Process from creation of zip file', function (done) { - const _program = Object.assign({ deployZipfile: '/aaaa/bbbb' }, program); + const filePath = path.join(path.resolve('/aaaa'), 'bbbb'); + const _program = Object.assign({ deployZipfile: filePath }, program); this.timeout(30000); // give it time to zip lambda._archive(_program, function (err, data) { // same test as "installs and zips with an index.js file and node_modules/async" @@ -475,7 +479,7 @@ describe('node-lambda', function () { }); var result = _.includes(contents, 'index.js'); assert.equal(result, true); - result = _.includes(contents, 'node_modules/async/lib/async.js'); + result = _.includes(contents, path.join('node_modules', 'async', 'lib', 'async.js')); assert.equal(result, true); done(); }); @@ -534,9 +538,9 @@ describe('node-lambda', function () { it('should create sample files', function () { lambda.setup(program); - const libPath = `${__dirname}/../lib`; + const libPath = path.join(__dirname, '..', 'lib'); targetFiles.forEach(function(targetFile) { - const boilerplateFile = `${libPath}/${targetFile}.example`; + const boilerplateFile = path.join(libPath, `${targetFile}.example`); assert.equal( fs.readFileSync(targetFile).toString(), @@ -556,11 +560,12 @@ describe('node-lambda', function () { }); it('program.eventSourceFile is invalid value', function () { - program.eventSourceFile = '/hoge/fuga'; + const dirPath = path.join(path.resolve('/hoge'), 'fuga'); + program.eventSourceFile = dirPath; assert.throws( () => { lambda._eventSourceList(program); }, Error, - "ENOENT: no such file or directory, open '/hoge/fuga'" + `ENOENT: no such file or directory, open '${dirPath}'` ); }); @@ -647,7 +652,7 @@ describe('node-lambda', function () { describe('_updateScheduleEvents', function () { const aws = require('aws-sdk-mock'); - const ScheduleEvents = require('../lib/schedule_events'); + const ScheduleEvents = require(path.join('..', 'lib', 'schedule_events')); const eventSourcesJsonValue = { ScheduleEvents: [{ ScheduleName: 'node-lambda-test-schedule', @@ -733,9 +738,9 @@ describe('node-lambda', function () { assert.equal(fs.readFileSync('newContext.json').toString(), '{"FOO"="bar"\n"BAZ"="bing"\n}'); assert.equal(fs.readFileSync('newEvent.json').toString(), '{"FOO"="bar"}'); - const libPath = `${__dirname}/../lib`; + const libPath = path.join(__dirname, '..', 'lib'); filesCreatedBySetup.forEach(function(targetFile) { - const boilerplateFile = `${libPath}/${targetFile}.example`; + const boilerplateFile = path.join(libPath, `${targetFile}.example`); assert.equal( fs.readFileSync(targetFile).toString(),