tendrils-core 0.0.4

Core library for Tendrils
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Contains tests specific to link actions.
//! See also [`crate::tests::common_action_tests`].

use crate::path_ext::{PathExt, UniPath};
use crate::test_utils::{
    set_ra,
    symlink_expose,
    Setup,
    uac_enabled,
};
use crate::{
    link_tendril,
    ActionLog,
    FsoType,
    Location,
    Tendril,
    TendrilActionError,
    TendrilActionSuccess,
    TendrilMode,
};
use rstest::rstest;
use std::fs::{create_dir_all, metadata, set_permissions};
use std::path::{Path, PathBuf};

/// See also [`crate::tests::common_action_tests::remote_is_unchanged`] for
/// `dry_run` case
#[rstest]
fn local_exists_symlink_to_local_is_created(
    #[values(true, false)] force: bool,
    #[values(true, false)] as_dir: bool,
) {
    let setup = Setup::new();
    let exp_remote_path;
    let mut tendril;
    if as_dir {
        setup.make_local_nested_file();
        exp_remote_path = setup.remote_dir.clone();
        tendril = setup.dir_tendril();
    }
    else {
        setup.make_local_file();
        exp_remote_path = setup.remote_file.clone();
        tendril = setup.file_tendril();
    }
    tendril.mode = TendrilMode::Link;
    assert!(!setup.remote_file.exists());
    assert!(!setup.remote_dir.exists());

    let actual = link_tendril(&tendril, false, force);

    let exp_local_type;
    if as_dir {
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Local nested file contents"
        );
        assert!(setup.remote_dir.is_symlink());
        exp_local_type = Some(FsoType::Dir);
        assert_eq!(
            std::fs::read_link(setup.remote_dir).unwrap(),
            setup.local_dir,
        );
    }
    else {
        assert_eq!(setup.remote_file_contents(), "Local file contents");
        assert!(setup.remote_file.is_symlink());
        exp_local_type = Some(FsoType::File);
        assert_eq!(
            std::fs::read_link(setup.remote_file).unwrap(),
            setup.local_file,
        );
    }
    assert_eq!(
        actual,
        ActionLog::new(
            exp_local_type,
            None,
            exp_remote_path,
            Ok(TendrilActionSuccess::New),
        )
    );
}

/// Note: This doesn't apply if the local doesn't exist.
/// See [`local_doesnt_exist_but_td_repo_does_copies_remote_to_local_then_links_unless_dryrun`]
#[rstest]
#[case(true)]
#[case(false)]
fn remote_exists_and_is_not_symlink_returns_type_mismatch_error_unless_forced(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_remote_file();
    setup.make_remote_nested_file();
    setup.make_local_file();
    setup.make_local_nested_file();

    let mut file_tendril = setup.file_tendril();
    file_tendril.mode = TendrilMode::Link;

    let mut dir_tendril = setup.dir_tendril();
    dir_tendril.mode = TendrilMode::Link;

    let file_actual =
        link_tendril(&file_tendril, dry_run, force);
    let dir_actual =
        link_tendril(&dir_tendril, dry_run, force);

    let exp_file_result;
    let exp_dir_result;
    match (dry_run, force) {
        (_, false) => {
            exp_file_result = Err(TendrilActionError::TypeMismatch {
                loc: Location::Dest,
                mistype: FsoType::File,
            });
            exp_dir_result = Err(TendrilActionError::TypeMismatch {
                loc: Location::Dest,
                mistype: FsoType::Dir,
            });
        }
        (false, true) => {
            exp_file_result = Ok(TendrilActionSuccess::Overwrite);
            exp_dir_result = Ok(TendrilActionSuccess::Overwrite);
        }
        (true, true) => {
            exp_file_result = Ok(TendrilActionSuccess::OverwriteSkipped);
            exp_dir_result = Ok(TendrilActionSuccess::OverwriteSkipped);
        }
    };

    if force && !dry_run {
        assert!(setup.remote_file.is_symlink());
        assert!(setup.remote_dir.is_symlink());
        assert_eq!(setup.remote_file_contents(), "Local file contents");
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Local nested file contents"
        );
    }
    else {
        assert!(!setup.remote_file.is_symlink());
        assert!(!setup.remote_dir.is_symlink());
        assert_eq!(setup.remote_file_contents(), "Remote file contents");
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Remote nested file contents"
        );
    }
    assert_eq!(
        file_actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::File),
            setup.remote_file,
            exp_file_result,
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_dir,
            exp_dir_result,
        )
    );
}

