Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support npm7 #550

Merged
merged 3 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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