From cf80c6a2577272e3abec3ff4e24fc598ea93b1c7 Mon Sep 17 00:00:00 2001 From: Tomoaki Abe Date: Fri, 5 Feb 2021 16:48:52 +0900 Subject: [PATCH] Support npm7 (#550) * fix: _codeDirectory() If tmpdir is symbolic link and npm>=7, `this._npmInstall()` may not work properly. * test: fix the test because the 'install' specification has changed in npm7 * chore: add comment --- lib/main.js | 4 +++- test/main.js | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/main.js b/lib/main.js index 9f7fca81..0bd723cc 100644 --- a/lib/main.js +++ b/lib/main.js @@ -518,7 +518,9 @@ Emulate only the body of the API Gateway event. } _codeDirectory () { - return path.join(os.tmpdir(), `${path.basename(path.resolve('.'))}-lambda`) + // Why realpathSync?: + // If tmpdir is symbolic link and npm>=7, `this._npmInstall()` may not work properly. + return path.join(fs.realpathSync(os.tmpdir()), `${path.basename(path.resolve('.'))}-lambda`) } _cleanDirectory (codeDirectory, keepNodeModules) { diff --git a/test/main.js b/test/main.js index 72fb8499..0a86f598 100644 --- a/test/main.js +++ b/test/main.js @@ -153,7 +153,10 @@ describe('lib/main', function () { describe('_codeDirectory', () => { it('Working directory in the /tmp directory', () => { - assert.equal(lambda._codeDirectory(), path.join(os.tmpdir(), 'node-lambda-lambda')) + assert.equal( + lambda._codeDirectory(), + path.join(fs.realpathSync(os.tmpdir()), 'node-lambda-lambda') + ) }) }) @@ -586,25 +589,34 @@ describe('lib/main', function () { }) }) - describe('_npmInstall', () => { + describe('_npmInstall', function () { + _timeout({ this: this, sec: 30 }) // ci should be faster than install + // npm treats files as packages when installing, and so removes them. // Test with `devDependencies` packages that are not installed with the `--production` option. const nodeModulesMocha = path.join(codeDirectory, 'node_modules', 'mocha') beforeEach(() => { return lambda._cleanDirectory(codeDirectory).then(() => { - fs.ensureDirSync(nodeModulesMocha) + fs.copySync( + path.join('node_modules', 'aws-sdk'), + path.join(codeDirectory, 'node_modules', 'aws-sdk') + ) return lambda._fileCopy(program, '.', codeDirectory, true) }) }) describe('when package-lock.json does exist', () => { - it('should use "npm ci"', function () { - _timeout({ this: this, sec: 30 }) // ci should be faster than install - + it('should use "npm ci"', () => { + const beforeAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk')) return lambda._npmInstall(program, codeDirectory).then(() => { - const contents = fs.readdirSync(codeDirectory) - assert.include(contents, 'node_modules') + const contents = fs.readdirSync(path.join(codeDirectory, 'node_modules')) + assert.include(contents, 'dotenv') + + // To remove and then install. + // beforeAwsSdkStat.ctimeMs < afterAwsSdkStat.ctimeMs + const afterAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk')) + assert.isBelow(beforeAwsSdkStat.ctimeMs, afterAwsSdkStat.ctimeMs) // Not installed with the `--production` option. assert.isFalse(fs.existsSync(nodeModulesMocha)) @@ -617,15 +629,19 @@ describe('lib/main', function () { return fs.removeSync(path.join(codeDirectory, 'package-lock.json')) }) - it('should use "npm install"', function () { - _timeout({ this: this, sec: 60 }) // install should be slower than ci - + it('should use "npm install"', () => { + const beforeAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk')) return lambda._npmInstall(program, codeDirectory).then(() => { - const contents = fs.readdirSync(codeDirectory) - assert.include(contents, 'node_modules') + const contents = fs.readdirSync(path.join(codeDirectory, 'node_modules')) + assert.include(contents, 'dotenv') - // It remains because it is not erased before installation. - assert.isTrue(fs.existsSync(nodeModulesMocha)) + // Installed packages will remain intact. + // beforeAwsSdkStat.ctimeMs === afterAwsSdkStat.ctimeMs + const afterAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk')) + assert.equal(beforeAwsSdkStat.ctimeMs, afterAwsSdkStat.ctimeMs) + + // Not installed with the `--production` option. + assert.isFalse(fs.existsSync(nodeModulesMocha)) }) }) })