-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunc_read_file_test.go
101 lines (88 loc) · 2.26 KB
/
func_read_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
package testing
import (
"os"
"testing"
)
func Test_Func_ReadFile(t *testing.T) {
rsl := `
a, b = read_file("data/test_file.txt")
print(a)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ "size_bytes": 9, "content": "hello bob" }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Func_ReadFile_ErrEmptyIfNoError(t *testing.T) {
rsl := `
a, b = read_file("data/test_file.txt")
print(b)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Func_ReadFile_OneArg(t *testing.T) {
rsl := `
a = read_file("data/test_file.txt")
print(a)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ "size_bytes": 9, "content": "hello bob" }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Func_ReadFile_NoExist(t *testing.T) {
rsl := `
a, b = read_file("does_not_exist.txt")
print(b)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ "code": "RAD20005", "msg": "open does_not_exist.txt: no such file or directory" }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Func_ReadFile_NoPermission(t *testing.T) {
filePath := "data/no_permission.txt"
info, _ := os.Stat(filePath)
originalPerms := info.Mode().Perm()
os.Chmod(filePath, originalPerms&^0444)
rsl := `
a, b = read_file("data/no_permission.txt")
print(b)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ "code": "RAD20004", "msg": "open data/no_permission.txt: permission denied" }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
os.Chmod(filePath, originalPerms)
}
func Test_Func_ReadFile_ErrorsOnDirectory(t *testing.T) {
rsl := `
a, b = read_file("data/")
print(b)
`
setupAndRunCode(t, rsl, "--color=never")
expected := `{ "code": "RAD20003", "msg": "read data/: is a directory" }
`
assertOnlyOutput(t, stdOutBuffer, expected)
assertNoErrors(t)
}
func Test_Func_ReadFile_ExitErrorsIfNoErrVar(t *testing.T) {
rsl := `
a = read_file("does_not_exist.txt")
`
setupAndRunCode(t, rsl, "--color=never")
expected := `Error at L2:5
a = read_file("does_not_exist.txt")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
open does_not_exist.txt: no such file or directory (error RAD20005)
`
assertError(t, 1, expected)
}