#[rstest]
fn remote_parent_doesnt_exist_creates_full_parent_structure(
    #[values(true, false)] force: bool,
    #[values(true, false)] as_dir: bool,
) {
    let setup = Setup::new();
    let exp_remote_path;
    let mut tendril;
    if as_dir {
        setup.make_local_subdir_nested_file();
        exp_remote_path = setup.remote_subdir_dir.clone();
        tendril = setup.subdir_dir_tendril();
        assert!(!setup.remote_subdir_dir.parent().unwrap().exists());
    }
    else {
        setup.make_local_subdir_file();
        exp_remote_path = setup.remote_subdir_file.clone();
        tendril = setup.subdir_file_tendril();
        assert!(!setup.remote_subdir_file.parent().unwrap().exists());
    }
    tendril.mode = TendrilMode::Link;

    let actual = link_tendril(&tendril, false, force);

    let exp_local_type;
    if as_dir {
        assert_eq!(
            setup.remote_subdir_nested_file_contents(),
            "Local subdir nested file contents"
        );
        exp_local_type = Some(FsoType::Dir);
        assert_eq!(
            std::fs::read_link(setup.remote_subdir_dir).unwrap(),
            setup.local_subdir_dir,
        );
    }
    else {
        assert_eq!(setup.remote_subdir_file_contents(), "Local subdir file contents");
        exp_local_type = Some(FsoType::File);
        assert_eq!(
            std::fs::read_link(setup.remote_subdir_file).unwrap(),
            setup.local_subdir_file,
        );
    }
    assert_eq!(
        actual,
        ActionLog::new(
            exp_local_type,
            None,
            exp_remote_path,
            Ok(TendrilActionSuccess::New),
        )
    );
}

#[rstest]
#[case(true)]
#[case(false)]
fn local_is_symlink_returns_type_mismatch_error_unless_forced(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_target_file();
    setup.make_target_nested_file();
    create_dir_all(&setup.group_dir).unwrap();
    symlink_expose(&setup.local_file, &setup.target_file, false, false)
        .unwrap();
    symlink_expose(&setup.local_dir, &setup.target_dir, false, false).unwrap();

    let mut file_tendril = setup.file_tendril();
    file_tendril.mode = TendrilMode::Link;

    let mut dir_tendril = setup.dir_tendril();
    dir_tendril.mode = TendrilMode::Link;

    let file_actual =
        link_tendril(&file_tendril, dry_run, force);
    let dir_actual =
        link_tendril(&dir_tendril, dry_run, force);

    let exp_file_result;
    let exp_dir_result;
    match (dry_run, force) {
        (_, false) => {
            exp_file_result = Err(TendrilActionError::TypeMismatch {
                loc: Location::Source,
                mistype: FsoType::SymFile,
            });
            exp_dir_result = Err(TendrilActionError::TypeMismatch {
                loc: Location::Source,
                mistype: FsoType::SymDir,
            });
        }
        (false, true) => {
            exp_file_result = Ok(TendrilActionSuccess::New);
            exp_dir_result = Ok(TendrilActionSuccess::New);
        }
        (true, true) => {
            exp_file_result = Ok(TendrilActionSuccess::NewSkipped);
            exp_dir_result = Ok(TendrilActionSuccess::NewSkipped);
        }
    };

    assert_eq!(
        file_actual,
        ActionLog::new(
            Some(FsoType::SymFile),
            None,
            setup.remote_file.clone(),
            exp_file_result,
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(
            Some(FsoType::SymDir),
            None,
            setup.remote_dir.clone(),
            exp_dir_result,
        )
    );
    assert_eq!(setup.local_file_contents(), "Target file contents");
    assert_eq!(
        setup.local_nested_file_contents(),
        "Target nested file contents"
    );
    assert!(setup.local_file.is_symlink());
    assert!(setup.local_dir.is_symlink());
    assert_eq!(setup.td_repo.read_dir().iter().count(), 1);
    if force && !dry_run {
        assert_eq!(setup.remote_file_contents(), "Target file contents");
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Target nested file contents"
        );
    }
    else {
        assert!(!setup.remote_file.exists());
        assert!(!setup.remote_dir.exists());
    }
}

