appdata_test.go raw
1 package appdata_test
2
3 import (
4 "os"
5 "os/user"
6 "path/filepath"
7 "runtime"
8 "testing"
9 "unicode"
10
11 "github.com/p9c/gel/appdata"
12 )
13
14 // TestAppDataDir tests the API for Dir to ensure it gives expected results for various operating systems.
15 func TestAppDataDir(t *testing.T) {
16 // App name plus upper and lowercase variants.
17 appName := "myapp"
18 appNameUpper := string(unicode.ToUpper(rune(appName[0]))) + appName[1:]
19 appNameLower := string(unicode.ToLower(rune(appName[0]))) + appName[1:]
20 // When we're on Windows, set the expected local and roaming directories per the environment vars. When we aren't
21 // on Windows, the function should return the current directory when forced to provide the Windows path since the
22 // environment variables won't exist.
23 winLocal := "."
24 winRoaming := "."
25 if runtime.GOOS == "windows" {
26 localAppData := os.Getenv("LOCALAPPDATA")
27 roamingAppData := os.Getenv("APPDATA")
28 if localAppData == "" {
29 localAppData = roamingAppData
30 }
31 winLocal = filepath.Join(localAppData, appNameUpper)
32 winRoaming = filepath.Join(roamingAppData, appNameUpper)
33 }
34 // Get the home directory to use for testing expected results.
35 var homeDir string
36 usr, e := user.Current()
37 if e != nil {
38 t.Errorf("user.Current: %v", e)
39 return
40 }
41 homeDir = usr.HomeDir
42 // Mac node data directory.
43 macAppData := filepath.Join(homeDir, "Library", "Application Support")
44 tests := []struct {
45 goos string
46 appName string
47 roaming bool
48 want string
49 }{
50 // Various combinations of application name casing, leading period, operating system, and roaming flags.
51 {"windows", appNameLower, false, winLocal},
52 {"windows", appNameUpper, false, winLocal},
53 {"windows", "." + appNameLower, false, winLocal},
54 {"windows", "." + appNameUpper, false, winLocal},
55 {"windows", appNameLower, true, winRoaming},
56 {"windows", appNameUpper, true, winRoaming},
57 {"windows", "." + appNameLower, true, winRoaming},
58 {"windows", "." + appNameUpper, true, winRoaming},
59 {"linux", appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
60 {"linux", appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
61 {"linux", "." + appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
62 {"linux", "." + appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
63 {"darwin", appNameLower, false, filepath.Join(macAppData, appNameUpper)},
64 {"darwin", appNameUpper, false, filepath.Join(macAppData, appNameUpper)},
65 {"darwin", "." + appNameLower, false, filepath.Join(macAppData, appNameUpper)},
66 {"darwin", "." + appNameUpper, false, filepath.Join(macAppData, appNameUpper)},
67 {"openbsd", appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
68 {"openbsd", appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
69 {"openbsd", "." + appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
70 {"openbsd", "." + appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
71 {"freebsd", appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
72 {"freebsd", appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
73 {"freebsd", "." + appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
74 {"freebsd", "." + appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
75 {"netbsd", appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
76 {"netbsd", appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
77 {"netbsd", "." + appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
78 {"netbsd", "." + appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
79 {"plan9", appNameLower, false, filepath.Join(homeDir, appNameLower)},
80 {"plan9", appNameUpper, false, filepath.Join(homeDir, appNameLower)},
81 {"plan9", "." + appNameLower, false, filepath.Join(homeDir, appNameLower)},
82 {"plan9", "." + appNameUpper, false, filepath.Join(homeDir, appNameLower)},
83 {"unrecognized", appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
84 {"unrecognized", appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
85 {"unrecognized", "." + appNameLower, false, filepath.Join(homeDir, "."+appNameLower)},
86 {"unrecognized", "." + appNameUpper, false, filepath.Join(homeDir, "."+appNameLower)},
87 // No application name provided, so expect current directory.
88 {"windows", "", false, "."},
89 {"windows", "", true, "."},
90 {"linux", "", false, "."},
91 {"darwin", "", false, "."},
92 {"openbsd", "", false, "."},
93 {"freebsd", "", false, "."},
94 {"netbsd", "", false, "."},
95 {"plan9", "", false, "."},
96 {"unrecognized", "", false, "."},
97 // Single dot provided for application name, so expect current
98 // directory.
99 {"windows", ".", false, "."},
100 {"windows", ".", true, "."},
101 {"linux", ".", false, "."},
102 {"darwin", ".", false, "."},
103 {"openbsd", ".", false, "."},
104 {"freebsd", ".", false, "."},
105 {"netbsd", ".", false, "."},
106 {"plan9", ".", false, "."},
107 {"unrecognized", ".", false, "."},
108 }
109 t.Logf("Running %d tests", len(tests))
110 for i, test := range tests {
111 ret := TstAppDataDir(test.goos, test.appName, test.roaming)
112 if ret != test.want {
113 t.Errorf(
114 "AppDataDir #%d (%s) does not match - "+
115 "expected got %s, want %s", i, test.goos, ret,
116 test.want,
117 )
118 continue
119 }
120 }
121 }
122
123 // TstAppDataDir makes the internal appDataDir function available to the test package.
124 func TstAppDataDir(goos, appName string, roaming bool) string {
125 return appdata.GetDataDir(goos, appName, roaming)
126 }
127