Skip to content

Commit 05a26b5

Browse files
authored
Make sure the thrown exception for mkdir is "ALREADY_EXISTS" (#91)
1 parent b1fd801 commit 05a26b5

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/directory.toit

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ The default directory separator for the underlying operating system.
1313
*/
1414
SEPARATOR/string ::= (system.platform == system.PLATFORM-WINDOWS) ? "\\" : "/"
1515

16-
/** Removes an empty directory. */
16+
/**
17+
Removes an empty directory.
18+
19+
Throws a "FILE_NOT_FOUND" exception if the path does not exist or
20+
is not a directory.
21+
*/
1722
rmdir path/string -> none:
1823
#primitive.file.rmdir
1924

@@ -24,14 +29,17 @@ Does not follow symlinks, but removes the symlink itself.
2429
2530
If $force is true, also deletes files in read-only directories. This only
2631
has an effect when $recursive is set.
32+
33+
Throws a "FILE_NOT_FOUND" exception if the path does not exist or
34+
is not a directory.
2735
*/
2836
rmdir path/string --recursive/bool --force/bool=false -> none:
2937
if not recursive:
3038
rmdir path
3139
return
3240
dir-stat := file.stat --no-follow-links path
33-
if dir-stat[file.ST-TYPE] != file.DIRECTORY:
34-
throw "NOT_A_DIRECTORY"
41+
if not dir-stat or dir-stat[file.ST-TYPE] != file.DIRECTORY:
42+
throw "FILE_NOT_FOUND"
3543

3644
// A queue of directories to delete. Each entry is a pair of a path and a
3745
// boolean indicating whether the directory is known to be empty.

tests/mkdir_rmdir_fail_test.toit

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2025 Toit contributors.
2+
// Use of this source code is governed by a Zero-Clause BSD license that can
3+
// be found in the tests/TESTS_LICENSE file.
4+
5+
import expect show *
6+
import system
7+
8+
import host.file
9+
import host.directory show *
10+
11+
with-tmp-dir [block]:
12+
tmp-dir := mkdtemp "/tmp/mkdir-rmdir-test-"
13+
try:
14+
block.call tmp-dir
15+
finally:
16+
rmdir --recursive --force tmp-dir
17+
18+
main:
19+
with-tmp-dir: | tmp-dir |
20+
mkdir "$tmp-dir/foo"
21+
expect-throw "ALREADY_EXISTS": mkdir "$tmp-dir/foo"
22+
23+
not-found-path := "$tmp-dir-/bar"
24+
expect-throw "FILE_NOT_FOUND": rmdir not-found-path
25+
expect-throw "FILE_NOT_FOUND": rmdir --recursive not-found-path
26+
27+
not-dir-path := "$tmp-dir/file.txt"
28+
file.write-contents "not a directory" --path=not-dir-path
29+
expect-throw "FILE_NOT_FOUND": rmdir not-dir-path
30+
expect-throw "FILE_NOT_FOUND": rmdir --recursive not-dir-path

0 commit comments

Comments
 (0)