Files
MyGoNavi/internal/app/update_install_mode_test.go
Syngnat 3ac390f0dc 🐛 fix(update): 支持 Windows 便携版 ZIP 自动更新
- 将便携版更新资产切换为普通 ZIP,并兼容复用已下载包
- 解压后映射版本化目标文件名,保留旧版 EXE 更新兼容
- 将内嵌 PowerShell 更新脚本策略收紧为 RemoteSigned
- 补充 ZIP、目标路径与启动参数回归测试
2026-07-23 13:07:03 +08:00

107 lines
4.7 KiB
Go

package app
import (
"os"
"path/filepath"
"testing"
)
func TestResolveUpdateInstallModeForExecutableUsesSiblingMSIMarker(t *testing.T) {
installDir := t.TempDir()
executablePath := filepath.Join(installDir, "GoNavi.exe")
if got := resolveUpdateInstallModeForExecutable("windows", executablePath); got != updateInstallModePortable {
t.Fatalf("install mode without marker = %q, want portable", got)
}
markerPath := filepath.Join(installDir, windowsMSIInstallMarker)
if err := os.WriteFile(markerPath, []byte("msi\n"), 0o644); err != nil {
t.Fatalf("WriteFile marker: %v", err)
}
if got := resolveUpdateInstallModeForExecutable("windows", executablePath); got != updateInstallModeMSI {
t.Fatalf("install mode with marker = %q, want msi", got)
}
if got := resolveUpdateInstallModeForExecutable("linux", executablePath); got != updateInstallModeUnknown {
t.Fatalf("non-Windows install mode = %q, want unknown", got)
}
}
func TestResolveUpdateInstallModeForExecutableRejectsMarkerDirectory(t *testing.T) {
installDir := t.TempDir()
if err := os.Mkdir(filepath.Join(installDir, windowsMSIInstallMarker), 0o755); err != nil {
t.Fatalf("Mkdir marker: %v", err)
}
if got := resolveUpdateInstallModeForExecutable("windows", filepath.Join(installDir, "GoNavi.exe")); got != updateInstallModeUnknown {
t.Fatalf("install mode with invalid marker directory = %q, want unknown", got)
}
}
func TestExpectedAssetNameForWindowsInstallMode(t *testing.T) {
cases := []struct {
name string
arch string
installMode updateInstallMode
want string
}{
{name: "amd64 portable", arch: "amd64", installMode: updateInstallModePortable, want: "GoNavi-1.2.3-Windows-Amd64-Portable.zip"},
{name: "amd64 msi", arch: "amd64", installMode: updateInstallModeMSI, want: "GoNavi-1.2.3-Windows-Amd64-Installer.msi"},
{name: "arm64 portable", arch: "arm64", installMode: updateInstallModePortable, want: "GoNavi-1.2.3-Windows-Arm64-Portable.zip"},
{name: "arm64 msi", arch: "arm64", installMode: updateInstallModeMSI, want: "GoNavi-1.2.3-Windows-Arm64-Installer.msi"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := expectedAssetNameForExecutableAndInstallMode("windows", tc.arch, "v1.2.3", "", tc.installMode)
if err != nil {
t.Fatalf("expectedAssetNameForExecutableAndInstallMode: %v", err)
}
if got != tc.want {
t.Fatalf("asset name = %q, want %q", got, tc.want)
}
})
}
}
func TestResolveUpdateWorkspaceDirForPlatformSeparatesMSIFromInstallDirectory(t *testing.T) {
installTarget := filepath.Join("C:\\Program Files", "GoNavi", "GoNavi.exe")
userCacheDir := filepath.Join("C:\\Users", "tester", "AppData", "Local")
msiDir := resolveUpdateWorkspaceDirForPlatform("windows", "1.2.3", updateInstallModeMSI, installTarget, userCacheDir)
wantMSIDir := filepath.Join(userCacheDir, "GoNavi", "updates")
if msiDir != wantMSIDir {
t.Fatalf("MSI workspace = %q, want %q", msiDir, wantMSIDir)
}
portableDir := resolveUpdateWorkspaceDirForPlatform("windows", "1.2.3", updateInstallModePortable, installTarget, userCacheDir)
if portableDir != filepath.Dir(installTarget) {
t.Fatalf("portable workspace = %q, want install directory %q", portableDir, filepath.Dir(installTarget))
}
}
func TestValidateUpdatePackageForCurrentInstallModeRejectsModeAndSuffixMismatch(t *testing.T) {
originalResolveMode := updateResolveInstallMode
t.Cleanup(func() { updateResolveInstallMode = originalResolveMode })
updateResolveInstallMode = func() updateInstallMode { return updateInstallModeMSI }
if err := validateUpdatePackageForCurrentInstallMode("windows", updateInstallModeMSI, updatePackageTypeMSI, `C:\\tmp\\GoNavi-Installer.msi`); err != nil {
t.Fatalf("valid MSI package rejected: %v", err)
}
if err := validateUpdatePackageForCurrentInstallMode("windows", updateInstallModePortable, updatePackageTypePortable, `C:\\tmp\\GoNavi-Portable.exe`); err == nil {
t.Fatal("expected changed install mode to be rejected")
}
if err := validateUpdatePackageForCurrentInstallMode("windows", updateInstallModeMSI, updatePackageTypeMSI, `C:\\tmp\\GoNavi-Installer.exe`); err == nil {
t.Fatal("expected invalid MSI suffix to be rejected")
}
}
func TestPortableUpdatePackageAcceptsZipAndLegacyExe(t *testing.T) {
for _, assetPath := range []string{
`C:\\tmp\\GoNavi-Portable.zip`,
`C:\\tmp\\GoNavi-Portable.exe`,
} {
if !isUpdatePackageCompatibleWithInstallMode("windows", updateInstallModePortable, updatePackageTypePortable, assetPath) {
t.Fatalf("valid portable package rejected: %s", assetPath)
}
}
if isUpdatePackageCompatibleWithInstallMode("windows", updateInstallModePortable, updatePackageTypePortable, `C:\\tmp\\GoNavi-Installer.msi`) {
t.Fatal("expected MSI suffix to be rejected for portable mode")
}
}