/// See [`crate::tests::common_action_tests::remote_symlink_is_unchanged]
/// for `dry_run` equivalent
#[rstest]
#[case(true)]
#[case(false)]
fn existing_symlinks_at_remote_are_overwritten(#[case] force: bool) {
    let setup = Setup::new();
    setup.make_local_file();
    setup.make_local_nested_file();
    setup.make_target_file();
    setup.make_target_nested_file();
    symlink_expose(&setup.remote_file, &setup.target_file, false, true)
        .unwrap();
    symlink_expose(&setup.remote_dir, &setup.target_dir, false, true).unwrap();

    let mut file_tendril = setup.file_tendril();
    file_tendril.mode = TendrilMode::Link;

    let mut dir_tendril = setup.dir_tendril();
    dir_tendril.mode = TendrilMode::Link;

    let file_actual =
        link_tendril(&file_tendril, false, force);
    let dir_actual =
        link_tendril(&dir_tendril, false, force);

    assert_eq!(
        file_actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::SymFile),
            setup.remote_file.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::SymDir),
            setup.remote_dir.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    assert!(setup.remote_file.is_symlink());
    assert!(setup.remote_dir.is_symlink());
    assert_eq!(setup.remote_file_contents(), "Local file contents");
    assert_eq!(
        setup.remote_nested_file_contents(),
        "Local nested file contents"
    );

    assert!(setup.remote_file.is_symlink());
    assert!(setup.remote_dir.is_symlink());
    assert_eq!(setup.remote_file_contents(), "Local file contents");
    assert_eq!(
        setup.remote_nested_file_contents(),
        "Local nested file contents"
    );
}

#[rstest]
#[cfg_attr(windows, ignore)] // The Windows syscall reduces these paths
fn symlink_uses_repo_path_exactly_as_given(
    #[values(true, false)] force: bool,
    #[values(true, false)] remote_exists: bool,
) {
    let setup = Setup::new();
    setup.make_local_file();

    // Repo path with redundant components
    let given_repo_path = UniPath::from(
        setup.temp_dir.path().join_raw(Path::new("./././TendrilsRepo/../TendrilsRepo"))
    );
    let tendril = Tendril::new_expose(
        given_repo_path,
        PathBuf::from("SomeApp/misc.txt"),
        setup.remote_file.clone().into(),
        TendrilMode::Link,
    ).unwrap();

    let exp_remote_type;
    let exp_success;
    if remote_exists {
        setup.make_target_file();
        symlink_expose(&setup.remote_file, &setup.target_file, false, true)
            .unwrap();

        exp_remote_type = Some(FsoType::SymFile);
        exp_success = Ok(TendrilActionSuccess::Overwrite);
    }
    else {
        exp_remote_type = None;
        exp_success = Ok(TendrilActionSuccess::New);
    }

    let actual = link_tendril(&tendril, false, force);

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            exp_remote_type,
            setup.remote_file.clone(),
            exp_success,
        )
    );

    assert_eq!(
        std::fs::read_link(setup.remote_file).unwrap(),
        setup.temp_dir.path().join_raw(
            Path::new("./././TendrilsRepo/../TendrilsRepo/SomeApp/misc.txt")
        )
    );
}

#[rstest]
#[case(true)]
#[case(false)]
fn no_read_access_from_local_file_returns_success(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_td_repo_dir();
    setup.make_local_nra_file();

    let tendril = Tendril::new_expose(
        setup.uni_td_repo(),
        "SomeApp/nra.txt".into(),
        setup.remote_nra_file.clone().into(),
        TendrilMode::Link,
    )
    .unwrap();

    let actual = link_tendril(&tendril, dry_run, force);

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::NewSkipped);
    }
    else {
        exp_result = Ok(TendrilActionSuccess::New);
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            None,
            setup.remote_nra_file.clone(),
            exp_result,
        )
    );
    assert_eq!(
        setup.remote_nra_file.exists(),
        !dry_run
    );
}

