-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
89 lines (77 loc) · 2.21 KB
/
main_test.go
File metadata and controls
89 lines (77 loc) · 2.21 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
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// TestAppPath verifies that the application path is correctly determined
// for different operating systems. It sets up test cases for Windows,
// Darwin (macOS), Plan 9, and Linux, and checks the expected application
// paths against the actual paths returned by the AppPath function.
func TestAppPath(t *testing.T) {
homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Failed to get user home directory: %v", err)
}
tests := []struct {
name string
goos string
expected string
envVarName string
envVarValue string
}{
{
name: "windows",
goos: "windows",
expected: filepath.Join(os.Getenv("LOCALAPPDATA"), DefaultAppName),
},
{
name: "darwin",
goos: "darwin",
expected: filepath.Join(homeDir, "Library", "Application Support", DefaultAppName),
},
{
name: "plan9",
goos: "plan9",
expected: filepath.Join(homeDir, strings.ToLower(DefaultAppName)),
},
{
name: "linux",
goos: "linux",
expected: filepath.Join(homeDir, "."+strings.ToLower(DefaultAppName)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.envVarName != "" {
os.Setenv(tt.envVarName, tt.envVarValue)
defer os.Unsetenv(tt.envVarName)
}
assert.Equal(t, tt.expected, AppPath(tt.goos, homeDir))
})
}
}
// TestEnsureAppPathExists checks the EnsureAppPathExists function to
// ensure it correctly handles the existence of the application path.
func TestEnsureAppPathExists(t *testing.T) {
tmpDir := os.TempDir()
appPath := filepath.Join(tmpDir, DefaultAppName)
// Case 1: Directory does not exist and is created successfully.
t.Run("DirectoryDoesNotExist", func(t *testing.T) {
defer os.RemoveAll(appPath)
err := EnsureAppPathExists(appPath)
assert.NoError(t, err)
_, err = os.Stat(appPath)
assert.False(t, os.IsNotExist(err))
})
// Case 2: Directory already exists.
t.Run("DirectoryAlreadyExists", func(t *testing.T) {
defer os.RemoveAll(appPath)
err := os.MkdirAll(appPath, AppDirPermissions)
assert.NoError(t, err)
err = EnsureAppPathExists(appPath)
assert.NoError(t, err)
})
}