Debian Codegraph @

tests/by-util/test_sync.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 std::fs;
7 use tempfile::tempdir;
8 use uutests::new_ucmd;
9
10 #[test]
11 fn test_invalid_arg() {
12 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
13 }
14
15 #[test]
16 fn test_sync_default() {
17 new_ucmd!().succeeds();
18 }
19
20 #[test]
21 fn test_sync_incorrect_arg() {
22 new_ucmd!().arg("--foo").fails();
23 }
24
25 #[test]
26 fn test_sync_fs() {
27 let temporary_directory = tempdir().unwrap();
28 let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
29 new_ucmd!()
30 .arg("--file-system")
31 .arg(&temporary_path)
32 .succeeds();
33 }
34
35 #[test]
36 fn test_sync_fs_without_files_falls_back_to_full_sync() {
37 new_ucmd!().arg("--file-system").succeeds();
38 }
39
40 #[test]
41 fn test_sync_data() {
42 // Todo add a second arg
43 let temporary_directory = tempdir().unwrap();
44 let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
45 new_ucmd!().arg("--data").arg(&temporary_path).succeeds();
46 }
47
48 #[test]
49 fn test_sync_no_existing_files() {
50 new_ucmd!()
51 .arg("--data")
52 .arg("do-no-exist")
53 .fails()
54 .stderr_contains("error opening");
55 }
56
57 #[test]
58 fn test_sync_data_but_not_file() {
59 new_ucmd!()
60 .arg("--data")
61 .fails()
62 .stderr_contains("sync: --data needs at least one argument");
63 }
64
65 #[cfg(any(target_os = "linux", target_os = "android"))]
66 #[cfg(feature = "chmod")]
67 #[test]
68 fn test_sync_no_permission_dir() {
69 use uutests::util::TestScenario;
70 use uutests::util_name;
71
72 let ts = TestScenario::new(util_name!());
73 let at = &ts.fixtures;
74 let dir = "foo";
75 at.mkdir_all(dir);
76
77 ts.ccmd("chmod").arg("0").arg(dir).succeeds();
78 let result = ts.ucmd().arg("--data").arg(dir).fails();
79 result.stderr_contains("sync: error opening 'foo': Permission denied");
80 let result = ts.ucmd().arg(dir).fails();
81 result.stderr_contains("sync: error opening 'foo': Permission denied");
82 }
83
84 #[cfg(not(windows))]
85 #[cfg(feature = "chmod")]
86 #[test]
87 fn test_sync_no_permission_file() {
88 use uutests::util::TestScenario;
89 use uutests::util_name;
90
91 let ts = TestScenario::new(util_name!());
92 let at = &ts.fixtures;
93 let f = "file";
94 at.touch(f);
95
96 ts.ccmd("chmod").arg("0200").arg(f).succeeds();
97 ts.ucmd().arg(f).succeeds();
98 }
99
100 #[cfg(any(target_os = "linux", target_os = "android"))]
101 #[test]
102 fn test_sync_data_nonblock_flag_reset() {
103 // Test that O_NONBLOCK flag is properly reset when syncing files
104 use uutests::util::TestScenario;
105 use uutests::util_name;
106
107 let ts = TestScenario::new(util_name!());
108 let at = &ts.fixtures;
109 let test_file = "test_file.txt";
110
111 // Create a test file
112 at.write(test_file, "test content");
113
114 // Run sync --data with the file - should succeed
115 ts.ucmd().arg("--data").arg(test_file).succeeds();
116 }
117
118 #[cfg(any(target_os = "linux", target_os = "android"))]
119 #[test]
120 fn test_sync_fs_nonblock_flag_reset() {
121 // Test that O_NONBLOCK flag is properly reset when syncing filesystems
122 use std::fs;
123 use tempfile::tempdir;
124
125 let temporary_directory = tempdir().unwrap();
126 let temporary_path = fs::canonicalize(temporary_directory.path()).unwrap();
127
128 // Run sync --file-system with the path - should succeed
129 new_ucmd!()
130 .arg("--file-system")
131 .arg(&temporary_path)
132 .succeeds();
133 }
134
135 #[cfg(any(target_os = "linux", target_os = "android"))]
136 #[test]
137 fn test_sync_fdatasync_error_handling() {
138 // Test that fdatasync properly handles file opening errors
139 new_ucmd!()
140 .arg("--data")
141 .arg("/nonexistent/path/to/file")
142 .fails()
143 .stderr_contains("error opening");
144 }
145
146 #[cfg(target_vendor = "apple")]
147 #[test]
148 fn test_sync_syncfs_error_handling_macos() {
149 // Test that syncfs properly handles invalid paths on macOS
150 new_ucmd!()
151 .arg("--file-system")
152 .arg("/nonexistent/path/to/file")
153 .fails()
154 .stderr_contains("error opening");
155 }
156
157 #[test]
158 fn test_sync_multiple_files() {
159 // Test syncing multiple files at once
160 use std::fs;
161 use tempfile::tempdir;
162
163 let temporary_directory = tempdir().unwrap();
164 let temp_path = temporary_directory.path();
165
166 // Create multiple test files
167 let file1 = temp_path.join("file1.txt");
168 let file2 = temp_path.join("file2.txt");
169
170 fs::write(&file1, "content1").unwrap();
171 fs::write(&file2, "content2").unwrap();
172
173 // Sync both files
174 new_ucmd!().arg("--data").arg(&file1).arg(&file2).succeeds();
175 }
176
177 #[test]
178 fn test_sync_multiple_nonexistent_files() {
179 let result = new_ucmd!().arg("--data").arg("bad1").arg("bad2").fails();
180
181 result.stderr_is("sync: error opening 'bad1': No such file or directory\nsync: error opening 'bad2': No such file or directory\n");
182 }
183
184 #[cfg(any(target_os = "linux", target_os = "android"))]
185 #[test]
186 fn test_sync_data_fifo_fails_immediately() {
187 use std::time::Duration;
188 use uutests::util::TestScenario;
189 use uutests::util_name;
190
191 let ts = TestScenario::new(util_name!());
192 let at = &ts.fixtures;
193 at.mkfifo("test-fifo");
194
195 ts.ucmd()
196 .arg("--data")
197 .arg(at.plus_as_string("test-fifo"))
198 .timeout(Duration::from_secs(2))
199 .fails()
200 .stderr_contains("error syncing")
201 .stderr_contains("test-fifo")
202 .stderr_contains("Invalid input");
203 }