github.com/ChicK00o/awgo@v0.29.4/util/build/info_test.go (about) 1 // Copyright (c) 2019 Dean Jackson <deanishe@deanishe.net> 2 // MIT Licence applies http://opensource.org/licenses/MIT 3 4 package build 5 6 import ( 7 "fmt" 8 "os" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 var ( 16 rootDirV3 = "./testdata/v3" 17 rootDirV4 = "./testdata/v4" 18 rootDirInvalid = "./testdata/invalid" 19 syncDirV3 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred 3") 20 prefsBundleV3 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred 3/Alfred.alfredpreferences") 21 wfDirV3 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred 3/Alfred.alfredpreferences/workflows") 22 cacheDirV3 = os.ExpandEnv("${HOME}/Library/Caches/com.runningwithcrayons.Alfred-3/Workflow Data") 23 dataDirV3 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred 3/Workflow Data") 24 syncDirV4 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred") 25 prefsBundleV4 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred/Alfred.alfredpreferences") 26 wfDirV4 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred/Alfred.alfredpreferences/workflows") 27 cacheDirV4 = os.ExpandEnv("${HOME}/Library/Caches/com.runningwithcrayons.Alfred/Workflow Data") 28 dataDirV4 = os.ExpandEnv("${HOME}/Library/Application Support/Alfred/Workflow Data") 29 30 testPlist = InfoPlist("./testdata/info.plist") 31 ) 32 33 func withEnv(env map[string]string, fn func()) { 34 prev := map[string]string{} 35 prevSet := map[string]bool{} 36 for key, value := range env { 37 prev[key], prevSet[key] = os.LookupEnv(key) 38 panicOnError(os.Setenv(key, value)) 39 } 40 41 fn() 42 43 for key, value := range prev { 44 if prevSet[key] { 45 panicOnError(os.Setenv(key, value)) 46 } else { 47 panicOnError(os.Unsetenv(key)) 48 } 49 } 50 } 51 52 func TestWorkflowInfo(t *testing.T) { 53 var ( 54 name = "AwGo" 55 bundleID = "net.deanishe.awgo" 56 version = "1.2.0" 57 ) 58 59 testInfo := func(t *testing.T) { 60 info, err := NewInfo(LibDir(rootDirV4), testPlist) 61 require.Nil(t, err, "NewInfo failed") 62 assert.Equal(t, name, info.Name, "unexpected name") 63 assert.Equal(t, bundleID, info.BundleID, "unexpected bundle ID") 64 assert.Equal(t, version, info.Version, "unexpected version") 65 } 66 67 testInfo(t) 68 69 t.Run("read info.plist", func(t *testing.T) { 70 // Read workflow data from info.plist 71 env := map[string]string{ 72 "alfred_workflow_bundleid": "", 73 "alfred_workflow_name": "", 74 "alfred_workflow_version": "", 75 } 76 withEnv(env, func() { 77 testInfo(t) 78 }) 79 }) 80 81 t.Run("info.plist has priority over env", func(t *testing.T) { 82 env := map[string]string{ 83 "alfred_workflow_bundleid": "net.deanishe.wrong-bundleid", 84 "alfred_workflow_name": "Wrong Name", 85 "alfred_workflow_version": "0.0.1", 86 } 87 withEnv(env, func() { 88 testInfo(t) 89 }) 90 }) 91 } 92 93 // Read Alfred version number from environment or based on 94 // presence of configuration files. 95 func TestAlfredVersion(t *testing.T) { 96 tests := []struct { 97 dir string 98 envvar string 99 x int 100 err bool 101 }{ 102 {rootDirV3, "", 3, false}, 103 {rootDirV3, "3", 3, false}, 104 {rootDirV3, "4", 4, false}, 105 {rootDirV4, "", 4, false}, 106 {rootDirV4, "4", 4, false}, 107 {".", "", 0, true}, 108 {".", "four", 0, true}, 109 } 110 111 for _, td := range tests { 112 td := td // pin variable 113 t.Run(fmt.Sprintf("dir=%q, env=%q", td.dir, td.envvar), func(t *testing.T) { 114 withEnv(map[string]string{ 115 "alfred_version": td.envvar, 116 // ensure defaults 117 "alfred_workflow_data": "", 118 "alfred_workflow_cache": "", 119 }, func() { 120 info, err := NewInfo(LibDir(td.dir), testPlist) 121 if td.err { 122 assert.NotNil(t, err, "expected error") 123 return 124 } 125 require.Nil(t, err, "unexpected error") 126 assert.Equal(t, td.x, info.AlfredMajorVersion, "unexpected version") 127 }) 128 }) 129 } 130 } 131 132 func TestDirs(t *testing.T) { 133 tests := []struct { 134 name string 135 version string 136 dir string 137 plist Option 138 x int 139 fail bool 140 }{ 141 {"default", "", rootDirV4, testPlist, 4, false}, 142 {"v4", "4", rootDirV4, testPlist, 4, false}, 143 {"v4 (version=0)", "", rootDirV4, testPlist, 4, false}, 144 {"v3", "3", rootDirV3, testPlist, 3, false}, 145 {"v3 (v4 dir)", "3", rootDirV4, testPlist, 3, false}, 146 // invalid input 147 {"non-existent info.plist", "", rootDirV4, InfoPlist("./invalid"), 0, true}, 148 {"invalid info.plist", "", rootDirV4, InfoPlist("./testdata/invalid.plist"), 0, true}, 149 {"non-existent info.plist", "", "./invalid", testPlist, 0, true}, 150 {"invalid prefs.json", "", rootDirInvalid, testPlist, 0, true}, 151 {"invalid Alfred Preferences prefs", "3", rootDirInvalid, testPlist, 0, true}, 152 } 153 154 for _, td := range tests { 155 td := td // pin variable 156 t.Run(td.name, func(t *testing.T) { 157 withEnv(map[string]string{ 158 "alfred_version": td.version, 159 // ensure defaults 160 "alfred_workflow_data": "", 161 "alfred_workflow_cache": "", 162 }, func() { 163 info, err := NewInfo(LibDir(td.dir), td.plist) 164 165 if td.fail { 166 assert.NotNil(t, err, td.name) 167 return 168 } 169 170 require.Nil(t, err, "NewInfo failed") 171 172 if td.x == 3 { 173 assert.Equal(t, syncDirV3, info.AlfredSyncDir, "unexpected AlfredSyncDir") 174 assert.Equal(t, prefsBundleV3, info.AlfredPrefsBundle, "unexpected PrefsBundle") 175 assert.Equal(t, wfDirV3, info.AlfredWorkflowDir, "unexpected AlfredWorkflowDir") 176 assert.Equal(t, cacheDirV3, info.AlfredCacheDir, "unexpected AlfredCacheDir") 177 assert.Equal(t, dataDirV3, info.AlfredDataDir, "unexpected AlfredDataDir") 178 } else { 179 assert.Equal(t, syncDirV4, info.AlfredSyncDir, "unexpected AlfredSyncDir") 180 assert.Equal(t, prefsBundleV4, info.AlfredPrefsBundle, "unexpected PrefsBundle") 181 assert.Equal(t, wfDirV4, info.AlfredWorkflowDir, "unexpected AlfredWorkflowDir") 182 assert.Equal(t, cacheDirV4, info.AlfredCacheDir, "unexpected AlfredCacheDir") 183 assert.Equal(t, dataDirV4, info.AlfredDataDir, "unexpected AlfredDataDir") 184 } 185 }) 186 }) 187 } 188 } 189 190 func TestEnv(t *testing.T) { 191 t.Parallel() 192 193 info, err := NewInfo(LibDir(rootDirV4), testPlist) 194 require.Nil(t, err, "NewInfo failed") 195 196 tests := []struct { 197 key, x string 198 }{ 199 {"alfred_workflow_name", info.Name}, 200 {"alfred_workflow_version", info.Version}, 201 {"alfred_workflow_bundleid", info.BundleID}, 202 {"alfred_workflow_uid", info.BundleID}, 203 {"alfred_workflow_cache", info.CacheDir}, 204 {"alfred_workflow_data", info.DataDir}, 205 {"alfred_preferences", info.AlfredPrefsBundle}, 206 {"alfred_version", fmt.Sprintf("%d", info.AlfredMajorVersion)}, 207 } 208 env := info.Env() 209 for _, td := range tests { 210 td := td // pin variable 211 t.Run(td.key, func(t *testing.T) { 212 t.Parallel() 213 assert.Equal(t, td.x, env[td.key], "unexpected value") 214 }) 215 } 216 } 217 218 func panicOnError(err error) { 219 if err != nil { 220 panic(err) 221 } 222 }