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
//! Tests that the updater function behaves properly, for additional
//! tests see the similar [`super::batch_tendril_action_tests`] module

use crate::path_ext::UniPath;
use crate::test_utils::{
    get_disposable_dir,
    is_empty,
    Setup,
};
use crate::{
    batch_tendril_action,
    ActionLog,
    ActionMode,
    CallbackUpdater,
    FsoType,
    Location,
    RawTendril,
    TendrilActionError,
    TendrilActionSuccess,
    TendrilLog,
    TendrilMode,
    TendrilReport,
};
use fs_extra::file::read_to_string;
use rstest::rstest;
use serial_test::serial;
use std::fs::{create_dir_all, write};
use std::path::PathBuf;
use tempdir::TempDir;

#[rstest]
fn given_empty_list_returns_empty(
    #[values(ActionMode::Push, ActionMode::Pull)]
    mode: ActionMode,
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let temp_parent_dir =
        TempDir::new_in(get_disposable_dir(), "ParentDir").unwrap();
    let given_td_repo = temp_parent_dir.path().join("TendrilsRepo").into();

    let mut count_call_counter = 0;
    let mut before_call_counter = 0;
    let mut after_call_counter = 0;
    let mut count_actual = -1;
    let mut before_actual = vec![];
    let mut after_actual = vec![];
    let count_fn = |c| {
        count_call_counter += 1;
        count_actual = c;
    };
    let before_fn = |raw| {
        before_call_counter += 1;
        before_actual.push(raw);
    };
    let after_fn = |report| {
        after_call_counter += 1;
        after_actual.push(report);
    };
    let updater =
        CallbackUpdater::<_, _, _, ActionLog>::new(count_fn, before_fn, after_fn);

    batch_tendril_action(updater, mode, &given_td_repo, vec![], dry_run, force);

    assert_eq!(count_call_counter, 1);
    assert_eq!(before_call_counter, 0);
    assert_eq!(after_call_counter, 0);
    assert_eq!(count_actual, 0);
    assert!(before_actual.is_empty());
    assert!(after_actual.is_empty());
    assert!(is_empty(given_td_repo.inner()))
}

#[rstest]
fn returns_result_after_each_operation(
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_local_file();
    setup.make_local_nested_file();
    let t1 = setup.file_tendril_raw();
    let mut t2 = t1.clone();
    t2.remote = setup.remote_nested_file.to_string_lossy().to_string();

    let expected_success = match dry_run {
        true => Ok(TendrilActionSuccess::NewSkipped),
        false => Ok(TendrilActionSuccess::New),
    };
    let mut count_call_counter = 0;
    let mut before_call_counter = 0;
    let mut after_call_counter = 0;
    let mut count_actual = -1;
    let mut before_actual = vec![];
    let mut after_actual = vec![];
    let count_fn = |c| {
        count_call_counter += 1;
        count_actual = c;
        assert_eq!(count_actual, 2);
    };
    let before_fn = |raw| {
        before_call_counter += 1;
        if before_call_counter == 1 {
            assert_eq!(raw, t1);
        }
        else {
            assert_eq!(raw, t2);
        }

        before_actual.push(raw);
    };
    let after_fn = |r| {
        after_call_counter += 1;
        if after_call_counter == 1 {
            assert_eq!(r, TendrilReport {
                raw_tendril: t1.clone(),
                log: Ok(ActionLog::new(
                    Some(FsoType::File),
                    None,
                    setup.remote_file.clone(),
                    expected_success.clone(),
                )),
            });
            if dry_run {
                assert!(!setup.remote_file.exists())
            }
            else {
                assert_eq!(setup.remote_file_contents(), "Local file contents");
            }
            assert!(!setup.remote_dir.exists())
        }
        else if after_call_counter == 2 {
            assert_eq!(r, TendrilReport {
                raw_tendril: t2.clone(),
                log: Ok(ActionLog::new(
                    Some(FsoType::File),
                    None,
                    setup.remote_nested_file.clone(),
                    expected_success.clone(),
                )),
            });
            if dry_run {
                assert!(!setup.remote_file.exists());
                assert!(!setup.remote_dir.exists());
            }
            else {
                assert_eq!(setup.remote_file_contents(), "Local file contents");
                assert_eq!(
                    setup.remote_nested_file_contents(),
                    "Local file contents" // Note lack of "nested"
                );
            }
        }
        else {
            panic!("Updater was called too many times");
        }

        after_actual.push(r);
    };
    let updater =
        CallbackUpdater::<_, _, _, ActionLog>::new(count_fn, before_fn, after_fn);

    batch_tendril_action(
        updater,
        ActionMode::Push,
        &UniPath::from(&setup.td_repo),
        vec![t1.clone(), t2.clone()],
        dry_run,
        force,
    );

    assert_eq!(count_call_counter, 1);
    assert_eq!(before_call_counter, 2);
    assert_eq!(after_call_counter, 2);
}

