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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
//! Companion crate to ‘bintest' and 'testcall’, implements facilities for running tests in
//! directories.
//!
//!
//! # Description
//! Allows creating of (temporary) test directories, optionally with a custom callback for cleanup.
//! Populating these with content for testing and provide assertion to validate the content.
//!
//!
//! # Example
//!
//! ```
//! # use tempfile::TempDir;
//! # use testpath::*;
//! fn test_something() {
//!     let tmpdir = TempDir::new().expect("TempDir created");
//!     tmpdir.create_file("path/to/testfile", "Hello File!".as_bytes());
//!     tmpdir
//!         .sub_path("path/to/testfile")
//!         .assert_utf8("Hello File!");
//! }
//! ```
//!
//!
//! # Panics vs. Results
//!
//! 'testpath' is made explicitly for writing tests. To ease this it prefers aborting by panic
//! over error handling. When anything goes wrong the test is aborted and the cause is
//! reported.
//!
use std::fs;
use std::fs::File;
use std::io;
use std::io::{BufReader, Read};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use testcall::regex::*;
use path_absolutize::*;

/// Trait for test directoy objects
pub trait TestPath {
    /// Return the underlying Path of an TestPath implementation
    fn path(&self) -> &Path;

    /// Return a canonalized/normalized PathBuf to components within the testpath. Assert and
    /// panic when path escapes from the testpath. Handles non existing components.
    #[track_caller]
    fn sub_path<P>(&self, subcomponents: P) -> SubPath
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let testpath = self.path();
        let subpath = PathBuf::from(&testpath)
            .join(subcomponents.as_ref())
            .normalize();
        assert!(subpath.starts_with(testpath), "escaped from testpath");
        SubPath {
            _testpath: self,
            subpath,
        }
    }

    /// Return a canonalized/normalized PathBuf to components within the testpath. Assert and
    /// panic when path escapes from the testpath. Asserts that the given subpath exists.
    #[track_caller]
    fn sub_path_exists<P>(&self, subcomponents: P) -> SubPath
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let path = self.sub_path(subcomponents);
        assert!(path.exists(), "path exists: {:?}", path.subpath);
        path
    }

    /// Return a canonalized/normalized PathBuf to components within the testpath. Assert and
    /// panic when path escapes from the testpath. Asserts that the given subpath does not exist.
    #[track_caller]
    fn sub_path_available<P>(&self, subcomponents: P) -> SubPath
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let path = self.sub_path(subcomponents);
        assert!(!path.exists(), "path does not exist: {:?}", path.subpath);
        path
    }
}

