Skip to content

Commit

Permalink
better error info for browser downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 20, 2022
1 parent 0acf6e2 commit b9c1f60
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
18 changes: 11 additions & 7 deletions lib/launcher/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (lc *Browser) httpClient() *http.Client {
func (lc *Browser) Get() (string, error) {
defer leakless.LockPort(lc.LockPort)()

if lc.Exists() {
if lc.Validate() == nil {
return lc.Destination(), nil
}

Expand All @@ -251,21 +251,25 @@ func (lc *Browser) MustGet() string {
return p
}

// Exists returns true if the browser executable path exists.
// If the executable is malformed it will return false.
func (lc *Browser) Exists() bool {
// Validate returns nil if the browser executable valid.
// If the executable is malformed it will return error.
func (lc *Browser) Validate() error {
_, err := os.Stat(lc.Destination())
if err != nil {
return false
return err
}

cmd := exec.Command(lc.Destination(), "--headless", "--no-sandbox",
"--disable-gpu", "--dump-dom", "about:blank")
b, err := cmd.CombinedOutput()
if err != nil {
return false
return fmt.Errorf("failed to run the browser: %w\n%s", err, b)
}
return bytes.Contains(b, []byte(`<html><head></head><body></body></html>`))
if !bytes.Contains(b, []byte(`<html><head></head><body></body></html>`)) {
return errors.New("the browser executable doesn't support headless mode")
}

return nil
}

// LookPath searches for the browser executable from often used paths on current operating system.
Expand Down
4 changes: 4 additions & 0 deletions lib/launcher/fixtures/chrome-empty/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package main ...
package main

func main() {}
8 changes: 8 additions & 0 deletions lib/launcher/fixtures/chrome-exit-err/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package main ...
package main

import "os"

func main() {
os.Exit(1)
}
14 changes: 10 additions & 4 deletions lib/launcher/launcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -252,10 +253,15 @@ func TestBrowserExists(t *testing.T) {

b := launcher.NewBrowser()
b.Revision = 0
g.False(b.Exists())
g.Err(b.Validate())

// fake a broken executable
g.E(utils.Mkdir(b.Destination()))
g.E(utils.Mkdir(filepath.Base(b.Destination())))
g.Cleanup(func() { _ = os.RemoveAll(b.Destination()) })
g.False(b.Exists())

g.E(exec.Command("go", "build", "-o", b.Destination(), "./fixtures/chrome-exit-err").CombinedOutput())
g.Has(b.Validate().Error(), "failed to run the browser")

g.E(utils.Mkdir(filepath.Base(b.Destination())))
g.E(exec.Command("go", "build", "-o", b.Destination(), "./fixtures/chrome-empty").CombinedOutput())
g.Eq(b.Validate().Error(), "the browser executable doesn't support headless mode")
}

0 comments on commit b9c1f60

Please sign in to comment.