-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileSpec.scala
147 lines (123 loc) · 3.35 KB
/
FileSpec.scala
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
package scalauv
import LibUv.*
import scalanative.unsafe.*
import scalanative.unsigned.*
import java.nio.charset.StandardCharsets
import org.junit.Test
import org.junit.Assert.*
import scala.scalanative.libc.string
import java.nio.file.Files
import java.nio.file.Path
final class FileSpec {
@Test
def readFile(): Unit = {
val expected = "Hello, world!"
val filename = c"src/test/resources/test.txt"
Zone {
val loop = uv_default_loop()
val BufSize: CSize = 1024.toUInt
val buffer = alloc[Byte](BufSize)
val readFileHandle = FileReq
.use { openReq =>
uv_fs_open(
loop,
openReq,
filename,
FileOpenFlags.O_RDONLY,
0,
null
)
}
.checkErrorThrowIO()
val bytesRead = FileReq
.use { readReq =>
val iov = IOVector.stackAllocateForBuffer(buffer, BufSize.toUInt)
uv_fs_read(
loop,
readReq,
readFileHandle,
iov.nativeBuffers,
iov.nativeNumBuffers,
-1,
null
)
}
.checkErrorThrowIO()
val readText = fromCString(buffer, StandardCharsets.UTF_8)
FileReq
.use { closeReq =>
uv_fs_close(loop, closeReq, readFileHandle, null)
}
.checkErrorThrowIO()
assertEquals(13, bytesRead)
assertEquals(expected, readText)
}
}
@Test
def readNonExistentFileReturnsError() = {
val loop = uv_default_loop()
val result = FileReq
.use { openReq =>
uv_fs_open(
loop,
openReq,
c"i_do_not_exist.fake",
FileOpenFlags.O_RDONLY,
0,
null
)
}
assertEquals("error code", ErrorCodes.ENOENT, result)
}
@Test
def writeNewFile(): Unit = {
val text = "my country is the world, and my religion is to do good"
val filename = "src/test/resources/write-test.txt"
Zone {
val loop = uv_default_loop()
val cText = toCString(text, StandardCharsets.UTF_8)
val cFilename = toCString(filename, StandardCharsets.UTF_8)
val fileHandle = FileReq
.use { openReq =>
uv_fs_open(
loop,
openReq,
cFilename,
FileOpenFlags.O_WRONLY | FileOpenFlags.O_CREAT | FileOpenFlags.O_TRUNC,
CreateMode.S_IRUSR | CreateMode.S_IWUSR,
null
)
}
.checkErrorThrowIO()
val bytesWritten = FileReq
.use { writeReq =>
val iov =
IOVector.stackAllocateForBuffer(cText, string.strlen(cText).toUInt)
uv_fs_write(
loop,
writeReq,
fileHandle,
iov.nativeBuffers,
iov.nativeNumBuffers,
-1,
null
)
}
.checkErrorThrowIO()
FileReq
.use { closeReq =>
uv_fs_close(loop, closeReq, fileHandle, null)
}
.checkErrorThrowIO()
val path = Path.of(filename)
val actualText = Files.readString(path)
FileReq.use { deleteReq =>
uv_fs_unlink(loop, deleteReq, cFilename, null)
}
val exists = Files.exists(path)
assertEquals(text.length(), bytesWritten)
assertEquals(text, actualText)
assertFalse("created file should be deleted", exists)
}
}
}