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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! Contains tests specific to push actions.
//! See also [`crate::tests::common_action_tests`].

use crate::test_utils::{
    set_ra,
    symlink_expose,
    Setup,
    uac_enabled,
};
use crate::{
    push_tendril,
    ActionLog,
    FsoType,
    Location,
    Tendril,
    TendrilActionError,
    TendrilActionSuccess,
    TendrilMode,
};
use rstest::rstest;
use std::fs::{
    create_dir_all,
    metadata,
    read_to_string,
    set_permissions,
    write,
};

/// See also [`crate::tests::common_action_tests::remote_is_unchanged`] for
/// `dry_run` case
#[rstest]
fn local_exists_copies_to_remote(
    #[values(true, false)] force: bool,
    #[values(true, false)] as_dir: bool,
) {
    let setup = Setup::new();
    let exp_local_type;
    let exp_remote_path;
    let tendril;
    if as_dir {
        setup.make_local_nested_file();
        exp_local_type = Some(FsoType::Dir);
        exp_remote_path = setup.remote_dir.clone();
        tendril = setup.dir_tendril();
    }
    else {
        setup.make_local_file();
        exp_local_type = Some(FsoType::File);
        exp_remote_path = setup.remote_file.clone();
        tendril = setup.file_tendril();
    }

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

    assert_eq!(
        actual,
        ActionLog::new(
            exp_local_type,
            None,
            exp_remote_path,
            Ok(TendrilActionSuccess::New),
        )
    );
    if as_dir {
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Local nested file contents"
        );
    }
    else {
        assert_eq!(setup.remote_file_contents(), "Local file contents");
    }
    assert_eq!(setup.group_dir.read_dir().iter().count(), 1);
}

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

    let file_tendril = setup.file_tendril();
    let dir_tendril = setup.dir_tendril();

    let file_actual =
        push_tendril(&file_tendril, dry_run, force);
    let dir_actual =
        push_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!(setup.local_file.is_symlink());
    assert!(setup.local_dir.is_symlink());
    assert!(!setup.remote_file.is_symlink());
    assert!(!setup.remote_dir.is_symlink());
    if force && !dry_run {
        assert_eq!(setup.remote_file.file_name().unwrap(), "misc.txt");
        assert_eq!(setup.remote_file_contents(), "Target file contents");
        assert_eq!(setup.remote_dir.file_name().unwrap(), "misc");
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Target nested file contents"
        );
    }
    else {
        assert!(!setup.remote_file.exists());
        assert!(!setup.remote_dir.exists());
    }
}

#[rstest]
#[case(true)]
#[case(false)]
fn remote_is_symlink_returns_type_mismatch_error_unless_forced(
    #[case] dry_run: bool,
    #[values(true, false)] 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 file_tendril = setup.file_tendril();
    let dir_tendril = setup.dir_tendril();

    let file_actual =
        push_tendril(&file_tendril, dry_run, force);
    let dir_actual =
        push_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::SymFile,
            });
            exp_dir_result = Err(TendrilActionError::TypeMismatch {
                loc: Location::Dest,
                mistype: FsoType::SymDir,
            });
        }
        (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);
        }
    };

    assert_eq!(
        file_actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::SymFile),
            setup.remote_file.clone(),
            exp_file_result,
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::SymDir),
            setup.remote_dir.clone(),
            exp_dir_result,
        )
    );
    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(), "Target file contents");
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Target nested file contents"
        );
    }
}

#[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_local_type;
    let exp_remote_path;
    let tendril;
    if as_dir {
        setup.make_local_subdir_nested_file();
        exp_local_type = Some(FsoType::Dir);
        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_local_type = Some(FsoType::File);
        exp_remote_path = setup.remote_subdir_file.clone();
        tendril = setup.subdir_file_tendril();
        assert!(!setup.remote_subdir_file.parent().unwrap().exists());
    }

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

    assert_eq!(
        actual,
        ActionLog::new(
            exp_local_type,
            None,
            exp_remote_path,
            Ok(TendrilActionSuccess::New),
        )
    );
    if as_dir {
        assert_eq!(
            setup.remote_subdir_nested_file_contents(),
            "Local subdir nested file contents"
        );
    }
    else {
        assert_eq!(setup.remote_subdir_file_contents(), "Local subdir file contents");
    }
    assert_eq!(setup.group_dir.read_dir().iter().count(), 1);
}

