-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts_macos_bundle_version_test.go
More file actions
93 lines (77 loc) · 2.58 KB
/
scripts_macos_bundle_version_test.go
File metadata and controls
93 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//go:build darwin
package main
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestSyncMacOSBundleVersionScript(t *testing.T) {
t.Run("syncs bundle version from release tag", func(t *testing.T) {
appPath := createTempAppBundle(t)
cmd := exec.Command("bash", "scripts/sync-macos-bundle-version.sh", appPath, "v0.1.10")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("sync-macos-bundle-version.sh error = %v, output = %s", err, output)
}
plist := readPlistAsJSON(t, filepath.Join(appPath, "Contents", "Info.plist"))
if got := plist["CFBundleShortVersionString"]; got != "0.1.10" {
t.Fatalf("CFBundleShortVersionString = %#v, want %q", got, "0.1.10")
}
if got := plist["CFBundleVersion"]; got != "0.1.10" {
t.Fatalf("CFBundleVersion = %#v, want %q", got, "0.1.10")
}
})
t.Run("rejects non release version", func(t *testing.T) {
appPath := createTempAppBundle(t)
cmd := exec.Command("bash", "scripts/sync-macos-bundle-version.sh", appPath, "dev")
output, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("sync-macos-bundle-version.sh expected error, output = %s", output)
}
if !strings.Contains(string(output), "unsupported app version") {
t.Fatalf("sync-macos-bundle-version.sh output = %q, want unsupported app version", output)
}
})
}
func createTempAppBundle(t *testing.T) string {
t.Helper()
appPath := filepath.Join(t.TempDir(), "GetTokens.app")
contentsPath := filepath.Join(appPath, "Contents")
if err := os.MkdirAll(contentsPath, 0o755); err != nil {
t.Fatalf("MkdirAll() error = %v", err)
}
const plist = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleName</key>
<string>GetTokens</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1.0.0</string>
</dict>
</plist>
`
plistPath := filepath.Join(contentsPath, "Info.plist")
if err := os.WriteFile(plistPath, []byte(plist), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
return appPath
}
func readPlistAsJSON(t *testing.T, plistPath string) map[string]any {
t.Helper()
cmd := exec.Command("plutil", "-convert", "json", "-o", "-", plistPath)
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("plutil error = %v, output = %s", err, output)
}
var data map[string]any
if err := json.Unmarshal(output, &data); err != nil {
t.Fatalf("json.Unmarshal() error = %v, output = %s", err, output)
}
return data
}