Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.3

0.7.3 / tests / files / files.lucb

## The `files` module against its archive: a file written, read back, described and
## removed, in a temporary place.
import thread

test "a file round trip":
    let directory = io.path.temp()
    let path = try strings.join([directory, "/luce-base-files-test.txt"], "")
    defer strings.release(path)
    try write((c.str)path, "one line\n".bytes)
    assert(exists((c.str)path))
    let back = try read((c.str)path)
    defer back.release()
    assert((str)back.value == "one line\n")
    let info = try metadata((c.str)path)
    assert(info.size == 9)
    try remove_file((c.str)path)
    assert(not exists((c.str)path))

test "a temporary file loses its name and still reads back":
    let directory = io.path.temp()
    let path = try strings.join([directory, "/luce-base-files-temporary.bin"], "")
    defer strings.release(path)
    var opened = try File.open((c.str)path, OpenMode.create_temporary, permissions = 384)
    # POSIX: no name left to find; the open file lives on until it closes. Windows keeps
    # the name until the last handle closes (FILE_FLAG_DELETE_ON_CLOSE).
    assert(exists((c.str)path) == platform.windows)
    assert((try opened.write("scratch".bytes)) == 7)
    var back: u8[7]
    _ = try opened.seek(0, SeekOrigin.start)
    assert((try opened.read(back)) == 7)
    assert((str)back[..<7] == "scratch")
    try opened.close()
    assert(not exists((c.str)path))

func discard_temporary(context: void*?):
    let made = (TemporaryFile*)(context else trap("no temporary file"))
    made.destroy()

test "a temporary file made on one thread is discarded on another":
    var made: TemporaryFile
    with memory.heap:
        made = try TemporaryFile.create((c.str)io.path.temp())
    let name = try strings.copy(made.path())
    defer strings.release(name)
    var worker = try thread.spawn(discard_temporary, (void*)&made)
    try worker.join()
    assert(not exists((c.str)name))

test "canonical paths resolve links, and a missing entry is missing":
    let base = try canonical((c.str)io.path.temp())
    defer base.release()
    let created = try create_temporary_directory((c.str)base.value)
    defer created.release()
    let root = created.value
    defer remove_tree((c.str)root) catch ignored:
        _ = ignored
    let real = try strings.join([root, "/real"], "")
    try create_directory((c.str)real)
    let file = try strings.join([real, "/file.txt"], "")
    try write((c.str)file, "x".bytes)
    let link = try strings.join([root, "/link"], "")
    try create_symlink((c.str)real, (c.str)link, true)
    # through the link, and through a `..` that leaves it, the file's own path comes back
    let through = try strings.join([link, "/file.txt"], "")
    assert(try canonical_text((c.str)through) == file)
    let around = try strings.join([link, "/../real/file.txt"], "")
    assert(try canonical_text((c.str)around) == file)
    assert(try canonical_text((c.str)link) == real)
    let absent = try strings.join([root, "/absent.txt"], "")
    let answer = canonical_text((c.str)absent) catch failure:
        assert(failure.code == missing)
        recover "missing"
    assert(answer == "missing")

test "a listing and a walk own their names and sort them":
    var temporary = try TemporaryDirectory()
    defer temporary.destroy()
    let root = temporary.path()
    let inner = try strings.join([root, "/b"], "")
    try create_directory((c.str)inner)
    let deep = try strings.join([root, "/b/c.txt"], "")
    try write((c.str)deep, "".bytes)
    let top = try strings.join([root, "/a.txt"], "")
    try write((c.str)top, "".bytes)
    let names = try list((c.str)root)
    defer names.release()
    assert(names.value.length == 2 and names.value[0] == "a.txt" and names.value[1] == "b")
    let found = try walk((c.str)root)
    defer found.release()
    assert(found.value.length == 3 and found.value[1] == "b" and found.value[2] == "b/c.txt")

test "a file reads lines, text and bytes, and writes where the reader stands":
    var temporary = try TemporaryDirectory()
    defer temporary.destroy()
    let path = try strings.join([temporary.path(), "/lines.txt"], "")
    try write((c.str)path, "one\r\ntwo\nthree".bytes)
    var file = try File.open((c.str)path, OpenMode.read_write)
    defer file.destroy()
    assert(try file.read_line() == "one")
    assert(try file.read_text(2) == "tw")
    try file.write_text("O")
    assert(try file.read_line() == "")
    assert(try file.read_line() == "three")
    assert(try file.read_line() == none)
    _ = try file.seek(0)
    assert((str)(try file.read_bytes()) == "one\r\ntwO\nthree")

## A canonical path as a text of the current allocator, for comparing.
func canonical_text(path: c.str) -> str!:
    let resolved = try canonical(path)
    defer resolved.release()
    return try strings.copy(resolved.value)

test "a path longer than Windows' MAX_PATH is made, written, read and removed":
    let base = try strings.join([io.path.temp(), "/luce-std-files-long-path"], "")
    defer strings.release(base)
    try remove_tree((c.str)base)
    # 13 levels of 31 characters: over 400 characters below the temporary directory
    var levels: str[14]
    levels[0] = base
    for index in 1..<levels.length:
        levels[index] = "a-directory-name-of-some-length"
    let deep = try strings.join(levels, "/")
    defer strings.release(deep)
    assert(deep.length > 400)
    try ensure_directory(deep)
    let file = try strings.join([deep, "/data.txt"], "")
    defer strings.release(file)
    try write((c.str)file, "deep".bytes)
    assert(exists((c.str)file))
    let back = try read((c.str)file)
    defer back.release()
    assert((str)back.value == "deep")
    try remove_tree((c.str)base)
    assert(not exists((c.str)base))