Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a better error message when Chromium isn't found #1137

Merged
merged 8 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 33 additions & 19 deletions chromium/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type BrowserType struct {
vu k6modules.VU
hooks *common.Hooks
k6Metrics *k6ext.CustomMetrics
execPath string // path to the Chromium executable
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
randSrc *rand.Rand
envLookupper env.LookupFunc
}
Expand Down Expand Up @@ -184,7 +183,12 @@ func (b *BrowserType) launch(
}
flags["user-data-dir"] = dataDir.Dir

browserProc, err := b.allocate(ctx, opts, flags, dataDir, logger)
path, err := executablePath(opts.ExecutablePath, b.envLookupper, exec.LookPath)
if err != nil {
return nil, 0, fmt.Errorf("finding browser executable: %w", err)
}

browserProc, err := b.allocate(ctx, path, flags, dataDir, logger)
if browserProc == nil {
return nil, 0, fmt.Errorf("launching browser: %w", err)
}
Expand Down Expand Up @@ -224,7 +228,7 @@ func (b *BrowserType) Name() string {

// allocate starts a new Chromium browser process and returns it.
func (b *BrowserType) allocate(
ctx context.Context, opts *common.BrowserOptions,
ctx context.Context, path string,
flags map[string]any, dataDir *storage.Dir,
logger *log.Logger,
) (_ *common.BrowserProcess, rerr error) {
Expand All @@ -240,23 +244,32 @@ func (b *BrowserType) allocate(
return nil, err
}

path := opts.ExecutablePath
if path == "" {
path = b.ExecutablePath()
}

return common.NewLocalBrowserProcess(bProcCtx, path, args, dataDir, bProcCtxCancel, logger) //nolint: wrapcheck
}

// ExecutablePath returns the path where the extension expects to find the browser executable.
func (b *BrowserType) ExecutablePath() (execPath string) {
if b.execPath != "" {
return b.execPath
var (
// ErrChromeNotInstalled is returned when the Chrome executable is not found.
ErrChromeNotInstalled = errors.New("neither chrome nor chromium is installed on this system")

// ErrChromeNotFoundAtPath is returned when the Chrome executable is not found at the given path.
ErrChromeNotFoundAtPath = errors.New("neither chrome nor chromium found on the path")
)

// executablePath returns the path where the extension expects to find the browser executable.
func executablePath(
path string,
env env.LookupFunc,
lookPath func(file string) (string, error), // os.LookPath
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
) (string, error) {
// find the browser executable in the user provided path
if path := strings.TrimSpace(path); path != "" {
if _, err := lookPath(path); err == nil {
return path, nil
}
return "", fmt.Errorf("%w: %s", ErrChromeNotFoundAtPath, path)
}
defer func() {
b.execPath = execPath
}()

// find the browser executable in the default paths below
paths := []string{
// Unix-like
"headless_shell",
Expand All @@ -277,16 +290,17 @@ func (b *BrowserType) ExecutablePath() (execPath string) {
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Chromium.app/Contents/MacOS/Chromium",
}
if userProfile, ok := b.envLookupper("USERPROFILE"); ok {
// find the browser executable in the user profile
if userProfile, ok := env("USERPROFILE"); ok {
paths = append(paths, filepath.Join(userProfile, `AppData\Local\Google\Chrome\Application\chrome.exe`))
}
for _, path := range paths {
if _, err := exec.LookPath(path); err == nil {
return path
if _, err := lookPath(path); err == nil {
return path, nil
}
}

return ""
return "", ErrChromeNotInstalled
}

// parseArgs parses command-line arguments and returns them.
Expand Down
101 changes: 101 additions & 0 deletions chromium/browser_type_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package chromium

import (
"io/fs"
"net"
"path/filepath"
"testing"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/env"

k6lib "go.k6.io/k6/lib"
"go.k6.io/k6/lib/types"
Expand Down Expand Up @@ -150,3 +153,101 @@ func TestBrowserTypePrepareFlags(t *testing.T) {
})
}
}

func TestExecutablePath(t *testing.T) {
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

// we pick a random file name to look for in our tests
// this doesn't matter as long as it's in the paths we look for
// in ExecutablePath function.
const chromiumExecutable = "google-chrome"

userProvidedPath := filepath.Join("path", "to", "chromium")

fileNotExists := func(file string) (string, error) {
return "", fs.ErrNotExist
}

tests := map[string]struct {
userProvidedPath string // user provided path
lookPath func(file string) (string, error) // determines if a file exists
userProfile env.LookupFunc // user profile folder lookup

wantPath string
wantErr error
}{
"without_chromium": {
userProvidedPath: "",
lookPath: fileNotExists,
userProfile: env.EmptyLookup,
wantPath: "",
wantErr: ErrChromeNotInstalled,
},
"with_chromium": {
userProvidedPath: "",
lookPath: func(file string) (string, error) {
if file == chromiumExecutable {
return "", nil
}
return "", fs.ErrNotExist
},
userProfile: env.EmptyLookup,
wantPath: chromiumExecutable,
wantErr: nil,
},
"without_chromium_in_user_path": {
userProvidedPath: userProvidedPath,
lookPath: fileNotExists,
userProfile: env.EmptyLookup,
wantPath: "",
wantErr: ErrChromeNotFoundAtPath,
},
"with_chromium_in_user_path": {
userProvidedPath: userProvidedPath,
lookPath: func(file string) (string, error) {
if file == userProvidedPath {
return "", nil
}
return "", fs.ErrNotExist
},
userProfile: env.EmptyLookup,
wantPath: userProvidedPath,
wantErr: nil,
},
"without_chromium_in_user_profile": {
userProvidedPath: "",
lookPath: fileNotExists,
userProfile: env.ConstLookup("USERPROFILE", `home`),
wantPath: "",
wantErr: ErrChromeNotInstalled,
},
"with_chromium_in_user_profile": {
userProvidedPath: "",
lookPath: func(file string) (string, error) { // we look chrome.exe in the user profile
if file == filepath.Join("home", `AppData\Local\Google\Chrome\Application\chrome.exe`) {
return "", nil
}
return "", fs.ErrNotExist
},
userProfile: env.ConstLookup("USERPROFILE", `home`),
wantPath: filepath.Join("home", `AppData\Local\Google\Chrome\Application\chrome.exe`),
wantErr: nil,
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

path, err := executablePath(tt.userProvidedPath, tt.userProfile, tt.lookPath)

assert.Equal(t, tt.wantPath, path)

if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
return
}
assert.NoError(t, err)
})
}
}