#[rstest]
fn local_doesnt_exist_returns_io_error_not_found(
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
    #[values(true, false)] repo_exists: bool,
) {
    let setup = Setup::new();
    let file_tendril = setup.file_tendril();
    let dir_tendril = setup.dir_tendril();
    let exp_remote_type_file;
    let exp_remote_type_dir;
    setup.make_remote_file();
    setup.make_remote_dir();
    if repo_exists {
        setup.make_td_repo_dir();
    }
    exp_remote_type_file = Some(FsoType::File);
    exp_remote_type_dir = Some(FsoType::Dir);
    assert_eq!(setup.td_repo.exists(), repo_exists);

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

    let exp_loc = Location::Source;
    let exp_result = Err(TendrilActionError::IoError {
        kind: std::io::ErrorKind::NotFound,
        loc: exp_loc,
    });
    assert_eq!(
        file_actual,
        ActionLog::new(
            None,
            exp_remote_type_file,
            setup.remote_file.clone(),
            exp_result.clone(),
        )
    );
    assert_eq!(
        dir_actual,
        ActionLog::new(
            None,
            exp_remote_type_dir,
            setup.remote_dir.clone(),
            exp_result,
        )
    );
}

// AKA `source_is_file_and_dest_is_dir`
#[rstest]
#[case(TendrilMode::CopyMerge)]
#[case(TendrilMode::CopyOverwrite)]
fn local_is_file_and_remote_is_dir_returns_type_mismatch_error_unless_forced(
    #[case] mode: TendrilMode,
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_local_file();
    create_dir_all(&setup.remote_file).unwrap();

    let mut tendril = setup.file_tendril();
    tendril.mode = mode;

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

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::Dir),
            setup.remote_file.clone(),
            exp_result,
        )
    );
    if force && !dry_run {
        assert_eq!(&setup.remote_file_contents(), "Local file contents");
        assert_eq!(&setup.local_file_contents(), "Local file contents");
    }
    else {
        assert!(&setup.remote_file.is_dir());
        assert_eq!(&setup.local_file_contents(), "Local file contents");
    }
}

// AKA `source_is_dir_and_dest_is_file`
#[rstest]
#[case(TendrilMode::CopyMerge)]
#[case(TendrilMode::CopyOverwrite)]
fn local_is_dir_and_remote_is_file_returns_type_mismatch_error_unless_forced(
    #[case] mode: TendrilMode,
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_local_nested_file();
    write(&setup.remote_dir, "I'm a file!").unwrap();

    let mut tendril = setup.dir_tendril();
    tendril.mode = mode;

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

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::File),
            setup.remote_dir.clone(),
            exp_result,
        )
    );
    assert!(setup.local_dir.is_dir());
    if force && !dry_run {
        assert_eq!(
            setup.remote_nested_file_contents(),
            "Local nested file contents"
        );
        assert_eq!(setup.remote_dir.read_dir().iter().count(), 1);
        assert_eq!(setup.local_dir.read_dir().iter().count(), 1);
    }
    else {
        let remote_dir_contents = read_to_string(&setup.remote_dir).unwrap();
        assert_eq!(remote_dir_contents, "I'm a file!");
        assert_eq!(setup.local_dir.read_dir().iter().count(), 1);
    }
}

#[rstest]
#[case(TendrilMode::CopyMerge)]
#[case(TendrilMode::CopyOverwrite)]
fn file_tendril_overwrites_remote_file_regardless_of_dir_merge_mode(
    #[case] mode: TendrilMode,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_remote_file();
    setup.make_local_file();

    let mut tendril = setup.file_tendril();
    tendril.mode = mode;

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::File),
            setup.remote_file.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    assert_eq!(setup.remote_file_contents(), "Local file contents");
}

#[rstest]
#[case(true)]
#[case(false)]
fn dir_overwrite_w_dir_tendril_replaces_remote_dir_recursively(
    #[case] force: bool,
) {
    let setup = Setup::new();
    let remote_nested_dir = &setup.remote_dir.join("NestedDir");
    let remote_new_2nested_file = remote_nested_dir.join("new_nested.txt");
    let remote_extra_2nested_file = remote_nested_dir.join("extra_nested.txt");
    let local_nested_dir = &setup.local_dir.join("NestedDir");
    let local_new_2nested_file = local_nested_dir.join("new_nested.txt");
    setup.make_remote_nested_file();
    setup.make_local_nested_file();
    create_dir_all(&remote_nested_dir).unwrap();
    create_dir_all(&local_nested_dir).unwrap();
    write(&local_new_2nested_file, "I'm not in the remote dir").unwrap();
    write(&remote_extra_2nested_file, "I'm not in the local dir").unwrap();

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

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_dir.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    let remote_new_2nested_file_contents =
        read_to_string(remote_new_2nested_file).unwrap();
    assert_eq!(
        setup.remote_nested_file_contents(),
        "Local nested file contents"
    );
    assert_eq!(remote_new_2nested_file_contents, "I'm not in the remote dir");
    assert!(!remote_extra_2nested_file.exists());
}