/// Trait for test directoy objects
pub trait Fixtures: TestPath {
    /// Create a file with the given content in the test directory. Any leading directories
    /// are created automatically. The file itself must not already exist.
    #[track_caller]
    fn create_file<P>(&self, name: P, content: &[u8]) -> &Self
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let path = self.sub_path_available(name);

        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).expect("create directory");
        }

        fs::write(path.subpath, content).expect("create file");

        self
    }

    /// Create a directory within the test directory. Any leading directories
    /// are created automatically. The path must not exist already.
    #[track_caller]
    fn create_dir<P>(&self, name: P) -> &Self
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let path = self.sub_path_available(name);
        fs::create_dir_all(path).expect("create directory");
        self
    }

    /// Install something (from outside) into 'self'.
    /// 'from' must be some existing directory or a file, symlinks are resolved.
    ///
    /// Following semantics apply when `from` is a *file*:
    ///
    /// | self is     | What is done                                                                    |
    /// |-------------|---------------------------------------------------------------------------------|
    /// | nonexistant | `self` last component is the new filename, parent dirs are created              |
    /// | dir         | `from` is copied into `self`, but won't overwrite an existing file              |
    /// | file        | `self` gets overwritten                                                         |
    ///
    /// Following semantics apply when `from` is a *directory*:
    ///
    /// | self is     | What is done                                                                    |
    /// |-------------|---------------------------------------------------------------------------------|
    /// | nonexistant | `self` including parents are created, the content of `from` is copied into that |
    /// | dir         | the content of `from` is copied into `self`                                     |
    /// | file        | *ERROR*                                                                         |
    ///
    #[track_caller]
    fn install_from<P>(&self, from: P) -> &Self
    where
        P: AsRef<Path> + Sized,
        Self: Sized,
    {
        let from: PathBuf = if from.as_ref() == Path::new("") {
            PathBuf::from(".")
        } else {
            from.as_ref().normalize()
        };
        assert!(from.exists(), "source does not exist: {:?}", &from);

        if from.is_file() {
            if !self.path().exists() {
                if let Some(parent) = self.path().parent() {
                    fs::create_dir_all(parent).expect("create directories");
                }
                fs::copy(&from, self.path()).expect("file copied");
            }
            if self.path().is_file() {
                fs::copy(&from, self.path()).expect("file copied");
            } else if self.path().is_dir() {
                let target = PathBuf::from(self.path()).join(from.file_name().unwrap());
                assert!(
                    !target.exists(),
                    "won't overwrite existing file: {:?}",
                    &target
                );
                fs::copy(&from, &target).expect("file copied");
            } else {
                panic!("unsupported file type: {:?}", self.path());
            }
        } else if from.is_dir() {
            if !self.path().exists() {
                fs::create_dir_all(self.path()).expect("create directories");
                copy_all(&from, self.path());
            }
            if self.path().is_file() {
                panic!(
                    "cant not copy a dir into a file: {:?} -> {:?}",
                    &from,
                    self.path()
                );
            } else if self.path().is_dir() {
                copy_all(&from, self.path());
            } else {
                panic!("unsupported file type: {:?}", self.path());
            }
        } else {
            panic!("unsupported file type: {:?}", &from);
        }

        self
    }

    /// Delete an element from a testpath. Directories are deleted as well.  This trait
    /// functions defaults to `unimplemented` because it is deemed to be dangerous. Only the
    /// trait implementations which create an disposable directory implement it.
    #[track_caller]
    fn delete<N>(&self, _name: &N) -> &Self
    where
        N: AsRef<Path> + ?Sized,
    {
        unimplemented!()
    }
}

/// Assertions on content of a TestPath
pub trait PathAssertions: TestPath {
    /// Assert that at the given path exists
    #[track_caller]
    fn assert_exists(&self) -> &Self {
        assert!(self.path().exists(), "path exists");
        self
    }

    /// Assert that the given path does not exist
    #[track_caller]
    fn assert_available(&self) -> &Self {
        assert!(!self.path().exists(), "path does not exist");
        self
    }

    /// Assert that the given path is a directory
    #[track_caller]
    fn assert_is_dir(&self) -> &Self {
        assert!(self.path().is_dir());
        self
    }

    /// Assert that the given path is a file
    #[track_caller]
    fn assert_is_file(&self) -> &Self {
        assert!(self.path().is_file());
        self
    }

    /// Assert that the given path is a symlink
    #[track_caller]
    fn assert_is_symlink(&self) -> &Self {
        assert!(self
            .path()
            .symlink_metadata()
            .unwrap()
            .file_type()
            .is_symlink());
        self
    }

    /// Assert that the given path resolves to a element of the given size
    #[track_caller]
    fn assert_size(&self, size: u64) -> &Self {
        assert_eq!(self.path().metadata().unwrap().len(), size);
        self
    }

    /// Assert that the given path resolves to a element of more than the given size
    #[track_caller]
    fn assert_size_greater(&self, size: u64) -> &Self {
        assert!(self.path().metadata().unwrap().len() > size);
        self
    }

    /// Assert that the given path resolves to a element of less than the given size
    #[track_caller]
    fn assert_size_smaller(&self, size: u64) -> &Self {
        assert!(self.path().metadata().unwrap().len() < size);
        self
    }

    /// Assert that self contains exactly the same content than another path (directories are
    /// recursed).
    #[track_caller]
    fn assert_equal<M>(&self, with: &M) -> &Self
    where
        M: AsRef<Path> + ?Sized,
        Self: Sized,
    {
        let with: PathBuf = if with.as_ref() == Path::new("") {
            PathBuf::from(".")
        } else {
            with.as_ref().normalize()
        };

        compare_all(self.path(), with.as_ref(), ComparePolicy::Exact);

        self
    }

