-
Notifications
You must be signed in to change notification settings - Fork 56
/
gosh_test.go
73 lines (63 loc) · 1.73 KB
/
gosh_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
package main
import (
"bytes"
"context"
"os"
"strings"
"testing"
)
var (
testPluginsDir = "./plugins"
)
func TestShellNew(t *testing.T) {
shell := New()
if shell.pluginsDir != testPluginsDir {
t.Error("pluginsDir not set")
}
}
func TestShellInit(t *testing.T) {
shell := New()
shell.pluginsDir = testPluginsDir
ctx := context.WithValue(context.TODO(), "gosh.stdout", os.Stdout)
if err := shell.Init(ctx); err != nil {
t.Fatal(err)
}
if len(shell.commands) <= 0 {
t.Error("failed to load plugins from", testPluginsDir)
}
if _, ok := shell.commands["hello"]; !ok {
t.Error("missing 'hello' command from test module")
}
if _, ok := shell.commands["goodbye"]; !ok {
t.Error("missing 'goodbye' command from test module")
}
}
func TestShellHandle(t *testing.T) {
shell := New()
shell.pluginsDir = testPluginsDir
ctx := context.WithValue(context.TODO(), "gosh.stdout", os.Stdout)
if err := shell.Init(ctx); err != nil {
t.Fatal(err)
}
helloOut := bytes.NewBufferString("")
shell.ctx = context.WithValue(context.TODO(), "gosh.stdout", helloOut)
if _, err := shell.handle(shell.ctx, "testhello"); err == nil {
t.Error("this test should have failed with command not found")
}
if _, err := shell.handle(shell.ctx, "hello"); err != nil {
t.Error(err)
}
printedOut := strings.TrimSpace(helloOut.String())
if printedOut != "hello there" {
t.Error("did not get expected output from testcmd")
}
byeOut := bytes.NewBufferString("")
shell.ctx = context.WithValue(context.TODO(), "gosh.stdout", byeOut)
if _, err := shell.handle(shell.ctx, "goodbye"); err != nil {
t.Error(err)
}
printedOut = strings.TrimSpace(byeOut.String())
if printedOut != "bye bye" {
t.Error("did not get expected output from testcmd")
}
}