Files
HackBrowserData/cmd/hack-browser-data/list_test.go
T
Roger a794674bd7 refactor: delete the superseded top-level packages (#667)
After the switch-over only two cmd tests used browser/, filemanager/, types/, and utils/fileutil.
The list test now compares against goldens of the output it proved equal to the old code's, and the
export test keeps its own fixture records; the binaries built before and after are byte-identical.
2026-10-29 14:59:13 +08:00

63 lines
1.6 KiB
Go

package main
import (
"bytes"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/browser"
)
func TestListGolden(t *testing.T) {
home := t.TempDir()
setHome(t, home)
var paths []string
for _, inst := range chromiumFixtureInstalls {
dir := userDataDirOf(t, home, inst.key)
chromiumFixtureUserData(t, dir, inst.key)
paths = append(paths, dir, "<"+inst.key+">")
}
dir := userDataDirOf(t, home, "firefox")
firefoxFixtureUserData(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")
safariFixtureLibrary(t, library)
assertListGoldens(t, "list-safari", strings.NewReplacer(library, "<library>"))
}
// The goldens hold 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
cmd := listCmd()
cmd.SetOut(&out)
name, args := prefix+".golden", []string{}
if detail {
name, args = prefix+"-detail.golden", []string{"--detail"}
}
runCmd(t, cmd, args...)
assertGolden(t, name, paths.Replace(out.String()))
}
}
func userDataDirOf(t *testing.T, home, id string) string {
t.Helper()
def, ok := browser.Lookup(id)
require.True(t, ok, id)
return filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0]))
}