    /// Assert that self is is structurally equivalent to another path (contain the same
    /// files). File contents are not compared.
    #[track_caller]
    fn assert_structure<M>(&self, with: &M) -> &Self
    where
        M: AsRef<Path> + ?Sized,
        Self: Sized,
    {
        let with: PathBuf = if with.as_ref() == Path::new("") {
            PathBuf::from(".")
        } else {
            with.as_ref().normalize()
        };

        compare_all(self.path(), with.as_ref(), ComparePolicy::Structure);

        self
    }

    /// Assert that self contains the same content than another path for files that exist on
    /// both sides. Surplus files on either side are ignored.
    #[track_caller]
    fn assert_existing<M>(&self, with: &M) -> &Self
    where
        M: AsRef<Path> + ?Sized,
        Self: Sized,
    {
        let with: PathBuf = if with.as_ref() == Path::new("") {
            PathBuf::from(".")
        } else {
            with.as_ref().normalize()
        };

        compare_all(self.path(), with.as_ref(), ComparePolicy::Existing);

        self
    }

    /// Assert that a file content matches the given regex in utf8.
    #[track_caller]
    fn assert_utf8(&self, regex: &str) -> &Self {
        let mut data = String::new();
        File::open(self.path())
            .expect("readable file")
            .read_to_string(&mut data)
            .expect("all data read");

        let (ok, bytes) = regex_match_utf8(data.as_bytes(), regex);
        assert!(
            ok,
            "{:?} does not match {}\ngot: {}",
            self.path(), regex, bytes
        );
        self
    }

    /// Assert that a file content matches the given regex in bytes.
    #[track_caller]
    fn assert_bytes(&self, regex: &str) -> &Self {
        let mut data = Vec::new();
        File::open(self.path())
            .expect("readable file")
            .read_to_end(&mut data)
            .expect("all data read");

        let (ok, bytes) = regex_match_bytes(data.as_slice(), regex);
        assert!(
            ok,
            "{:?} does not match:{}\ngot {}",
            self.path(), regex, bytes
        );
        self
    }

    /// Return all captures from a regex in utf8.
    #[track_caller]
    fn captures_utf8(&self, regex: &str) -> Captured {
        let mut data = String::new();
        File::open(self.path())
            .expect("readable file")
            .read_to_string(&mut data)
            .expect("all data read");

        captures_utf8(data.as_bytes(), regex)
    }
}

/// A Path that lives within a TestPath and must not outlive it.
pub struct SubPath<'a> {
    subpath: PathBuf,
    _testpath: &'a dyn TestPath, // contrains the lifetime to the parent
}

impl Deref for SubPath<'_> {
    type Target = PathBuf;
    fn deref(&self) -> &Self::Target {
        &self.subpath
    }
}

impl AsRef<Path> for SubPath<'_> {
    fn as_ref(&self) -> &Path {
        &self.subpath
    }
}

impl TestPath for Path {
    fn path(&self) -> &Path {
        self
    }
}

impl Fixtures for Path {}
impl PathAssertions for Path {}

impl TestPath for PathBuf {
    fn path(&self) -> &Path {
        self.as_path()
    }
}

impl Fixtures for PathBuf {}
impl PathAssertions for PathBuf {}

impl TestPath for TempDir {
    fn path(&self) -> &Path {
        self.path()
    }
}

impl Fixtures for TempDir {
    fn delete<N>(&self, name: &N) -> &Self
    where
        N: AsRef<Path> + ?Sized,
    {
        let name = PathBuf::from(self.path()).join(name);
        if name.is_dir() {
            fs::remove_dir_all(name).expect("directory deleted");
        } else {
            fs::remove_file(name).expect("file deleted");
        }
        self
    }
}
impl PathAssertions for TempDir {}

/// Augment a TempDir with a custom callback function that can do additional cleanup work
/// (like unmounting filesystem etc.)
pub struct TempDirCleanup {
    dir: TempDir,
    cleanup_fn: fn(&TempDir),
}

impl Drop for TempDirCleanup {
    fn drop(&mut self) {
        (self.cleanup_fn)(&self.dir);
    }
}

impl TestPath for TempDirCleanup {
    fn path(&self) -> &Path {
        self.dir.path()
    }
}

