msy 0.4.7

Modern musl rsync alternative - Fast, parallel file synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Integration tests for file comparison modes
//!
//! These tests verify --ignore-times, --size-only, --checksum, -u/--update,
//! --ignore-existing, and rsync compatibility flags.

use std::fs;
use std::process::Command;
use std::thread;
use std::time::Duration;
use tempfile::TempDir;

fn sy_bin() -> String {
	env!("CARGO_BIN_EXE_sy").to_string()
}

fn setup_git_repo(dir: &TempDir) {
	Command::new("git").args(["init"]).current_dir(dir.path()).output().unwrap();
}

// =============================================================================
// --ignore-times Tests
// =============================================================================

#[test]
fn test_ignore_times_forces_comparison() {
	// --ignore-times should compare files even if mtime matches
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	// Create identical files
	fs::write(source.path().join("file.txt"), "content").unwrap();
	fs::write(dest.path().join("file.txt"), "content").unwrap();

	// Make mtimes match
	let source_meta = fs::metadata(source.path().join("file.txt")).unwrap();
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_last_modification_time(&source_meta)).unwrap();

	// First sync without --ignore-times should skip (mtime and size match)
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(
		stdout.contains("skipped") || stdout.contains("Skipped") || stdout.contains("Files skipped:     1"),
		"File should be skipped when mtime matches"
	);

	// Now change dest content but keep mtime the same
	fs::write(dest.path().join("file.txt"), "different").unwrap();
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_last_modification_time(&source_meta)).unwrap();

	// With --ignore-times, should detect the difference and update
	let output = Command::new(sy_bin())
		.args(["--ignore-times", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success(), "sy --ignore-times failed: {}", String::from_utf8_lossy(&output.stderr));

	// File should have been updated
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "content", "--ignore-times should update file even when mtime matches");
}

