Skip to content

Commit 462804f

Browse files
committed
fix(ci): make hmr fixture writes atomic
1 parent f146397 commit 462804f

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

apps/mokup-web-demo/test/e2e/hmr.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { readFile, writeFile } from 'node:fs/promises'
1+
import { readFile } from 'node:fs/promises'
22
import { join } from 'node:path'
33
import { expect, test } from '@playwright/test'
4-
import { writeJson } from '../../../../tests/e2e/utils/fs'
4+
import { writeJson, writeTextFile } from '../../../../tests/e2e/utils/fs'
55
import { repoRoot } from '../../../../tests/e2e/utils/paths'
66

77
const mockDir = join(repoRoot, 'apps/mokup-web-demo/mock')
@@ -24,7 +24,7 @@ async function backupFile(filePath: string) {
2424

2525
async function restoreAll() {
2626
for (const backup of backups.reverse()) {
27-
await writeFile(backup.path, backup.content, 'utf8')
27+
await writeTextFile(backup.path, backup.content)
2828
}
2929
backups.length = 0
3030
}
@@ -108,7 +108,7 @@ test.describe('edit JSON mock files', () => {
108108
'}',
109109
'',
110110
].join('\n')
111-
await writeFile(filePath, newContent, 'utf8')
111+
await writeTextFile(filePath, newContent)
112112

113113
await pollApi(request, '/api/about', body => body['e2e'] === stamp)
114114
})
@@ -200,7 +200,7 @@ test.describe('edit TS handler mock files', () => {
200200
'export default rule',
201201
'',
202202
].join('\n')
203-
await writeFile(filePath, newContent, 'utf8')
203+
await writeTextFile(filePath, newContent)
204204

205205
await pollApi(request, '/api/health', body => body['marker'] === marker)
206206
})
@@ -220,7 +220,7 @@ test.describe('edit TS handler mock files', () => {
220220
'export default rule',
221221
'',
222222
].join('\n')
223-
await writeFile(filePath, newContent, 'utf8')
223+
await writeTextFile(filePath, newContent)
224224

225225
await pollApi(request, '/api/override', body => body['marker'] === marker)
226226
})
@@ -242,7 +242,7 @@ test.describe('edit TS handler mock files', () => {
242242
'export default rule',
243243
'',
244244
].join('\n')
245-
await writeFile(filePath, newContent, 'utf8')
245+
await writeTextFile(filePath, newContent)
246246

247247
await pollApi(request, '/api/search', body => body['marker'] === marker)
248248
})
@@ -269,7 +269,7 @@ test.describe('edit TS handler mock files', () => {
269269
'export default rule',
270270
'',
271271
].join('\n')
272-
await writeFile(filePath, newContent, 'utf8')
272+
await writeTextFile(filePath, newContent)
273273

274274
await expect.poll(async () => {
275275
try {
@@ -306,7 +306,7 @@ test.describe('edit TS handler mock files', () => {
306306
'export default rule',
307307
'',
308308
].join('\n')
309-
await writeFile(filePath, newContent, 'utf8')
309+
await writeTextFile(filePath, newContent)
310310

311311
await pollApi(request, '/api/users/42', body => body['marker'] === marker && body['id'] === '42')
312312
})

tests/e2e/utils/fs.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
1+
import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises'
2+
import { dirname, join } from 'node:path'
3+
import process from 'node:process'
24

35
export async function ensureEmptyDir(dir: string) {
46
await rm(dir, { recursive: true, force: true })
@@ -10,7 +12,16 @@ export async function readJson<T>(filePath: string): Promise<T> {
1012
return JSON.parse(raw) as T
1113
}
1214

15+
export async function writeTextFile(filePath: string, contents: string) {
16+
const tempPath = join(
17+
dirname(filePath),
18+
`.mokup-e2e-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.tmp`,
19+
)
20+
await writeFile(tempPath, contents, 'utf8')
21+
await rename(tempPath, filePath)
22+
}
23+
1324
export async function writeJson(filePath: string, value: unknown) {
1425
const raw = JSON.stringify(value, null, 2)
15-
await writeFile(filePath, `${raw}\n`, 'utf8')
26+
await writeTextFile(filePath, `${raw}\n`)
1627
}

0 commit comments

Comments
 (0)