impl Fixtures for TempDirCleanup {
    fn delete<N>(&self, name: &N) -> &Self
    where
        N: AsRef<Path> + ?Sized,
    {
        let name = PathBuf::from(self.path()).join(name);
        if name.is_dir() {
            fs::remove_dir_all(name).expect("directory deleted");
        } else {
            fs::remove_file(name).expect("file deleted");
        }
        self
    }
}
impl PathAssertions for TempDirCleanup {}

impl TempDirCleanup {
    /// creates a temporary directory with a cleanup function to be called at drop time.
    //TODO: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
    pub fn new(cleanup_fn: fn(&TempDir)) -> io::Result<Self> {
        Ok(TempDirCleanup {
            dir: TempDir::new()?,
            cleanup_fn,
        })
    }
}

// normalize paths in rust including components that do not exist yet
trait PathNormalize {
    fn normalize(&self) -> PathBuf;
}

impl PathNormalize for Path {
    fn normalize(&self) -> PathBuf {
        self.absolutize().expect("absolute path").into_owned()
    }
}

/// copy the contents of 'from' into 'to', recursively, symlinks resolved, existing files
/// overwritten, errors panic
fn copy_all(from: &Path, to: &Path) {
    //PLANNED: use async/smol for parallel copy
    for entry in from.read_dir().expect("got dir iterator") {
        let entry = entry.expect("entry");
        let dest = PathBuf::from(to).join(entry.file_name());
        println!("dest is {:?}", dest);
        let entry = entry.path().canonicalize().expect("existing entry");
        if entry.is_file() {
            fs::copy(entry.path(), &dest).expect("file copied");
        } else if entry.is_dir() {
            fs::create_dir_all(&dest).expect("dir created");
            copy_all(&entry, &dest);
        } else {
            panic!("unsupported file type: {:?}", entry.path());
        }
    }
}

/// Policy when comparing directories:
///  * Exact
///    all files must exist on both sides and contain the same content
///  * ExistingExact
///    Only existing files are compared, when a file is missing on either side it gets ignored
///  * Presence
///    Checks only for presence on both sides, but don't compare the content
#[derive(Copy, Clone, PartialEq)]
enum ComparePolicy {
    Exact,
    Existing,
    Structure,
}

/// compare the contents of 'a' with 'b', recursively, symlinks resolved, existing files
/// overwritten, errors panic
fn compare_all(a: &Path, b: &Path, compare_policy: ComparePolicy) {
    if a.is_file() && b.is_file() {
        if compare_policy != ComparePolicy::Structure {
            compare_file(&a, &b);
        }
    } else if a.is_dir() && b.is_dir() {
        compare_dir(&a, &b, compare_policy);
    } else {
        panic!(
            "can't compare {:?} with {:?} because of different file types",
            &a, &b
        );
    }
}

fn compare_file(a: &Path, b: &Path) {
    let a_reader = BufReader::with_capacity(65536, File::open(a).expect("a is readable"));
    let b_reader = BufReader::with_capacity(65536, File::open(b).expect("b is readable"));
    for (index, bytes) in a_reader.bytes().zip(b_reader.bytes()).enumerate() {
        match bytes {
            (Ok(a_byte), Ok(b_byte)) => {
                assert_eq!(
                    a_byte, b_byte,
                    "file {:?} doesn't match file {:?} at byte {}",
                    a, b, index
                );
            }
            (Err(err), _) => {
                panic!("failed reading: {:?}, reason: {}", a, err);
            }
            (_, Err(err)) => {
                panic!("failed reading: {:?}, reason: {}", b, err);
            }
        };
    }
}

fn compare_dir(a: &Path, b: &Path, compare_policy: ComparePolicy) {
    use std::collections::HashSet;
    let a_contents = a
        .read_dir()
        .expect("a is a readable directory")
        .map(|a| a.expect("valid entry").file_name())
        .collect::<HashSet<_>>();

    let b_contents = b
        .read_dir()
        .expect("b is a readable directory")
        .map(|b| b.expect("valid entry").file_name())
        .collect::<HashSet<_>>();

    if compare_policy == ComparePolicy::Exact {
        assert_eq!(
            a_contents.symmetric_difference(&b_contents).next(),
            None,
            "directory content matches"
        );
    }

    a_contents.intersection(&b_contents).for_each(|name| {
        compare_all(
            &a.to_path_buf().join(name),
            &b.to_path_buf().join(name),
            compare_policy,
        )
    });
}

