insta/
utils.rs

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
use std::{
    borrow::Cow,
    env,
    io::Write,
    path::Path,
    process::{Command, Stdio},
};

/// Are we running in in a CI environment?
pub fn is_ci() -> bool {
    match env::var("CI").ok().as_deref() {
        Some("false") | Some("0") | Some("") => false,
        None => env::var("TF_BUILD").is_ok(),
        Some(_) => true,
    }
}

#[cfg(feature = "colors")]
pub use console::style;

#[cfg(not(feature = "colors"))]
mod fake_colors {
    pub struct FakeStyledObject<D>(D);

    macro_rules! style_attr {
        ($($name:ident)*) => {
            $(
                #[inline]
                pub fn $name(self) -> FakeStyledObject<D> { self }
            )*
        }
    }

    impl<D> FakeStyledObject<D> {
        style_attr!(red green yellow cyan bold dim underlined);
    }

    impl<D: std::fmt::Display> std::fmt::Display for FakeStyledObject<D> {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            std::fmt::Display::fmt(&self.0, f)
        }
    }

    pub fn style<D>(val: D) -> FakeStyledObject<D> {
        FakeStyledObject(val)
    }
}

#[cfg(not(feature = "colors"))]
pub use self::fake_colors::*;

/// Returns the term width that insta should use.
pub fn term_width() -> usize {
    #[cfg(feature = "colors")]
    {
        console::Term::stdout().size().1 as usize
    }
    #[cfg(not(feature = "colors"))]
    {
        74
    }
}

/// Converts a path into a string that can be persisted.
pub fn path_to_storage(path: &Path) -> String {
    #[cfg(windows)]
    {
        path.to_str().unwrap().replace('\\', "/")
    }

    #[cfg(not(windows))]
    {
        path.to_string_lossy().into()
    }
}

/// Tries to format a given rust expression with rustfmt
pub fn format_rust_expression(value: &str) -> Cow<'_, str> {
    const PREFIX: &str = "const x:() = ";
    const SUFFIX: &str = ";\n";
    if let Ok(mut proc) = Command::new("rustfmt")
        .arg("--emit=stdout")
        .arg("--edition=2018")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
    {
        {
            let stdin = proc.stdin.as_mut().unwrap();
            stdin.write_all(PREFIX.as_bytes()).unwrap();
            stdin.write_all(value.as_bytes()).unwrap();
            stdin.write_all(SUFFIX.as_bytes()).unwrap();
        }
        if let Ok(output) = proc.wait_with_output() {
            if output.status.success() {
                // slice between after the prefix and before the suffix
                // (currently 14 from the start and 2 before the end, respectively)
                let start = PREFIX.len() + 1;
                let end = output.stdout.len() - SUFFIX.len();
                return std::str::from_utf8(&output.stdout[start..end])
                    .unwrap()
                    .replace("\r\n", "\n")
                    .into();
            }
        }
    }
    Cow::Borrowed(value)
}

#[test]
fn test_format_rust_expression() {
    use crate::assert_snapshot;
    assert_snapshot!(format_rust_expression("vec![1,2,3]"), @"vec![1, 2, 3]");
    assert_snapshot!(format_rust_expression("vec![1,2,3].iter()"), @"vec![1, 2, 3].iter()");
    assert_snapshot!(format_rust_expression(r#"    "aoeu""#), @r###""aoeu""###);
    assert_snapshot!(format_rust_expression(r#"  "aoe😄""#), @r###""aoe😄""###);
    assert_snapshot!(format_rust_expression("😄😄😄😄😄"), @"😄😄😄😄😄")
}