#[rstest]
#[case(true)]
#[case(false)]
fn dir_merge_w_dir_tendril_merges_w_local_dir_recursively(#[case] force: bool) {
    let setup = Setup::new();
    let remote_nested_dir = &setup.remote_dir.join("NestedDir");
    let remote_new_2nested_file = remote_nested_dir.join("new_nested.txt");
    let remote_extra_2nested_file = remote_nested_dir.join("extra_nested.txt");
    let local_nested_dir = &setup.local_dir.join("NestedDir");
    let local_new_2nested_file = local_nested_dir.join("new_nested.txt");
    setup.make_remote_nested_file();
    setup.make_local_nested_file();
    create_dir_all(&remote_nested_dir).unwrap();
    create_dir_all(&local_nested_dir).unwrap();
    write(&local_new_2nested_file, "I'm not in the remote dir").unwrap();
    write(&remote_extra_2nested_file, "I'm not in the local dir").unwrap();

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

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_dir.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    let remote_new_2nested_file_contents =
        read_to_string(remote_new_2nested_file).unwrap();
    let remote_extra_2nested_file_contents =
        read_to_string(remote_extra_2nested_file).unwrap();
    assert_eq!(
        setup.remote_nested_file_contents(),
        "Local nested file contents"
    );
    assert_eq!(remote_new_2nested_file_contents, "I'm not in the remote dir");
    assert_eq!(remote_extra_2nested_file_contents, "I'm not in the local dir");
}

#[rstest]
#[case(true)]
#[case(false)]
fn dir_overwrite_w_subdir_dir_tendril_replaces_remote_dir_recursively(
    #[case] force: bool,
) {
    let setup = Setup::new();
    let remote_nested_dir = &setup.remote_subdir_dir.join("NestedDir");
    let remote_new_2nested_file = remote_nested_dir.join("new_nested.txt");
    let remote_extra_2nested_file = remote_nested_dir.join("extra_nested.txt");
    let local_nested_dir = &setup.local_subdir_dir.join("NestedDir");
    let local_new_2nested_file = local_nested_dir.join("new_nested.txt");
    setup.make_remote_subdir_nested_file();
    setup.make_local_subdir_nested_file();
    create_dir_all(&remote_nested_dir).unwrap();
    create_dir_all(&local_nested_dir).unwrap();
    write(&local_new_2nested_file, "I'm not in the remote dir").unwrap();
    write(&remote_extra_2nested_file, "I'm not in the local dir").unwrap();

    let mut tendril = setup.subdir_dir_tendril();
    tendril.mode = TendrilMode::CopyOverwrite;

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_subdir_dir.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    let remote_new_2nested_file_contents =
        read_to_string(remote_new_2nested_file).unwrap();
    assert_eq!(
        setup.remote_subdir_nested_file_contents(),
        "Local subdir nested file contents"
    );
    assert_eq!(remote_new_2nested_file_contents, "I'm not in the remote dir");
    assert!(!remote_extra_2nested_file.exists());
}

#[rstest]
#[case(true)]
#[case(false)]
fn dir_merge_w_subdir_dir_tendril_merges_w_local_dir_recursively(
    #[case] force: bool,
) {
    let setup = Setup::new();
    let remote_nested_dir = &setup.remote_subdir_dir.join("NestedDir");
    let remote_new_2nested_file = remote_nested_dir.join("new_nested.txt");
    let remote_extra_2nested_file = remote_nested_dir.join("extra_nested.txt");
    let local_nested_dir = &setup.local_subdir_dir.join("NestedDir");
    let local_new_2nested_file = local_nested_dir.join("new_nested.txt");
    setup.make_remote_subdir_nested_file();
    setup.make_local_subdir_nested_file();
    create_dir_all(&remote_nested_dir).unwrap();
    create_dir_all(&local_nested_dir).unwrap();
    write(&local_new_2nested_file, "I'm not in the remote dir").unwrap();
    write(&remote_extra_2nested_file, "I'm not in the local dir").unwrap();

    let mut tendril = setup.subdir_dir_tendril();
    tendril.mode = TendrilMode::CopyMerge;

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

    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_subdir_dir.clone(),
            Ok(TendrilActionSuccess::Overwrite),
        )
    );
    let remote_new_2nested_file_contents =
        read_to_string(remote_new_2nested_file).unwrap();
    let remote_extra_2nested_file_contents =
        read_to_string(remote_extra_2nested_file).unwrap();
    assert_eq!(
        setup.remote_subdir_nested_file_contents(),
        "Local subdir nested file contents"
    );
    assert_eq!(remote_new_2nested_file_contents, "I'm not in the remote dir");
    assert_eq!(remote_extra_2nested_file_contents, "I'm not in the local dir");
}