#[test]
fn test_ignore_times_with_identical_files() {
	// --ignore-times with truly identical files should still skip
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	fs::write(source.path().join("file.txt"), "identical").unwrap();
	fs::write(dest.path().join("file.txt"), "identical").unwrap();

	let output = Command::new(sy_bin())
		.args(["--ignore-times", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Content should still be identical (no unnecessary overwrite)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "identical");
}

// =============================================================================
// --size-only Tests
// =============================================================================

#[test]
fn test_size_only_skips_mtime_check() {
	// --size-only should only compare file sizes, not mtime
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	// Create files with same size but different content
	fs::write(source.path().join("file.txt"), "AAAA").unwrap(); // 4 bytes
	fs::write(dest.path().join("file.txt"), "BBBB").unwrap(); // 4 bytes

	// Wait to ensure different mtime
	thread::sleep(Duration::from_millis(100));

	// Touch source to make it newer
	let now = std::time::SystemTime::now();
	filetime::set_file_mtime(source.path().join("file.txt"), filetime::FileTime::from_system_time(now)).unwrap();

	// With --size-only, should NOT update (same size)
	let output = Command::new(sy_bin())
		.args(["--size-only", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success(), "sy --size-only failed: {}", String::from_utf8_lossy(&output.stderr));

	// Content should NOT have changed (size was same)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "BBBB", "--size-only should skip files with same size");
}

#[test]
fn test_size_only_updates_different_size() {
	// --size-only should update files with different sizes
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	fs::write(source.path().join("file.txt"), "longer content").unwrap();
	fs::write(dest.path().join("file.txt"), "short").unwrap();

	let output = Command::new(sy_bin())
		.args(["--size-only", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Content should have been updated (different size)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "longer content", "--size-only should update files with different sizes");
}

// =============================================================================
// --checksum Tests
// =============================================================================

#[test]
fn test_checksum_compares_content() {
	// --checksum should compare file content, not mtime
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	// Create files with same size but different content
	fs::write(source.path().join("file.txt"), "AAAA").unwrap();
	fs::write(dest.path().join("file.txt"), "BBBB").unwrap();

	// Make dest newer (normally would not be updated)
	let future = std::time::SystemTime::now() + Duration::from_secs(3600);
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_system_time(future)).unwrap();

	// With --checksum, should update based on content difference
	let output = Command::new(sy_bin())
		.args(["--checksum", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success(), "sy --checksum failed: {}", String::from_utf8_lossy(&output.stderr));

	// Content should have been updated (checksum different)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "AAAA", "--checksum should update files with different content");
}

#[test]
fn test_checksum_skips_identical_content() {
	// --checksum should skip files with identical content
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	fs::write(source.path().join("file.txt"), "identical").unwrap();
	fs::write(dest.path().join("file.txt"), "identical").unwrap();

	// Make mtimes different
	let past = std::time::SystemTime::now() - Duration::from_secs(3600);
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_system_time(past)).unwrap();

	let output = Command::new(sy_bin())
		.args(["--checksum", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Content should still be identical
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "identical");
}

// =============================================================================
// Mutual Exclusivity Tests
// =============================================================================

#[test]
fn test_comparison_flags_mutually_exclusive() {
	// --ignore-times, --size-only, and --checksum should be mutually exclusive
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Try --ignore-times with --size-only
	let output = Command::new(sy_bin())
		.args(["--ignore-times", "--size-only", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(!output.status.success(), "Should fail with mutually exclusive flags");
	let stderr = String::from_utf8_lossy(&output.stderr);
	assert!(
		stderr.contains("mutually exclusive") || stderr.contains("cannot be used with"),
		"Error message should mention mutual exclusivity: {}",
		stderr
	);
}

// =============================================================================
// Default Behavior Tests
// =============================================================================

#[test]
fn test_default_uses_mtime_and_size() {
	// Default behavior: compare mtime and size
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();
	setup_git_repo(&source);

	fs::write(source.path().join("file.txt"), "content").unwrap();

	// Initial sync
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	assert!(dest.path().join("file.txt").exists());

	// Second sync should skip (same mtime and size)
	let output = Command::new(sy_bin()).args([&format!("{}/", source.path().display()), dest.path().to_str().unwrap()]).output().unwrap();

	assert!(output.status.success());
	let stdout = String::from_utf8_lossy(&output.stdout);

	// Should show file was skipped
	assert!(
		stdout.contains("skipped") || stdout.contains("Skipped") || stdout.contains("Files skipped:     1") || stdout.contains("Files updated:     0"),
		"File should be skipped on second sync with default mode: {}",
		stdout
	);
}

// =============================================================================
// -u/--update Tests (skip if dest is newer)
// =============================================================================

#[test]
fn test_update_skips_newer_dest() {
	// -u/--update should skip files where destination is newer than source
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::write(source.path().join("file.txt"), "source content").unwrap();
	fs::write(dest.path().join("file.txt"), "newer dest content").unwrap();

	// Make dest file newer than source
	let future = std::time::SystemTime::now() + Duration::from_secs(3600);
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_system_time(future)).unwrap();

	// With -u, should skip the file (dest is newer)
	let output = Command::new(sy_bin())
		.args(["-u", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Content should NOT have changed (dest was newer)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "newer dest content", "-u should skip files where dest is newer");
}

#[test]
fn test_update_long_flag() {
	// --update should work the same as -u
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::write(source.path().join("file.txt"), "source").unwrap();
	fs::write(dest.path().join("file.txt"), "dest").unwrap();

	// Make dest newer
	let future = std::time::SystemTime::now() + Duration::from_secs(3600);
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_system_time(future)).unwrap();

	let output = Command::new(sy_bin())
		.args(["--update", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "dest", "--update should skip newer dest files");
}

#[test]
fn test_update_copies_older_dest() {
	// -u should still copy when source is newer than dest
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::write(source.path().join("file.txt"), "new source").unwrap();
	fs::write(dest.path().join("file.txt"), "old dest").unwrap();

	// Make source newer than dest
	thread::sleep(Duration::from_millis(100));
	let now = std::time::SystemTime::now();
	filetime::set_file_mtime(source.path().join("file.txt"), filetime::FileTime::from_system_time(now)).unwrap();

	let past = now - Duration::from_secs(3600);
	filetime::set_file_mtime(dest.path().join("file.txt"), filetime::FileTime::from_system_time(past)).unwrap();

	let output = Command::new(sy_bin())
		.args(["-u", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Content SHOULD have changed (source was newer)
	let dest_content = fs::read_to_string(dest.path().join("file.txt")).unwrap();
	assert_eq!(dest_content, "new source", "-u should update when source is newer");
}

// =============================================================================
// --ignore-existing Tests
// =============================================================================

#[test]
fn test_ignore_existing_skips_existing_files() {
	// --ignore-existing should skip files that already exist in dest
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::write(source.path().join("existing.txt"), "source version").unwrap();
	fs::write(source.path().join("new.txt"), "new file").unwrap();
	fs::write(dest.path().join("existing.txt"), "dest version").unwrap();

	let output = Command::new(sy_bin())
		.args(["--ignore-existing", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success());

	// Existing file should NOT have changed
	let existing_content = fs::read_to_string(dest.path().join("existing.txt")).unwrap();
	assert_eq!(existing_content, "dest version", "--ignore-existing should not overwrite existing files");

	// New file should have been created
	let new_content = fs::read_to_string(dest.path().join("new.txt")).unwrap();
	assert_eq!(new_content, "new file", "--ignore-existing should still create new files");
}

// =============================================================================
// rsync Compatibility Tests
// =============================================================================

#[test]
fn test_rsync_r_flag_accepted() {
	// -r should be silently accepted (sy is always recursive)
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::create_dir_all(source.path().join("subdir")).unwrap();
	fs::write(source.path().join("subdir/file.txt"), "nested").unwrap();

	// -r should work without error
	let output = Command::new(sy_bin())
		.args(["-r", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success(), "-r flag should be accepted: {}", String::from_utf8_lossy(&output.stderr));

	// Nested file should exist (sy is always recursive)
	assert!(dest.path().join("subdir/file.txt").exists());
}

#[test]
fn test_rsync_avr_combination() {
	// -avr is common rsync muscle memory, should work
	let source = TempDir::new().unwrap();
	let dest = TempDir::new().unwrap();

	fs::write(source.path().join("file.txt"), "content").unwrap();

	let output = Command::new(sy_bin())
		.args(["-avr", &format!("{}/", source.path().display()), dest.path().to_str().unwrap()])
		.output()
		.unwrap();

	assert!(output.status.success(), "-avr should work: {}", String::from_utf8_lossy(&output.stderr));

	assert!(dest.path().join("file.txt").exists());
}

// =============================================================================
// Short Flag Tests
// =============================================================================

#[test]
fn test_w_short_flag_recognized() {
	// -w should be recognized as --watch (we can't easily test watch behavior,
	// but we can verify the flag is parsed)
	let output = Command::new(sy_bin()).args(["--help"]).output().unwrap();

	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("-w, --watch"), "-w should be short for --watch");
}

#[test]
fn test_z_short_flag_recognized() {
	// -z should be recognized as --compress
	let output = Command::new(sy_bin()).args(["--help"]).output().unwrap();

	let stdout = String::from_utf8_lossy(&output.stdout);
	assert!(stdout.contains("-z, --compress"), "-z should be short for --compress");
}