| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "sort" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // playwrightChrome is the newest Chromium in a Playwright browser cache |
| 13 | // under home, or "". rastrillo's harness looks in Linux's cache alone |
| 14 | // (~/.cache/ms-playwright/chromium-*/chrome-linux64/chrome), so on a Mac |
| 15 | // `make test-browser` found nothing unless RASTRILLO_CHROME was set by |
| 16 | // hand. The browser tests (zoom_browser_test.go) hand it this. It lives |
| 17 | // in an untagged file so the plain suite tests the finder itself. |
| 18 | func playwrightChrome(home string) string { |
| 19 | var hits []string |
| 20 | for _, layout := range []string{ |
| 21 | // Linux, as the harness already knows it. |
| 22 | filepath.Join(".cache", "ms-playwright", "chromium-*", "chrome-linux64", "chrome"), |
| 23 | // macOS since Playwright moved to Chrome for Testing. |
| 24 | filepath.Join("Library", "Caches", "ms-playwright", "chromium-*", "chrome-mac*", "Google Chrome for Testing.app", "Contents", "MacOS", "Google Chrome for Testing"), |
| 25 | // macOS before it. |
| 26 | filepath.Join("Library", "Caches", "ms-playwright", "chromium-*", "chrome-mac*", "Chromium.app", "Contents", "MacOS", "Chromium"), |
| 27 | } { |
| 28 | m, _ := filepath.Glob(filepath.Join(home, layout)) |
| 29 | hits = append(hits, m...) |
| 30 | } |
| 31 | if len(hits) == 0 { |
| 32 | return "" |
| 33 | } |
| 34 | sort.SliceStable(hits, func(i, j int) bool { return chromiumRevision(hits[i]) > chromiumRevision(hits[j]) }) |
| 35 | return hits[0] |
| 36 | } |
| 37 | |
| 38 | // chromiumRevision is the N of the chromium-N directory in path, or -1. |
| 39 | func chromiumRevision(path string) int { |
| 40 | for _, part := range strings.Split(filepath.ToSlash(path), "/") { |
| 41 | if rest, ok := strings.CutPrefix(part, "chromium-"); ok { |
| 42 | if n, err := strconv.Atoi(rest); err == nil { |
| 43 | return n |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | return -1 |
| 48 | } |
| 49 | |
| 50 | func TestPlaywrightChromeFindsEveryCacheLayout(t *testing.T) { |
| 51 | touch := func(t *testing.T, path string) { |
| 52 | t.Helper() |
| 53 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(path, nil, 0o755); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | cft := func(home, rev, arch string) string { |
| 61 | return filepath.Join(home, "Library", "Caches", "ms-playwright", "chromium-"+rev, "chrome-"+arch, "Google Chrome for Testing.app", "Contents", "MacOS", "Google Chrome for Testing") |
| 62 | } |
| 63 | |
| 64 | t.Run("none", func(t *testing.T) { |
| 65 | if got := playwrightChrome(t.TempDir()); got != "" { |
| 66 | t.Errorf("empty home: %q", got) |
| 67 | } |
| 68 | }) |
| 69 | t.Run("macOS, Chrome for Testing", func(t *testing.T) { |
| 70 | home := t.TempDir() |
| 71 | want := cft(home, "1234", "mac-arm64") |
| 72 | touch(t, want) |
| 73 | if got := playwrightChrome(home); got != want { |
| 74 | t.Errorf("got %q, want %q", got, want) |
| 75 | } |
| 76 | }) |
| 77 | t.Run("macOS, newest revision wins", func(t *testing.T) { |
| 78 | home := t.TempDir() |
| 79 | touch(t, filepath.Join(home, "Library", "Caches", "ms-playwright", "chromium-1148", "chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium")) |
| 80 | touch(t, cft(home, "1228", "mac-arm64")) |
| 81 | want := cft(home, "1234", "mac-arm64") |
| 82 | touch(t, want) |
| 83 | if got := playwrightChrome(home); got != want { |
| 84 | t.Errorf("got %q, want %q", got, want) |
| 85 | } |
| 86 | }) |
| 87 | t.Run("macOS, older Chromium.app", func(t *testing.T) { |
| 88 | home := t.TempDir() |
| 89 | want := filepath.Join(home, "Library", "Caches", "ms-playwright", "chromium-1148", "chrome-mac", "Chromium.app", "Contents", "MacOS", "Chromium") |
| 90 | touch(t, want) |
| 91 | if got := playwrightChrome(home); got != want { |
| 92 | t.Errorf("got %q, want %q", got, want) |
| 93 | } |
| 94 | }) |
| 95 | t.Run("Linux", func(t *testing.T) { |
| 96 | home := t.TempDir() |
| 97 | want := filepath.Join(home, ".cache", "ms-playwright", "chromium-1234", "chrome-linux64", "chrome") |
| 98 | touch(t, want) |
| 99 | if got := playwrightChrome(home); got != want { |
| 100 | t.Errorf("got %q, want %q", got, want) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | |