Skip to content

Commit 907633f

Browse files
authored
Merge pull request #855 from abetomo/combat-flakiness
fix(tests) v3: combat flakiness
2 parents 8b3ee91 + c0248a8 commit 907633f

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

test/main.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ const _timeout = function (params) {
9595
}
9696
}
9797

98+
const forceRemoveSync = (targetPath) => {
99+
if (typeof fs.rmSync === 'function') {
100+
fs.rmSync(targetPath, { recursive: true, force: true })
101+
return
102+
}
103+
// fs-extra's removeSync always removes recursively and handles non-existent paths gracefully,
104+
// so no options are needed here. This is functionally equivalent to the rmSync call above.
105+
fs.removeSync(targetPath)
106+
}
107+
98108
// It does not completely reproduce the response of the actual API.
99109
const lambdaMockSettings = {
100110
addPermission: {},
@@ -142,14 +152,10 @@ const lambdaMockSettings = {
142152

143153
/* global before, after, beforeEach, afterEach, describe, it */
144154
describe('lib/main', function () {
145-
if (process.platform === 'win32') {
146-
// It seems that it takes very long time for file operation in Windows.
147-
// So set `timeout(300000)` for the whole test.
148-
this.timeout(300000)
149-
} else if (process.platform === 'darwin') {
150-
// It seems that it takes time for file operation in Mac.
151-
// So set `timeout(120000)` for the whole test.
152-
this.timeout(120000)
155+
if (['win32', 'darwin'].includes(process.platform)) {
156+
// It seems that it takes time for file operation in Windows and Mac.
157+
// So set timeout(180000) (sec: 180) for the whole test.
158+
this.timeout(180000)
153159
}
154160

155161
before(() => {
@@ -507,8 +513,8 @@ describe('lib/main', function () {
507513
fs.writeFileSync('fuga', '')
508514
})
509515
after(() => {
510-
['fuga', '__unittest'].forEach((path) => {
511-
fs.removeSync(path)
516+
['fuga', '__unittest'].forEach((targetPath) => {
517+
forceRemoveSync(targetPath)
512518
})
513519
})
514520

@@ -589,7 +595,7 @@ describe('lib/main', function () {
589595

590596
it('_fileCopy should not include package.json when --prebuiltDirectory is set', () => {
591597
const buildDir = '.build_' + Date.now()
592-
after(() => fs.removeSync(buildDir))
598+
after(() => forceRemoveSync(buildDir))
593599

594600
fs.mkdirSync(buildDir)
595601
fs.writeFileSync(path.join(buildDir, 'testa'), '')
@@ -623,7 +629,7 @@ describe('lib/main', function () {
623629

624630
describe('when package-lock.json does not exist', () => {
625631
beforeEach(() => {
626-
fs.removeSync(path.join(codeDirectory, 'package-lock.json'))
632+
forceRemoveSync(path.join(codeDirectory, 'package-lock.json'))
627633
})
628634

629635
it('returns false', () => {
@@ -746,7 +752,7 @@ describe('lib/main', function () {
746752
})
747753

748754
describe('_packageInstall', function () {
749-
_timeout({ this: this, sec: 60 }) // ci should be faster than install
755+
_timeout({ this: this, sec: 180 }) // ci should be faster than install
750756

751757
// npm treats files as packages when installing, and so removes them.
752758
// Test with `devDependencies` packages that are not installed with the `--production` option.
@@ -783,7 +789,7 @@ describe('lib/main', function () {
783789
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson))
784790

785791
// Remove package-lock.json because it does not match the package.json to which optionalDependencies was added.
786-
fs.removeSync(path.join(codeDirectory, 'package-lock.json'))
792+
forceRemoveSync(path.join(codeDirectory, 'package-lock.json'))
787793
}
788794

789795
const testOptionalDependenciesIsInstalled = async (packageManager) => {
@@ -825,7 +831,7 @@ describe('lib/main', function () {
825831

826832
describe('when package-lock.json does not exist', () => {
827833
beforeEach(() => {
828-
return fs.removeSync(path.join(codeDirectory, 'package-lock.json'))
834+
forceRemoveSync(path.join(codeDirectory, 'package-lock.json'))
829835
})
830836

831837
it('should use "npm install"', () => {
@@ -878,7 +884,7 @@ describe('lib/main', function () {
878884
})
879885

880886
describe('_packageInstall (When codeDirectory contains characters to be escaped)', function () {
881-
_timeout({ this: this, sec: 30 }) // give it time to build the node modules
887+
_timeout({ this: this, sec: 180 }) // give it time to build the node modules
882888

883889
beforeEach(() => {
884890
// Since '\' can not be included in the file or directory name in Windows
@@ -892,7 +898,7 @@ describe('lib/main', function () {
892898
})
893899

894900
afterEach(() => {
895-
fs.removeSync(codeDirectory)
901+
forceRemoveSync(codeDirectory)
896902
codeDirectory = lambda._codeDirectory()
897903
})
898904

@@ -981,7 +987,7 @@ describe('lib/main', function () {
981987
})
982988

983989
describe('_zip', function () {
984-
_timeout({ this: this, sec: 60 }) // give it time to zip
990+
_timeout({ this: this, sec: 180 }) // give it time to zip
985991

986992
const beforeTask = async (packageManager) => {
987993
await lambda._cleanDirectory(codeDirectory)
@@ -1051,7 +1057,7 @@ describe('lib/main', function () {
10511057
describe('_archive', () => {
10521058
// archive.files's name is a slash delimiter regardless of platform.
10531059
it('installs and zips with an index.js file and node_modules/dotenv (It is also a test of `_buildAndArchive`)', function () {
1054-
_timeout({ this: this, sec: 30 }) // give it time to zip
1060+
_timeout({ this: this, sec: 180 }) // give it time to zip
10551061

10561062
return lambda._archive({ ...program, sourceDirectory: sourceDirectoryForTest }).then((data) => {
10571063
const archive = new Zip(data)
@@ -1064,9 +1070,9 @@ describe('lib/main', function () {
10641070
})
10651071

10661072
it('packages a prebuilt module without installing (It is also a test of `_archivePrebuilt`)', function () {
1067-
_timeout({ this: this, sec: 30 }) // give it time to zip
1073+
_timeout({ this: this, sec: 180 }) // give it time to zip
10681074
const buildDir = '.build_' + Date.now()
1069-
after(() => fs.removeSync(buildDir))
1075+
after(() => forceRemoveSync(buildDir))
10701076

10711077
fs.mkdirSync(buildDir)
10721078
fs.mkdirSync(path.join(buildDir, 'd'))
@@ -1092,11 +1098,11 @@ describe('lib/main', function () {
10921098
})
10931099

10941100
it('cleans the temporary directory before running `_archivePrebuilt`', function () {
1095-
_timeout({ this: this, sec: 30 }) // give it time to zip
1101+
_timeout({ this: this, sec: 180 }) // give it time to zip
10961102
const buildDir = '.build_' + Date.now()
10971103
const codeDir = lambda._codeDirectory()
10981104
const tmpFile = path.join(codeDir, 'deleteme')
1099-
after(() => fs.removeSync(buildDir))
1105+
after(() => forceRemoveSync(buildDir))
11001106

11011107
fs.mkdirSync(codeDir, { recursive: true })
11021108
fs.writeFileSync(tmpFile, '...')
@@ -1114,7 +1120,7 @@ describe('lib/main', function () {
11141120
const testZipFile = path.join(os.tmpdir(), 'node-lambda-test.zip')
11151121
let bufferExpected = null
11161122
before(function () {
1117-
_timeout({ this: this, sec: 30 }) // give it time to zip
1123+
_timeout({ this: this, sec: 180 }) // give it time to zip
11181124

11191125
return lambda._zip(program, codeDirectory).then((data) => {
11201126
bufferExpected = data
@@ -1159,7 +1165,7 @@ describe('lib/main', function () {
11591165
deployZipfile: filePath,
11601166
sourceDirectory: sourceDirectoryForTest
11611167
}
1162-
_timeout({ this: this, sec: 30 }) // give it time to zip
1168+
_timeout({ this: this, sec: 180 }) // give it time to zip
11631169
return lambda._archive(_program).then((data) => {
11641170
// same test as "installs and zips with an index.js file and node_modules/dotenv"
11651171
const archive = new Zip(data)
@@ -1671,7 +1677,7 @@ describe('lib/main', function () {
16711677

16721678
describe('Lambda.prototype.deploy()', () => {
16731679
it('simple test with mock', function () {
1674-
_timeout({ this: this, sec: 30 }) // give it time to zip
1680+
_timeout({ this: this, sec: 180 }) // give it time to zip
16751681
return lambda.deploy({ ...program, sourceDirectory: sourceDirectoryForTest }).then((result) => {
16761682
assert.isUndefined(result)
16771683
})
@@ -1683,10 +1689,9 @@ describe('lib/main', function () {
16831689
return lambda._updateTags(
16841690
lambdaClient,
16851691
'arn:aws:lambda:eu-central-1:1234567:function:test',
1686-
{ tagKey: 'tagValue' }).then((result) => {
1687-
assert.deepEqual(
1688-
result, {}
1689-
)
1692+
{ tagKey: 'tagValue' }
1693+
).then((result) => {
1694+
assert.deepEqual(result, {})
16901695
})
16911696
})
16921697
})

0 commit comments

Comments
 (0)