Skip to content

Commit 4ba98fd

Browse files
committed
test: timestamped logs changes
1 parent de7201e commit 4ba98fd

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

tests/netlify-deploy.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async function packNextRuntimeImpl() {
2323
}
2424

2525
let packNextRuntimePromise: ReturnType<typeof packNextRuntimeImpl> | null = null
26+
let nextRuntimePacked = false
2627
function packNextRuntime() {
2728
if (!packNextRuntimePromise) {
2829
packNextRuntimePromise = packNextRuntimeImpl()
@@ -38,13 +39,35 @@ export class NextDeployInstance extends NextInstance {
3839
private _shouldDeleteDeploy: boolean = false
3940
private _isCurrentlyDeploying: boolean = false
4041
private _deployOutput: string = ''
42+
private _setupStartTime = Date.now()
4143

4244
public get buildId() {
4345
// get deployment ID via fetch since we can't access
4446
// build artifacts directly
4547
return this._buildId
4648
}
4749

50+
private packNextRuntime() {
51+
if (!packNextRuntimePromise) {
52+
if (!nextRuntimePacked) {
53+
this._deployOutput += this.getTimestampPrefix() + 'Pack Next Runtime ...\n'
54+
}
55+
packNextRuntimePromise = packNextRuntimeImpl()
56+
packNextRuntimePromise.then(() => {
57+
nextRuntimePacked = true
58+
})
59+
if (!nextRuntimePacked) {
60+
this._deployOutput += this.getTimestampPrefix() + 'Pack Next Runtime DONE\n'
61+
}
62+
}
63+
64+
return packNextRuntimePromise
65+
}
66+
67+
private getTimestampPrefix() {
68+
return `[${new Date().toISOString()}] (+${((Date.now() - this._setupStartTime) / 1000).toFixed(3)}s) `
69+
}
70+
4871
public async setup(parentSpan: Span) {
4972
if (process.env.SITE_URL && process.env.BUILD_ID) {
5073
require('console').log('Using existing deployment: ' + process.env.SITE_URL)
@@ -54,35 +77,36 @@ export class NextDeployInstance extends NextInstance {
5477
return
5578
}
5679

57-
let deployStartTime = Date.now()
58-
5980
this._isCurrentlyDeploying = true
6081

61-
const setupStartTime = Date.now()
62-
82+
this._deployOutput += this.getTimestampPrefix() + 'Setting up test dir ...\n'
6383
// create the test site
6484
await super.createTestDir({ parentSpan, skipInstall: true })
85+
this._deployOutput += this.getTimestampPrefix() + 'Setting up test dir DONE\n'
6586

6687
// If the test fixture has node modules we need to move them aside then merge them in after
6788

6889
const nodeModules = path.join(this.testDir, 'node_modules')
6990
const nodeModulesBak = `${nodeModules}.bak`
7091

7192
if (fs.existsSync(nodeModules)) {
93+
this._deployOutput += this.getTimestampPrefix() + 'Rename node_modules ...\n'
7294
await fs.rename(nodeModules, nodeModulesBak)
95+
this._deployOutput += this.getTimestampPrefix() + 'Rename node_modules DONE\n'
7396
}
7497

75-
const { runtimePackageName, runtimePackageTarballPath } = await packNextRuntime()
98+
const { runtimePackageName, runtimePackageTarballPath } = await this.packNextRuntime()
7699

77100
const handleOutput = (chunk) => {
78-
const timestampPrefix = `[${new Date().toISOString()}] (+${((Date.now() - deployStartTime) / 1000).toFixed(3)}s) `
101+
const timestampPrefix = this.getTimestampPrefix()
79102

80103
this._deployOutput +=
81104
(this._deployOutput === '' || this._deployOutput.endsWith('\n') ? timestampPrefix : '') +
82105
chunk.toString().replace(/\n(?=.)/gm, `\n${timestampPrefix}`)
83106
}
84107

85108
// install dependencies
109+
this._deployOutput += this.getTimestampPrefix() + 'Install dependencies ...\n'
86110
const installResPromise = execa('npm', ['i', runtimePackageTarballPath, '--legacy-peer-deps'], {
87111
cwd: this.testDir,
88112
})
@@ -91,14 +115,17 @@ export class NextDeployInstance extends NextInstance {
91115
installResPromise.stderr.on('data', handleOutput)
92116

93117
await installResPromise
118+
this._deployOutput += this.getTimestampPrefix() + 'Install dependencies DONE\n'
94119

95120
if (fs.existsSync(nodeModulesBak)) {
96121
// move the contents of the fixture node_modules into the installed modules
122+
this._deployOutput += this.getTimestampPrefix() + 'Move fixture node_modules ...\n'
97123
for (const file of await fs.readdir(nodeModulesBak)) {
98124
await fs.move(path.join(nodeModulesBak, file), path.join(nodeModules, file), {
99125
overwrite: true,
100126
})
101127
}
128+
this._deployOutput += this.getTimestampPrefix() + 'Move fixture node_modules DONE\n'
102129
}
103130

104131
// use next runtime package installed by the test runner
@@ -166,8 +193,21 @@ export class NextDeployInstance extends NextInstance {
166193
deployResPromise.stdout.on('data', handleOutput)
167194
deployResPromise.stderr.on('data', handleOutput)
168195

196+
deployResPromise.on('error', (err) => {
197+
this._deployOutput += this.getTimestampPrefix() + `Error during deployment: ${err.message}\n`
198+
require('console').error(`Error during deployment: ${err.message}`)
199+
})
200+
201+
const deployHeartbeat = setInterval(() => {
202+
this._deployOutput += this.getTimestampPrefix() + 'Waiting for deployment ...\n'
203+
}, 5000)
204+
169205
const deployRes = await deployResPromise
170206

207+
clearInterval(deployHeartbeat)
208+
209+
require('console').error(`Deploy finished. Processing output...`)
210+
171211
if (deployRes.exitCode !== 0) {
172212
// clear deploy output to avoid printing it again in destroy()
173213
this._deployOutput = ''
@@ -210,7 +250,7 @@ export class NextDeployInstance extends NextInstance {
210250
).trim()
211251

212252
require('console').log(`Got buildId: ${this._buildId}`)
213-
require('console').log(`Setup time: ${(Date.now() - setupStartTime) / 1000.0}s`)
253+
require('console').log(`Setup time: ${(Date.now() - this._setupStartTime) / 1000.0}s`)
214254

215255
this._isCurrentlyDeploying = false
216256
}

0 commit comments

Comments
 (0)