File: test_helpers.rs

package info (click to toggle)
rust-findutils 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,252 kB
  • sloc: sh: 129; python: 31; makefile: 9
file content (100 lines) | stat: -rw-r--r-- 2,878 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 Google Inc.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

use std::cell::RefCell;
use std::env;
use std::io::{Cursor, Read, Write};
use std::path::Path;
use std::time::SystemTime;

use findutils::find::matchers::{Follow, MatcherIO, WalkEntry};
use findutils::find::Dependencies;

/// A copy of `find::tests::FakeDependencies`.
/// TODO: find out how to share #[cfg(test)] functions/structs between unit
/// and integration tests.
pub struct FakeDependencies {
    pub output: RefCell<Cursor<Vec<u8>>>,
    now: SystemTime,
}

impl FakeDependencies {
    pub fn new() -> Self {
        Self {
            output: RefCell::new(Cursor::new(Vec::<u8>::new())),
            now: SystemTime::now(),
        }
    }

    pub fn new_matcher_io(&self) -> MatcherIO<'_> {
        MatcherIO::new(self)
    }

    pub fn get_output_as_string(&self) -> String {
        let mut cursor = self.output.borrow_mut();
        cursor.set_position(0);
        let mut contents = String::new();
        cursor.read_to_string(&mut contents).unwrap();
        contents
    }
}

impl Dependencies for FakeDependencies {
    fn get_output(&self) -> &RefCell<dyn Write> {
        &self.output
    }

    fn now(&self) -> SystemTime {
        self.now
    }
}

pub fn path_to_testing_commandline() -> String {
    let mut path_to_use = env::current_exe()
        // this will be something along the lines of /my/homedir/findutils/target/debug/deps/findutils-5532804878869ef1
        .expect("can't find path of this executable")
        .parent()
        .expect("can't find parent directory of this executable")
        .to_path_buf();
    // and we want /my/homedir/findutils/target/debug/testing-commandline
    if path_to_use.ends_with("deps") {
        path_to_use.pop();
    }
    path_to_use = path_to_use.join("testing-commandline");
    path_to_use.to_string_lossy().to_string()
}

#[cfg(windows)]
/// A copy of find::tests::fix_up_slashes.
/// TODO: find out how to share #[cfg(test)] functions/structs between unit
/// and integration tests.
pub fn fix_up_slashes(path: &str) -> String {
    path.replace("/", "\\")
}

#[cfg(not(windows))]
pub fn fix_up_slashes(path: &str) -> String {
    path.to_string()
}

/// A copy of `find::matchers::tests::get_dir_entry_for`.
/// TODO: find out how to share #[cfg(test)] functions/structs between unit
/// and integration tests.
pub fn get_dir_entry_for(root: &str, path: &str) -> WalkEntry {
    let root = fix_up_slashes(root);
    let root = Path::new(&root);

    let path = fix_up_slashes(path);
    let path = if path.is_empty() {
        root.to_owned()
    } else {
        root.join(path)
    };

    let depth = path.components().count() - root.components().count();

    WalkEntry::new(path, depth, Follow::Never)
}