forked from alanjds/grumpy
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfile_test.go
423 lines (398 loc) · 17.6 KB
/
file_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grumpy
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"
)
var (
wantFileOperationRead = 1
wantFileOperationWrite = 2
)
func TestFileInit(t *testing.T) {
f := newTestFile("blah blah")
defer f.cleanup()
cases := []invokeTestCase{
{args: wrapArgs(newObject(FileType), f.path), want: None},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(TypeErrorType, "'__init__' requires 2 arguments")},
{args: wrapArgs(newObject(FileType), f.path, "abc"), wantExc: mustCreateException(ValueErrorType, `invalid mode string: "abc"`)},
{args: wrapArgs(newObject(FileType), "nonexistent-file"), wantExc: mustCreateException(IOErrorType, "open nonexistent-file: no such file or directory")},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "__init__", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileClosed(t *testing.T) {
f := newTestFile("foo\nbar")
defer f.cleanup()
closedFile := f.open("r")
// This puts the file into an invalid state since Grumpy thinks
// it's open even though the underlying file was closed.
closedFile.file.Close()
cases := []invokeTestCase{
{args: wrapArgs(newObject(FileType)), want: True.ToObject()},
{args: wrapArgs(f.open("r")), want: False.ToObject()},
{args: wrapArgs(closedFile), want: False.ToObject()},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "closed", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileCloseExit(t *testing.T) {
f := newTestFile("foo\nbar")
defer f.cleanup()
for _, method := range []string{"close", "__exit__"} {
closedFile := f.open("r")
// This puts the file into an invalid state since Grumpy thinks
// it's open even though the underlying file was closed.
closedFile.file.Close()
cases := []invokeTestCase{
{args: wrapArgs(newObject(FileType)), want: None},
{args: wrapArgs(f.open("r")), want: None},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFile.file.Close().Error())},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, method, &cas); err != "" {
t.Error(err)
}
}
}
}
func TestFileGetName(t *testing.T) {
fun := wrapFuncForTest(func(f *Frame, file *File) (*Object, *BaseException) {
return GetAttr(f, file.ToObject(), NewStr("name"), nil)
})
foo := newTestFile("foo")
defer foo.cleanup()
cases := []invokeTestCase{
{args: wrapArgs(foo.open("r")), want: NewStr(foo.path).ToObject()},
{args: wrapArgs(newObject(FileType)), want: NewStr("<uninitialized file>").ToObject()},
}
for _, cas := range cases {
if err := runInvokeTestCase(fun, &cas); err != "" {
t.Error(err)
}
}
}
func TestFileIter(t *testing.T) {
files := makeTestFiles()
defer files.cleanup()
closedFile := files[0].open("r")
closedFile.file.Close()
_, closedFileReadError := closedFile.file.Read(make([]byte, 10))
cases := []invokeTestCase{
{args: wrapArgs(files[0].open("r")), want: newTestList("foo").ToObject()},
{args: wrapArgs(files[0].open("rU")), want: newTestList("foo").ToObject()},
{args: wrapArgs(files[1].open("r")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[1].open("rU")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r")), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(files[2].open("rU")), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(files[3].open("r")), want: newTestList("foo\r\n").ToObject()},
{args: wrapArgs(files[3].open("rU")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[4].open("r")), want: newTestList("foo\rbar").ToObject()},
{args: wrapArgs(files[4].open("rU")), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFileReadError.Error())},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(ValueErrorType, "I/O operation on closed file")},
}
for _, cas := range cases {
if err := runInvokeTestCase(ListType.ToObject(), &cas); err != "" {
t.Error(err)
}
}
}
func TestFileNext(t *testing.T) {
files := makeTestFiles()
defer files.cleanup()
closedFile := files[0].open("r")
closedFile.file.Close()
_, closedFileReadError := closedFile.file.Read(make([]byte, 10))
cases := []invokeTestCase{
{args: wrapArgs(files[0].open("r")), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[0].open("rU")), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[1].open("r")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[1].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[3].open("r")), want: NewStr("foo\r\n").ToObject()},
{args: wrapArgs(files[3].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[4].open("r")), want: NewStr("foo\rbar").ToObject()},
{args: wrapArgs(files[4].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "unbound method next() must be called with file instance as first argument (got nothing instead)")},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFileReadError.Error())},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(ValueErrorType, "I/O operation on closed file")},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "next", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileRead(t *testing.T) {
f := newTestFile("foo\nbar")
defer f.cleanup()
closedFile := f.open("r")
closedFile.file.Close()
_, closedFileReadError := closedFile.file.Read(make([]byte, 10))
cases := []invokeTestCase{
{args: wrapArgs(f.open("r")), want: NewStr("foo\nbar").ToObject()},
{args: wrapArgs(f.open("r"), 3), want: NewStr("foo").ToObject()},
{args: wrapArgs(f.open("r"), 1000), want: NewStr("foo\nbar").ToObject()},
{args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "unbound method read() must be called with file instance as first argument (got nothing instead)")},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFileReadError.Error())},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(ValueErrorType, "I/O operation on closed file")},
{args: wrapArgs(newObject(FileType), "abc"), wantExc: mustCreateException(ValueErrorType, "invalid literal for int() with base 10: abc")},
{args: wrapArgs(newObject(FileType), 123, 456), wantExc: mustCreateException(TypeErrorType, "'read' of 'file' requires 2 arguments")},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "read", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileReadLine(t *testing.T) {
files := makeTestFiles()
defer files.cleanup()
closedFile := files[0].open("r")
closedFile.file.Close()
_, closedFileReadError := closedFile.file.Read(make([]byte, 10))
partialReadFile := files[5].open("rU")
partialReadFile.readLine(-1)
cases := []invokeTestCase{
{args: wrapArgs(files[0].open("r")), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[0].open("rU")), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[1].open("r")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[1].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r"), 2), want: NewStr("fo").ToObject()},
{args: wrapArgs(files[2].open("r"), 3), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[2].open("r"), 4), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r"), 5), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[3].open("r")), want: NewStr("foo\r\n").ToObject()},
{args: wrapArgs(files[3].open("rU")), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[3].open("rU"), 3), want: NewStr("foo").ToObject()},
{args: wrapArgs(files[3].open("rU"), 4), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[3].open("rU"), 5), want: NewStr("foo\n").ToObject()},
{args: wrapArgs(files[4].open("r")), want: NewStr("foo\rbar").ToObject()},
{args: wrapArgs(files[4].open("rU")), want: NewStr("foo\n").ToObject()},
// Ensure that reading after a \r\n returns the requested
// number of bytes when possible. Check that the trailing \n
// does not count toward the bytes read.
{args: wrapArgs(partialReadFile, 3), want: NewStr("bar").ToObject()},
{args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "unbound method readline() must be called with file instance as first argument (got nothing instead)")},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFileReadError.Error())},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(ValueErrorType, "I/O operation on closed file")},
{args: wrapArgs(newObject(FileType), "abc"), wantExc: mustCreateException(ValueErrorType, "invalid literal for int() with base 10: abc")},
{args: wrapArgs(newObject(FileType), 123, 456), wantExc: mustCreateException(TypeErrorType, "'readline' of 'file' requires 2 arguments")},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "readline", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileReadLines(t *testing.T) {
files := makeTestFiles()
defer files.cleanup()
closedFile := files[0].open("r")
closedFile.file.Close()
_, closedFileReadError := closedFile.file.Read(make([]byte, 10))
partialReadFile := files[5].open("rU")
partialReadFile.readLine(-1)
cases := []invokeTestCase{
{args: wrapArgs(files[0].open("r")), want: newTestList("foo").ToObject()},
{args: wrapArgs(files[0].open("rU")), want: newTestList("foo").ToObject()},
{args: wrapArgs(files[1].open("r")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[1].open("rU")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r")), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(files[2].open("rU")), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(files[2].open("r"), 2), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r"), 3), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r"), 4), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[2].open("r"), 5), want: newTestList("foo\n", "bar").ToObject()},
{args: wrapArgs(files[3].open("r")), want: newTestList("foo\r\n").ToObject()},
{args: wrapArgs(files[3].open("rU")), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[3].open("rU"), 3), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[3].open("rU"), 4), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[3].open("rU"), 5), want: newTestList("foo\n").ToObject()},
{args: wrapArgs(files[4].open("r")), want: newTestList("foo\rbar").ToObject()},
{args: wrapArgs(files[4].open("rU")), want: newTestList("foo\n", "bar").ToObject()},
// Ensure that reading after a \r\n returns the requested
// number of bytes when possible. Check that the trailing \n
// does not count toward the bytes read.
{args: wrapArgs(partialReadFile, 3), want: newTestList("bar\n").ToObject()},
{args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "unbound method readlines() must be called with file instance as first argument (got nothing instead)")},
{args: wrapArgs(closedFile), wantExc: mustCreateException(IOErrorType, closedFileReadError.Error())},
{args: wrapArgs(newObject(FileType)), wantExc: mustCreateException(ValueErrorType, "I/O operation on closed file")},
{args: wrapArgs(newObject(FileType), "abc"), wantExc: mustCreateException(ValueErrorType, "invalid literal for int() with base 10: abc")},
{args: wrapArgs(newObject(FileType), 123, 456), wantExc: mustCreateException(TypeErrorType, "'readlines' of 'file' requires 2 arguments")},
}
for _, cas := range cases {
if err := runInvokeMethodTestCase(FileType, "readlines", &cas); err != "" {
t.Error(err)
}
}
}
func TestFileStrRepr(t *testing.T) {
fun := newBuiltinFunction("TestFileStrRepr", func(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkFunctionArgs(f, "TestFileStrRepr", args, ObjectType, StrType); raised != nil {
return nil, raised
}
o := args[0]
re := regexp.MustCompile(toStrUnsafe(args[1]).Value())
s, raised := ToStr(f, o)
if raised != nil {
return nil, raised
}
Assert(f, GetBool(re.MatchString(s.Value())).ToObject(), nil)
s, raised = Repr(f, o)
if raised != nil {
return nil, raised
}
Assert(f, GetBool(re.MatchString(s.Value())).ToObject(), nil)
return None, nil
}).ToObject()
f := newTestFile("foo\nbar")
defer f.cleanup()
closedFile := f.open("r").ToObject()
mustNotRaise(fileClose(NewRootFrame(), []*Object{closedFile}, nil))
// Open a file for write.
writeFile := f.open("wb")
cases := []invokeTestCase{
{args: wrapArgs(f.open("r"), `^<open file "[^"]+", mode "r" at \w+>$`), want: None},
{args: wrapArgs(writeFile, `^<open file "[^"]+", mode "wb" at \w+>$`), want: None},
{args: wrapArgs(newObject(FileType), `^<closed file "<uninitialized file>", mode "<uninitialized file>" at \w+>$`), want: None},
{args: wrapArgs(closedFile, `^<closed file "[^"]+", mode "r" at \w+>$`), want: None},
}
for _, cas := range cases {
if err := runInvokeTestCase(fun, &cas); err != "" {
t.Error(err)
}
}
}
func TestFileWrite(t *testing.T) {
fun := newBuiltinFunction("TestFileWrite", func(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
if raised := checkMethodArgs(f, "TestFileWrite", args, StrType, StrType, StrType); raised != nil {
return nil, raised
}
writeFile, raised := FileType.Call(f, args[:2], nil)
if raised != nil {
return nil, raised
}
write, raised := GetAttr(f, writeFile, NewStr("write"), nil)
if raised != nil {
return nil, raised
}
if _, raised := write.Call(f, args[2:], nil); raised != nil {
return nil, raised
}
contents, err := ioutil.ReadFile(toStrUnsafe(args[0]).Value())
if err != nil {
return nil, f.RaiseType(RuntimeErrorType, fmt.Sprintf("error reading file: %s", err.Error()))
}
return NewStr(string(contents)).ToObject(), nil
}).ToObject()
// Create a temporary directory and cd to it.
dir, err := ioutil.TempDir("", "TestFileWrite")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(dir)
oldWd, err := os.Getwd()
if err != nil {
t.Fatalf("Getwd() failed: %s", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("Chdir(%q) failed: %s", dir, err)
}
defer os.Chdir(oldWd)
for _, filename := range []string{"truncate.txt", "readonly.txt", "append.txt", "rplus.txt", "aplus.txt", "wplus.txt"} {
if err := ioutil.WriteFile(filename, []byte(filename), 0644); err != nil {
t.Fatalf("ioutil.WriteFile(%q) failed: %s", filename, err)
}
}
cases := []invokeTestCase{
{args: wrapArgs("noexist.txt", "w", "foo\nbar"), want: NewStr("foo\nbar").ToObject()},
{args: wrapArgs("truncate.txt", "w", "new contents"), want: NewStr("new contents").ToObject()},
{args: wrapArgs("append.txt", "a", "\nbar"), want: NewStr("append.txt\nbar").ToObject()},
{args: wrapArgs("rplus.txt", "r+", "fooey"), want: NewStr("fooey.txt").ToObject()},
{args: wrapArgs("noexistplus1.txt", "r+", "pooey"), wantExc: mustCreateException(IOErrorType, "open noexistplus1.txt: no such file or directory")},
{args: wrapArgs("aplus.txt", "a+", "\napper"), want: NewStr("aplus.txt\napper").ToObject()},
{args: wrapArgs("noexistplus3.txt", "a+", "snappbacktoreality"), want: NewStr("snappbacktoreality").ToObject()},
{args: wrapArgs("wplus.txt", "w+", "destructo"), want: NewStr("destructo").ToObject()},
{args: wrapArgs("noexistplus2.txt", "w+", "wapper"), want: NewStr("wapper").ToObject()},
{args: wrapArgs("readonly.txt", "r", "foo"), wantExc: mustCreateException(IOErrorType, "write readonly.txt: bad file descriptor")},
}
for _, cas := range cases {
if err := runInvokeTestCase(fun, &cas); err != "" {
t.Error(err)
}
}
}
type testFile struct {
path string
files []*File
}
func newTestFile(contents string) *testFile {
osFile, err := ioutil.TempFile("", "")
if err != nil {
panic(err)
}
if _, err := osFile.WriteString(contents); err != nil {
panic(err)
}
if err := osFile.Close(); err != nil {
panic(err)
}
return &testFile{path: osFile.Name()}
}
func (f *testFile) cleanup() {
for _, file := range f.files {
file.file.Close()
}
os.Remove(f.path)
}
func (f *testFile) open(mode string) *File {
args := wrapArgs(f.path, mode)
o := mustNotRaise(FileType.Call(NewRootFrame(), args, nil))
if o == nil || !o.isInstance(FileType) {
panic(fmt.Sprintf("file%v = %v, want file object", args, o))
}
file := toFileUnsafe(o)
f.files = append(f.files, file)
return file
}
type testFileSlice []*testFile
func makeTestFiles() testFileSlice {
return []*testFile{
newTestFile("foo"),
newTestFile("foo\n"),
newTestFile("foo\nbar"),
newTestFile("foo\r\n"),
newTestFile("foo\rbar"),
newTestFile("foo\r\nbar\r\nbaz"),
}
}
func (files testFileSlice) cleanup() {
for _, f := range files {
f.cleanup()
}
}