nushell/tests/tests.rs

105 lines
2.4 KiB
Rust
Raw Normal View History

2019-06-02 05:37:09 +02:00
#[cfg(test)]
mod tests {
2019-06-15 04:24:13 +02:00
use std::error::Error;
2019-06-02 05:37:09 +02:00
use std::io::prelude::*;
2019-06-15 04:24:13 +02:00
use std::path::PathBuf;
2019-06-02 05:37:09 +02:00
use std::process::{Command, Stdio};
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())
2019-06-15 04:24:13 +02:00
.spawn()
{
2019-06-02 05:37:09 +02:00
Ok(process) => process,
2019-06-15 04:24:13 +02:00
Err(why) => panic!("Can't run test {}", why.description()),
2019-06-02 05:37:09 +02:00
};
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()) {
2019-06-15 04:24:13 +02:00
Err(why) => panic!("couldn't write to wc stdin: {}", why.description()),
Ok(_) => {}
2019-06-02 05:37:09 +02:00
}
let mut s = String::new();
match process.stdout.unwrap().read_to_string(&mut s) {
2019-06-15 04:24:13 +02:00
Err(why) => panic!("couldn't read stdout: {}", why.description()),
2019-06-02 05:37:09 +02:00
Ok(_) => {
let s = s.replace("\r\n", "\n");
assert_eq!(s, baseline_out);
}
}
}
#[test]
2019-06-03 03:26:29 +02:00
fn open_toml() {
2019-06-02 05:37:09 +02:00
test_helper("open_toml");
}
2019-06-03 02:37:16 +02:00
#[test]
2019-06-03 03:26:29 +02:00
fn open_json() {
2019-06-03 02:37:16 +02:00
test_helper("open_json");
}
2019-06-11 08:32:42 +02:00
#[test]
fn open_xml() {
test_helper("open_xml");
}
2019-06-16 08:43:40 +02:00
#[test]
fn open_ini() {
test_helper("open_ini");
}
2019-06-03 02:37:16 +02:00
#[test]
fn json_roundtrip() {
test_helper("json_roundtrip");
}
#[test]
fn toml_roundtrip() {
test_helper("toml_roundtrip");
}
#[test]
fn sort_by() {
test_helper("sort_by");
}
#[test]
fn split() {
test_helper("split");
}
2019-06-15 04:24:13 +02:00
#[test]
fn enter() {
test_helper("enter");
}
2019-06-18 05:45:50 +02:00
#[test]
fn lines() {
test_helper("lines");
}
2019-06-03 03:26:29 +02:00
}