forked from everettraven/packageless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
241 lines (195 loc) · 4.77 KB
/
main_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/everettraven/packageless/utils"
)
type TestCase struct {
Name string
Args []string
Err bool
ExpectedErr string
}
func TestMain_OsArgs(t *testing.T) {
if testing.Short() {
t.Skip("short testing specified, skipping this test")
}
//Create a list of structs to hold data info for testing cases for windows
casesWindows := []TestCase{
{
"Install Test",
[]string{"packageless", "install", "python"},
false,
"",
},
{
//We know this one will fail because go test doesn't include tty which the docker command we run uses
//This test failing lets us know the command is attempting to run properly
"Run Test",
[]string{"packageless", "run", "python"},
true,
"",
},
{
"Update Test",
[]string{"packageless", "update", "python"},
false,
"",
},
{
"Upgrade Test",
[]string{"packageless", "upgrade", "python"},
false,
"",
},
{
"Uninstall Test",
[]string{"packageless", "uninstall", "python"},
false,
"",
},
}
//Create a list of structs to hold data info for testing cases for windows
casesUnix := []TestCase{
{
"Install Test",
[]string{"packageless", "install", "python"},
false,
"",
},
{
//We know this one will fail because go test doesn't include tty which the docker command we run uses
//This test failing lets us know the command is attempting to run properly
"Run Test",
[]string{"packageless", "run", "python"},
true,
"",
},
{
"Update Test",
[]string{"packageless", "update", "python"},
false,
"",
},
{
"Upgrade Test",
[]string{"packageless", "upgrade", "python"},
false,
"",
},
{
"Uninstall Test",
[]string{"packageless", "uninstall", "python"},
false,
"",
},
}
//Copy necessary files to the test build location
ex, err := os.Executable()
if err != nil {
t.Fatal(err)
}
ed := filepath.Dir(ex)
if err != nil {
t.Fatal(err)
}
//Create the .packageless directory and necessary subdirectories in the executable directory
err = utils.NewUtility().MakeDir(ed + "/.packageless")
if err != nil {
t.Fatal(err)
}
err = utils.NewUtility().MakeDir(ed + "/.packageless/pims_config")
if err != nil {
t.Fatal(err)
}
err = utils.NewUtility().MakeDir(ed + "/.packageless/pims")
if err != nil {
t.Fatal(err)
}
err = utils.NewUtility().MakeDir(ed + "/Documents/WindowsPowerShell")
if err != nil {
t.Fatal(err)
}
err = Copy("./config.hcl", ed+"/.packageless/config.hcl")
if err != nil {
t.Fatal(err)
}
//Change the HOME/USERPROFILE environment variable to point to the executable directory
//First we need to save the old HOME/USERPROFILE environment variable so we can change it back later
var oldHomeEnv string
//If the user is on windows we need to change the USERPROFILE value, otherwise it is the HOME value
if runtime.GOOS == "windows" {
oldHomeEnv = os.Getenv("USERPROFILE")
//set a new HOME ENV variable
err = os.Setenv("USERPROFILE", ed)
if err != nil {
t.Fatalf("Error trying to set new USERPROFILE env value: %s", err.Error())
}
} else {
oldHomeEnv = os.Getenv("HOME")
//set a new HOME ENV variable
err = os.Setenv("HOME", ed)
if err != nil {
t.Fatalf("Error trying to set new HOME env value: %s", err.Error())
}
}
var cases []TestCase
if runtime.GOOS == "windows" {
cases = casesWindows
} else {
cases = casesUnix
}
//Loop through the test cases
for i, tc := range cases {
t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
fmt.Println(i)
//Set the args when running
os.Args = tc.Args
//Run the main function
exit, err := wrappedMain()
if (exit != 0) != tc.Err {
t.Fatalf("Fail - Exit code did not match the expected | Received Error: %s", err)
} else {
if tc.ExpectedErr != "" {
if err.Error() != tc.ExpectedErr {
t.Fatalf("Fail - Expected Error: %s | Received: %s", tc.ExpectedErr, err.Error())
}
}
}
})
}
//Before we exit the tests lets set the HOME/USERPROFILE env var value back
if runtime.GOOS == "windows" {
//set a new HOME ENV variable
err = os.Setenv("USERPROFILE", oldHomeEnv)
if err != nil {
t.Fatalf("Failed to set the USERPROFILE env value back to the original value of '%s': %s", oldHomeEnv, err.Error())
}
} else {
err = os.Setenv("HOME", oldHomeEnv)
if err != nil {
t.Fatalf("Failed to set the HOME env value back to the original value of '%s': %s", oldHomeEnv, err.Error())
}
}
}
//Function to copy files
func Copy(src string, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}