Skip to content

Commit

Permalink
Support npm7 (#550)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
abetomo authored Feb 5, 2021
1 parent 6d2bca4 commit cf80c6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 31 additions & 15 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
})
})

Expand Down Expand Up @@ -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))
Expand All @@ -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))
})
})
})
Expand Down

0 comments on commit cf80c6a

Please sign in to comment.