Files
HackBrowserData/cmd/hack-browser-data/list_test.go
T
Roger 9e9d1c531d refactor: rename identifiers and expand the browser table (#685)
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case.
The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
2026-11-09 17:25:35 +08:00

65 lines
1.9 KiB
Go

package main
import (
"bytes"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
"github.com/moond4rk/hackbrowserdata/internal/golden"
)
func TestListGolden(t *testing.T) {
home := t.TempDir()
setHome(t, home)
var paths []string
for _, fixture := range browsertest.ChromiumUserDataDirs {
dir := userDataDirOf(t, home, fixture.Name)
browsertest.WriteChromiumUserDataDir(t, dir, fixture.Name)
paths = append(paths, dir, "<"+fixture.Name+">")
}
dir := userDataDirOf(t, home, "firefox")
browsertest.WriteFirefoxUserDataDir(t, dir)
paths = append(paths, dir, "<firefox>", `\`, "/")
assertListGoldens(t, "list", strings.NewReplacer(paths...))
}
func TestListGoldenSafari(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("Safari is in the browser table on macOS only")
}
home := t.TempDir()
setHome(t, home)
library := filepath.Join(home, "Library")
browsertest.WriteSafariLibrary(t, library)
assertListGoldens(t, "list-safari", strings.NewReplacer(library, "<library>"))
}
// assertListGoldens compares with the fixture's paths as placeholders, so one golden serves every OS.
func assertListGoldens(t *testing.T, prefix string, paths *strings.Replacer) {
t.Helper()
for _, detail := range []bool{false, true} {
var out bytes.Buffer
command := newListCommand()
command.SetOut(&out)
name, args := prefix+".golden", []string{}
if detail {
name, args = prefix+"-detail.golden", []string{"--detail"}
}
runCmd(t, command, args...)
golden.Assert(t, name, paths.Replace(out.String()))
}
}
func userDataDirOf(t *testing.T, home, browserName string) string {
t.Helper()
namedBrowser, ok := browser.Lookup(browserName)
require.True(t, ok, browserName)
return filepath.Join(home, filepath.FromSlash(namedBrowser.Platform().UserDataDirs[0]))
}