|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSyncMacOSBundleVersionScriptUpdatesBinaryPlist(t *testing.T) { |
| 13 | + if runtime.GOOS != "darwin" { |
| 14 | + t.Skip("requires macOS plist tooling") |
| 15 | + } |
| 16 | + |
| 17 | + appPath := filepath.Join(t.TempDir(), "GetTokens.app") |
| 18 | + contentsPath := filepath.Join(appPath, "Contents") |
| 19 | + if err := os.MkdirAll(contentsPath, 0o755); err != nil { |
| 20 | + t.Fatalf("MkdirAll() error = %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + plistPath := filepath.Join(contentsPath, "Info.plist") |
| 24 | + plistContent := `<?xml version="1.0" encoding="UTF-8"?> |
| 25 | +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 26 | +<plist version="1.0"> |
| 27 | +<dict> |
| 28 | + <key>CFBundleShortVersionString</key> |
| 29 | + <string>0.0.1</string> |
| 30 | + <key>CFBundleVersion</key> |
| 31 | + <string>0.0.1</string> |
| 32 | +</dict> |
| 33 | +</plist> |
| 34 | +` |
| 35 | + if err := os.WriteFile(plistPath, []byte(plistContent), 0o644); err != nil { |
| 36 | + t.Fatalf("WriteFile() error = %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + convert := exec.Command("plutil", "-convert", "binary1", plistPath) |
| 40 | + if output, err := convert.CombinedOutput(); err != nil { |
| 41 | + t.Fatalf("plutil convert error = %v, output = %s", err, output) |
| 42 | + } |
| 43 | + |
| 44 | + cmd := exec.Command("bash", "scripts/sync-macos-bundle-version.sh", appPath, "v1.2.3") |
| 45 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 46 | + t.Fatalf("sync-macos-bundle-version.sh error = %v, output = %s", err, output) |
| 47 | + } |
| 48 | + |
| 49 | + shortVersion := plistBuddyPrint(t, plistPath, "CFBundleShortVersionString") |
| 50 | + if shortVersion != "1.2.3" { |
| 51 | + t.Fatalf("CFBundleShortVersionString = %q, want %q", shortVersion, "1.2.3") |
| 52 | + } |
| 53 | + |
| 54 | + bundleVersion := plistBuddyPrint(t, plistPath, "CFBundleVersion") |
| 55 | + if bundleVersion != "1.2.3" { |
| 56 | + t.Fatalf("CFBundleVersion = %q, want %q", bundleVersion, "1.2.3") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func plistBuddyPrint(t *testing.T, plistPath, key string) string { |
| 61 | + t.Helper() |
| 62 | + |
| 63 | + cmd := exec.Command("/usr/libexec/PlistBuddy", "-c", "Print :"+key, plistPath) |
| 64 | + output, err := cmd.CombinedOutput() |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("PlistBuddy print %s error = %v, output = %s", key, err, output) |
| 67 | + } |
| 68 | + |
| 69 | + return strings.TrimSpace(string(output)) |
| 70 | +} |
0 commit comments