nu_test_support/
lib.rs

1#![doc = include_str!("../README.md")]
2pub mod commands;
3pub mod fs;
4pub mod locale_override;
5pub mod macros;
6pub mod playground;
7use std::process::ExitStatus;
8
9// Needs to be reexported for `nu!` macro
10pub use nu_path;
11
12#[derive(Debug)]
13pub struct Outcome {
14    pub out: String,
15    pub err: String,
16    pub status: ExitStatus,
17}
18
19#[cfg(windows)]
20pub const NATIVE_PATH_ENV_VAR: &str = "Path";
21#[cfg(not(windows))]
22pub const NATIVE_PATH_ENV_VAR: &str = "PATH";
23
24#[cfg(windows)]
25pub const NATIVE_PATH_ENV_SEPARATOR: char = ';';
26#[cfg(not(windows))]
27pub const NATIVE_PATH_ENV_SEPARATOR: char = ':';
28
29impl Outcome {
30    pub fn new(out: String, err: String, status: ExitStatus) -> Outcome {
31        Outcome { out, err, status }
32    }
33}
34
35/// Reformat a multiline pipeline into a single line for use with `nu -c`
36///
37/// Warning: Will not correctly handle statements that are not `;` separated!
38pub fn pipeline(commands: &str) -> String {
39    commands
40        .trim()
41        .lines()
42        .map(|line| line.trim())
43        .collect::<Vec<&str>>()
44        .join(" ")
45        .trim_end()
46        .to_string()
47}
48
49pub fn nu_repl_code(source_lines: &[&str]) -> String {
50    let mut out = String::from("nu --testbin=nu_repl ...[ ");
51
52    for line in source_lines.iter() {
53        // convert each "line" to really be a single line to prevent nu! macro joining the newlines
54        // with ';'
55        let line = pipeline(line);
56
57        out.push('`');
58        out.push_str(&line);
59        out.push('`');
60        out.push(' ');
61    }
62
63    out.push(']');
64
65    out
66}
67
68pub fn shell_os_paths() -> Vec<std::path::PathBuf> {
69    let mut original_paths = vec![];
70
71    if let Some(paths) = std::env::var_os(NATIVE_PATH_ENV_VAR) {
72        original_paths = std::env::split_paths(&paths).collect::<Vec<_>>();
73    }
74
75    original_paths
76}
77
78#[cfg(test)]
79mod tests {
80    use super::pipeline;
81
82    #[test]
83    fn constructs_a_pipeline() {
84        let actual = pipeline(
85            r#"
86                open los_tres_amigos.txt
87                | from-csv
88                | get rusty_luck
89                | into int
90                | math sum
91                | echo "$it"
92            "#,
93        );
94
95        assert_eq!(
96            actual,
97            r#"open los_tres_amigos.txt | from-csv | get rusty_luck | into int | math sum | echo "$it""#
98        );
99    }
100}