#[cfg(test)]
#[cfg(unix)]
mod test_internals {
    use super::*;

    #[test]
    fn path_normalize() {
        assert_eq!(Path::new("/foo/bar"), Path::new("/foo/bar").normalize());
        assert_eq!(Path::new("/foo"), Path::new("/foo/bar/..").normalize());
        assert_eq!(Path::new("/foo/bar"), Path::new("/foo/./bar/.").normalize());
        assert_ne!(Path::new("/foo/bar"), Path::new("/foo/bar/..").normalize());
    }

    #[test]
    fn compare_file_same() {
        compare_file(Path::new("Cargo.toml"), Path::new("Cargo.toml"));
    }

    #[test]
    #[should_panic]
    fn compare_file_different() {
        compare_file(Path::new("Cargo.toml"), Path::new("Cargo.lock"));
    }

    #[test]
    fn compare_all_dir_dir_exact() {
        compare_all(Path::new("src"), Path::new("src"), ComparePolicy::Exact);
    }

    #[test]
    fn compare_all_dir_dir_existing() {
        compare_all(Path::new("src"), Path::new("src"), ComparePolicy::Existing);
    }

    #[test]
    fn compare_all_dir_dir_structure() {
        compare_all(Path::new("src"), Path::new("src"), ComparePolicy::Structure);
    }

    #[test]
    #[should_panic]
    fn compare_all_dir_file_fail() {
        compare_all(
            Path::new("src"),
            Path::new("Cargo.toml"),
            ComparePolicy::Structure,
        );
    }
}

#[cfg(test)]
#[cfg(unix)]
mod test_public_interface {
    use crate::*;
    use tempfile::TempDir;

    #[test]
    fn dircleanup() {
        let cleaned_up = {
            let tmpdir =
                TempDirCleanup::new(|_| println!("TempDir cleaned up")).expect("TempDir created");
            println!("TempDir path: {:?}", tmpdir.path());
            PathBuf::from(tmpdir.path())
        };

        assert!(!Path::new(&cleaned_up).exists(), "TempDir got deleted");
    }

