Skip to content

Commit

Permalink
refactor: remove Sprintf, increment, unlambda
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Apr 13, 2023
1 parent 0b936bf commit a4e3253
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
6 changes: 2 additions & 4 deletions allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,8 @@ func WSURLReadTimeout(t time.Duration) ExecAllocatorOption {
// Use chromedp.NoModifyURL to prevent it from modifying the url.
func NewRemoteAllocator(parent context.Context, url string, opts ...RemoteAllocatorOption) (context.Context, context.CancelFunc) {
a := &RemoteAllocator{
wsURL: url,
modifyURLFunc: func(ctx context.Context, wsURL string) (string, error) {
return modifyURL(ctx, wsURL)
},
wsURL: url,
modifyURLFunc: modifyURL,
}
for _, o := range opts {
o(a)
Expand Down
6 changes: 3 additions & 3 deletions allocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func TestExecAllocatorMissingWebsocketAddr(t *testing.T) {
// in GitHub Actions, the error text could be:
// "chrome failed to start:\n/bin/bash: /etc/profile.d/env_vars.sh: Permission denied\nmkdir: cannot create directory ‘/run/user/1001’: Permission denied\n[0321/081807.491906:ERROR:headless_shell.cc(720)] Invalid devtools server address\n"
want := `failed to start`
got := fmt.Sprintf("%v", Run(ctx))
got := Run(ctx).Error()
if !strings.Contains(got, want) {
t.Fatalf("want error to match %q, got %q", want, got)
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestCombinedOutputError(t *testing.T) {

ctx, cancel := NewContext(allocCtx)
defer cancel()
got := fmt.Sprint(Run(ctx))
got := Run(ctx).Error()
want := "failed to start"
if !strings.Contains(got, want) {
t.Fatalf("got %q, want %q", got, want)
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestStartsWithNonBlankTab(t *testing.T) {
t.Parallel()

allocCtx, cancel := NewExecAllocator(context.Background(),
append(allocOpts[:],
append(allocOpts,
ModifyCmdFunc(func(cmd *exec.Cmd) {
// it assumes that the last argument is "about:blank" and
// replace it with other URL.
Expand Down
9 changes: 4 additions & 5 deletions chromedp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ func testAllocateSeparate(tb testing.TB) (context.Context, context.CancelFunc) {
tb.Fatal(err)
}
ListenBrowser(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *runtime.EventExceptionThrown:
if ev, ok := ev.(*runtime.EventExceptionThrown); ok {
tb.Errorf("%+v\n", ev.ExceptionDetails)
}
})
Expand Down Expand Up @@ -498,7 +497,7 @@ func TestDialTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = NewBrowser(ctx, url, WithDialTimeout(time.Microsecond))
got, want := fmt.Sprintf("%v", err), "i/o timeout"
got, want := err.Error(), "i/o timeout"
if !strings.Contains(got, want) {
t.Fatalf("got %q, want %q", got, want)
}
Expand All @@ -521,7 +520,7 @@ func TestDialTimeout(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, err = NewBrowser(ctx, url, WithDialTimeout(0))
got := fmt.Sprintf("%v", err)
got := err.Error()
if !strings.Contains(got, "EOF") && !strings.Contains(got, "connection reset") {
t.Fatalf("got %q, want %q or %q", got, "EOF", "connection reset")
}
Expand Down Expand Up @@ -957,7 +956,7 @@ func TestDirectCloseTarget(t *testing.T) {
err := Run(ctx, ActionFunc(func(ctx context.Context) error {
return target.CloseTarget(c.Target.TargetID).Do(ctx)
}))
got := fmt.Sprint(err)
got := err.Error()
if !strings.Contains(got, want) {
t.Fatalf("want %q, got %q", want, got)
}
Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestRetryInterval(t *testing.T) {
// only after the number of result nodes >= s.exp .
count := WaitFunc(
func(ctx context.Context, f *cdp.Frame, eci cdpruntime.ExecutionContextID, ni ...cdp.NodeID) ([]*cdp.Node, error) {
retryCount += 1
retryCount++
return nil, ErrInvalidTarget
},
)
Expand Down

0 comments on commit a4e3253

Please sign in to comment.