#[rstest]
#[case(true)]
#[case(false)]
#[cfg_attr(target_os = "linux", ignore)]
fn no_read_access_from_local_file_returns_io_error_permission_denied_unless_dry_run(
    #[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::CopyOverwrite,
    )
    .unwrap();

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

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::NewSkipped);
    }
    else {
        exp_result = Err(TendrilActionError::IoError {
            kind: std::io::ErrorKind::PermissionDenied,
            loc: Location::Source,
        });
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            None,
            setup.remote_nra_file.clone(),
            exp_result,
        )
    );
    assert!(!setup.remote_nra_file.exists());
}

#[rstest]
#[case(true)]
#[case(false)]
#[cfg_attr(target_os = "linux", ignore)]
fn no_read_access_from_local_dir_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_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::CopyOverwrite,
    )
    .unwrap();

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

    set_ra(&setup.local_nra_dir, true);
    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::NewSkipped);
        assert!(!setup.remote_nra_dir.exists());
    }
    else if !uac_enabled() {
        exp_result = Ok(TendrilActionSuccess::New);
        assert!(setup.remote_nra_dir.exists());
    }
    else {
        exp_result = Err(TendrilActionError::IoError {
            kind: std::io::ErrorKind::PermissionDenied,
            loc: Location::Source,
        });
        assert!(!setup.remote_nra_dir.exists());
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            None,
            setup.remote_nra_dir.clone(),
            exp_result,
        )
    );
}

#[rstest]
#[case(true)]
#[case(false)]
#[cfg_attr(target_os = "linux", ignore)]
fn no_write_access_at_remote_file_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();

    // Set file read-only
    let mut perms = metadata(&setup.remote_file).unwrap().permissions();
    perms.set_readonly(true);
    set_permissions(&setup.remote_file, perms).unwrap();
    if std::env::consts::FAMILY == "unix" {
        // Unix does not consider permissions on the file when deleting, only on
        // its parent
        let mut parent_perms =
            metadata(&setup.parent_dir).unwrap().permissions();
        parent_perms.set_readonly(true);
        set_permissions(&setup.parent_dir, parent_perms).unwrap();
    }

    let tendril = setup.file_tendril();

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

    // Cleanup
    let mut parent_perms = metadata(&setup.parent_dir).unwrap().permissions();
    parent_perms.set_readonly(false);
    set_permissions(&setup.parent_dir, parent_perms).unwrap();

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::OverwriteSkipped);
        assert_eq!(setup.remote_file_contents(), "Remote 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(), "Remote file contents");
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::File),
            Some(FsoType::File),
            setup.remote_file.clone(),
            exp_result,
        )
    );
}

#[rstest]
#[case(true)]
#[case(false)]
#[cfg_attr(any(windows, target_os = "linux"), ignore)] // These permissions do not prevent write access on
                             // Windows. This must be done through the Security
                             // interface
fn no_write_access_at_remote_dir_returns_io_error_permission_denied_unless_dry_run(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_remote_nested_file();
    setup.make_local_nested_file();

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

    let tendril = setup.dir_tendril();

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

    // Cleanup
    perms.set_readonly(false);
    set_permissions(&setup.remote_dir, perms).unwrap();

    let exp_result;
    if dry_run {
        exp_result = Ok(TendrilActionSuccess::OverwriteSkipped);
    }
    else {
        exp_result = Err(TendrilActionError::IoError {
            kind: std::io::ErrorKind::PermissionDenied,
            loc: Location::Dest,
        });
    }
    assert_eq!(
        actual,
        ActionLog::new(
            Some(FsoType::Dir),
            Some(FsoType::Dir),
            setup.remote_dir.clone(),
            exp_result,
        )
    );
    assert_eq!(
        setup.remote_nested_file_contents(),
        "Remote nested file contents"
    );
}