Skip to content

Commit ae3e4a5

Browse files
committed
test: fix backward slash escape issue
1 parent 0ec2ab4 commit ae3e4a5

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

tests/dev_server.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,26 @@
88
*/
99

1010
import ts from 'typescript'
11+
import { platform } from 'node:os'
1112
import { test } from '@japa/runner'
13+
import { join, sep } from 'node:path'
1214
import { cliui } from '@poppinss/cliui'
1315
import { setTimeout as sleep } from 'node:timers/promises'
16+
1417
import { DevServer } from '../index.ts'
15-
import { join, sep } from 'node:path'
16-
import { platform } from 'node:os'
18+
19+
/**
20+
* When filePath using backward slashes is written to a file, the backslashes
21+
* are considered as escape charcaters, hence results in a wrong path.
22+
*
23+
* This method replaces the backward slashes with two backward slashes
24+
*/
25+
function normalizePathForWindows(filePath: string) {
26+
if (platform() === 'win32') {
27+
filePath = filePath.split(sep).join('\\\\')
28+
}
29+
return filePath
30+
}
1731

1832
test.group('DevServer', () => {
1933
test('start() and execute dev server hook', async ({ fs, assert, cleanup }) => {
@@ -215,16 +229,10 @@ test.group('DevServer', () => {
215229
}).timeout(8 * 1000)
216230

217231
test('restart server if hot-hook:full-reload message is received', async ({ assert, fs }) => {
218-
let filePath = join(fs.basePath, 'start/routes.ts')
219-
if (platform() === 'win32') {
220-
filePath = filePath.split(sep).join('\\\\')
221-
console.log({ filePath })
222-
}
223-
224232
await fs.createJson('tsconfig.json', { include: ['**/*'], exclude: [] })
225233
await fs.create(
226234
'bin/server.ts',
227-
`process.send({ type: 'hot-hook:full-reload', path: "${filePath}" });`
235+
`process.send({ type: 'hot-hook:full-reload', path: '${normalizePathForWindows(join(fs.basePath, 'start/routes.ts'))}' });`
228236
)
229237
await fs.dump('bin/server.ts')
230238
await fs.create('start/routes.ts', ``)
@@ -256,7 +264,7 @@ test.group('DevServer', () => {
256264
await fs.createJson('tsconfig.json', { include: ['**/*'], exclude: [] })
257265
await fs.create(
258266
'bin/server.ts',
259-
`process.send({ type: 'hot-hook:invalidated', path: '${join(fs.basePath, 'start/routes.ts')}' });`
267+
`process.send({ type: 'hot-hook:invalidated', path: '${normalizePathForWindows(join(fs.basePath, 'start/routes.ts'))}' });`
260268
)
261269
await fs.create('start/routes.ts', ``)
262270
await fs.create('.env', 'PORT=3334')
@@ -285,7 +293,7 @@ test.group('DevServer', () => {
285293
await fs.createJson('tsconfig.json', { include: ['**/*'], exclude: [] })
286294
await fs.create(
287295
'bin/server.ts',
288-
`process.send({ type: 'hot-hook:file-changed', action: 'change', path: '${join(fs.basePath, 'start/routes.ts')}' });`
296+
`process.send({ type: 'hot-hook:file-changed', action: 'change', path: '${normalizePathForWindows(join(fs.basePath, 'start/routes.ts'))}' });`
289297
)
290298
await fs.create('start/routes.ts', ``)
291299
await fs.create('.env', 'PORT=3334')

0 commit comments

Comments
 (0)