Skip to content

Commit 7ad0e39

Browse files
authored
Merge pull request #2294 from hydephp/fix/vite-test-issues
Improve Vite running detection on Windows
2 parents a4acb83 + 0b0d49f commit 7ad0e39

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

packages/framework/src/Console/Commands/ServeCommand.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function safeHandle(): int
7070

7171
$this->handleRunningProcesses();
7272

73+
if ($this->option('vite')) {
74+
$this->cleanupViteHotFile();
75+
}
76+
7377
return Command::SUCCESS;
7478
}
7579

@@ -220,13 +224,26 @@ protected function handleViteOutput(): void
220224
/** @experimental This feature may be removed before the final release. */
221225
protected function isPortAvailable(int $port): bool
222226
{
223-
$socket = @fsockopen('localhost', $port, $errno, $errstr, 1);
224-
if ($socket !== false) {
225-
fclose($socket);
227+
$addresses = ['localhost', '127.0.0.1'];
226228

227-
return false;
229+
foreach ($addresses as $address) {
230+
$socket = @fsockopen($address, $port, $errno, $errstr, 1);
231+
if ($socket !== false) {
232+
fclose($socket);
233+
234+
return false;
235+
}
228236
}
229237

230238
return true;
231239
}
240+
241+
protected function cleanupViteHotFile(): void
242+
{
243+
$hotFile = 'app/storage/framework/runtime/vite.hot';
244+
245+
if (Filesystem::exists($hotFile)) {
246+
Filesystem::unlinkIfExists($hotFile);
247+
}
248+
}
232249
}

packages/framework/tests/Feature/Commands/ServeCommandTest.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function testHydeServeCommandWithViteOption()
226226
->expectsOutput('vite latest output')
227227
->assertExitCode(0);
228228

229-
$this->assertFileExists('app/storage/framework/runtime/vite.hot');
229+
$this->assertFileDoesNotExist('app/storage/framework/runtime/vite.hot');
230230
}
231231

232232
public function testHydeServeCommandWithViteOptionButViteNotRunning()
@@ -269,18 +269,24 @@ public function testHydeServeCommandWithViteOptionButViteNotRunning()
269269
->expectsOutput('Starting the HydeRC server... Use Ctrl+C to stop')
270270
->assertExitCode(0);
271271

272-
$this->assertFileExists('app/storage/framework/runtime/vite.hot');
272+
$this->assertFileDoesNotExist('app/storage/framework/runtime/vite.hot');
273273
}
274274

275275
public function testHydeServeCommandWithViteOptionThrowsWhenPortIsInUse()
276276
{
277-
$socket = stream_socket_server('tcp://127.0.0.1:5173');
278-
279-
$this->artisan('serve --vite')
280-
->expectsOutputToContain('Unable to start Vite server: Port 5173 is already in use')
281-
->assertExitCode(1);
282-
283-
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
277+
$socket = stream_socket_server('tcp://127.0.0.1:5173', $errno, $errstr);
278+
279+
if ($socket === false) {
280+
$this->markTestSkipped("Unable to create test socket server: $errstr (errno: $errno)");
281+
}
282+
283+
try {
284+
$this->artisan('serve --vite')
285+
->expectsOutputToContain('Unable to start Vite server: Port 5173 is already in use')
286+
->assertExitCode(1);
287+
} finally {
288+
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
289+
}
284290
}
285291

286292
protected function binaryPath(): string

packages/framework/tests/Feature/Views/MetadataViewTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ protected function setUp(): void
2626

2727
$this->withSiteUrl();
2828
config(['hyde.cache_busting' => false]);
29+
30+
@unlink('app/storage/framework/runtime/vite.hot');
2931
}
3032

3133
protected function build(?string $page = null): void

0 commit comments

Comments
 (0)