Skip to content

Commit

Permalink
add a ModifyCmdFunc ExecAllocatorOption (#674)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pmurley committed May 9, 2021
1 parent c836a29 commit 538d83f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
26 changes: 23 additions & 3 deletions allocate.go
Expand Up @@ -108,6 +108,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
Expand Down Expand Up @@ -170,16 +172,24 @@ 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 {
return nil, err
}
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
Expand Down Expand Up @@ -391,6 +401,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.
Expand Down
28 changes: 28 additions & 0 deletions allocate_test.go
Expand Up @@ -348,3 +348,31 @@ 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)
}
}

0 comments on commit 538d83f

Please sign in to comment.