tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

79 lignes · 2501 octetsBrut · Historique
1// Files and directories (std/file, ADR 0049 and issue #471): whole-file IO,
2// directories, removal and replacement. The original file is never deleted
3// (both backends may run this program at the same time); each mutation test
4// uses a unique temporary directory and removes it.
5
6import std/file
7import std/io
8import std/option
9import std/os
10import std/random.{type random}
11import std/result
12
13pub fn main :: -> () / <io, random>
14{
15 let tmp = option.unwrap_or (os.env "TMPDIR") "/tmp"
16 let path = tmp <> "/louise_run_file_test.txt"
17 // Never created by this program: `exists` is `False` even on a rerun.
18 let missing = path <> ".nope"
19 io.println (show (file.exists missing))
20 io.println (show (file.write path "hello file"))
21 io.println (show (file.exists path))
22 io.println (show (file.size path))
23 io.println (show (file.read path))
24 io.println (show (file.read missing))
25
26 let root = tmp <> "/louise_run_file_ops_" <> random.token 16
27 let nested = root <> "/parent/child"
28 let single = root <> "/single"
29 io.println (show (file.make_dirs nested))
30 io.println (show (file.make_dirs nested))
31 io.println (show (file.make_dir single))
32 io.println (show (result.is_error (file.make_dir single)))
33
34 // Rename over an existing file, not just to an unused name.
35 let original = nested <> "/original.txt"
36 let replacement = nested <> "/replacement.txt"
37 let _ = file.write original "old"
38 let _ = file.write replacement "new"
39 io.println (show (file.rename replacement original))
40 io.println (show (file.read original))
41 io.println (show (file.exists replacement))
42
43 // `remove` handles files and empty directories, but not recursive trees.
44 let empty = root <> "/empty"
45 io.println (show (file.make_dir empty))
46 io.println (show (file.remove original))
47 io.println (show (file.remove empty))
48 io.println (show (file.exists original))
49 let tree = root <> "/tree"
50 let blob = tree <> "/a/b/blob.txt"
51 io.println (show (file.make_dirs (tree <> "/a/b")))
52 let _ = file.write blob "blob"
53 io.println (show (result.is_error (file.remove tree)))
54 io.println (show (file.remove_all tree))
55 io.println (show (result.is_error (file.list_dir tree)))
56 io.println (show (file.remove_all root))
57}
58//> False
59//> Ok(())
60//> True
61//> Ok(10)
62//> Ok(hello file)
63//> Error(No such file or directory (os error 2))
64//> Ok(())
65//> Ok(())
66//> Ok(())
67//> True
68//> Ok(())
69//> Ok(new)
70//> False
71//> Ok(())
72//> Ok(())
73//> Ok(())
74//> False
75//> Ok(())
76//> True
77//> Ok(())
78//> True
79//> Ok(())