-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutil_windows_test.go
128 lines (116 loc) · 3.62 KB
/
util_windows_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
//go:build windows
// +build windows
package main
import (
"os/exec"
"strings"
"testing"
"time"
)
func Test_stop(t *testing.T) {
t.Parallel()
// Ensure no notepad.exe is running.
_ = exec.Command("taskkill.exe", "/f", "/im", "notepad.exe").Run()
// Start notepad.exe as a child process, then kill the parent process with
// Process.Kill().
cmd := exec.Command("cmd.exe", "/c", "notepad.exe")
err := cmd.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond) // Give time for it to start before killing.
_ = cmd.Process.Kill()
_ = cmd.Wait()
// Assert that Process.Kill() did not kill the child notepad.exe process by
// checking that it exists with tasklist.
b, err := exec.Command("tasklist.exe", "/nh", "/fi", "imagename eq notepad.exe").CombinedOutput()
if err != nil {
t.Fatal(err)
}
output := string(b)
if !strings.Contains(output, "notepad.exe") {
t.Fatalf("notepad.exe was successfully killed by Process.Kill(): %s", output)
}
// Ensure no notepad.exe is running.
_ = exec.Command("taskkill.exe", "/f", "/im", "notepad.exe").Run()
// Start notepad.exe as a child process again, then kill the parent process
// with kill().
cmd = exec.Command("cmd.exe", "/c", "notepad.exe")
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond) // Give time for it to start before killing.
stop(cmd)
// Assert that kill() killed the child notepad.exe process.
b, err = exec.Command("tasklist.exe", "/nh", "/fi", "imagename eq notepad.exe").CombinedOutput()
if err != nil {
t.Fatal(err)
}
output = string(b)
if strings.Contains(output, "notepad.exe") {
t.Fatalf("notepad.exe was not successfully killed by cleanup(): %s", output)
}
}
func Test_joinArgs(t *testing.T) {
type TestTable struct {
description string
args []string
want string
}
tests := []TestTable{{
description: "bare string",
args: []string{"echo", "test"},
want: "echo test",
}, {
description: "contains spaces",
args: []string{"echo", "hello goodbye"},
want: "echo 'hello goodbye'",
}, {
description: "simple args",
args: []string{"echo", "hello", "goodbye"},
want: "echo hello goodbye",
}, {
description: "single quote",
args: []string{"echo", "don't you know the dewey decimal system?"},
want: "echo 'don''t you know the dewey decimal system?'",
}, {
description: "args with single quote",
args: []string{"echo", "don't", "you", "know", "the", "dewey", "decimal", "system?"},
want: "echo 'don''t' you know the dewey decimal system?",
}, {
description: "tilde bang",
args: []string{"echo", "~user", "u~ser", " ~user", "!~user"},
want: "echo ~user u~ser ' ~user' !~user",
}, {
description: "glob brackets",
args: []string{"echo", "foo*", "M{ovies,usic}", "ab[cd]", "%3"},
want: "echo 'foo*' 'M{ovies,usic}' ab[cd] %3",
}, {
description: "empty string",
args: []string{"echo", "one", "", "three"},
want: "echo one '' three",
}, {
description: "parens",
args: []string{"echo", "some(parentheses)"},
want: "echo 'some(parentheses)'",
}, {
description: "special chars",
args: []string{"echo", "$some_ot~her_)spe!cial_*_characters"},
want: "echo '$some_ot~her_)spe!cial_*_characters'",
}, {
description: "quote space",
args: []string{"echo", "' "},
want: "echo ''' '",
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
got := joinArgs(tt.args)
if got != tt.want {
t.Errorf("\ngot: %q\nwant: %q", got, tt.want)
}
})
}
}