Browser plugin: keep the Playwright server's output when it crashes

Aperta Adatta ai principianti
#2,040 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
php
Ambito
testing

Direzione di ricerca

Inizia leggendo src/Playwright/Servers/PlaywrightNpmServer.php in pestphp/pest-plugin-browser, soprattutto per capire come viene avviato il processo del server e come rileva la riga che indica che è in ascolto. Controlla i test esistenti per PlaywrightNpmServer e aggiungi test per la scrittura dei log nel file configurato e il rilevamento all’avvio; il lavoro è completato quando il log registra l’output se PEST_BROWSER_SERVER_LOG è impostato, mentre il comportamento quando non è impostato rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

PlaywrightNpmServer reads the Playwright server's output through a pipe held by the test process. When the server crashes mid-suite, whatever it printed last is lost with the pipe: the suite reports hundreds of Client unexpectedly closed failures and nothing says why the server went down.

Proposal

An opt-in environment variable, PEST_BROWSER_SERVER_LOG. When it names a file, the server command appends its stdout and stderr to that file as it comes, and startup detection reads the "listening" line from the file instead of the pipe. When the variable is unset, nothing changes.

Why a file

A file survives the process that wrote it. After a crash the last lines are still there, and a CI job can keep them as an artifact.

Patch

About 35 lines in src/Playwright/Servers/PlaywrightNpmServer.php of pestphp/pest-plugin-browser (written against 5.0.1). We have been running it since early November on a large browser suite across 6 parallel processes. Happy to open a pull request against 5.x if the approach suits you.

Diff
diff --git a/src/Playwright/Servers/PlaywrightNpmServer.php b/src/Playwright/Servers/PlaywrightNpmServer.php
--- a/src/Playwright/Servers/PlaywrightNpmServer.php
+++ b/src/Playwright/Servers/PlaywrightNpmServer.php
@@ -24,6 +24,11 @@
     private const string PLAYWRIGHT_VERSION = '1.62.1';
 
     /**
+     * The environment variable naming the file the server's output is appended to.
+     */
+    public const string LOG = 'PEST_BROWSER_SERVER_LOG';
+
+    /**
      * The underlying process instance, if any.
      */
     private ?SystemProcess $systemProcess = null;
@@ -60,11 +65,27 @@
             return;
         }
 
-        $this->systemProcess = SystemProcess::fromShellCommandline(sprintf(
+        $command = sprintf(
             $this->command,
             $this->host,
             $this->port,
-        ), $this->baseDirectory, [
+        );
+
+        // The file the server's output is appended to as it comes, when one is
+        // named: the output a pipe holds is lost with the process that reads it,
+        // and a server that crashes writes nothing once it is gone.
+        $log = getenv(self::LOG);
+        $log = is_string($log) && $log !== '' ? $log : null;
+        $offset = 0;
+
+        if ($log !== null) {
+            $command .= ' >> '.escapeshellarg($log).' 2>&1';
+
+            clearstatcache(true, $log);
+            $offset = is_file($log) ? (int) filesize($log) : 0;
+        }
+
+        $this->systemProcess = SystemProcess::fromShellCommandline($command, $this->baseDirectory, [
             'APP_URL' => sprintf('http://%s:%d', $this->host, $this->port),
         ]);
 
@@ -72,10 +93,22 @@
 
         $this->systemProcess->start();
 
-        $this->systemProcess->waitUntil(
-            fn (string $type, string $output): bool => str_contains($output, $this->until)
-        );
+        if ($log === null) {
+            $this->systemProcess->waitUntil(
+                fn (string $type, string $output): bool => str_contains($output, $this->until)
+            );
+        } else {
+            while ($this->isRunning()) {
+                $written = @file_get_contents($log, false, null, $offset);
 
+                if (is_string($written) && str_contains($written, $this->until)) {
+                    break;
+                }
+
+                usleep(10_000);
+            }
+        }
+
         if ($this->isRunning() === false) {
             self::ensurePlaywrightIsInstalledAndVersionIsSupported();
 
Lingua principale
PHP
Stelle
11.8k
Fork
559
Merge medio
5g 23h
PR unite (30g)
24

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pestphp/pest

Tutte le issue di pestphp/pest

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.