rivia-vfs 0.1.3

Ergonomic facade for the Rivia Virtual FileSystem
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
#[allow(unused_imports)]
use super::prelude::*;

/// Setup Vfs testing components using the current provider is
///
/// This provides an abstraction over VirtualFileSystem implementations such that we can easily
/// switch out a Memfs backend for a Stdfs backend without modifying the testing algorithms. Vfs
/// tests will default to using the `testing::TEST_TEMP_DIR` as the root of testing and create a new
/// directory inside that using the derived fully qualified function name or given function name
/// when it can't be derived.
///
/// ### Warning
/// Since doc tests always have a default function name of `rust_out::main` its required to override
/// the `func_name` param to get a unique directory to work with in the Stdfs case as you won't get
/// a unique directory created to work from and could cause testing collisions.
///
/// ### Returns
/// * `tmpdir` - the temp directory that was created for the test function to work in
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// let tmpdir = assert_setup!("unique_func_name");
/// assert_remove_all!(&tmpdir);
/// ```
#[macro_export]
macro_rules! assert_setup {
    ($func:expr) => {{
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone(), $func);
        tmpdir
    }};
    () => {{
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone());
        tmpdir
    }};
}

/// Setup Vfs testing components with Memfs provider
///
/// This provides an abstraction over VirtualFileSystem implementations such that we can easily
/// switch out a Memfs backend for a Stdfs backend without modifying the testing algorithms. Vfs
/// tests will default to using the `testing::TEST_TEMP_DIR` as the root of testing and create a new
/// directory inside that using the derived fully qualified function name or given function name
/// when it can't be derived.
///
/// ### Warning
/// Since doc tests always have a default function name of `rust_out::main` its required to override
/// the `func_name` param to get a unique directory to work with in the Stdfs case as you won't get
/// a unique directory created to work from and could cause testing collisions.
///
/// ### Returns
/// * `tmpdir` - the temp directory that was created for the test function to work in
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// let tmpdir = assert_memfs_setup!("unique_func_name");
/// assert_remove_all!(&tmpdir);
/// ```
#[macro_export]
macro_rules! assert_memfs_setup {
    ($func:expr) => {{
        assert!(vfs::set_memfs().is_ok());
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone(), $func);
        tmpdir
    }};
    () => {{
        assert!(vfs::set_memfs().is_ok());
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone());
        tmpdir
    }};
}

/// Setup Vfs testing components with Stdfs provider
///
/// This provides an abstraction over VirtualFileSystem implementations such that we can easily
/// switch out a Memfs backend for a Stdfs backend without modifying the testing algorithms. Vfs
/// tests will default to using the `testing::TEST_TEMP_DIR` as the root of testing and create a new
/// directory inside that using the derived fully qualified function name or given function name
/// when it can't be derived.
///
/// ### Warning
/// Since doc tests always have a default function name of `rust_out::main` its required to override
/// the `func_name` param to get a unique directory to work with in the Stdfs case as you won't get
/// a unique directory created to work from and could cause testing collisions.
///
/// ### Returns
/// * `tmpdir` - the temp directory that was created for the test function to work in
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// let tmpdir = assert_stdfs_setup!("unique_func_name");
/// assert_remove_all!(&tmpdir);
/// ```
#[macro_export]
macro_rules! assert_stdfs_setup {
    ($func:expr) => {{
        assert!(vfs::set_stdfs().is_ok());
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone(), $func);
        tmpdir
    }};
    () => {{
        assert!(vfs::set_stdfs().is_ok());
        let (_, tmpdir) = assert_vfs_setup!(vfs::VFS.read().unwrap().clone());
        tmpdir
    }};
}

/// Assert the copy of a file
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// let file1 = vfs::root().mash("file1");
/// let file2 = vfs::root().mash("file2");
/// assert_write_all!(&file1, "this is a test");
/// assert_copyfile!(&file1, &file2);
/// ```
#[macro_export]
macro_rules! assert_copyfile {
    ($from:expr, $to:expr) => {
        assert_vfs_copyfile!(vfs::VFS.read().unwrap().clone(), $from, $to)
    };
}

