Skip to content

Commit cf80c6a

Browse files
authored
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
1 parent 6d2bca4 commit cf80c6a

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

lib/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,9 @@ Emulate only the body of the API Gateway event.
518518
}
519519

520520
_codeDirectory () {
521-
return path.join(os.tmpdir(), `${path.basename(path.resolve('.'))}-lambda`)
521+
// Why realpathSync?:
522+
// If tmpdir is symbolic link and npm>=7, `this._npmInstall()` may not work properly.
523+
return path.join(fs.realpathSync(os.tmpdir()), `${path.basename(path.resolve('.'))}-lambda`)
522524
}
523525

524526
_cleanDirectory (codeDirectory, keepNodeModules) {

test/main.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ describe('lib/main', function () {
153153

154154
describe('_codeDirectory', () => {
155155
it('Working directory in the /tmp directory', () => {
156-
assert.equal(lambda._codeDirectory(), path.join(os.tmpdir(), 'node-lambda-lambda'))
156+
assert.equal(
157+
lambda._codeDirectory(),
158+
path.join(fs.realpathSync(os.tmpdir()), 'node-lambda-lambda')
159+
)
157160
})
158161
})
159162

@@ -586,25 +589,34 @@ describe('lib/main', function () {
586589
})
587590
})
588591

589-
describe('_npmInstall', () => {
592+
describe('_npmInstall', function () {
593+
_timeout({ this: this, sec: 30 }) // ci should be faster than install
594+
590595
// npm treats files as packages when installing, and so removes them.
591596
// Test with `devDependencies` packages that are not installed with the `--production` option.
592597
const nodeModulesMocha = path.join(codeDirectory, 'node_modules', 'mocha')
593598

594599
beforeEach(() => {
595600
return lambda._cleanDirectory(codeDirectory).then(() => {
596-
fs.ensureDirSync(nodeModulesMocha)
601+
fs.copySync(
602+
path.join('node_modules', 'aws-sdk'),
603+
path.join(codeDirectory, 'node_modules', 'aws-sdk')
604+
)
597605
return lambda._fileCopy(program, '.', codeDirectory, true)
598606
})
599607
})
600608

601609
describe('when package-lock.json does exist', () => {
602-
it('should use "npm ci"', function () {
603-
_timeout({ this: this, sec: 30 }) // ci should be faster than install
604-
610+
it('should use "npm ci"', () => {
611+
const beforeAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk'))
605612
return lambda._npmInstall(program, codeDirectory).then(() => {
606-
const contents = fs.readdirSync(codeDirectory)
607-
assert.include(contents, 'node_modules')
613+
const contents = fs.readdirSync(path.join(codeDirectory, 'node_modules'))
614+
assert.include(contents, 'dotenv')
615+
616+
// To remove and then install.
617+
// beforeAwsSdkStat.ctimeMs < afterAwsSdkStat.ctimeMs
618+
const afterAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk'))
619+
assert.isBelow(beforeAwsSdkStat.ctimeMs, afterAwsSdkStat.ctimeMs)
608620

609621
// Not installed with the `--production` option.
610622
assert.isFalse(fs.existsSync(nodeModulesMocha))
@@ -617,15 +629,19 @@ describe('lib/main', function () {
617629
return fs.removeSync(path.join(codeDirectory, 'package-lock.json'))
618630
})
619631

620-
it('should use "npm install"', function () {
621-
_timeout({ this: this, sec: 60 }) // install should be slower than ci
622-
632+
it('should use "npm install"', () => {
633+
const beforeAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk'))
623634
return lambda._npmInstall(program, codeDirectory).then(() => {
624-
const contents = fs.readdirSync(codeDirectory)
625-
assert.include(contents, 'node_modules')
635+
const contents = fs.readdirSync(path.join(codeDirectory, 'node_modules'))
636+
assert.include(contents, 'dotenv')
626637

627-
// It remains because it is not erased before installation.
628-
assert.isTrue(fs.existsSync(nodeModulesMocha))
638+
// Installed packages will remain intact.
639+
// beforeAwsSdkStat.ctimeMs === afterAwsSdkStat.ctimeMs
640+
const afterAwsSdkStat = fs.statSync(path.join(codeDirectory, 'node_modules', 'aws-sdk'))
641+
assert.equal(beforeAwsSdkStat.ctimeMs, afterAwsSdkStat.ctimeMs)
642+
643+
// Not installed with the `--production` option.
644+
assert.isFalse(fs.existsSync(nodeModulesMocha))
629645
})
630646
})
631647
})

0 commit comments

Comments
 (0)