Files
magpie/internal/appdir/userdirs_windows_test.go
T
yetone 9f1734645a appdir: exports go to the Downloads folder the system keeps, not home/Downloads
1Shin on Discord (#feedback, 11-02): an export (Settings' backup, a
request saved from the archive, a session as Markdown, the usage file)
joined $HOME/Downloads, which is wrong when Downloads was moved: Windows'
known folder (to another drive or OneDrive) or the XDG one on Linux
(~/下载 on a Chinese desktop).

appdir.UserFolder(kind) asks the system: SHGetKnownFolderPath
(FOLDERID_Downloads/Desktop/Documents, KF_FLAG_DONT_VERIFY) on Windows,
used when magpie's home is the profile Windows keeps it for (a home given
of its own, as a test's, keeps home\Downloads); $XDG_CONFIG_HOME/
user-dirs.dirs on Linux and the BSDs, read as xdg-user-dir does ("$HOME/x"
or "/x", quoted, backslash escapes, the last line wins); ~/Downloads on a
Mac. A failed lookup is home/kind, as before. gui's downloads() tries it,
then home/Downloads, then home. appdir's sharedFolder (a data folder
beside magpie there isn't portable data) adds the system's folders to
home's, and Cursor's AppImage is looked for in the system's Downloads too.
update's isDownloadFolder already asked Windows for its known folders and
is unchanged.

Run at this head (rebased on origin/main):
- mote (macOS): gofmt -l clean; go vet ./internal/appdir ./internal/gui
  ./internal/agent; GOOS=windows go vet of those and ./internal/update;
  GOOS=linux go vet -tags nogui of the three; go test ./internal/appdir ok,
  -run "Backup|Export|Archive" ./internal/gui ok, -run
  "Cursor|Desktop|Sandbox" ./internal/agent ok.
- wsl-run (Ubuntu 24.04): appdir's UserFolder/SharedFolder/ResolveDownloads
  tests and gui's TestExportGoesToTheSystemsDownloads,
  TestBackupLibraryRoundTrip and the session exports pass. Against the real
  xdg-user-dir in a sandbox HOME, "$HOME/下载", "/mnt/d/My Downloads" and
  "$HOME/a \"b\"" read the same; "$HOME/" gives home without the trailing
  slash.
- win-run (Windows 11): the appdir tests pass, and
  TestKnownFolderIsExplorers checks the known folder against Explorer's
  User Shell Folders value (read only). Windows expands that value's
  %USERPROFILE% once a process, with the first call's environment, so the
  check runs in a child of its own. The real profile gives
  C:\Users\yeton\Downloads, Desktop and Documents.

Broken by hand: no user-dirs.dirs read, or no profile check on Windows:
TestUserFolderXDG, XDGLines, XDGConfigHome and TestUserFolderWindows go
red. gui's downloads() without the system's folder:
TestExportGoesToTheSystemsDownloads red in WSL (saved in ~/Downloads, want
~/下载).

Not tried: a Downloads really redirected on Windows (to D:\ or OneDrive).
Moving it means writing the user's own User Shell Folders on the test box,
so the redirect is a faked known folder in TestUserFolderWindows only. Not
run: the GUI suite (no GUI file changed) and a full go test ./....
2026-11-02 05:19:45 +08:00

50 lines
1.5 KiB
Go

package appdir
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"golang.org/x/sys/windows/registry"
)
// The known folder read is Windows' own: the user's Downloads as Explorer
// keeps it in User Shell Folders (where a move to another drive or
// OneDrive is written), read only. Windows expands its %USERPROFILE% once
// a process, with the environment of the first call, so the check runs in
// a child of its own that makes that call here.
func TestKnownFolderIsExplorers(t *testing.T) {
if os.Getenv("MAGPIE_KNOWN_FOLDER_CHILD") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=^TestKnownFolderIsExplorers$", "-test.v")
cmd.Env = append(os.Environ(), "MAGPIE_KNOWN_FOLDER_CHILD=1")
out, err := cmd.CombinedOutput()
t.Logf("%s", out)
if err != nil {
t.Fatal(err)
}
return
}
path, profile := osKnownFolder(DownloadsFolder)
if path == "" || profile == "" {
t.Fatalf("no known folder: %q, %q", path, profile)
}
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders`, registry.QUERY_VALUE)
if err != nil {
t.Skip("no User Shell Folders:", err)
}
defer k.Close()
v, _, err := k.GetStringValue("{374DE290-123F-4565-9164-39C4925E467B}")
if err != nil {
t.Skip("Downloads not in User Shell Folders:", err)
}
if v, err = registry.ExpandString(v); err != nil {
t.Fatal(err)
}
if !strings.EqualFold(filepath.Clean(v), filepath.Clean(path)) {
t.Errorf("known folder %q, Explorer's %q", path, v)
}
t.Logf("Downloads %s, profile %s", path, profile)
}