feat: add file create, delete, writeBytes and FileFlags class to IO module

Implement File.create() and File.delete() static methods with async libuv operations, add writeBytes() instance method for writing at offsets, introduce FileFlags constants class mirroring POSIX open() flags, refactor File.open() to accept flag parameters via openWithFlags(), and update documentation and module registration accordingly.
This commit is contained in:
Bob Nystrom
2016-02-20 17:23:42 +00:00
parent 38f48fbce1
commit 60fc3ff59c
21 changed files with 463 additions and 52 deletions
+22
View File
@@ -0,0 +1,22 @@
import "io" for File
// Create a new file.
var file = File.create("file.temp")
System.print(file.isOpen) // expect: true
System.print(file.size) // expect: 0
file.writeBytes("stuff")
file.close()
System.print(File.size("file.temp")) // expect: 5
// Overwrite a file.
file = File.create("file.temp")
// Truncates.
System.print(file.size) // expect: 0
file.close()
File.delete("file.temp")
// TODO: Test it cannot be read from.
+25
View File
@@ -0,0 +1,25 @@
import "io" for File
// Create a new file.
var f
File.create("file.temp") {|file|
f = file
System.print(file.isOpen) // expect: true
System.print(file.size) // expect: 0
file.writeBytes("stuff")
}
// Closed on block exit.
System.print(f.isOpen) // expect: false
System.print(File.size("file.temp")) // expect: 5
// Overwrite a file.
File.create("file.temp") {|file|
// Truncates.
System.print(file.size) // expect: 0
}
File.delete("file.temp")
// TODO: Test return value.
+13
View File
@@ -0,0 +1,13 @@
import "io" for File, Stat
// Create a file.
var file = File.create("file.temp")
file.close()
File.delete("file.temp")
// TODO: More graceful API to tell if a file exists.
var error = Fiber.new {
Stat.path("file.temp")
}.try()
System.print(error) // expect: no such file or directory
+3
View File
@@ -0,0 +1,3 @@
import "io" for File
File.delete("nonexistent") // expect runtime error: no such file or directory
+14
View File
@@ -0,0 +1,14 @@
import "io" for File, Stat
// Create a file and don't close it.
var file = File.create("file.temp")
File.delete("file.temp")
file.close()
// TODO: More graceful API to tell if a file exists.
var error = Fiber.new {
Stat.path("file.temp")
}.try()
System.print(error) // expect: no such file or directory
+1
View File
@@ -14,3 +14,4 @@ System.print(stash.isOpen) // expect: false
System.print(File.open("test/io/file/open_block.wren") {|file|}) // expect: null
// TODO: Test a fiber aborting inside the block.
// TODO: Test return value.
+12
View File
@@ -0,0 +1,12 @@
import "io" for File
File.create("file.temp") {|file|
// Appends.
file.writeBytes("one")
file.writeBytes("two")
file.writeBytes("three")
}
System.print(File.read("file.temp")) // expect: onetwothree
File.delete("file.temp")
+10
View File
@@ -0,0 +1,10 @@
import "io" for File
var file = File.create("file.temp")
file.close()
System.print(Fiber.new {
file.writeBytes("one")
}.try()) // expect: File is not open.
File.delete("file.temp")
@@ -0,0 +1,9 @@
import "io" for File
System.print(Fiber.new {
File.create("file.temp") {|file|
file.writeBytes(123, 0)
}
}.try()) // expect: Bytes must be a string.
File.delete("file.temp")
+11
View File
@@ -0,0 +1,11 @@
import "io" for File
File.create("file.temp") {|file|
file.writeBytes("prettyshort", 0)
file.writeBytes("agoodbitlonger", 2)
file.writeBytes("short", 3)
}
System.print(File.read("file.temp")) // expect: prashortitlonger
File.delete("file.temp")
@@ -0,0 +1,9 @@
import "io" for File
System.print(Fiber.new {
File.create("file.temp") {|file|
file.writeBytes("", -1)
}
}.try()) // expect: Offset cannot be negative.
File.delete("file.temp")
@@ -0,0 +1,9 @@
import "io" for File
System.print(Fiber.new {
File.create("file.temp") {|file|
file.writeBytes("", 1.2)
}
}.try()) // expect: Offset must be an integer.
File.delete("file.temp")
@@ -0,0 +1,9 @@
import "io" for File
System.print(Fiber.new {
File.create("file.temp") {|file|
file.writeBytes("", "string")
}
}.try()) // expect: Offset must be an integer.
File.delete("file.temp")
+9
View File
@@ -0,0 +1,9 @@
import "io" for File
System.print(Fiber.new {
File.create("file.temp") {|file|
file.writeBytes(123)
}
}.try()) // expect: Bytes must be a string.
File.delete("file.temp")