    #[test]
    fn create_file() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("path/to/testfile", b"Hello File!");
        tmpdir.sub_path("path/to/testfile").assert_exists();
    }

    #[test]
    fn create_file_in_sub_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        let subdir = tmpdir.sub_path("path/to");
        subdir.create_file("testfile", b"Hello File!");
        tmpdir.sub_path("path/to/testfile").assert_exists();
        subdir.sub_path("testfile").assert_exists();
    }

    #[test]
    fn create_file_in_sub_dir_chained() {
        TempDir::new()
            .expect("TempDir created")
            .sub_path("path/to")
            .create_file("testfile", b"Hello File!")
            .sub_path("testfile")
            .assert_exists();
    }

    #[test]
    #[should_panic]
    fn create_file_fail() {
        let tmpdir = TempDir::new().expect("TempDir created");
        println!("TempDir path: {:?}", tmpdir.path());
        tmpdir.create_file("path/to/testfile", "Hello File!".as_bytes());
        tmpdir.sub_path("path/to/wrongfile").assert_exists();
    }

    #[test]
    #[should_panic]
    fn create_file_again_fails() {
        let tmpdir = TempDir::new().expect("TempDir created");
        println!("TempDir path: {:?}", tmpdir.path());
        tmpdir.create_file("path/to/testfile", "Hello File!".as_bytes());
        tmpdir.create_file("path/to/testfile", "Hello File again!".as_bytes());
    }

    #[test]
    fn create_is_something() {
        let tmpdir = TempDir::new().expect("TempDir created");
        println!("TempDir path: {:?}", tmpdir.path());
        tmpdir.create_file("path/to/testfile", "Hello File!".as_bytes());
        tmpdir
            .sub_path("path/to")
            .assert_is_dir()
            .sub_path("testfile")
            .assert_exists()
            .assert_is_file();
    }

    #[test]
    fn create_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        println!("TempDir path: {:?}", tmpdir.path());
        tmpdir.create_dir("path/to/test/dir");
        tmpdir.sub_path("path/to/test/dir").assert_is_dir();
    }

    #[test]
    #[should_panic]
    fn create_dir_fail() {
        let tmpdir = TempDir::new().expect("TempDir created");
        println!("TempDir path: {:?}", tmpdir.path());
        tmpdir.sub_path("doesnotexist").assert_is_dir();
    }

    #[test]
    #[should_panic]
    fn install_from_nonexistant() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.install_from("doesnotexist");
    }

    #[test]
    fn install_from_file_to_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.install_from("Cargo.toml");
        tmpdir.sub_path("Cargo.toml").assert_equal("Cargo.toml");
    }

    #[test]
    #[should_panic]
    fn install_from_file_to_dir_existing_file() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("Cargo.toml", "Hello File!".as_bytes());
        tmpdir.install_from("Cargo.toml");
    }

    #[test]
    fn install_from_file_to_file() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir.sub_path("testfile").install_from("Cargo.toml");
        tmpdir.sub_path("testfile").assert_equal("Cargo.toml");
    }

    #[test]
    fn install_from_file_to_nonexisting_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir
            .sub_path("subdir/Cargo.toml")
            .install_from("Cargo.toml");
        tmpdir
            .sub_path("subdir/Cargo.toml")
            .assert_equal("Cargo.toml");
    }

    #[test]
    fn install_from_dir_to_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.install_from("src");
        tmpdir.assert_equal("src");
    }

    #[test]
    fn install_from_dir_to_nonexisting_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.sub_path("src").install_from("src");
        tmpdir.sub_path("src").assert_equal("src");
    }

    #[test]
    fn install_from_dir_to_dir_overwriting_file() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("lib.rs", "Hello File!".as_bytes());
        tmpdir.install_from("src");
        tmpdir.assert_equal("src");
    }

    #[test]
    #[cfg(feature = "expensive_tests")]
    fn install_from_current_dir_to_dir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.install_from("");
        tmpdir.sub_path("Cargo.toml").assert_equal("Cargo.toml");
    }

    #[test]
    fn install_from_dir_to_dir_structurecheck() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.install_from("src");
        tmpdir.assert_structure("src");
    }

    #[test]
    #[should_panic]
    fn install_from_dir_to_file() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("src", "Hello File!".as_bytes());
        tmpdir.sub_path("src").install_from("src");
    }

    #[test]
    fn delete_file_in_tempdir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir.delete("testfile");
        tmpdir.sub_path("testfile").assert_available();
    }

    #[test]
    fn delete_dir_in_tempdir() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_dir("test/dir");
        tmpdir.delete("test");
        tmpdir.sub_path("test").assert_available();
    }

    #[test]
    #[should_panic]
    fn delete_in_path() {
        let underlay = TempDir::new().expect("TempDir created");
        let tmpdir = PathBuf::from(underlay.path());
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir.delete("testfile");
    }

    #[test]
    fn assert_utf8() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir
            .sub_path("testfile")
            .assert_utf8("Hello File!");
    }

    #[test]
    #[should_panic]
    fn assert_utf8_nomatch() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir
            .sub_path("testfile")
            .assert_utf8("Hello World");
    }

    #[test]
    #[should_panic]
    fn assert_utf8_not_utf8() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", b"\xfaFile!");
        tmpdir
            .sub_path("testfile")
            .assert_utf8("Hello File!");
    }

    #[test]
    fn assert_bytes() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir
            .sub_path("testfile")
            .assert_bytes("Hello File!");
    }

    #[test]
    #[should_panic]
    fn assert_bytes_nomatch() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        tmpdir
            .sub_path("testfile")
            .assert_bytes("Hello World!");
    }

    #[test]
    fn captures_utf8() {
        let tmpdir = TempDir::new().expect("TempDir created");
        tmpdir.create_file("testfile", "Hello File!".as_bytes());
        let captures = tmpdir
            .sub_path("testfile")
            .captures_utf8("(?P<first>[^ ]*) (?P<second>[^ ]*)");

        assert_eq!(&captures[0], "Hello File!");
        assert_eq!(&captures[1], "Hello");
        assert_eq!(&captures[2], "File!");
        assert_eq!(&captures["first"], "Hello");
        assert_eq!(&captures["second"], "File!");
    }
}