forked from madlambda/nash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nash_test.go
138 lines (106 loc) · 2.27 KB
/
nash_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
129
130
131
132
133
134
135
136
137
138
package nash
import (
"bytes"
"os"
"testing"
"io/ioutil"
"github.com/NeowayLabs/nash/sh"
"github.com/NeowayLabs/nash/tests"
)
// only testing the public API
// bypass to internal sh.Shell
func TestExecuteFile(t *testing.T) {
testfile := tests.Testdir + "/ex1.sh"
var out bytes.Buffer
shell, cleanup := newShell(t)
defer cleanup()
shell.SetNashdPath(tests.Nashcmd)
shell.SetStdout(&out)
shell.SetStderr(os.Stderr)
shell.SetStdin(os.Stdin)
err := shell.ExecuteFile(testfile)
if err != nil {
t.Error(err)
return
}
if string(out.Bytes()) != "hello world\n" {
t.Errorf("Wrong command output: '%s'", string(out.Bytes()))
return
}
}
func TestExecuteString(t *testing.T) {
shell, cleanup := newShell(t)
defer cleanup()
var out bytes.Buffer
shell.SetStdout(&out)
err := shell.ExecuteString("-ínput-", "echo -n AAA")
if err != nil {
t.Error(err)
return
}
if string(out.Bytes()) != "AAA" {
t.Errorf("Unexpected '%s'", string(out.Bytes()))
return
}
out.Reset()
err = shell.ExecuteString("-input-", `
PROMPT="humpback> "
setenv PROMPT
`)
if err != nil {
t.Error(err)
return
}
prompt := shell.Prompt()
if prompt != "humpback> " {
t.Errorf("Invalid prompt = %s", prompt)
return
}
}
func TestSetvar(t *testing.T) {
shell,cleanup := newShell(t)
defer cleanup()
shell.Newvar("__TEST__", sh.NewStrObj("something"))
var out bytes.Buffer
shell.SetStdout(&out)
err := shell.Exec("TestSetvar", `echo -n $__TEST__`)
if err != nil {
t.Error(err)
return
}
if string(out.Bytes()) != "something" {
t.Errorf("Value differ: '%s' != '%s'", string(out.Bytes()), "something")
return
}
val, ok := shell.Getvar("__TEST__")
if !ok || val.String() != "something" {
t.Errorf("Getvar doesn't work: '%s' != '%s'", val, "something")
return
}
}
func newShell(t *testing.T) (*Shell, func()) {
t.Helper()
nashpath, pathclean := tmpdir(t)
nashroot, rootclean := tmpdir(t)
s, err := New(nashpath, nashroot)
if err != nil {
t.Fatal(err)
}
return s, func() {
pathclean()
rootclean()
}
}
func tmpdir(t *testing.T) (string, func()) {
t.Helper()
dir, err := ioutil.TempDir("", "nash-tests")
if err != nil {
t.Fatal(err)
}
return dir, func() {
err := os.RemoveAll(dir)
if err != nil {
t.Fatal(err)
}
}
}