sketch: exclude internal processes (headless-chrome) from port monitoring
Add SKETCH_IGNORE_PORTS environment variable to headless-shell browser processes
and modify port monitoring to exclude processes with this variable.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sff3b145df27ee3bek
diff --git a/loop/port_monitor_test.go b/loop/port_monitor_test.go
index 1339720..ec847bd 100644
--- a/loop/port_monitor_test.go
+++ b/loop/port_monitor_test.go
@@ -2,6 +2,8 @@
import (
"context"
+ "os"
+ "os/exec"
"testing"
"time"
@@ -224,6 +226,42 @@
}
}
+// TestPortMonitor_ShouldIgnoreProcess tests the shouldIgnoreProcess function.
+func TestPortMonitor_ShouldIgnoreProcess(t *testing.T) {
+ agent := createTestAgent(t)
+ pm := NewPortMonitor(agent, 100*time.Millisecond)
+
+ // Test with current process (should not be ignored)
+ currentPid := os.Getpid()
+ if pm.shouldIgnoreProcess(currentPid) {
+ t.Errorf("current process should not be ignored")
+ }
+
+ // Test with invalid PID
+ if pm.shouldIgnoreProcess(0) {
+ t.Errorf("invalid PID should not be ignored")
+ }
+ if pm.shouldIgnoreProcess(-1) {
+ t.Errorf("negative PID should not be ignored")
+ }
+
+ // Test with a process that has SKETCH_IGNORE_PORTS=1
+ cmd := exec.Command("sleep", "5")
+ cmd.Env = append(os.Environ(), "SKETCH_IGNORE_PORTS=1")
+ err := cmd.Start()
+ if err != nil {
+ t.Fatalf("failed to start test process: %v", err)
+ }
+ defer cmd.Process.Kill()
+
+ // Allow a moment for the process to start
+ time.Sleep(100 * time.Millisecond)
+
+ if !pm.shouldIgnoreProcess(cmd.Process.Pid) {
+ t.Errorf("process with SKETCH_IGNORE_PORTS=1 should be ignored")
+ }
+}
+
// createTestAgent creates a minimal test agent for testing.
func createTestAgent(t *testing.T) *Agent {
// Create a minimal agent for testing