// Files and directories (std/file, ADR 0049 and issue #471): whole-file IO, // directories, removal and replacement. The original file is never deleted // (both backends may run this program at the same time); each mutation test // uses a unique temporary directory and removes it. import std/file import std/io import std/option import std/os import std/random.{type random} import std/result pub fn main :: -> () / { let tmp = option.unwrap_or (os.env "TMPDIR") "/tmp" let path = tmp <> "/louise_run_file_test.txt" // Never created by this program: `exists` is `False` even on a rerun. let missing = path <> ".nope" io.println (show (file.exists missing)) io.println (show (file.write path "hello file")) io.println (show (file.exists path)) io.println (show (file.size path)) io.println (show (file.read path)) io.println (show (file.read missing)) let root = tmp <> "/louise_run_file_ops_" <> random.token 16 let nested = root <> "/parent/child" let single = root <> "/single" io.println (show (file.make_dirs nested)) io.println (show (file.make_dirs nested)) io.println (show (file.make_dir single)) io.println (show (result.is_error (file.make_dir single))) // Rename over an existing file, not just to an unused name. let original = nested <> "/original.txt" let replacement = nested <> "/replacement.txt" let _ = file.write original "old" let _ = file.write replacement "new" io.println (show (file.rename replacement original)) io.println (show (file.read original)) io.println (show (file.exists replacement)) // `remove` handles files and empty directories, but not recursive trees. let empty = root <> "/empty" io.println (show (file.make_dir empty)) io.println (show (file.remove original)) io.println (show (file.remove empty)) io.println (show (file.exists original)) let tree = root <> "/tree" let blob = tree <> "/a/b/blob.txt" io.println (show (file.make_dirs (tree <> "/a/b"))) let _ = file.write blob "blob" io.println (show (result.is_error (file.remove tree))) io.println (show (file.remove_all tree)) io.println (show (result.is_error (file.list_dir tree))) io.println (show (file.remove_all root)) } //> False //> Ok(()) //> True //> Ok(10) //> Ok(hello file) //> Error(No such file or directory (os error 2)) //> Ok(()) //> Ok(()) //> Ok(()) //> True //> Ok(()) //> Ok(new) //> False //> Ok(()) //> Ok(()) //> Ok(()) //> False //> Ok(()) //> True //> Ok(()) //> True //> Ok(())