From 30535a3029c8efc9e0e200d210113c2ce3dbe277 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 24 Apr 2017 17:35:06 +0900 Subject: [PATCH 1/4] Fix using `path` --- lib/main.js | 27 +++++++++++---------- test/main.js | 66 +++++++++++++++++++++++++++------------------------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/lib/main.js b/lib/main.js index 7653c3c0..5c106c4f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,15 +1,15 @@ '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 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; @@ -20,8 +20,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)); @@ -51,9 +54,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); }; @@ -228,14 +231,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('.', 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 3f47d931..b57e17ca 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'); @@ -40,7 +42,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 () { @@ -221,18 +223,18 @@ describe('node-lambda', function () { }); it('rsync 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._rsync(program, path, codeDirectory, true, function(err, result) { + program.prebuiltDirectory = buildDir; + lambda._rsync(program, buildDir, codeDirectory, true, function(err, result) { var contents = fs.readdirSync(codeDirectory); result = !_.includes(contents, 'package.json') && _.includes(contents, 'testa'); @@ -312,12 +314,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(); }); }); @@ -372,34 +374,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(); }); @@ -407,7 +409,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 @@ -433,11 +435,11 @@ describe('node-lambda', function () { }); it('_readArchive fails (does not exists file)', function (done) { - const _program = Object.assign({ deployZipfile: '/aaaa/bbbb' }, program); + const _program = Object.assign({ deployZipfile: path.join('aaaa', 'bbbb') }, 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 [${path.join('aaaa', 'bbbb')}]`); done(); }); }); @@ -453,7 +455,7 @@ 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 _program = Object.assign({ deployZipfile: path.join('aaaa', 'bbbb') }, 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" @@ -463,7 +465,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(); }); @@ -544,11 +546,11 @@ describe('node-lambda', function () { }); it('program.eventSourceFile is invalid value', function () { - program.eventSourceFile = '/hoge/fuga'; + program.eventSourceFile = path.join('hoge', 'fuga'); assert.throws( () => { lambda._eventSourceList(program); }, Error, - "ENOENT: no such file or directory, open '/hoge/fuga'" + `ENOENT: no such file or directory, open '${path.join('hoge', 'fuga')}'` ); }); @@ -635,7 +637,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', From 2d599b69c0e9ff77f44bfd48e300372845aa0e8a Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 24 Apr 2017 17:58:07 +0900 Subject: [PATCH 2/4] Modify to path from root --- test/main.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/main.js b/test/main.js index b57e17ca..34b1b7b2 100644 --- a/test/main.js +++ b/test/main.js @@ -435,11 +435,12 @@ describe('node-lambda', function () { }); it('_readArchive fails (does not exists file)', function (done) { - const _program = Object.assign({ deployZipfile: path.join('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 [${path.join('aaaa', 'bbbb')}]`); + assert.equal(err.message, `No such Zipfile [${filePath}]`); done(); }); }); @@ -455,7 +456,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: path.join('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" @@ -546,11 +548,12 @@ describe('node-lambda', function () { }); it('program.eventSourceFile is invalid value', function () { - program.eventSourceFile = path.join('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 '${path.join('hoge', 'fuga')}'` + `ENOENT: no such file or directory, open '${dirPath}'` ); }); From a0287b4c7831a697d9bc26a207836a2832ab2f6e Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 24 Apr 2017 18:09:37 +0900 Subject: [PATCH 3/4] Modify path of postInstallScript --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 5c106c4f..4554470b 100644 --- a/lib/main.js +++ b/lib/main.js @@ -232,7 +232,7 @@ Lambda.prototype._npmInstall = function (program, codeDirectory, callback) { Lambda.prototype._postInstallScript = function (program, codeDirectory, callback) { var scriptFilename = 'post_install.sh'; - var cmd = path.join('.', scriptFilename) + ' ' + program.environment; + var cmd = path.join(codeDirectory, scriptFilename) + ' ' + program.environment; var filePath = path.join(codeDirectory, scriptFilename); From 009bbfc8d4b584336103e095d70cf588ab0a8d77 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 26 Apr 2017 17:33:49 +0900 Subject: [PATCH 4/4] Fix to use `path` object --- test/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/main.js b/test/main.js index 2e4e3a0a..4c7a7315 100644 --- a/test/main.js +++ b/test/main.js @@ -218,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(); }); @@ -538,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(), @@ -738,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(),