1 #![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
2 
3 use std::{env, process};
4 use test_programs::preview1::{assert_errno, create_file, open_scratch_directory};
5 
test_remove_directory(dir_fd: wasip1::Fd)6 unsafe fn test_remove_directory(dir_fd: wasip1::Fd) {
7     // Create a directory in the scratch directory.
8     wasip1::path_create_directory(dir_fd, "dir").expect("creating a directory");
9 
10     // Test that removing it succeeds.
11     wasip1::path_remove_directory(dir_fd, "dir")
12         .expect("remove_directory on a directory should succeed");
13 
14     // There isn't consistient behavior across operating systems of whether removing with a
15     // directory where the path has a trailing slash succeeds or fails, so we won't test
16     // that behavior.
17 
18     // Create a temporary file.
19     create_file(dir_fd, "file");
20 
21     // Test that removing it with no trailing slash fails.
22     assert_errno!(
23         wasip1::path_remove_directory(dir_fd, "file")
24             .expect_err("remove_directory without a trailing slash on a file should fail"),
25         wasip1::ERRNO_NOTDIR
26     );
27 
28     // Test that removing it with a trailing slash fails.
29     assert_errno!(
30         wasip1::path_remove_directory(dir_fd, "file/")
31             .expect_err("remove_directory with a trailing slash on a file should fail"),
32         wasip1::ERRNO_NOTDIR
33     );
34 
35     wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
36 }
37 
main()38 fn main() {
39     let mut args = env::args();
40     let prog = args.next().unwrap();
41     let arg = if let Some(arg) = args.next() {
42         arg
43     } else {
44         eprintln!("usage: {prog} <scratch directory>");
45         process::exit(1);
46     };
47 
48     // Open scratch directory
49     let dir_fd = match open_scratch_directory(&arg) {
50         Ok(dir_fd) => dir_fd,
51         Err(err) => {
52             eprintln!("{err}");
53             process::exit(1)
54         }
55     };
56 
57     // Run the tests.
58     unsafe { test_remove_directory(dir_fd) }
59 }
60