Skip to content

Commit 5afc7b7

Browse files
authored
feat: remove hardcoded subdirectories to RunOptions.DriverDirectory and PLAYWRIGHT_DRIVER_PATH (#501)
BREAKING CHANGE: When specifying the playwright driver directory via `RunOptions.DriverDirectory` or the `PLAYWRIGHT_DRIVER_PATH` environment variable, you need to point directly to the directory of the corresponding version. The previously hard-coded `ms-playwright-go/x.xx.x` subdirectory is now only applies to the default cache directory.
1 parent d060d01 commit 5afc7b7

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

run.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var (
3232
//
3333
// It's required for playwright-go to work.
3434
type PlaywrightDriver struct {
35-
driverDirectory, Version string
36-
options *RunOptions
35+
Version string
36+
options *RunOptions
3737
}
3838

3939
func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) {
@@ -42,9 +42,8 @@ func NewDriver(options ...*RunOptions) (*PlaywrightDriver, error) {
4242
return nil, err
4343
}
4444
return &PlaywrightDriver{
45-
options: transformed,
46-
driverDirectory: filepath.Join(transformed.DriverDirectory, "ms-playwright-go", playwrightCliVersion),
47-
Version: playwrightCliVersion,
45+
options: transformed,
46+
Version: playwrightCliVersion,
4847
}, nil
4948
}
5049

@@ -65,13 +64,15 @@ func getDefaultCacheDirectory() (string, error) {
6564
}
6665

6766
func (d *PlaywrightDriver) isUpToDateDriver() (bool, error) {
68-
if _, err := os.Stat(d.driverDirectory); os.IsNotExist(err) {
69-
if err := os.MkdirAll(d.driverDirectory, 0o777); err != nil {
67+
if _, err := os.Stat(d.options.DriverDirectory); os.IsNotExist(err) {
68+
if err := os.MkdirAll(d.options.DriverDirectory, 0o777); err != nil {
7069
return false, fmt.Errorf("could not create driver directory: %w", err)
7170
}
7271
}
73-
if _, err := os.Stat(getDriverCliJs(d.driverDirectory)); os.IsNotExist(err) {
72+
if _, err := os.Stat(getDriverCliJs(d.options.DriverDirectory)); os.IsNotExist(err) {
7473
return false, nil
74+
} else if err != nil {
75+
return false, fmt.Errorf("could not check if driver is up2date: %w", err)
7576
}
7677
cmd := d.Command("--version")
7778
output, err := cmd.Output()
@@ -81,12 +82,13 @@ func (d *PlaywrightDriver) isUpToDateDriver() (bool, error) {
8182
if bytes.Contains(output, []byte(d.Version)) {
8283
return true, nil
8384
}
84-
return false, nil
85+
// avoid triggering downloads and accidentally overwriting files
86+
return false, fmt.Errorf("driver exists but version not %s in : %s", d.Version, d.options.DriverDirectory)
8587
}
8688

8789
// Command returns an exec.Cmd for the driver.
8890
func (d *PlaywrightDriver) Command(arg ...string) *exec.Cmd {
89-
cmd := exec.Command(getNodeExecutable(d.driverDirectory), append([]string{getDriverCliJs(d.driverDirectory)}, arg...)...)
91+
cmd := exec.Command(getNodeExecutable(d.options.DriverDirectory), append([]string{getDriverCliJs(d.options.DriverDirectory)}, arg...)...)
9092
cmd.SysProcAttr = defaultSysProcAttr
9193
return cmd
9294
}
@@ -117,7 +119,7 @@ func (d *PlaywrightDriver) Uninstall() error {
117119
}
118120

119121
d.log("Removing driver...")
120-
if err := os.RemoveAll(d.driverDirectory); err != nil {
122+
if err := os.RemoveAll(d.options.DriverDirectory); err != nil {
121123
return fmt.Errorf("could not remove driver directory: %w", err)
122124
}
123125

@@ -129,13 +131,13 @@ func (d *PlaywrightDriver) Uninstall() error {
129131
func (d *PlaywrightDriver) DownloadDriver() error {
130132
up2Date, err := d.isUpToDateDriver()
131133
if err != nil {
132-
return fmt.Errorf("could not check if driver is up2date: %w", err)
134+
return err
133135
}
134136
if up2Date {
135137
return nil
136138
}
137139

138-
d.log(fmt.Sprintf("Downloading driver to %s", d.driverDirectory))
140+
d.log(fmt.Sprintf("Downloading driver to %s", d.options.DriverDirectory))
139141

140142
body, err := downloadDriver(d.getDriverURLs())
141143
if err != nil {
@@ -147,7 +149,7 @@ func (d *PlaywrightDriver) DownloadDriver() error {
147149
}
148150

149151
for _, zipFile := range zipReader.File {
150-
zipFileDiskPath := filepath.Join(d.driverDirectory, zipFile.Name)
152+
zipFileDiskPath := filepath.Join(d.options.DriverDirectory, zipFile.Name)
151153
if zipFile.FileInfo().IsDir() {
152154
if err := os.MkdirAll(zipFileDiskPath, os.ModePerm); err != nil {
153155
return fmt.Errorf("could not create directory: %w", err)
@@ -219,12 +221,14 @@ func (d *PlaywrightDriver) uninstallBrowsers() error {
219221

220222
// RunOptions are custom options to run the driver
221223
type RunOptions struct {
222-
// DriverDirectory is the directory where the playwright cli will be downloaded.
223-
// Default depends on the platform:
224+
// DriverDirectory points to the playwright driver directory.
225+
// It should have two subdirectories: node and package.
226+
// You can also specify it using the environment variable PLAYWRIGHT_DRIVER_PATH.
227+
//
228+
// Default is user cache directory + "/ms-playwright-go/x.xx.xx":
224229
// - Windows: %USERPROFILE%\AppData\Local
225230
// - macOS: ~/Library/Caches
226231
// - Linux: ~/.cache
227-
// You can specify here or set the environment variable PLAYWRIGHT_DRIVER_PATH
228232
DriverDirectory string
229233
SkipInstallBrowsers bool
230234
// if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit)
@@ -259,7 +263,7 @@ func Run(options ...*RunOptions) (*Playwright, error) {
259263
}
260264
up2date, err := driver.isUpToDateDriver()
261265
if err != nil || !up2date {
262-
return nil, fmt.Errorf("please install the driver (v%s) and browsers first: %w", playwrightCliVersion, err)
266+
return nil, fmt.Errorf("please install the driver (v%s) first: %w", playwrightCliVersion, err)
263267
}
264268
connection, err := driver.run()
265269
if err != nil {
@@ -280,11 +284,11 @@ func transformRunOptions(options ...*RunOptions) (*RunOptions, error) {
280284
option.DriverDirectory = os.Getenv("PLAYWRIGHT_DRIVER_PATH")
281285
}
282286
if option.DriverDirectory == "" {
283-
var err error
284-
option.DriverDirectory, err = getDefaultCacheDirectory()
287+
cacheDirectory, err := getDefaultCacheDirectory()
285288
if err != nil {
286289
return nil, fmt.Errorf("could not get default cache directory: %w", err)
287290
}
291+
option.DriverDirectory = filepath.Join(cacheDirectory, "ms-playwright-go", playwrightCliVersion)
288292
}
289293
if option.Stdout == nil {
290294
option.Stdout = os.Stdout

0 commit comments

Comments
 (0)