Debian Codegraph @

tests/by-util/test_unlink.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 use uutests::at_and_ucmd;
7 use uutests::new_ucmd;
8
9 #[test]
10 fn test_invalid_arg() {
11 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
12 }
13
14 #[test]
15 fn test_unlink_file() {
16 let (at, mut ucmd) = at_and_ucmd!();
17 let file = "test_unlink_file";
18
19 at.touch(file);
20
21 ucmd.arg(file).succeeds().no_stderr();
22
23 assert!(!at.file_exists(file));
24 }
25
26 #[test]
27 fn test_unlink_multiple_files() {
28 let (at, mut ucmd) = at_and_ucmd!();
29
30 let file_a = "test_unlink_multiple_file_a";
31 let file_b = "test_unlink_multiple_file_b";
32
33 at.touch(file_a);
34 at.touch(file_b);
35
36 ucmd.arg(file_a)
37 .arg(file_b)
38 .fails()
39 .stderr_contains("Usage");
40 }
41
42 #[test]
43 fn test_unlink_directory() {
44 let (at, mut ucmd) = at_and_ucmd!();
45 let dir = "dir";
46
47 at.mkdir(dir);
48
49 let res = ucmd.arg(dir).fails();
50 let stderr = res.stderr_str();
51 assert!(
52 stderr == "unlink: cannot unlink 'dir': Is a directory\n"
53 || stderr == "unlink: cannot unlink 'dir': Permission denied\n"
54 );
55 }
56
57 #[test]
58 fn test_unlink_nonexistent() {
59 let file = "test_unlink_nonexistent";
60
61 new_ucmd!()
62 .arg(file)
63 .fails()
64 .stderr_is("unlink: cannot unlink 'test_unlink_nonexistent': No such file or directory\n");
65 }
66
67 #[test]
68 fn test_unlink_symlink() {
69 let (at, mut ucmd) = at_and_ucmd!();
70
71 at.touch("foo");
72 at.symlink_file("foo", "bar");
73
74 ucmd.arg("bar").succeeds().no_stderr();
75
76 assert!(at.file_exists("foo"));
77 assert!(!at.file_exists("bar"));
78 }
79
80 #[test]
81 #[cfg(target_os = "linux")]
82 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
83 fn test_unlink_non_utf8_paths() {
84 use std::ffi::OsStr;
85 use std::os::unix::ffi::OsStrExt;
86
87 let (at, mut ucmd) = at_and_ucmd!();
88 // Create a test file with non-UTF-8 bytes in the name
89 let non_utf8_bytes = b"test_\xFF\xFE.txt";
90 let non_utf8_name = OsStr::from_bytes(non_utf8_bytes);
91
92 at.touch(non_utf8_name);
93 assert!(at.file_exists(non_utf8_name));
94
95 // Test that unlink handles non-UTF-8 file names without crashing
96 ucmd.arg(non_utf8_name).succeeds();
97
98 assert!(!at.file_exists(non_utf8_name));
99 }