Files
magpie/internal/appdir/appdir_test.go
T
yetone fff1607a30 portable: a portable magpie on Windows keeps WebView2's profile in data\webview2 and makes no Start-menu shortcut, and an installed one makes the shortcut once and never puts back one the user deleted (#508, suixinex: 便携模式时还是会创建 C:\Users\<username>\AppData\Roaming\magpie.exe\ 文件夹; 开始菜单的快捷方式删除后再打开magpie会重新添加) — the AppData\Roaming\magpie.exe folder is WebView2's user data folder: Wails v3 passes WindowsOptions.WebviewUserDataPath to go-webview2 as its DataPath, and left empty it is %APPDATA%\<exe name>, which portable mode (appdir) never set. The app's Windows options (windowsOptions) now give it appdir.WebView(): data\webview2 when portable, "" (WebView2's default, so the pages' storage and cookies there are kept) when installed. The shortcut came back because shortcut.Ensure ran at every start of the desktop app and wrote Magpie.lnk whenever it was missing or stale, with nothing recording that it had been made. Ensure now does nothing when portable (no shortcut, no App Paths entry); installed, it makes the shortcut on the first start and records that in start-menu-shortcut in magpie's folder (with the exe), keeps one that is still there pointed at the exe (moved exe, update), and makes none once the file is recorded and the shortcut is gone. A user who deleted it under an older magpie isn't given it back on updating either: a missing shortcut with no record but an App Paths\magpie.exe entry already there (every older magpie wrote it at each start) counts as deleted and is recorded as such. The Start menu, the shortcut writer and App Paths are behind function variables, so the decision is tested with fakes on any OS. Checked for other leaks: autostart only refreshes an entry the user turned on (Refresh never enables), the magpie:// link registration is needed for import links and stays, as appdir's doc says, as do temp folders; the update stages beside the exe, caches already follow appdir. Not changed: the Mac's WKWebView and Linux's WebKitGTK keep their own data in the profile (Wails has no option for it), which #508 didn't report. Nothing ran on Windows (the test box isn't reachable): no real WebView2 was started with the data path and no real shortcut was made or deleted. TestEnsureOnce, TestEnsureAfterOlderMagpie, TestEnsurePortable (fake Start menu in a temp folder), TestWebView and TestWindowsOptionsWebViewData fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet ./..., GOOS=windows go test -c for shortcut, gui and appdir, go test -tags nogui -count=1 ./... under a temp HOME pass (TestUsageKeepsTimeToFirstToken and TestUserPath failed once on timing, pass rerun alone; untouched).
2026-10-02 14:39:49 +08:00

161 lines
4.9 KiB
Go

