Skip to content

Commit e597e0f

Browse files
committed
Local exec unit test
1 parent 59a91a9 commit e597e0f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestRemoteStdin(t *testing.T) {
5151
}
5252
}
5353

54-
func TestExec(t *testing.T) {
54+
func TestRemoteExec(t *testing.T) {
5555
mockServerHandler := func(w http.ResponseWriter, r *http.Request) {
5656
ws, err := websocket.Accept(w, r, nil)
5757
if err != nil {
@@ -100,13 +100,14 @@ func TestExec(t *testing.T) {
100100
go func() {
101101
wg.Add(1)
102102
defer wg.Done()
103+
103104
stderr, err := ioutil.ReadAll(process.Stderr())
104105
assert.Success(t, "read stderr", err)
105106
assert.Equal(t, "len stderr", 0, len(stderr))
106107
}()
107108

108109
err = process.Wait()
109-
assert.Success(t, "wait", err)
110+
assert.Success(t, "wait for process to complete", err)
110111

111112
wg.Wait()
112113
}

localexec_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package wsep
2+
3+
import (
4+
"context"
5+
"io/ioutil"
6+
"os"
7+
"strings"
8+
"sync"
9+
"testing"
10+
"time"
11+
12+
"cdr.dev/slog/sloggers/slogtest/assert"
13+
)
14+
15+
func TestLocalExec(t *testing.T) {
16+
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
17+
defer cancel()
18+
19+
execer := LocalExecer{}
20+
process, err := execer.Start(ctx, Command{
21+
Command: "pwd",
22+
})
23+
assert.Success(t, "start local cmd", err)
24+
25+
var wg sync.WaitGroup
26+
go func() {
27+
wg.Add(1)
28+
defer wg.Done()
29+
30+
stdout, err := ioutil.ReadAll(process.Stdout())
31+
assert.Success(t, "read stdout", err)
32+
wd, err := os.Getwd()
33+
assert.Success(t, "get real working dir", err)
34+
35+
assert.Equal(t, "stdout", wd, strings.TrimSuffix(string(stdout), "\n"))
36+
}()
37+
go func() {
38+
wg.Add(1)
39+
defer wg.Done()
40+
41+
stderr, err := ioutil.ReadAll(process.Stderr())
42+
assert.Success(t, "read stderr", err)
43+
assert.Equal(t, "len stderr", 0, len(stderr))
44+
}()
45+
46+
err = process.Wait()
47+
assert.Success(t, "wait for process to complete", err)
48+
49+
wg.Wait()
50+
}

0 commit comments

Comments
 (0)