tomo/test/paths.tm

131 lines
2.8 KiB
Plaintext
Raw Permalink Normal View History

2024-09-08 23:43:15 -07:00
# Tests for file paths
2025-04-06 13:07:23 -07:00
func main()
>> (/).exists()
2024-09-08 23:43:15 -07:00
= yes
>> (~/).exists()
2024-09-08 23:43:15 -07:00
= yes
2024-09-09 12:03:51 -07:00
>> (~/Downloads/file(1).txt)
= (~/Downloads/file(1).txt)
2024-09-09 12:03:51 -07:00
>> (/half\)paren)
= (/half\)paren)
2024-09-09 12:03:51 -07:00
2024-09-09 11:37:13 -07:00
>> filename := "example.txt"
>> (~).child(filename)
= (~/example.txt)
2024-09-09 11:37:13 -07:00
>> tmpdir := (/tmp/tomo-test-path-XXXXXX).unique_directory()
>> (/tmp).subdirectories().has(tmpdir)
2024-09-08 23:43:15 -07:00
= yes
>> tmpfile := (tmpdir++(./one.txt))
>> tmpfile.write("Hello world")
>> tmpfile.append("!")
>> tmpfile.read()
= "Hello world!"?
>> tmpfile.read_bytes()!
2025-04-04 22:08:12 -07:00
= [0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21]
>> tmpdir.files().has(tmpfile)
2024-09-08 23:43:15 -07:00
= yes
2025-04-06 13:49:40 -07:00
if tmp_lines := tmpfile.by_line() then
2024-09-15 12:53:38 -07:00
>> [line for line in tmp_lines]
= ["Hello world!"]
2025-04-06 13:07:23 -07:00
else
2024-09-15 12:53:38 -07:00
fail("Couldn't read lines in $tmpfile")
>> (./does-not-exist.xxx).read()
2025-04-04 22:07:09 -07:00
= none
>> (./does-not-exist.xxx).read_bytes()
2025-04-04 22:07:09 -07:00
= none
2025-04-06 13:49:40 -07:00
if lines := (./does-not-exist.xxx).by_line() then
2024-09-15 12:53:38 -07:00
fail("I could read lines in a nonexistent file")
2025-04-06 13:07:23 -07:00
else
2024-09-15 12:53:38 -07:00
pass
>> tmpfile.remove()
2024-09-08 23:43:15 -07:00
>> tmpdir.files().has(tmpfile)
2024-09-08 23:43:15 -07:00
= no
>> tmpdir.remove()
2024-09-08 23:43:15 -07:00
2024-09-09 00:52:56 -07:00
>> p := (/foo/baz.x/qux.tar.gz)
>> p.base_name()
2024-09-09 00:52:56 -07:00
= "qux.tar.gz"
>> p.parent()
= (/foo/baz.x)
>> p.extension()
2024-09-09 00:52:56 -07:00
= "tar.gz"
>> p.extension(full=no)
2024-09-09 00:52:56 -07:00
= "gz"
>> (~/.foo).extension()
2024-09-09 00:52:56 -07:00
= ""
>> (~/foo).extension()
2024-09-09 00:52:56 -07:00
= ""
2024-09-09 01:49:02 -07:00
>> (~/.foo.baz.qux).extension()
2024-09-09 01:49:02 -07:00
= "baz.qux"
>> (/).parent()
= (/)
>> (~/x/.).parent()
= (~)
>> (~/x).parent()
= (~)
>> (.).parent()
= (..)
>> (..).parent()
= (../..)
>> (../foo).parent()
= (..)
# Concatenation tests:
2025-04-06 10:40:17 -07:00
say("Basic relative path concatenation:")
>> (/foo) ++ (./baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Concatenation with a current directory (`.`):")
>> (/foo/bar) ++ (./.)
= (/foo/bar)
2025-04-06 10:40:17 -07:00
say("Trailing slash in the first path:")
>> (/foo/) ++ (./baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Trailing slash in the second path:")
>> (/foo/bar) ++ (./baz/)
= (/foo/bar/baz)
2025-04-06 10:40:17 -07:00
say("Removing redundant current directory (`.`):")
>> (/foo/bar) ++ (./baz/./qux)
= (/foo/bar/baz/qux)
2025-04-06 10:40:17 -07:00
say("Removing redundant parent directory (`..`):")
>> (/foo/bar) ++ (./baz/qux/../quux)
= (/foo/bar/baz/quux)
2025-04-06 10:40:17 -07:00
say("Collapsing `..` to navigate up:")
>> (/foo/bar/baz) ++ (../qux)
= (/foo/bar/qux)
2025-04-06 10:40:17 -07:00
say("Current directory and parent directory mixed:")
>> (/foo/bar) ++ (././../baz)
= (/foo/baz)
2025-04-06 10:40:17 -07:00
say("Path begins with a `.`:")
>> (/foo) ++ (./baz/../qux)
= (/foo/qux)
2025-04-06 10:40:17 -07:00
say("Multiple slashes:")
>> (/foo) ++ (./baz//qux)
= (/foo/baz/qux)
2025-04-06 10:40:17 -07:00
say("Complex path with multiple `.` and `..`:")
>> (/foo/bar/baz) ++ (./.././qux/./../quux)
= (/foo/bar/quux)
2025-04-06 10:40:17 -07:00
say("Globbing:")
>> (./*.tm).glob()