This repository was archived by the owner on Jun 30, 2022. It is now read-only.
forked from bhendo/go-powershell
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshell_test.go
76 lines (65 loc) · 1.45 KB
/
shell_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
package powershell
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/k3s-io/go-powershell/backend"
)
type context struct {
shell Shell
}
func TestLocalShell(t *testing.T) {
c := context{}
dir, err := ioutil.TempDir("", "pwsh-test")
if err != nil {
t.Errorf("error from TempDir: %v", err)
}
defer os.RemoveAll(dir)
f, err := ioutil.TempFile(dir, "cowabunga")
if err != nil {
t.Errorf("error from Tempfile: %v", err)
}
f.Close()
if err := os.Chdir(dir); err != nil {
t.Errorf("error from Chdir: %v", err)
}
s, err := New(&backend.Local{})
if err != nil {
t.Skip("error from New: ", err)
}
c.shell = s
t.Run("it can run commands", c.basicTest)
t.Run("it correctly parses its boundary token", c.boundaryTest)
}
func (c *context) basicTest(t *testing.T) {
sout, serr, err := c.shell.Execute(`echo "hi from stdout"`)
if err != nil {
t.Errorf("error from Execute: %v", err)
}
if sout != "hi from stdout\r\n" {
t.Errorf("unexpected stdout: %q", sout)
}
if serr != "" {
t.Errorf("unexpected stderr: %q", sout)
}
}
func (c *context) boundaryTest(t *testing.T) {
done := make(chan bool)
for i := 0; i < 1000; i++ {
timeout := time.After(10 * time.Second)
go func() {
_, _, err := c.shell.Execute("ls")
if err != nil {
t.Errorf("error from Execute: %v", err)
}
done <- true
}()
select {
case <-timeout:
t.Fatalf("Timed out waiting for command after %d iterations", i)
case <-done:
continue
}
}
}