From 3dc2e8895e02bfb894c5664542dc9fea36a7ed8b Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 27 Apr 2017 12:44:17 +0900 Subject: [PATCH 1/5] Bugfix `_fileCopy` exclude Add test of cases that were missing consideration --- lib/main.js | 4 ++-- test/main.js | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/main.js b/lib/main.js index fd339500..7ba3ef3c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -204,8 +204,8 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c })(); const pattern = '{' + excludes.map(function (str) { - if (str.charAt(0) == '/') - return path.join(srcAbsolutePath, str); + if (str.indexOf('/') >= 0) + return path.join(srcAbsolutePath, str.replace(/\/$/, '')); return str; }).join(',') + '}' diff --git a/test/main.js b/test/main.js index 4c7a7315..7b91b248 100644 --- a/test/main.js +++ b/test/main.js @@ -177,6 +177,16 @@ describe('node-lambda', function () { }); function rsyncTests(funcName) { + before(function () { + fs.mkdirSync('build'); + fs.mkdirSync('__unittest'); + fs.writeFileSync(path.join('__unittest', 'piyo')); + }); + after(function () { + fs.removeSync('build'); + fs.removeSync('__unittest'); + }); + beforeEach(function (done) { lambda._cleanDirectory(codeDirectory, done); }); @@ -187,7 +197,7 @@ describe('node-lambda', function () { ['index.js', 'package.json'].forEach(function (needle) { assert.include(contents, needle, `Target: "${needle}"`); }); - ['node_modules'].forEach(function (needle) { + ['node_modules', 'build'].forEach(function (needle) { assert.notInclude(contents, needle, `Target: "${needle}"`); }); done(); @@ -198,7 +208,7 @@ describe('node-lambda', function () { beforeEach(function (done) { // *main* => lib/main.js // In case of specifying files under the directory with wildcards - program.excludeGlobs = '*.png test *main*'; + program.excludeGlobs = '*.png test *main* __unittest/*'; done(); }); @@ -215,11 +225,16 @@ describe('node-lambda', function () { it(funcName + ' excludes files matching excludeGlobs', function (done) { lambda[funcName](program, '.', codeDirectory, true, function (err, result) { var contents = fs.readdirSync(codeDirectory); + assert.include(contents, '__unittest', `Target: "__unittest"`); + ['node-lambda.png', 'test'].forEach(function (needle) { assert.notInclude(contents, needle, `Target: "${needle}"`); }); + contents = fs.readdirSync(path.join(codeDirectory, 'lib')); assert.notInclude(contents, 'main.js', 'Target: "lib/main.js"'); + contents = fs.readdirSync(path.join(codeDirectory, '__unittest')); + assert.isTrue(contents.length == 0, 'directory:__unittest is empty'); done(); }); }); From f72e873e96d65e50fd4dc0c5c5f3e93784079be7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Thu, 27 Apr 2017 17:22:36 +0900 Subject: [PATCH 2/5] Bugfix `_fileCopy` exclude Add test of cases that were missing consideration --- lib/main.js | 21 ++++++++++++++++++--- test/main.js | 30 +++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/main.js b/lib/main.js index 7ba3ef3c..7a0be99a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -203,11 +203,20 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c .concat(excludeNodeModules ? ['/node_modules'] : []); })(); + // Formatting for `filter` of `fs.copy` + const dirBlobs = []; const pattern = '{' + excludes.map(function (str) { + if (str.match(/\/$/)) { + str = str.substr(0, str.length - 1); + dirBlobs.push(str); + } + if (str.charAt('0') == '/') + return path.join(srcAbsolutePath, str); if (str.indexOf('/') >= 0) - return path.join(srcAbsolutePath, str.replace(/\/$/, '')); + return path.join(path.resolve('/**'), str); return str; - }).join(',') + '}' + }).join(',') + '}'; + const dirPatternRegExp = new RegExp(`(${dirBlobs.join('|')})$`); fs.mkdirs(dest, function (err) { if (err) { @@ -220,7 +229,13 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c // include package.json unless prebuiltDirectory is set return true; } - return !minimatch(src, pattern, { matchBase: true }); + + if (!minimatch(src, pattern, { matchBase: true })) + return true; + // Directory check. Even if `src` is a directory it will not end with '/'. + if (!dirPatternRegExp.test(src)) + return false; + return !fs.statSync(src).isDirectory(); } }; fs.copy(src, dest, options, function (err) { diff --git a/test/main.js b/test/main.js index 7b91b248..1ced5767 100644 --- a/test/main.js +++ b/test/main.js @@ -179,12 +179,15 @@ describe('node-lambda', function () { function rsyncTests(funcName) { before(function () { fs.mkdirSync('build'); - fs.mkdirSync('__unittest'); - fs.writeFileSync(path.join('__unittest', 'piyo')); + fs.mkdirsSync(path.join('__unittest', 'hoge')); + fs.mkdirsSync(path.join('__unittest', 'fuga')); + fs.writeFileSync(path.join('__unittest', 'hoge', 'piyo')); + fs.writeFileSync('fuga'); }); after(function () { - fs.removeSync('build'); - fs.removeSync('__unittest'); + ['build', 'fuga', '__unittest'].forEach(function (path) { + fs.removeSync(path); + }); }); beforeEach(function (done) { @@ -208,7 +211,13 @@ describe('node-lambda', function () { beforeEach(function (done) { // *main* => lib/main.js // In case of specifying files under the directory with wildcards - program.excludeGlobs = '*.png test *main* __unittest/*'; + program.excludeGlobs = [ + '*.png', + 'test', + '*main*', + path.join('__unittest', 'hoge', '*'), + 'fuga/' + ].join(' '); done(); }); @@ -225,7 +234,9 @@ describe('node-lambda', function () { it(funcName + ' excludes files matching excludeGlobs', function (done) { lambda[funcName](program, '.', codeDirectory, true, function (err, result) { var contents = fs.readdirSync(codeDirectory); - assert.include(contents, '__unittest', `Target: "__unittest"`); + ['__unittest', 'fuga'].forEach(function (needle) { + assert.include(contents, needle, `Target: "${needle}"`); + }); ['node-lambda.png', 'test'].forEach(function (needle) { assert.notInclude(contents, needle, `Target: "${needle}"`); @@ -233,8 +244,13 @@ describe('node-lambda', function () { contents = fs.readdirSync(path.join(codeDirectory, 'lib')); assert.notInclude(contents, 'main.js', 'Target: "lib/main.js"'); + contents = fs.readdirSync(path.join(codeDirectory, '__unittest')); - assert.isTrue(contents.length == 0, 'directory:__unittest is empty'); + assert.include(contents, 'hoge', 'Target: "__unittest/hoge"'); + assert.notInclude(contents, 'fuga', 'Target: "__unittest/fuga"'); + + contents = fs.readdirSync(path.join(codeDirectory, '__unittest', 'hoge')); + assert.isTrue(contents.length == 0, 'directory:__unittest/hoge is empty'); done(); }); }); From 9d0db389153f3801148d576fefdb0fc76d3514aa Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 1 May 2017 10:07:12 +0900 Subject: [PATCH 3/5] Fix to get from `path.sep` --- lib/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index df918dda..ded8611f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -206,13 +206,13 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c // Formatting for `filter` of `fs.copy` const dirBlobs = []; const pattern = '{' + excludes.map(function (str) { - if (str.match(/\/$/)) { + if (str.charAt(str.length - 1) == path.sep) { str = str.substr(0, str.length - 1); dirBlobs.push(str); } - if (str.charAt('0') == '/') + if (str.charAt(0) == path.sep) return path.join(srcAbsolutePath, str); - if (str.indexOf('/') >= 0) + if (str.indexOf(path.sep) >= 0) return path.join(path.resolve('/**'), str); return str; }).join(',') + '}'; From 510d0fcf901e263a28b503e2b354fb5fefce1cb7 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 1 May 2017 10:32:59 +0900 Subject: [PATCH 4/5] Fix to make exclude specified separator `path.sep` --- lib/main.js | 4 ++-- test/main.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/main.js b/lib/main.js index ded8611f..9b619963 100644 --- a/lib/main.js +++ b/lib/main.js @@ -197,10 +197,10 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c '.editorconfig', 'deploy.env', '*.log', - '/build/' + path.join(path.sep, 'build', path.sep); ] .concat(program.excludeGlobs ? program.excludeGlobs.split(' ') : []) - .concat(excludeNodeModules ? ['/node_modules'] : []); + .concat(excludeNodeModules ? [path.join(path.sep, 'node_modules')] : []); })(); // Formatting for `filter` of `fs.copy` diff --git a/test/main.js b/test/main.js index e8a6a906..ff9b2014 100644 --- a/test/main.js +++ b/test/main.js @@ -215,7 +215,7 @@ describe('node-lambda', function () { 'test', '*main*', path.join('__unittest', 'hoge', '*'), - 'fuga/' + path.join('fuga', path.sep) ].join(' '); done(); }); From 40426a9f41e136f10a706a9b901e6c16d5c9f106 Mon Sep 17 00:00:00 2001 From: abetomo Date: Mon, 1 May 2017 10:36:19 +0900 Subject: [PATCH 5/5] Remove unnecessary semicolon --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 9b619963..a86bcc41 100644 --- a/lib/main.js +++ b/lib/main.js @@ -197,7 +197,7 @@ Lambda.prototype._fileCopy = function (program, src, dest, excludeNodeModules, c '.editorconfig', 'deploy.env', '*.log', - path.join(path.sep, 'build', path.sep); + path.join(path.sep, 'build', path.sep) ] .concat(program.excludeGlobs ? program.excludeGlobs.split(' ') : []) .concat(excludeNodeModules ? [path.join(path.sep, 'node_modules')] : []);