Skip to content

Commit e398590

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

File tree

1 file changed

+67
-7
lines changed

1 file changed

+67
-7
lines changed

tests/netlify-deploy.ts

Lines changed: 67 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
@@ -163,11 +190,44 @@ export class NextDeployInstance extends NextInstance {
163190
},
164191
)
165192

193+
this._deployOutput +=
194+
this.getTimestampPrefix() + `Started deploy, PID: ${deployResPromise.pid}\n`
195+
require('console').log(`Started deploy, PID: ${deployResPromise.pid}`)
196+
166197
deployResPromise.stdout.on('data', handleOutput)
167198
deployResPromise.stderr.on('data', handleOutput)
168199

200+
deployResPromise.on('error', (err) => {
201+
this._deployOutput += this.getTimestampPrefix() + `Error during deployment: ${err.message}\n`
202+
require('console').error(`Error during deployment: ${err.message}`)
203+
})
204+
205+
const deployHeartbeat = setInterval(() => {
206+
this._deployOutput +=
207+
this.getTimestampPrefix() + 'Waiting for netlify deploy process to finish ...\n'
208+
}, 5000)
209+
210+
deployResPromise
211+
.then((result) => {
212+
require('console').log(`Netlify deploy process finished.`)
213+
this._deployOutput += this.getTimestampPrefix() + 'Netlify deploy process finished.\n'
214+
})
215+
.catch((err) => {
216+
require('console').log(`Netlify deploy process failed. ` + err)
217+
this._deployOutput += this.getTimestampPrefix() + 'Netlify deploy process failed. ' + err
218+
})
219+
.finally(() => {
220+
require('console').log(`Netlify deploy process finally.`)
221+
this._deployOutput += this.getTimestampPrefix() + 'Netlify deploy process finally.\n'
222+
clearInterval(deployHeartbeat)
223+
})
224+
169225
const deployRes = await deployResPromise
170226

227+
clearInterval(deployHeartbeat)
228+
229+
require('console').log(`Deploy finished. Processing output...`)
230+
171231
if (deployRes.exitCode !== 0) {
172232
// clear deploy output to avoid printing it again in destroy()
173233
this._deployOutput = ''
@@ -210,7 +270,7 @@ export class NextDeployInstance extends NextInstance {
210270
).trim()
211271

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

215275
this._isCurrentlyDeploying = false
216276
}

0 commit comments

Comments
 (0)