-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfix_test.go
138 lines (102 loc) · 3.05 KB
/
pathfix_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package pathfix_test
import (
"os"
"runtime"
"strings"
"testing"
"github.com/haroldadmin/pathfix"
)
func BenchmarkPathFix(b *testing.B) {
for i := 0; i < b.N; i++ {
pathfix.Fix()
}
}
func TestPathFix(t *testing.T) {
ogPath := os.Getenv("PATH")
ogShell := os.Getenv("SHELL")
// Workaround for missing shell on github workflow runners
if ogShell == "" {
ogShell = "/bin/bash"
os.Setenv("SHELL", ogShell)
}
t.Run("should fix current process's PATH", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Test not applicable to Windows")
}
defer resetEnv(t, ogPath, ogShell)
t.Logf("Current PATH: %s\n\n", os.Getenv("PATH"))
os.Unsetenv("PATH")
if path := os.Getenv("PATH"); path != "" {
t.Error("Failed to unset PATH for this process")
}
err := pathfix.Fix()
if err != nil {
t.Errorf("Expected no errors, got %v", err)
}
if path := os.Getenv("PATH"); path == "" {
t.Errorf("Fixing path did not work. Current path: %s", path)
}
})
t.Run("should return an error if SHELL env var is not set", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Test not applicable to Windows")
}
defer resetEnv(t, ogPath, ogShell)
os.Unsetenv("SHELL")
if shell := os.Getenv("SHELL"); shell != "" {
t.Error("Failed to unset SHELL env var")
}
err := pathfix.Fix()
if err == nil {
t.Error("Expected an error, got none")
}
})
t.Run("should not attempt to fix PATH if running on windows", func(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skipf("Not running on windows, this test is not applicable")
}
defer resetEnv(t, ogPath, ogShell)
os.Unsetenv("PATH")
if path := os.Getenv("PATH"); path != "" {
t.Error("Failed to unset PATH")
}
if err := pathfix.Fix(); err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
if path := os.Getenv("PATH"); path != "" {
t.Error("Path fix was performed, expected it to be skipped")
}
})
t.Run("should return an error if shell process fails", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Test not applicable to Windows")
}
defer resetEnv(t, ogPath, ogShell)
// Supply an invalid value for SHELL executable so that invoking it fails
os.Setenv("SHELL", "/blahblahyadayada")
if err := pathfix.Fix(); err == nil {
t.Error("Expected an error, got none")
}
})
t.Run("should append to the old PATH if it is not empty", func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Test not applicable to Windows")
}
defer resetEnv(t, ogPath, ogShell)
currentPath := "~/blah" + string(os.PathListSeparator) + os.Getenv("PATH")
t.Logf("Starting with PATH:\n%s\n\n", currentPath)
os.Setenv("PATH", currentPath)
if err := pathfix.Fix(); err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
path := os.Getenv("PATH")
if !strings.HasPrefix(path, currentPath) {
t.Error("New PATH was not appended to old path: Could not find old PATH at the start")
}
})
}
func resetEnv(t *testing.T, ogPath, ogShell string) {
t.Helper()
os.Setenv("PATH", ogPath)
os.Setenv("SHELL", ogShell)
}