/// Assert that a file or directory exists
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_exists!(vfs::root());
/// ```
#[macro_export]
macro_rules! assert_exists {
    ($path:expr) => {
        assert_vfs_exists!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert the given path doesn't exist
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_exists!("foo");
/// ```
#[macro_export]
macro_rules! assert_no_exists {
    ($path:expr) => {
        assert_vfs_no_exists!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path exists and is a directory
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_dir!("foo");
/// assert_mkdir_p!("foo");
/// assert_is_dir!("foo");
/// ```
#[macro_export]
macro_rules! assert_is_dir {
    ($path:expr) => {
        assert_vfs_is_dir!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path isn't a directory
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_dir!("foo");
/// assert_mkdir_p!("foo");
/// assert_is_dir!("foo");
/// ```
#[macro_export]
macro_rules! assert_no_dir {
    ($path:expr) => {
        assert_vfs_no_dir!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path exists and is a file
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_file!("foo");
/// assert_mkfile!("foo");
/// assert_is_file!("foo");
/// ```
#[macro_export]
macro_rules! assert_is_file {
    ($path:expr) => {
        assert_vfs_is_file!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path isn't a file
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_file!("foo");
/// ```
#[macro_export]
macro_rules! assert_no_file {
    ($path:expr) => {
        assert_vfs_no_file!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path exists and is a symlink
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_symlink!("foo");
/// assert_symlink!("foo", "bar");
/// assert_is_symlink!("foo");
/// ```
#[macro_export]
macro_rules! assert_is_symlink {
    ($path:expr) => {
        assert_vfs_is_symlink!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert that the given path isn't a symlink
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_symlink!("foo");
/// ```
#[macro_export]
macro_rules! assert_no_symlink {
    ($path:expr) => {
        assert_vfs_no_symlink!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert the creation of the given directory with the given mode
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_dir!("foo");
/// assert_mkdir_m!("foo", 0o40777);
/// assert_is_dir!("foo");
/// ```
#[macro_export]
macro_rules! assert_mkdir_m {
    ($path:expr, $mode:expr) => {
        assert_vfs_mkdir_m!(vfs::VFS.read().unwrap().clone(), $path, $mode)
    };
}

/// Assert the creation of the given directory.
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_dir!("foo");
/// assert_mkdir_p!("foo");
/// assert_is_dir!("foo");
/// ```
#[macro_export]
macro_rules! assert_mkdir_p {
    ($path:expr) => {
        assert_vfs_mkdir_p!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert the creation of a file. If the file exists no change is made
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_file!("foo");
/// assert_mkfile!("foo");
/// assert_is_file!("foo");
/// ```
#[macro_export]
macro_rules! assert_mkfile {
    ($path:expr) => {
        assert_vfs_mkfile!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert data read from the file matches the input data
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_file!("foo");
/// assert_write_all!("foo", b"foobar 1");
/// assert_read_all!("foo", "foobar 1".to_string());
/// ```
#[macro_export]
macro_rules! assert_read_all {
    ($path:expr, $data:expr) => {
        assert_vfs_read_all!(vfs::VFS.read().unwrap().clone(), $path, $data)
    };
}

/// Assert the reading of a link's target relative path
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_mkfile!("file");
/// assert_symlink!("link", "file");
/// assert_readlink!("link", PathBuf::from("file"));
/// ```
#[macro_export]
macro_rules! assert_readlink {
    ($path:expr, $target:expr) => {
        assert_vfs_readlink!(vfs::VFS.read().unwrap().clone(), $path, $target)
    };
}

/// Assert the reading of a link's target absolute path
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_mkfile!("file");
/// assert_symlink!("link", "file");
/// assert_readlink_abs!("link", vfs::root().mash("file"));
/// ```
#[macro_export]
macro_rules! assert_readlink_abs {
    ($path:expr, $data:expr) => {
        assert_vfs_readlink_abs!(vfs::VFS.read().unwrap().clone(), $path, $data)
    };
}

/// Assert the removal of the target file or directory
///
/// ### Assertion Failures
/// * Assertion fails if the target is a directory that contains files
/// * Assertion fails if the file exists after `remove` is called
/// * Assertion fails if the `remove` call fails
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_mkfile!("foo");
/// assert_remove!("foo");
/// assert_no_exists!("foo");
/// ```
#[macro_export]
macro_rules! assert_remove {
    ($path:expr) => {
        assert_vfs_remove!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert the removal of the target path
///
/// ### Assertion Failures
/// * Assertion fails if `remove_all` fails
/// * Assertion fails if the target path still exists after the call to `remove_all`
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_mkdir_p!("foo/bar");
/// assert_remove_all!("foo");
/// assert_no_exists!("foo/bar");
/// assert_no_exists!("foo");
/// ```
#[macro_export]
macro_rules! assert_remove_all {
    ($path:expr) => {
        assert_vfs_remove_all!(vfs::VFS.read().unwrap().clone(), $path)
    };
}

/// Assert the creation of a symlink. If the symlink exists no change is made
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_symlink!("foo");
/// assert_symlink!("foo", "bar");
/// assert_is_symlink!("foo");
/// ```
#[macro_export]
macro_rules! assert_symlink {
    ($link:expr, $target:expr) => {
        assert_vfs_symlink!(vfs::VFS.read().unwrap().clone(), $link, $target)
    };
}

/// Assert data is written to the given file
///
/// ### Examples
/// ```
/// use rivia_vfs::prelude::*;
///
/// assert!(vfs::set_memfs().is_ok());
/// assert_no_file!("foo");
/// assert_write_all!("foo", b"foobar");
/// assert_is_file!("foo");
/// ```
#[macro_export]
macro_rules! assert_write_all {
    ($path:expr, $data:expr) => {
        assert_vfs_write_all!(vfs::VFS.read().unwrap().clone(), $path, $data)
    };
}

// Unit tests
// -------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests
{
    use crate::prelude::*;

    #[allow(dead_code)]
    fn dump_memfs()
    {
        if let Vfs::Memfs(ref x) = **vfs::VFS.read().unwrap() {
            println!("{}", x);
        }
    }

    #[test]
    fn test_assert_setup()
    {
        let tmpdir = assert_memfs_setup!();
        let expected =
            vfs::root().mash(testing::TEST_TEMP_DIR).mash("rivia_vfs::assert::tests::test_assert_setup");
        assert_eq!(&tmpdir, &expected);
        assert_exists!(&expected);

        // Try with a function name override
        let tmpdir = assert_memfs_setup!("foobar_setup");
        let expected = vfs::root().mash(testing::TEST_TEMP_DIR).mash("foobar_setup");
        assert_eq!(&tmpdir, &expected);
        assert_exists!(&expected);
    }

    #[test]
    fn test_assert_copyfile()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        let file2 = tmpdir.mash("file2");
        assert_write_all!(&file1, "this is a test");
        assert_copyfile!(&file1, &file2);
    }

    #[test]
    fn test_assert_exists_and_no_exists()
    {
        let tmpdir = assert_memfs_setup!();

        // Test file exists
        {
            let file = tmpdir.mash("file");
            assert_no_exists!(&file);
            assert_mkfile!(&file);
            assert_exists!(&file);
            assert_remove!(&file);
            assert_no_exists!(&file);
        }

        // Test dir exists
        {
            let dir1 = tmpdir.mash("dir1");
            assert_no_exists!(&dir1);
            assert_mkdir_p!(&dir1);
            assert_exists!(&dir1);
            assert_remove_all!(&dir1);
            assert_no_exists!(&dir1);
        }
    }

    #[test]
    fn test_assert_is_dir_no_dir()
    {
        let tmpdir = assert_memfs_setup!();
        let dir1 = tmpdir.mash("dir1");
        assert_no_dir!(&dir1);
        assert_mkdir_p!(&dir1);
        assert_is_dir!(&dir1);
    }

    #[test]
    fn test_assert_is_file_no_file()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        assert_no_file!(&file1);
        assert_mkfile!(&file1);
        assert_is_file!(&file1);
    }

    #[test]
    fn test_assert_is_symlink_no_symlink()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        let link1 = tmpdir.mash("link1");

        // happy path
        assert_no_symlink!(&file1);
        assert_symlink!(&link1, &file1);
        assert_is_symlink!(&link1);

        // exists and is not a symlink
        assert_mkfile!(&file1);
        assert_no_symlink!(&file1);
    }

    #[test]
    fn test_assert_mkdir_m()
    {
        let tmpdir = assert_memfs_setup!();
        let dir1 = tmpdir.mash("dir1");
        assert_no_dir!(&dir1);
        assert_mkdir_m!(&dir1, 0o40777);
        assert_eq!(vfs::mode(&dir1).unwrap(), 0o40777);
        assert_is_dir!(&dir1);
    }

    #[test]
    fn test_assert_mkdir_p()
    {
        let tmpdir = assert_memfs_setup!();
        let dir1 = tmpdir.mash("dir1");
        assert_no_dir!(&dir1);
        assert_mkdir_p!(&dir1);
        assert_is_dir!(&dir1);
    }

    #[test]
    fn test_assert_mkfile()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        assert_no_file!(&file1);
        assert_mkfile!(&file1);
        assert_is_file!(&file1);
    }

    #[test]
    fn test_assert_read_all()
    {
        let tmpdir = assert_memfs_setup!();
        let file = tmpdir.mash("foo");
        assert_write_all!(&file, b"foobar 1");
        assert_read_all!(&file, "foobar 1".to_string());
    }

    #[test]
    fn test_assert_readlink()
    {
        let tmpdir = assert_memfs_setup!();
        let dir = tmpdir.mash("dir");
        let link = dir.mash("link");
        let file = tmpdir.mash("file");
        assert_mkdir_p!(&dir);
        assert_mkfile!(&file);

        assert_no_symlink!(&link);
        assert_symlink!(&link, &file);
        assert_is_symlink!(&link);
        assert_readlink!(&link, PathBuf::from("..").mash("file"));
    }

    #[test]
    fn test_assert_readlink_abs()
    {
        let tmpdir = assert_memfs_setup!();
        let dir = tmpdir.mash("dir");
        let link = dir.mash("link");
        let file = tmpdir.mash("file");
        assert_mkdir_p!(&dir);
        assert_mkfile!(&file);

        assert_no_symlink!(&link);
        assert_symlink!(&link, &file);
        assert_is_symlink!(&link);
        assert_readlink_abs!(&link, &file);
    }

    #[test]
    fn test_assert_remove()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        assert_remove!(&file1);
        assert_mkfile!(&file1);
        assert_is_file!(&file1);
        assert_remove!(&file1);
        assert_no_file!(&file1);
    }

    #[test]
    fn test_assert_remove_all()
    {
        let tmpdir = assert_memfs_setup!();
        let file1 = tmpdir.mash("file1");
        assert_mkfile!(&file1);
        assert_is_file!(&file1);
        assert_remove_all!(&tmpdir);
        assert_no_dir!(&tmpdir);
    }

    #[test]
    fn test_assert_symlink()
    {
        let tmpdir = assert_memfs_setup!();
        let dir1 = tmpdir.mash("dir1");
        let file1 = dir1.mash("file1");
        let link1 = tmpdir.mash("link1");
        assert_mkdir_p!(&dir1);
        assert_mkfile!(&file1);

        assert_no_symlink!(&link1);
        assert_symlink!(&link1, &file1);
        assert_is_symlink!(&link1);
    }

    #[test]
    fn test_assert_write_all()
    {
        let tmpdir = assert_memfs_setup!();
        let file = tmpdir.mash("foo");
        assert_write_all!(&file, b"foobar 1");
        assert_read_all!(&file, "foobar 1".to_string());
    }
}