From 069d0e05ab8e03ac1db543be1b7654cc45e5efff Mon Sep 17 00:00:00 2001 From: Paul Murley Date: Sun, 2 Aug 2020 19:45:22 -0500 Subject: [PATCH 1/2] add a ModifyCmdFunc ExecAllocatorOption ModifyCmdFunc allows a function to be passed which will run on the exec.Cmd used to start the browser. This can be used to prevent chromedp from automatically passing os signals through to managed browsers, and to prevent browsers from being automatically killed on program exit. --- allocate.go | 26 +++++++++++++++++++++++--- allocate_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/allocate.go b/allocate.go index 5920cff9..9e0e0238 100644 --- a/allocate.go +++ b/allocate.go @@ -107,6 +107,8 @@ type ExecAllocator struct { wg sync.WaitGroup combinedOutputWriter io.Writer + + modifyCmdFunc func(cmd *exec.Cmd) } // allocTempDir is used to group all ExecAllocator temporary user data dirs in @@ -169,7 +171,12 @@ func (a *ExecAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*B os.RemoveAll(dataDir) } }() - allocateCmdOptions(cmd) + + if a.modifyCmdFunc != nil { + a.modifyCmdFunc(cmd) + } else { + allocateCmdOptions(cmd) + } stdout, err := cmd.StdoutPipe() if err != nil { @@ -177,8 +184,11 @@ func (a *ExecAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*B } cmd.Stderr = cmd.Stdout - if len(a.initEnv) > 0 { - cmd.Env = append(os.Environ(), a.initEnv...) + // Preserve environment variables set in the (lowest priority) existing + // environment, OverrideCmdFunc(), and Env (highest priority) + if len(a.initEnv) > 0 || len(cmd.Env) > 0 { + cmd.Env = append(os.Environ(), cmd.Env...) + cmd.Env = append(cmd.Env, a.initEnv...) } // We must start the cmd before calling cmd.Wait, as otherwise the two @@ -379,6 +389,16 @@ func Env(vars ...string) ExecAllocatorOption { } } +// ModifyCmdFunc allows for running an arbitrary function on the +// browser exec.Cmd object. This overrides the default version +// of the command which sends SIGKILL to any open browsers when +// the Go program exits. +func ModifyCmdFunc(f func(cmd *exec.Cmd)) ExecAllocatorOption { + return func(a *ExecAllocator) { + a.modifyCmdFunc = f + } +} + // UserDataDir is the command line option to set the user data dir. // // Note: set this option to manually set the profile directory used by Chrome. diff --git a/allocate_test.go b/allocate_test.go index d4ca4452..ce8827de 100644 --- a/allocate_test.go +++ b/allocate_test.go @@ -346,3 +346,30 @@ func TestWithBrowserOptionAlreadyAllocated(t *testing.T) { WithLogf(func(format string, args ...interface{}) {}), ) } + +func TestModifyCmdFunc(t *testing.T) { + t.Parallel() + + tz := "Atlantic/Reykjavik" + allocCtx, cancel := NewExecAllocator(context.Background(), + append([]ExecAllocatorOption{ + ModifyCmdFunc(func(cmd *exec.Cmd) { + cmd.Env = append(cmd.Env, "TZ=" + tz) + }), + }, allocOpts...)...) + defer cancel() + + ctx, cancel := NewContext(allocCtx) + defer cancel() + + var ret string + if err := Run(ctx, + Evaluate(`Intl.DateTimeFormat().resolvedOptions().timeZone`, &ret), + ); err != nil { + t.Fatal(err) + } + + if ret != tz { + t.Fatalf("got %s, want %s", ret, tz) + } +} \ No newline at end of file From 678235d775c8f53e942e236019f86a449eead1b1 Mon Sep 17 00:00:00 2001 From: Paul Murley Date: Sun, 2 Aug 2020 21:18:06 -0500 Subject: [PATCH 2/2] add newline --- allocate_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/allocate_test.go b/allocate_test.go index ce8827de..6eefe762 100644 --- a/allocate_test.go +++ b/allocate_test.go @@ -372,4 +372,5 @@ func TestModifyCmdFunc(t *testing.T) { if ret != tz { t.Fatalf("got %s, want %s", ret, tz) } -} \ No newline at end of file +} +