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

Add only-shell option to run options #518

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
15 changes: 14 additions & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ func (d *PlaywrightDriver) installBrowsers() error {
if d.options.Browsers != nil {
additionalArgs = append(additionalArgs, d.options.Browsers...)
}

if d.options.OnlyInstallShell {
additionalArgs = append(additionalArgs, "--only-shell")
}

if d.options.DryRun {
additionalArgs = append(additionalArgs, "--dry-run")
}

cmd := d.Command(additionalArgs...)
cmd.Stdout = d.options.Stdout
cmd.Stderr = d.options.Stderr
Expand All @@ -228,14 +237,18 @@ type RunOptions struct {
// - Windows: %USERPROFILE%\AppData\Local
// - macOS: ~/Library/Caches
// - Linux: ~/.cache
DriverDirectory string
DriverDirectory string
// OnlyInstallShell only downloads the headless shell. (For chromium browsers only)
OnlyInstallShell bool
SkipInstallBrowsers bool
// if not set and SkipInstallBrowsers is false, will download all browsers (chromium, firefox, webkit)
Browsers []string
Verbose bool // default true
Stdout io.Writer
Stderr io.Writer
Logger *slog.Logger
// DryRun does not install browser/dependencies. It will only print information.
DryRun bool
}

// Install does download the driver and the browsers.
Expand Down
65 changes: 52 additions & 13 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@ func TestRunOptionsRedirectStderr(t *testing.T) {
r, w := io.Pipe()
var output string
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
buf := bufio.NewReader(r)
for {
line, _, err := buf.ReadLine()
if err == io.EOF {
break
}
output += string(line)
}
_ = r.Close()
}()
readIOAsyncTilEOF(t, r, wg, &output)

driverPath := t.TempDir()
options := &RunOptions{
Expand All @@ -59,6 +47,40 @@ func TestRunOptionsRedirectStderr(t *testing.T) {
require.Contains(t, output, fmt.Sprintf("path=%s", driverPath))
}

func TestRunOptions_OnlyInstallShell(t *testing.T) {
if getBrowserName() != "chromium" {
t.Skip("chromium only")
return
}

r, w := io.Pipe()
var output string
wg := &sync.WaitGroup{}
readIOAsyncTilEOF(t, r, wg, &output)

driverPath := t.TempDir()
driver, err := NewDriver(&RunOptions{
Stdout: w,
DriverDirectory: driverPath,
Browsers: []string{getBrowserName()},
Verbose: true,
OnlyInstallShell: true,
DryRun: true,
})
require.NoError(t, err)
browserPath := t.TempDir()

t.Setenv("PLAYWRIGHT_BROWSERS_PATH", browserPath)

err = driver.Install()
require.NoError(t, err)
require.NoError(t, w.Close())
wg.Wait()

assert.Contains(t, output, "browser: chromium-headless-shell version")
assert.NotContains(t, output, "browser: chromium version")
}

func TestDriverInstall(t *testing.T) {
driverPath := t.TempDir()
driver, err := NewDriver(&RunOptions{
Expand Down Expand Up @@ -187,3 +209,20 @@ func getBrowserName() string {
}
return "chromium"
}

func readIOAsyncTilEOF(t *testing.T, r *io.PipeReader, wg *sync.WaitGroup, output *string) {
t.Helper()
wg.Add(1)
go func() {
defer wg.Done()
buf := bufio.NewReader(r)
for {
line, _, err := buf.ReadLine()
if err == io.EOF {
break
}
*output += string(line)
}
_ = r.Close()
}()
}
Loading