forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_test.go
186 lines (172 loc) · 4.56 KB
/
exec_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
file2 "github.com/rjkroege/edwood/file"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func acmeTestingMain() {
acmeshell = os.Getenv("acmeshell")
cwait = make(chan ProcessState)
cerr = make(chan error)
go func() {
for range cerr {
// Do nothing with command output.
}
}()
}
func TestRunproc(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("skipping on windows")
}
tt := []struct {
hard bool
startfail bool
waitfail bool
s, arg string
}{
{false, true, true, "", ""},
{false, true, true, " ", ""},
{false, true, true, " ", " "},
{false, false, false, "ls", ""},
{false, false, false, "ls .", ""},
{false, false, false, " ls . ", ""},
{false, false, false, " ls . ", ""},
{false, false, false, "ls", "."},
{false, false, false, "|ls", "."},
{false, false, false, "<ls", "."},
{false, false, false, ">ls", "."},
{false, true, true, "nonexistentcommand", ""},
// Hard: must be executed using a shell
{true, false, false, "ls '.'", ""},
{true, false, false, " ls '.' ", ""},
{true, false, false, " ls '.' ", ""},
{true, false, false, "ls '.'", "."},
{true, false, true, "dat\x08\x08ate", ""},
{true, false, true, "/non-existent-command", ""},
}
acmeTestingMain()
for _, tc := range tt {
// runproc goes into Hard path if acmeshell is non-empty.
// Unset acmeshell for non-hard cases.
if tc.hard {
acmeshell = os.Getenv("acmeshell")
} else {
acmeshell = ""
}
cpid := make(chan *os.Process)
done := make(chan struct{})
go func() {
err := runproc(nil, tc.s, "", false, "", tc.arg, &Command{}, cpid, false)
if tc.startfail && err == nil {
t.Errorf("expected command %q to fail", tc.s)
}
if !tc.startfail && err != nil {
t.Errorf("runproc failed for command %q: %v", tc.s, err)
}
close(done)
}()
proc := <-cpid
if !tc.waitfail && proc == nil {
t.Errorf("nil proc for command %v", tc.s)
}
if proc != nil {
status := <-cwait
if tc.waitfail && status.Success() {
t.Errorf("command %q exited with status %v", tc.s, status)
}
if !tc.waitfail && !status.Success() {
t.Errorf("command %q exited with status %v", tc.s, status)
}
}
<-done
}
}
func TestPutfile(t *testing.T) {
dir, err := ioutil.TempDir("", "edwood.test")
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
defer os.RemoveAll(dir)
filename := filepath.Join(dir, "hello.txt")
err = ioutil.WriteFile(filename, nil, 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
checkFile := func(t *testing.T, content string) {
b, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("ReadAll failed: %v", err)
}
s := string(b)
if s != content {
t.Errorf("file content is %q; expected %q", s, content)
}
}
want := "Hello, 世界\n"
w := &Window{
body: Text{
file: file2.MakeObservableEditableBuffer(filename, file2.RuneArray(want)),
},
}
f := w.body.file
file := w.body.file
cur := &w.body
cur.w = w
file.SetCurObserver(cur)
increaseMtime := func(t *testing.T, duration time.Duration) {
tm := file.Info().ModTime().Add(duration)
if err := os.Chtimes(filename, tm, tm); err != nil {
t.Fatalf("Chtimes failed: %v", err)
}
}
err = putfile(file, 0, f.Size(), filename)
if err == nil || !strings.Contains(err.Error(), "file already exists") {
t.Fatalf("putfile returned error %v; expected 'file already exists'", err)
}
err = putfile(file, 0, f.Size(), filename)
if err != nil {
t.Fatalf("putfile failed: %v", err)
}
checkFile(t, want)
// mtime increased but hash is the same
increaseMtime(t, time.Second)
err = putfile(file, 0, f.Size(), filename)
if err != nil {
t.Fatalf("putfile failed: %v", err)
}
checkFile(t, want)
// mtime increased and hash changed
want = "Hello, 世界\nThis line added outside of Edwood.\n"
err = ioutil.WriteFile(filename, []byte(""), 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
increaseMtime(t, time.Second)
err = putfile(file, 0, f.Size(), filename)
if err == nil || !strings.Contains(err.Error(), "modified since last read") {
t.Fatalf("putfile returned error %v; expected 'modified since last read'", err)
}
}
func TestExpandtabToggle(t *testing.T) {
want := true
w := &Window{
body: Text{
file: file2.MakeObservableEditableBuffer("", nil),
tabexpand: false,
tabstop: 4,
},
}
text := &w.body
text.w = w
text.tabexpand = !want
expandtab(text, text, text, false, false, "")
te := text.w.body.tabexpand
if te != want {
t.Errorf("tabexpand is set to %v; expected %v", te, want)
}
}