Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-canvas

Source of release 0.5.1

0.5.1 / src / tiles / scratch_tests.lucb

# The scratch folder's cleanup: which folders a sweep may take, and taking them.

test "only folders named as ours, holding at most a scratch file, are stale":
    assert(is_scratch_folder(".luce-0123456789abcdef0123456789abcdef"))
    assert(not is_scratch_folder(".luce-0123456789ABCDEF0123456789abcdef"))
    assert(not is_scratch_folder(".luce-0123"))
    assert(not is_scratch_folder("luce-0123456789abcdef0123456789abcdef0"))
    assert(not is_scratch_folder(".other0123456789abcdef0123456789abcdef"))
    var none_inside: str[]
    assert(only_scratch(none_inside))
    assert(only_scratch(["tiles.scratch"]))
    assert(not only_scratch(["notes.txt"]))
    assert(not only_scratch(["tiles.scratch", "notes.txt"]))

## Write `text` to the file `name` in `folder`.
func write_in(folder: str, name: str, text: str) -> !:
    let path = try paths.join(folder, name)
    defer path.release()
    try files.write((c.str)path.value, text.bytes)

test "a stale folder with its scratch file goes; one holding anything else stays":
    let root = try files.create_temporary_directory((c.str)temporary_root())
    defer root.release()
    let stale = try paths.join(root.value, ".luce-00000000000000000000000000000001")
    defer stale.release()
    try files.ensure_directory(stale.value)
    try write_in(stale.value, "tiles.scratch", "tiles")
    let kept = try paths.join(root.value, ".luce-00000000000000000000000000000002")
    defer kept.release()
    try files.ensure_directory(kept.value)
    try write_in(kept.value, "tiles.scratch", "tiles")
    try write_in(kept.value, "notes.txt", "mine")
    let empty = try paths.join(root.value, ".luce-00000000000000000000000000000003")
    defer empty.release()
    try files.ensure_directory(empty.value)
    remove_stale_folder(stale.value)
    remove_stale_folder(kept.value)
    remove_stale_folder(empty.value)
    assert(not files.exists((c.str)stale.value))
    assert(files.exists((c.str)kept.value))
    assert(not files.exists((c.str)empty.value))
    try files.remove_tree((c.str)root.value)

test "releasing the scratch file lets it and its folder go":
    _ = try scratch_file()
    assert(scratch != none)
    release_scratch()
    assert(scratch == none and scratch_folder == none)