package appdir
import (
"os"
"path/filepath"
"runtime"
"testing"
)
// root is a temp folder with its links followed (/var is /private/var on a
// Mac), as Resolve gives its answers.
func root(t *testing.T) string {
d, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
return d
}
func touch(t *testing.T, p string) string {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("x"), 0o755); err != nil {
t.Fatal(err)
}
return p
}
func mkdir(t *testing.T, p string) {
t.Helper()
if err := os.MkdirAll(p, 0o755); err != nil {
t.Fatal(err)
}
}
func TestResolve(t *testing.T) {
t.Setenv("APPIMAGE", "")
r := root(t)
// no data folder: installed
exe := touch(t, filepath.Join(r, "plain", "magpie"))
if got := Resolve(exe); got != "" {
t.Errorf("no data folder: %q, want installed", got)
}
// a file named data is not the folder
touch(t, filepath.Join(r, "plain", "data"))
if got := Resolve(exe); got != "" {
t.Errorf("data is a file: %q, want installed", got)
}
// a data folder beside it
exe = touch(t, filepath.Join(r, "port", "magpie.exe"))
mkdir(t, filepath.Join(r, "port", "data"))
if got, want := Resolve(exe), filepath.Join(r, "port", "data"); got != want {
t.Errorf("data folder: %q, want %q", got, want)
}
// a .portable marker: the data folder is made beside it when written
exe = touch(t, filepath.Join(r, "marker", "magpie"))
touch(t, filepath.Join(r, "marker", ".portable"))
if got, want := Resolve(exe), filepath.Join(r, "marker", "data"); got != want {
t.Errorf(".portable: %q, want %q", got, want)
}
// a Mac app: beside the bundle, never inside it
exe = touch(t, filepath.Join(r, "mac", "Magpie.app", "Contents", "MacOS", "magpie"))
mkdir(t, filepath.Join(r, "mac", "Magpie.app", "Contents", "MacOS", "data"))
if got := Resolve(exe); got != "" {
t.Errorf("data inside the bundle: %q, want installed", got)
}
mkdir(t, filepath.Join(r, "mac", "data"))
if got, want := Resolve(exe), filepath.Join(r, "mac", "data"); got != want {
t.Errorf("Mac app: %q, want %q", got, want)
}
}
func TestResolveSymlinkedExe(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlinks need privileges on Windows")
}
t.Setenv("APPIMAGE", "")
r := root(t)
real := touch(t, filepath.Join(r, "app", "magpie"))
mkdir(t, filepath.Join(r, "bin"))
link := filepath.Join(r, "bin", "magpie")
if err := os.Symlink(real, link); err != nil {
t.Fatal(err)
}
// a data folder beside the link only: the link isn't where magpie is
mkdir(t, filepath.Join(r, "bin", "data"))
if got := Resolve(link); got != "" {
t.Errorf("data beside the link: %q, want installed", got)
}
mkdir(t, filepath.Join(r, "app", "data"))
if got, want := Resolve(link), filepath.Join(r, "app", "data"); got != want {
t.Errorf("symlinked exe: %q, want %q", got, want)
}
}
func TestResolveAppImage(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("AppImage is Linux's")
}
r := root(t)
exe := touch(t, filepath.Join(r, "mount", "usr", "bin", "magpie"))
img := touch(t, filepath.Join(r, "apps", "Magpie.AppImage"))
mkdir(t, filepath.Join(r, "apps", "data"))
t.Setenv("APPIMAGE", img)
if got, want := Resolve(exe), filepath.Join(r, "apps", "data"); got != want {
t.Errorf("AppImage: %q, want %q", got, want)
}
}
func TestFolders(t *testing.T) {
t.Setenv("APPIMAGE", "")
r := root(t)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(r, "cfg"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(r, "cache"))
t.Cleanup(func() { UseExecutable("") })
UseExecutable("")
if Portable() != "" || Config() != filepath.Join(r, "cfg", "magpie") || Cache() != filepath.Join(r, "cache", "magpie") {
t.Errorf("installed: portable %q, config %q, cache %q", Portable(), Config(), Cache())
}
if d, _ := SystemCache(); filepath.Base(d) != "magpie" {
t.Errorf("installed system cache %q", d)
}
exe := touch(t, filepath.Join(r, "port", "magpie"))
data := filepath.Join(r, "port", "data")
mkdir(t, data)
UseExecutable(exe)
if Portable() != data || Config() != data || Cache() != filepath.Join(data, "cache") {
t.Errorf("portable: portable %q, config %q, cache %q", Portable(), Config(), Cache())
}
if d, _ := SystemCache(); d != filepath.Join(data, "cache") {
t.Errorf("portable system cache %q", d)
}
}
// #508: portable, WebView2's profile goes into the data folder, not
// %APPDATA%\magpie.exe; installed, it is left at WebView2's default.
func TestWebView(t *testing.T) {
t.Setenv("APPIMAGE", "")
r := root(t)
t.Cleanup(func() { UseExecutable("") })
UseExecutable("")
if got := WebView(); got != "" {
t.Errorf("installed: WebView() = %q, want the default", got)
}
exe := touch(t, filepath.Join(r, "port", "magpie.exe"))
touch(t, filepath.Join(r, "port", ".portable"))
UseExecutable(exe)
if got, want := WebView(), filepath.Join(r, "port", "data", "webview2"); got != want {
t.Errorf("portable: WebView() = %q, want %q", got, want)
}
}