#[rstest]
#[case(true)]
#[case(false)]
fn no_read_access_from_local_dir_returns_success(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_td_repo_dir();
    setup.make_local_nra_dir();

    let tendril = Tendril::new_expose(
        setup.uni_td_repo(),
        "SomeApp/nra".into(),
        setup.remote_nra_dir.clone().into(),
        TendrilMode::Link,
    )
    .unwrap();

    let actual = link_tendril(&tendril, dry_run, force);

    set_ra(&setup.local_nra_dir, true);
    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::NewSkipped);
    }
    else {
        exp_result = Ok(TendrilActionSuccess::New);
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            None,
            setup.remote_nra_dir.clone(),
            exp_result,
        )
    );
    assert_eq!(
        setup.remote_nra_dir.exists(),
        !dry_run
    );
}

// The symdir equivalent test is not included as both Windows and Unix
// appear to overwrite symdirs regardless of their permissions
#[rstest]
#[case(true)]
#[case(false)]
#[cfg_attr(unix, ignore)] // On most Unix implementations, the symlink permissions
                          // are ignored and the target's permissions are respected
fn no_write_access_at_remote_symfile_returns_io_error_permission_denied_unless_dry_run_or_uac_disabled(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_remote_file();
    setup.make_local_file();
    setup.make_target_file();
    symlink_expose(&setup.remote_file, &setup.target_file, false, true)
        .unwrap();

    // Set file read-only
    let mut perms = metadata(&setup.remote_file).unwrap().permissions();
    perms.set_readonly(true);
    set_permissions(&setup.remote_file, perms).unwrap();

    let mut tendril = setup.file_tendril();
    tendril.mode = TendrilMode::Link;

    let actual = link_tendril(&tendril, dry_run, force);

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::OverwriteSkipped);
        assert_eq!(setup.remote_file_contents(), "Target file contents");
    }
    else if !uac_enabled() {
        exp_result = Ok(TendrilActionSuccess::Overwrite);
        assert_eq!(setup.remote_file_contents(), "Local file contents");
    }
    else {
        exp_result = Err(TendrilActionError::IoError {
            kind: std::io::ErrorKind::PermissionDenied,
            loc: Location::Dest,
        });
        assert_eq!(setup.remote_file_contents(), "Target file contents");
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::SymFile),
            setup.remote_file.clone(),
            exp_result,
        )
    );
}

#[rstest]
fn non_link_mode_tendril_returns_mode_mismatch_error(
    #[values(TendrilMode::CopyMerge, TendrilMode::CopyOverwrite)]
    mode: TendrilMode,

    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    let mut tendril = setup.file_tendril();
    tendril.mode = mode;

    let actual = link_tendril(&tendril, dry_run, force);

    assert_eq!(
        actual,
        ActionLog::new(
            None,
            None,
            setup.remote_file,
            Err(TendrilActionError::ModeMismatch),
        )
    );
}

/// This is an exception to
/// [`remote_exists_and_is_not_symlink_returns_type_mismatch_error_unless_forced`]
#[rstest]
#[case(true)]
#[case(false)]
fn local_doesnt_exist_copies_remote_to_local_then_links_unless_dryrun(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
    #[values(true, false)] repo_exists: bool,
) {
    let setup = Setup::new();
    if repo_exists {
        setup.make_td_repo_dir();
    }
    setup.make_remote_file();
    setup.make_remote_nested_file();
    assert!(!setup.local_file.exists());
    assert!(!setup.local_dir.exists());

    let mut file_tendril = setup.file_tendril();
    let mut dir_tendril = setup.dir_tendril();
    file_tendril.mode = TendrilMode::Link;
    dir_tendril.mode = TendrilMode::Link;

    let file_actual =
        link_tendril(&file_tendril, dry_run, force);
    let dir_actual =
        link_tendril(&dir_tendril, dry_run, force);

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::OverwriteSkipped);
        assert!(!setup.local_file.exists());
        assert!(!setup.local_dir.exists());
    }
    else {
        exp_result = Ok(TendrilActionSuccess::Overwrite);
        assert_eq!(setup.local_file_contents(), "Remote file contents");
        assert_eq!(
            setup.local_nested_file_contents(),
            "Remote nested file contents"
        );
    }
    assert_eq!(
        file_actual,
        ActionLog::new(
            None,
            Some(FsoType::File),
            setup.remote_file,
            exp_result.clone(),
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(None, Some(FsoType::Dir), setup.remote_dir, exp_result,)
    );
}