#[rstest]
#[case(true)]
#[case(false)]
fn pull_returns_tendril_and_result_for_each_given(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let temp_grandparent_dir =
        TempDir::new_in(get_disposable_dir(), "ParentDir").unwrap();
    let given_td_repo = temp_grandparent_dir.path().join("TendrilsRepo");
    let given_parent_dir_a = temp_grandparent_dir.path().join("ParentA");
    let given_parent_dir_b = temp_grandparent_dir.path().join("ParentB");
    let remote_app1_file = given_parent_dir_a.join("misc1.txt");
    let remote_app1_dir = given_parent_dir_a.join("App1 Dir");
    let remote_app1_nested_file = remote_app1_dir.join("nested1.txt");
    let remote_app2_file_a = given_parent_dir_a.join("misc2.txt");
    let remote_app2_file_b = given_parent_dir_b.join("misc2.txt");
    let local_app1_file = given_td_repo.join("App1").join("misc1.txt");
    let local_app1_dir = given_td_repo.join("App1").join("App1 Dir");
    let local_app1_nested_file = local_app1_dir.join("nested1.txt");
    let local_app2_file_ab = given_td_repo.join("App2").join("misc2.txt");
    create_dir_all(&given_td_repo).unwrap();
    create_dir_all(&remote_app1_dir).unwrap();
    create_dir_all(&given_parent_dir_a).unwrap();
    create_dir_all(&given_parent_dir_b).unwrap();
    write(&remote_app1_file, "Remote app 1 file contents").unwrap();
    write(&remote_app2_file_a, "Remote app 2 file a contents").unwrap();
    write(&remote_app2_file_b, "Remote app 2 file b contents").unwrap();
    write(&remote_app1_nested_file, "Remote app 1 nested file contents")
        .unwrap();

    let mut given = vec![
        RawTendril::new("App1/misc1.txt"),
        RawTendril::new("App2/misc2.txt"),
        RawTendril::new("App2/misc2.txt"),
        RawTendril::new("App1/App1 Dir"),
        RawTendril::new("App3/I don't exist"),
    ];

    given[0].remote = given_parent_dir_a.join("misc1.txt").to_string_lossy().to_string();
    given[1].remote = given_parent_dir_a.join("misc2.txt").to_string_lossy().to_string();
    given[2].remote = given_parent_dir_b.join("misc2.txt").to_string_lossy().to_string();
    given[3].remote = given_parent_dir_a.join("App1 Dir").to_string_lossy().to_string();
    given[4].remote = given_parent_dir_a.join("I don't exist").to_string_lossy().to_string();

    let expected_success = match dry_run {
        true => Ok(TendrilActionSuccess::NewSkipped),
        false => Ok(TendrilActionSuccess::New),
    };
    let expected = vec![
        TendrilReport {
            raw_tendril: given[0].clone(),
            log: Ok(ActionLog::new(
                None,
                Some(FsoType::File),
                remote_app1_file,
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[1].clone(),
            log: Ok(ActionLog::new(
                None,
                Some(FsoType::File),
                remote_app2_file_a.clone(),
                expected_success.clone(),
            )),
        },
        // TODO: This should eventually not be included once the most recently modified
        // version is checked
        TendrilReport {
            raw_tendril: given[2].clone(),
            log: Ok(ActionLog::new(
                match dry_run {
                    true => None,
                    false => Some(FsoType::File),
                },
                Some(FsoType::File),
                remote_app2_file_b,
                match dry_run {
                    true => Ok(TendrilActionSuccess::NewSkipped),
                    false => Ok(TendrilActionSuccess::Overwrite),
                }
            )),
        },
        TendrilReport {
            raw_tendril: given[3].clone(),
            log: Ok(ActionLog::new(
                None,
                Some(FsoType::Dir),
                remote_app1_dir,
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[4].clone(),
            log: Ok(ActionLog::new(
                None,
                None,
                given_parent_dir_a.join("I don't exist"),
                Err(TendrilActionError::IoError {
                    kind: std::io::ErrorKind::NotFound,
                    loc: Location::Source,
                }),
            )),
        },
    ];

    let mut count_actual = -1;
    let mut before_actual = vec![];
    let mut after_actual = vec![];
    let count_fn = |c| count_actual = c;
    let before_fn = |raw| before_actual.push(raw);
    let after_fn = |report| after_actual.push(report);
    let updater =
        CallbackUpdater::<_, _, _, ActionLog>::new(count_fn, before_fn, after_fn);

    batch_tendril_action(
        updater,
        ActionMode::Pull,
        &UniPath::from(given_td_repo),
        given,
        dry_run,
        force
    );

    assert_eq!(after_actual, expected);

    if dry_run {
        assert!(!local_app1_file.exists());
        assert!(!local_app1_dir.exists());
        assert!(!local_app2_file_ab.exists());
        assert!(!local_app1_nested_file.exists());
    }
    else {
        let local_app1_file_contents = read_to_string(local_app1_file).unwrap();
        let local_app2_file_contents = read_to_string(local_app2_file_ab).unwrap();
        let local_app1_nested_file_contents =
            read_to_string(local_app1_nested_file).unwrap();

        assert_eq!(local_app1_file_contents, "Remote app 1 file contents");
        assert!(local_app1_dir.exists());
        // TODO: This should eventually only be the most recently modified file's contents
        assert_eq!(local_app2_file_contents, "Remote app 2 file b contents");
        assert_eq!(
            local_app1_nested_file_contents,
            "Remote app 1 nested file contents"
        );
    }
}

#[rstest]
#[case(true)]
#[case(false)]
fn push_returns_tendril_and_result_for_each_given_link_or_copy_type(
    #[case] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let temp_grandparent_dir =
        TempDir::new_in(get_disposable_dir(), "ParentDir").unwrap();
    let given_td_repo = temp_grandparent_dir.path().join("TendrilsRepo");
    let given_parent_dir_a = temp_grandparent_dir.path().join("ParentA");
    let given_parent_dir_b = temp_grandparent_dir.path().join("ParentB");
    let remote_app1_file = given_parent_dir_a.join("misc1.txt");
    let remote_app1_dir = given_parent_dir_a.join("App1 Dir");
    let remote_app1_nested_file = remote_app1_dir.join("nested1.txt");
    let remote_app2_file_a = given_parent_dir_a.join("misc2.txt");
    let remote_app2_file_b = given_parent_dir_b.join("misc2.txt");
    let local_app1_file = given_td_repo.join("App1").join("misc1.txt");
    let local_app1_dir = given_td_repo.join("App1").join("App1 Dir");
    let local_app1_nested_file = local_app1_dir.join("nested1.txt");
    let local_app2_file_ab = given_td_repo.join("App2").join("misc2.txt");
    create_dir_all(given_parent_dir_a.clone()).unwrap();
    create_dir_all(given_parent_dir_b.clone()).unwrap();
    create_dir_all(&local_app1_dir).unwrap();
    create_dir_all(&given_td_repo.join("App2")).unwrap();
    create_dir_all(&given_td_repo.join("App3")).unwrap();
    write(&local_app1_file, "Local app 1 file contents").unwrap();
    write(&local_app2_file_ab, "Local app 2 file contents").unwrap();
    write(&local_app1_nested_file, "Local app 1 nested file contents").unwrap();

    let mut given = vec![
        RawTendril::new("App1/misc1.txt"),
        RawTendril::new("App2/misc2.txt"),
        RawTendril::new("App2/misc2.txt"),
        RawTendril::new("App1/App1 Dir"),
        RawTendril::new("App3/I don't exist"),
    ];

    given[0].remote = given_parent_dir_a.join("misc1.txt").to_string_lossy().to_string();
    given[1].remote = given_parent_dir_a.join("misc2.txt").to_string_lossy().to_string();
    given[2].remote = given_parent_dir_b.join("misc2.txt").to_string_lossy().to_string();
    given[3].remote = given_parent_dir_a.join("App1 Dir").to_string_lossy().to_string();
    given[4].remote = given_parent_dir_a.join("I don't exist").to_string_lossy().to_string();

    given[0].mode = TendrilMode::Link;
    given[1].mode = TendrilMode::CopyOverwrite;
    given[2].mode = TendrilMode::Link;
    given[3].mode = TendrilMode::CopyMerge;
    given[4].mode = TendrilMode::CopyOverwrite;

    let expected_success = match dry_run {
        true => Ok(TendrilActionSuccess::NewSkipped),
        false => Ok(TendrilActionSuccess::New),
    };
    let expected = vec![
        TendrilReport {
            raw_tendril: given[0].clone(),
            log: Ok(ActionLog::new(
                Some(FsoType::File),
                None,
                remote_app1_file.clone(),
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[1].clone(),
            log: Ok(ActionLog::new(
                Some(FsoType::File),
                None,
                remote_app2_file_a.clone(),
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[2].clone(),
            log: Ok(ActionLog::new(
                Some(FsoType::File),
                None,
                remote_app2_file_b.clone(),
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[3].clone(),
            log: Ok(ActionLog::new(
                Some(FsoType::Dir),
                None,
                remote_app1_dir.clone(),
                expected_success.clone(),
            )),
        },
        TendrilReport {
            raw_tendril: given[4].clone(),
            log: Ok(ActionLog::new(
                None,
                None,
                given_parent_dir_a.join("I don't exist"),
                Err(TendrilActionError::IoError {
                    kind: std::io::ErrorKind::NotFound,
                    loc: Location::Source,
                }),
            )),
        },
    ];

    let mut count_actual = -1;
    let mut before_actual = vec![];
    let mut after_actual = vec![];
    let count_fn = |c| count_actual = c;
    let before_fn = |raw| before_actual.push(raw);
    let after_fn = |report| after_actual.push(report);
    let updater =
        CallbackUpdater::<_, _, _, ActionLog>::new(count_fn, before_fn, after_fn);

    batch_tendril_action(
        updater,
        ActionMode::Push,
        &UniPath::from(given_td_repo),
        given,
        dry_run,
        force,
    );

    assert_eq!(after_actual, expected);

    if dry_run {
        assert!(!remote_app1_file.exists());
        assert!(!remote_app2_file_a.exists());
        assert!(!remote_app2_file_b.exists());
        assert!(!remote_app1_nested_file.exists());
    }
    else {
        let remote_app1_file_contents =
            read_to_string(&remote_app1_file).unwrap();
        let remote_app2_file_a_contents =
            read_to_string(&remote_app2_file_a).unwrap();
        let remote_app2_file_b_contents =
            read_to_string(&remote_app2_file_b).unwrap();
        let remote_app1_nested_file_contents =
            read_to_string(&remote_app1_nested_file).unwrap();

        assert_eq!(remote_app1_file_contents, "Local app 1 file contents");
        assert_eq!(remote_app2_file_a_contents, "Local app 2 file contents");
        assert_eq!(remote_app2_file_b_contents, "Local app 2 file contents");
        assert_eq!(
            remote_app1_nested_file_contents,
            "Local app 1 nested file contents"
        );
        assert!(remote_app1_file.is_symlink());
        assert!(!remote_app2_file_a.is_symlink());
        assert!(remote_app2_file_b.is_symlink());
        assert!(!remote_app1_dir.is_symlink());
    }
}

#[rstest]
#[serial(SERIAL_MUT_ENV_VARS)]
fn remote_path_vars_are_resolved(
    #[values(ActionMode::Push, ActionMode::Pull)]
    mode: ActionMode,
    #[values(true, false)] dry_run: bool,
    #[values(true, false)] force: bool,
) {
    let setup = Setup::new();
    setup.make_td_repo_dir();
    let mut tendril = setup.file_tendril_raw();
    tendril.remote = "~/I_do_not_exist/<var>/misc.txt".to_string();
    let tendrils = vec![tendril.clone()];
    std::env::set_var("HOME", "My/Home");
    std::env::set_var("var", "value");

    use std::path::MAIN_SEPARATOR as SEP;
    let expected_resolved_path = format!(
        "{SEP}My{SEP}Home{SEP}I_do_not_exist{SEP}value{SEP}misc.txt"
    );
    let expected = vec![TendrilReport {
        raw_tendril: tendril,
        log: Ok(ActionLog::new(
            None,
            None,
            PathBuf::from(expected_resolved_path.clone()),
            Err(TendrilActionError::IoError {
                kind: std::io::ErrorKind::NotFound,
                loc: Location::Source,
            }),
        )),
    }];

    let mut count_actual = -1;
    let mut before_actual = vec![];
    let mut after_actual = vec![];
    let count_fn = |c| count_actual = c;
    let before_fn = |raw| before_actual.push(raw);
    let after_fn = |report| after_actual.push(report);
    let updater =
        CallbackUpdater::<_, _, _, ActionLog>::new(count_fn, before_fn, after_fn);

    batch_tendril_action(
        updater,
        mode,
        &UniPath::from(&setup.td_repo),
        tendrils,
        dry_run,
        force,
    );

    let actual_result_path = &after_actual[0].log.as_ref().unwrap().resolved_path();

    let actual_resolved_path_str = actual_result_path.to_string_lossy();
    assert_eq!(actual_resolved_path_str.into_owned(), expected_resolved_path);
    assert_eq!(after_actual, expected);
}

// TODO: Test when the second tendril is a parent/child to the first tendril