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

update browser package from master #302

Merged
merged 1 commit into from
Jul 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions auth/browser/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/trace"
"strings"
Expand Down Expand Up @@ -225,12 +224,12 @@ func l() logger.Interface {

// pwRepair attempts to repair the playwright installation.
func pwRepair(runopts *playwright.RunOptions) error {
driverDirectory, err := pwcompat.DriverDir(runopts)
ad, err := pwcompat.NewAdapter(runopts)
// check node permissions
if err != nil {
return err
return fmt.Errorf("repair: %w", err)
}
// check node permissions
if err := pwIsKnownProblem(driverDirectory); err != nil {
if err := pwWrongNodePerms(ad.DriverBinaryLocation); err != nil {
return err
}
return reinstall(runopts)
Expand All @@ -247,17 +246,17 @@ func Reinstall(browser Browser, verbose bool) error {

func reinstall(runopts *playwright.RunOptions) error {
l().Debugf("reinstalling browser: %s", runopts.Browsers[0])
drvdir, err := pwcompat.DriverDir(runopts)
ad, err := pwcompat.NewAdapter(runopts)
if err != nil {
return err
}
l().Debugf("removing %s", drvdir)
if err := os.RemoveAll(drvdir); err != nil {
l().Debugf("removing %s", ad.DriverDirectory)
if err := os.RemoveAll(ad.DriverDirectory); err != nil {
return err
}

// attempt to reinstall
l().Debugf("reinstalling %s", drvdir)
l().Debugf("reinstalling %s", ad.DriverDirectory)
if err := installFn(runopts); err != nil {
// we did everything we could, but it still failed.
return err
Expand All @@ -267,17 +266,17 @@ func reinstall(runopts *playwright.RunOptions) error {

var errUnknownProblem = errors.New("unknown problem")

// pwIsKnownProblem checks if the playwright installation is in a known
// pwWrongNodePerms checks if the playwright installation is in a known
// problematic state, and if yes, return nil. If the problem is unknown,
// returns an errUnknownProblem.
func pwIsKnownProblem(path string) error {
func pwWrongNodePerms(path string) error {
if runtime.GOOS == "windows" {
// this should not ever happen on windows, as this problem relates to
// executable flag not being set, which is not a thing in a
// DOS/Windows world.
return errors.New("impossible has just happened, call the exorcist")
}
fi, err := os.Stat(filepath.Join(path, "node"))
fi, err := os.Stat(path)
if err != nil {
return err
}
Expand Down
12 changes: 8 additions & 4 deletions auth/browser/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ func Test_pwRepair(t *testing.T) {
Browsers: []string{"chromium"},
DriverDirectory: baseDir,
}
dir, err := pwcompat.DriverDir(runopts)
ad, err := pwcompat.NewAdapter(runopts)
if err != nil {
t.Fatal(err)
}
dir := ad.DriverDirectory
if err != nil {
t.Fatal(err)
}
Expand All @@ -81,7 +85,7 @@ func makeFakeNode(t *testing.T, dir string, mode fs.FileMode) {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "node"), []byte("hello"), mode); err != nil {
if err := os.WriteFile(filepath.Join(dir, pwcompat.NodeExe), []byte("hello"), mode); err != nil {
t.Fatal(err)
}
}
Expand All @@ -90,14 +94,14 @@ func Test_pwIsKnownProblem(t *testing.T) {
t.Run("known executable permissions problem", func(t *testing.T) {
baseDir := t.TempDir()
makeFakeNode(t, baseDir, 0o644)
if err := pwIsKnownProblem(baseDir); err != nil {
if err := pwWrongNodePerms(filepath.Join(baseDir, pwcompat.NodeExe)); err != nil {
t.Fatal(err)
}
})
t.Run("other problem", func(t *testing.T) {
baseDir := t.TempDir()
makeFakeNode(t, baseDir, 0o755)
err := pwIsKnownProblem(baseDir)
err := pwWrongNodePerms(filepath.Join(baseDir, pwcompat.NodeExe))
if err == nil {
t.Fatal("unexpected success")
}
Expand Down
71 changes: 35 additions & 36 deletions auth/browser/pwcompat/pwcompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,58 @@
package pwcompat

import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"runtime"

"github.com/playwright-community/playwright-go"
)

// Workaround for unexported driver dir in playwright.

// newDriverFn is the function that creates a new driver. It is set to
// playwright.NewDriver by default, but can be overridden for testing.
var newDriverFn = playwright.NewDriver
var (
// environment related variables
homedir string = must(os.UserHomeDir())
cacheDir string // platform dependent
NodeExe string = "node"
)

func getDefaultCacheDirectory() (string, error) {
// pinched from playwright
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not get user home directory: %w", err)
}
switch runtime.GOOS {
case "windows":
return filepath.Join(userHomeDir, "AppData", "Local"), nil
case "darwin":
return filepath.Join(userHomeDir, "Library", "Caches"), nil
case "linux":
return filepath.Join(userHomeDir, ".cache"), nil
func must[T any](v T, e error) T {
if e != nil {
log.Panicf("error getting user home directory: %s", e)
}
return "", errors.New("could not determine cache directory")
return v
}

func NewDriver(runopts *playwright.RunOptions) (*playwright.PlaywrightDriver, error) {
drv, err := newDriverFn(runopts)
if err != nil {
return nil, fmt.Errorf("error initialising driver: %w", err)
}
return drv, nil
type Adapter struct {
DriverDirectory string
DriverBinaryLocation string

drv *playwright.PlaywrightDriver
opts *playwright.RunOptions
}

// DriverDir returns the driver directory, broken in this commit:
// https://github.com/playwright-community/playwright-go/pull/449/commits/372e209c776222f4681cf1b24a1379e3648dd982
func DriverDir(runopts *playwright.RunOptions) (string, error) {
drv, err := NewDriver(runopts)
func NewAdapter(runopts *playwright.RunOptions) (*Adapter, error) {
drv, err := playwright.NewDriver(runopts)
if err != nil {
return "", err
return nil, err
}
baseDriverDirectory, err := getDefaultCacheDirectory()
if err != nil {
return "", fmt.Errorf("it's just not your day: %w", err)
if cacheDir == "" { // i.e. freebsd etc.
cacheDir, _ = os.UserCacheDir()
}
driverDirectory := filepath.Join(nvl(runopts.DriverDirectory, baseDriverDirectory), "ms-playwright-go", drv.Version)
return driverDirectory, nil
drvdir := filepath.Join(nvl(runopts.DriverDirectory, cacheDir), "ms-playwright-go", drv.Version)
drvbin := filepath.Join(drvdir, NodeExe)

return &Adapter{
drv: drv,
opts: runopts,
DriverDirectory: drvdir,
DriverBinaryLocation: drvbin,
}, nil
}

func (a *Adapter) Driver() *playwright.PlaywrightDriver {
return a.drv
}

func nvl(first string, rest ...string) string {
Expand Down
8 changes: 8 additions & 0 deletions auth/browser/pwcompat/pwcompat_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pwcompat

import "path/filepath"

func init() {
cacheDir = filepath.Join(homedir, "Library", "Caches")
NodeExe = "node"
}
49 changes: 0 additions & 49 deletions auth/browser/pwcompat/pwcompat_darwin_test.go

This file was deleted.

8 changes: 8 additions & 0 deletions auth/browser/pwcompat/pwcompat_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pwcompat

import "path/filepath"

func init() {
cacheDir = filepath.Join(homedir, ".cache")
NodeExe = "node"
}
55 changes: 25 additions & 30 deletions auth/browser/pwcompat/pwcompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,43 @@
// workaround.
package pwcompat

import (
"testing"
import "testing"

"github.com/playwright-community/playwright-go"
)

func TestNewDriver(t *testing.T) {
t.Parallel()
func Test_nvl(t *testing.T) {
type args struct {
runopts *playwright.RunOptions
first string
rest []string
}
tests := []struct {
name string
args args
wantErr bool
name string
args args
want string
}{
{
"default dir",
args{&playwright.RunOptions{
DriverDirectory: "",
SkipInstallBrowsers: true,
Browsers: []string{"chrome"}},
},
false,
"first",
args{"first", []string{"second", "third"}},
"first",
},
{
"second",
args{"", []string{"second", "third"}},
"second",
},
{
"third",
args{"", []string{"", "third"}},
"third",
},
{
"custom dir",
args{&playwright.RunOptions{
DriverDirectory: t.TempDir(),
SkipInstallBrowsers: true,
Browsers: []string{"chrome"}},
},
false,
"empty",
args{"", []string{""}},
"",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := NewDriver(tt.args.runopts)
if (err != nil) != tt.wantErr {
t.Errorf("NewDriver() error = %v, wantErr %v", err, tt.wantErr)
return
if got := nvl(tt.args.first, tt.args.rest...); got != tt.want {
t.Errorf("nvl() = %v, want %v", got, tt.want)
}
})
}
Expand Down
8 changes: 8 additions & 0 deletions auth/browser/pwcompat/pwcompat_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pwcompat

import "path/filepath"

func init() {
cacheDir = filepath.Join(homedir, "AppData", "Local")
NodeExe = "node.exe"
}
Loading