-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathexecutil_test.go
More file actions
108 lines (96 loc) · 2.68 KB
/
Copy pathexecutil_test.go
File metadata and controls
108 lines (96 loc) · 2.68 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package executil
import (
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
var newLineMarker string
func init() {
if runtime.GOOS == "windows" {
newLineMarker = "\r\n"
} else {
newLineMarker = "\n"
}
}
func TestRun(t *testing.T) {
// try to run the echo command
s, err := Run("echo test")
require.Nil(t, err, "failed execution", err)
require.Equal(t, "test"+newLineMarker, s, "output doesn't contain expected result", s)
}
func TestRunAdv(t *testing.T) {
testcases := []struct {
GOOS string // OS
Command string
Expected string // expected output
Contains string // expected output contains
}{
// Tests With Flags
{"darwin", "uname -s", "Darwin", ""},
{"linux", "uname -s", "Linux", ""},
{"windows", "cmd /c ver", "", "Windows"},
// Tests With CMD PIPE
{"windows", `systeminfo | findstr /B /C:"OS Name"`, "", "Windows"},
{"darwin", `sw_vers | grep -i "ProductName"`, "", "macOS"},
{"linux", `uname -a | cut -d " " -f 1`, "Linux", ""},
// Other Shell Specific Features
{"windows", `cmd /c " echo This && echo Works"`, "This \r\nWorks", ""},
{"linux", "true && echo This Works", "This Works", ""},
{"darwin", "true && echo This Works", "This Works", ""},
}
runFunc := func(cmd string, expected string, contains string) {
s, err := Run(cmd)
require.Nilf(t, err, "%v failed to execute", cmd)
if expected != "" {
require.Equal(t, expected+newLineMarker, s)
} else if contains != "" {
require.Contains(t, s, contains)
} else {
t.Logf("Malformed test case : %v", cmd)
}
t.Logf("Test Successful: %v", cmd)
}
for _, v := range testcases {
switch v.GOOS {
case "windows":
if runtime.GOOS != "windows" {
continue
}
runFunc(v.Command, v.Expected, v.Contains)
case "darwin":
if runtime.GOOS != "darwin" {
continue
}
runFunc(v.Command, v.Expected, v.Contains)
case "linux":
if runtime.GOOS != "linux" {
continue
}
runFunc(v.Command, v.Expected, v.Contains)
default:
t.Logf("No Unit Test Available for this platform")
}
}
}
func TestRunSafe(t *testing.T) {
_, err := RunSafe(`whoami | grep Hello`)
require.Error(t, err)
}
func TestRunSh(t *testing.T) {
if runtime.GOOS == "windows" {
return
}
// try to run the echo command
s, err := RunSh("echo", "test")
require.Nil(t, err, "failed execution", err)
require.Equal(t, "test"+newLineMarker, s, "output doesn't contain expected result", s)
}
func TestRunPS(t *testing.T) {
if runtime.GOOS != "windows" {
return
}
// run powershell command (runs in both ps1 and ps2)
s, err := RunPS("get-host")
require.Nil(t, err, "failed execution", err)
require.Contains(t, s, "Microsoft.PowerShell", "failed to run powershell command get-host")
}