Skip to content

Commit dcc7070

Browse files
authored
Added Path.rename (#379)
1 parent de70a27 commit dcc7070

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

ci/expect_scripts/path-test.exp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,40 @@ expect "Testing Path functions..." {
8080
expect -re "Second file inode: \\\[\"\\d+\"\\\]" {
8181
expect "Inodes are equal: Bool.true" {
8282
expect "" {
83-
84-
# Cleanup phase
85-
expect "Cleaning up test files..." {
86-
expect "Files to clean up:" {
87-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_bytes\\.txt" {
88-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_hardlink\\.txt" {
89-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_json\\.json" {
90-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_original\\.txt" {
91-
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_utf8\\.txt" {
92-
expect "" {
93-
expect "Files remaining after cleanup: Bool.false" {
94-
expect "" {
95-
96-
# Final completion message
97-
expect "I ran all Path function tests." {
98-
expect eof {
99-
check_exit_and_segfault
83+
expect "Testing Path.rename!:" {
84+
expect "✓ Original file no longer exists" {
85+
expect "✓ Renamed file exists" {
86+
expect "✓ Renamed file has correct content" {
87+
expect "" {
88+
# Cleanup phase
89+
expect "Cleaning up test files..." {
90+
expect "Files to clean up:" {
91+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_bytes\\.txt" {
92+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_hardlink\\.txt" {
93+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_json\\.json" {
94+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_original\\.txt" {
95+
expect -re "-rw-r--r-- \\d+ \\w+ \\w+ \\d+ \\w+ +\\d+ \\d+:\\d+ test_path_utf8\\.txt" {
96+
expect "" {
97+
expect "Files remaining after cleanup: Bool.false" {
98+
expect "" {
99+
100+
# Final completion message
101+
expect "I ran all Path function tests." {
102+
expect eof {
103+
check_exit_and_segfault
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
100110
}
101111
}
102112
}
103113
}
104114
}
105115
}
106116
}
107-
}
108117
}
109118
}
110119
}

platform/Path.roc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module [
2121
delete_empty!,
2222
delete_all!,
2323
hard_link!,
24+
rename!,
2425
]
2526

2627
import InternalPath
@@ -394,3 +395,13 @@ hard_link! = |path_original, path_link|
394395
Host.hard_link!(InternalPath.to_bytes(path_original), InternalPath.to_bytes(path_link))
395396
|> Result.map_err(InternalIOErr.handle_err)
396397
|> Result.map_err(LinkErr)
398+
399+
## Renames a file or directory.
400+
##
401+
## This uses [rust's std::fs::rename](https://doc.rust-lang.org/std/fs/fn.rename.html).
402+
rename! : Path, Path => Result {} [PathErr IOErr]
403+
rename! = |from, to|
404+
from_path_bytes = InternalPath.to_bytes(from)
405+
to_path_bytes = InternalPath.to_bytes(to)
406+
Host.file_rename!(from_path_bytes, to_path_bytes)
407+
|> Result.map_err(|err| PathErr(InternalIOErr.handle_err(err)))

0 commit comments

Comments
 (0)