-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_system_path_test.go
97 lines (81 loc) · 2.29 KB
/
get_system_path_test.go
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
94
95
96
97
package hostsfile
import (
"errors"
"os"
"runtime"
"testing"
)
func TestGetSystemPath(t *testing.T) {
p1, err := GetSystemPath()
if err != nil {
t.Errorf("GetSystemPath returned an error: %v", err)
}
p2, err := GetSystemPathByOS(runtime.GOOS)
if err != nil {
t.Errorf("GetSystemPathByOS returned an error: %v", err)
}
if p1 != p2 {
t.Errorf("GetSystemPath returned incorrect path")
}
}
func TestGetSystemPathByOS(t *testing.T) {
err := os.Setenv("SystemRoot", `C:\Windows`)
if err != nil {
t.Errorf("Setenv returned an error: %v", err)
}
testCases := map[string]string{
"linux": "/etc/hosts",
"darwin": "/etc/hosts",
"aix": "/etc/hosts",
"dragonfly": "/etc/hosts",
"freebsd": "/etc/hosts",
"ios": "/etc/hosts",
"netbsd": "/etc/hosts",
"openbsd": "/etc/hosts",
"solaris": "/etc/hosts",
"windows": `C:\Windows\System32\drivers\etc\hosts`,
"android": "/system/etc/hosts",
"hurd": "/hurd/hosts",
"illumos": "/etc/inet/hosts",
"plan9": "/lib/ndb/local",
"zos": "SYS1.PARMLIB(HOSTS)",
}
statFile = func(path string) (os.FileInfo, error) {
return nil, nil
}
for osName, expectedPath := range testCases {
var path string
path, err = GetSystemPathByOS(osName)
if err != nil {
t.Errorf("GetSystemPathByOS for %s returned an error: %v", osName, err)
}
if path != expectedPath {
t.Errorf("GetSystemPathByOS for %s returned incorrect path: got %s, want %s", osName, path, expectedPath)
}
}
}
func TestGetSystemPathByOSWithInvalidOS(t *testing.T) {
_, err := GetSystemPathByOS("invalid")
if !errors.Is(err, ErrUnsupportedOperatingSystem) {
t.Errorf("expected `%s`, got `%s`", ErrUnsupportedOperatingSystem, err)
}
}
func TestGetSystemPathByOSSpecialCases(t *testing.T) {
testCases := map[string]string{
"android": "/etc/hosts",
"hurd": "/etc/hosts",
}
statFile = func(path string) (os.FileInfo, error) {
return nil, errors.New("file not found")
}
for osName, expectedPath := range testCases {
var path string
path, err := GetSystemPathByOS(osName)
if err != nil {
t.Errorf("GetSystemPathByOS for %s returned an error: %v", osName, err)
}
if path != expectedPath {
t.Errorf("GetSystemPathByOS for %s returned incorrect path: got %s, want %s", osName, path, expectedPath)
}
}
}