diff --git a/src/main.rs b/src/main.rs index ab6cf7e095..4705b34bb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ mod parser; mod prelude; mod shell; mod stream; +mod tests; use clap::{App, Arg}; use log::LevelFilter; diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000000..25b027af5a --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,61 @@ +#[cfg(test)] +mod tests { + use std::path::PathBuf; + use std::io::prelude::*; + use std::process::{Command, Stdio}; + use std::error::Error; + + fn test_helper(test_name: &str) { + let mut baseline_path = PathBuf::new(); + baseline_path.push("tests"); + baseline_path.push(test_name); + baseline_path.set_extension("out"); + + let mut txt_path = PathBuf::new(); + txt_path.push("tests"); + txt_path.push(test_name); + txt_path.set_extension("txt"); + + let executable = { + let mut buf = PathBuf::new(); + buf.push("target"); + buf.push("debug"); + buf.push("nu"); + buf + }; + + let process = match Command::new(executable) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() { + + Ok(process) => process, + Err(why) => panic!("Can't run test {}", why.description()) + }; + + let baseline_out = std::fs::read_to_string(baseline_path).unwrap(); + let baseline_out = baseline_out.replace("\r\n", "\n"); + let input_commands = std::fs::read_to_string(txt_path).unwrap(); + + match process.stdin.unwrap().write_all(input_commands.as_bytes()) { + Err(why) => panic!("couldn't write to wc stdin: {}", + why.description()), + Ok(_) => println!("sent pangram to wc"), + } + + let mut s = String::new(); + match process.stdout.unwrap().read_to_string(&mut s) { + Err(why) => panic!("couldn't read stdout: {}", + why.description()), + Ok(_) => { + let s = s.replace("\r\n", "\n"); + assert_eq!(s, baseline_out); + } + } + } + + #[test] + fn test_toml() { + test_helper("open_toml"); + } +} \ No newline at end of file diff --git a/tests/open_toml.out b/tests/open_toml.out new file mode 100644 index 0000000000..b39a36a7c1 --- /dev/null +++ b/tests/open_toml.out @@ -0,0 +1 @@ +2018 diff --git a/tests/open_toml.txt b/tests/open_toml.txt new file mode 100644 index 0000000000..4c93effadf --- /dev/null +++ b/tests/open_toml.txt @@ -0,0 +1,2 @@ +open tests\test.toml | select package.edition | echo $it +exit