|
4 | 4 | package main |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "os/exec" |
8 | | - "strings" |
9 | 7 | "testing" |
10 | | - "time" |
11 | 8 | ) |
12 | 9 |
|
13 | | -func Test_stop(t *testing.T) { |
14 | | - t.Parallel() |
15 | | - |
16 | | - // Ensure no notepad.exe is running. |
17 | | - _ = exec.Command("taskkill.exe", "/f", "/im", "notepad.exe").Run() |
18 | | - |
19 | | - // Start notepad.exe as a child process, then kill the parent process with |
20 | | - // Process.Kill(). |
21 | | - cmd := exec.Command("cmd.exe", "/c", "notepad.exe") |
22 | | - err := cmd.Start() |
23 | | - if err != nil { |
24 | | - t.Fatal(err) |
25 | | - } |
26 | | - time.Sleep(500 * time.Millisecond) // Give time for it to start before killing. |
27 | | - _ = cmd.Process.Kill() |
28 | | - _ = cmd.Wait() |
29 | | - |
30 | | - // Assert that Process.Kill() did not kill the child notepad.exe process by |
31 | | - // checking that it exists with tasklist. |
32 | | - b, err := exec.Command("tasklist.exe", "/nh", "/fi", "imagename eq notepad.exe").CombinedOutput() |
33 | | - if err != nil { |
34 | | - t.Fatal(err) |
35 | | - } |
36 | | - output := string(b) |
37 | | - if !strings.Contains(output, "notepad.exe") { |
38 | | - t.Fatalf("notepad.exe was successfully killed by Process.Kill(): %s", output) |
39 | | - } |
40 | | - |
41 | | - // Ensure no notepad.exe is running. |
42 | | - _ = exec.Command("taskkill.exe", "/f", "/im", "notepad.exe").Run() |
43 | | - |
44 | | - // Start notepad.exe as a child process again, then kill the parent process |
45 | | - // with kill(). |
46 | | - cmd = exec.Command("cmd.exe", "/c", "notepad.exe") |
47 | | - err = cmd.Start() |
48 | | - if err != nil { |
49 | | - t.Fatal(err) |
50 | | - } |
51 | | - time.Sleep(500 * time.Millisecond) // Give time for it to start before killing. |
52 | | - stop(cmd) |
53 | | - |
54 | | - // Assert that kill() killed the child notepad.exe process. |
55 | | - b, err = exec.Command("tasklist.exe", "/nh", "/fi", "imagename eq notepad.exe").CombinedOutput() |
56 | | - if err != nil { |
57 | | - t.Fatal(err) |
58 | | - } |
59 | | - output = string(b) |
60 | | - if strings.Contains(output, "notepad.exe") { |
61 | | - t.Fatalf("notepad.exe was not successfully killed by cleanup(): %s", output) |
62 | | - } |
63 | | -} |
64 | | - |
65 | 10 | func Test_joinArgs(t *testing.T) { |
66 | 11 | type TestTable struct { |
67 | 12 | description string |
|
0 commit comments