msy 0.4.2

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 CLI filter flags (--exclude, --include, --filter)
//!
//! These tests verify that filter flags work end-to-end, not just in unit tests.

use std::fs;
use std::process::Command;
use tempfile::TempDir;

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

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

// =============================================================================
// --exclude Tests
// =============================================================================

#[test]
fn test_exclude_flag_basic() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    // Create files
    fs::write(source.path().join("file.txt"), "content").unwrap();
    fs::write(source.path().join("debug.log"), "log").unwrap();
    fs::write(source.path().join("error.log"), "error").unwrap();

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

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

    // .log files should be excluded
    assert!(dest.path().join("file.txt").exists());
    assert!(
        !dest.path().join("debug.log").exists(),
        "*.log should be excluded"
    );
    assert!(
        !dest.path().join("error.log").exists(),
        "*.log should be excluded"
    );
}

#[test]
fn test_exclude_flag_multiple() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

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

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

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

    assert!(dest.path().join("file.txt").exists());
    assert!(!dest.path().join("debug.log").exists());
    assert!(!dest.path().join("cache.tmp").exists());
}

#[test]
fn test_exclude_directory() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    fs::write(source.path().join("file.txt"), "content").unwrap();
    fs::create_dir(source.path().join("node_modules")).unwrap();
    fs::write(source.path().join("node_modules/package.json"), "{}").unwrap();

    // Exclude directory with trailing slash
    let output = Command::new(sy_bin())
        .args([
            "--exclude",
            "node_modules/",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    assert!(dest.path().join("file.txt").exists());
    assert!(
        !dest.path().join("node_modules").exists(),
        "node_modules/ should be excluded"
    );
}

// =============================================================================
// --include Tests
// =============================================================================

#[test]
fn test_include_flag_basic() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    fs::write(source.path().join("main.rs"), "fn main() {}").unwrap();
    fs::write(source.path().join("lib.rs"), "pub fn lib() {}").unwrap();
    fs::write(source.path().join("readme.md"), "# Readme").unwrap();

    // Include only .rs files, exclude everything else
    let output = Command::new(sy_bin())
        .args([
            "--include",
            "*.rs",
            "--exclude",
            "*",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    // Only .rs files should be synced
    assert!(
        dest.path().join("main.rs").exists(),
        "*.rs should be included"
    );
    assert!(
        dest.path().join("lib.rs").exists(),
        "*.rs should be included"
    );
    assert!(
        !dest.path().join("readme.md").exists(),
        "*.md should be excluded"
    );
}

#[test]
fn test_include_exclude_order_matters() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

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

    // Include important.log first, then exclude all .log
    let output = Command::new(sy_bin())
        .args([
            "--include",
            "important.log",
            "--exclude",
            "*.log",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    // important.log should be included (first rule matches)
    assert!(
        dest.path().join("important.log").exists(),
        "important.log should be included (first match wins)"
    );
    // debug.log should be excluded (second rule matches)
    assert!(
        !dest.path().join("debug.log").exists(),
        "debug.log should be excluded"
    );
    assert!(dest.path().join("file.txt").exists());
}

// =============================================================================
// --filter Tests (rsync syntax)
// =============================================================================

#[test]
fn test_filter_flag_include_syntax() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    fs::write(source.path().join("main.rs"), "fn main() {}").unwrap();
    fs::write(source.path().join("test.py"), "print('test')").unwrap();

    // rsync-style filter: + for include
    let output = Command::new(sy_bin())
        .args([
            "--filter",
            "+ *.rs",
            "--filter",
            "- *",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    assert!(dest.path().join("main.rs").exists());
    assert!(!dest.path().join("test.py").exists());
}

#[test]
fn test_filter_flag_exclude_syntax() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

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

    // rsync-style filter: - for exclude
    let output = Command::new(sy_bin())
        .args([
            "--filter",
            "- *.log",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

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

// =============================================================================
// --exclude-from / --include-from Tests
// =============================================================================

#[test]
fn test_exclude_from_file() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    let exclude_file = TempDir::new().unwrap();
    setup_git_repo(&source);

    // Create exclude patterns file
    let patterns_path = exclude_file.path().join("excludes.txt");
    fs::write(&patterns_path, "*.log\n*.tmp\n# comment\nbuild/\n").unwrap();

    // Create source files
    fs::write(source.path().join("file.txt"), "content").unwrap();
    fs::write(source.path().join("debug.log"), "log").unwrap();
    fs::write(source.path().join("cache.tmp"), "tmp").unwrap();
    fs::create_dir(source.path().join("build")).unwrap();
    fs::write(source.path().join("build/out.txt"), "out").unwrap();

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

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

    assert!(dest.path().join("file.txt").exists());
    assert!(!dest.path().join("debug.log").exists());
    assert!(!dest.path().join("cache.tmp").exists());
    assert!(!dest.path().join("build").exists());
}

#[test]
fn test_include_from_file() {
    // Note: --include-from is processed AFTER --exclude, so to use include-from
    // with exclude, use --filter for explicit ordering, or use --exclude-from
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    let filter_file = TempDir::new().unwrap();
    setup_git_repo(&source);

    // Create filter patterns file with rsync syntax (order preserved within file)
    let patterns_path = filter_file.path().join("filters.txt");
    fs::write(&patterns_path, "+ *.rs\n+ *.toml\n- *\n").unwrap();

    // Create source files
    fs::write(source.path().join("main.rs"), "fn main() {}").unwrap();
    fs::write(source.path().join("Cargo.toml"), "[package]").unwrap();
    fs::write(source.path().join("readme.md"), "# Readme").unwrap();

    // Use --filter to load rules from file (preserves order)
    // Note: sy doesn't have --filter-from, so we load rules inline
    let output = Command::new(sy_bin())
        .args([
            "--filter",
            "+ *.rs",
            "--filter",
            "+ *.toml",
            "--filter",
            "- *",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    assert!(dest.path().join("main.rs").exists());
    assert!(dest.path().join("Cargo.toml").exists());
    assert!(!dest.path().join("readme.md").exists());
}

// =============================================================================
// Complex Filter Scenarios
// =============================================================================

#[test]
fn test_nested_directory_exclusion() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    // Create nested structure
    fs::create_dir_all(source.path().join("src/components")).unwrap();
    fs::write(source.path().join("src/main.rs"), "fn main() {}").unwrap();
    fs::write(
        source.path().join("src/components/button.rs"),
        "pub struct Button;",
    )
    .unwrap();
    fs::create_dir_all(source.path().join("target/debug")).unwrap();
    fs::write(source.path().join("target/debug/binary"), "binary").unwrap();

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

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

    assert!(dest.path().join("src/main.rs").exists());
    assert!(dest.path().join("src/components/button.rs").exists());
    assert!(
        !dest.path().join("target").exists(),
        "target/ should be excluded"
    );
}

#[test]
fn test_basename_vs_path_matching() {
    let source = TempDir::new().unwrap();
    let dest = TempDir::new().unwrap();
    setup_git_repo(&source);

    // Create structure to test basename matching
    fs::create_dir_all(source.path().join("dir1")).unwrap();
    fs::create_dir_all(source.path().join("dir2")).unwrap();
    fs::write(source.path().join("test.log"), "root log").unwrap();
    fs::write(source.path().join("dir1/test.log"), "dir1 log").unwrap();
    fs::write(source.path().join("dir2/other.log"), "dir2 log").unwrap();

    // Pattern without '/' should match basename anywhere
    let output = Command::new(sy_bin())
        .args([
            "--exclude",
            "test.log",
            &format!("{}/", source.path().display()),
            dest.path().to_str().unwrap(),
        ])
        .output()
        .unwrap();

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

    // test.log should be excluded everywhere (basename match)
    assert!(
        !dest.path().join("test.log").exists(),
        "test.log in root should be excluded"
    );
    assert!(
        !dest.path().join("dir1/test.log").exists(),
        "test.log in subdir should be excluded"
    );
    // other.log should exist (different name)
    assert!(dest.path().join("dir2/other.log").exists());
}