2019-07-17 19:51:18 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
pub use std::path::PathBuf;
|
2019-07-16 10:28:55 +00:00
|
|
|
|
2019-07-17 18:56:15 +00:00
|
|
|
#[macro_export]
|
2019-07-16 10:28:55 +00:00
|
|
|
macro_rules! nu {
|
|
|
|
($out:ident, $cwd:expr, $commands:expr) => {
|
2019-07-17 19:51:18 +00:00
|
|
|
pub use std::io::prelude::*;
|
|
|
|
pub use std::process::{Command, Stdio};
|
|
|
|
pub use std::error::Error;
|
2019-07-16 10:28:55 +00:00
|
|
|
|
2019-07-17 18:56:15 +00:00
|
|
|
let commands = &*format!(
|
|
|
|
"
|
2019-07-16 10:28:55 +00:00
|
|
|
cd {}
|
|
|
|
{}
|
2019-07-17 18:56:15 +00:00
|
|
|
exit",
|
|
|
|
$cwd, $commands
|
|
|
|
);
|
2019-07-16 10:28:55 +00:00
|
|
|
|
|
|
|
let process = match Command::new(helpers::executable_path())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
2019-07-17 18:56:15 +00:00
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(child) => child,
|
|
|
|
Err(why) => panic!("Can't run test {}", why.description()),
|
2019-07-16 10:28:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match process.stdin.unwrap().write_all(commands.as_bytes()) {
|
|
|
|
Err(why) => panic!("couldn't write to wc stdin: {}", why.description()),
|
|
|
|
Ok(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut _s = String::new();
|
|
|
|
|
|
|
|
match process.stdout.unwrap().read_to_string(&mut _s) {
|
|
|
|
Err(why) => panic!("couldn't read stdout: {}", why.description()),
|
2019-07-17 18:56:15 +00:00
|
|
|
Ok(_) => {
|
2019-07-16 10:28:55 +00:00
|
|
|
let _s = _s.replace("\r\n", "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:53:19 +00:00
|
|
|
let _s = _s.replace("\r\n", "");
|
2019-07-16 10:28:55 +00:00
|
|
|
let $out = _s.replace("\n", "");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-17 18:56:15 +00:00
|
|
|
#[macro_export]
|
2019-07-16 10:28:55 +00:00
|
|
|
macro_rules! nu_error {
|
|
|
|
($out:ident, $cwd:expr, $commands:expr) => {
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
2019-07-17 18:56:15 +00:00
|
|
|
let commands = &*format!(
|
|
|
|
"
|
2019-07-16 10:28:55 +00:00
|
|
|
cd {}
|
|
|
|
{}
|
2019-07-17 18:56:15 +00:00
|
|
|
exit",
|
|
|
|
$cwd, $commands
|
|
|
|
);
|
2019-07-16 10:28:55 +00:00
|
|
|
|
|
|
|
let mut process = Command::new(helpers::executable_path())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
.spawn()
|
|
|
|
.expect("couldn't run test");
|
|
|
|
|
|
|
|
let stdin = process.stdin.as_mut().expect("couldn't open stdin");
|
2019-07-17 18:56:15 +00:00
|
|
|
stdin
|
|
|
|
.write_all(commands.as_bytes())
|
|
|
|
.expect("couldn't write to stdin");
|
2019-07-16 10:28:55 +00:00
|
|
|
|
2019-07-17 18:56:15 +00:00
|
|
|
let output = process
|
|
|
|
.wait_with_output()
|
|
|
|
.expect("couldn't read from stderr");
|
2019-07-16 10:28:55 +00:00
|
|
|
let $out = String::from_utf8_lossy(&output.stderr);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-17 19:51:18 +00:00
|
|
|
pub fn create_file_at(full_path: &str) {
|
|
|
|
std::fs::write(PathBuf::from(full_path), "fake data".as_bytes()).expect("can not create file");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn file_exists_at(full_path: &str) -> bool {
|
|
|
|
PathBuf::from(full_path).exists()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_directory_at(full_path: &str) {
|
|
|
|
std::fs::remove_dir_all(PathBuf::from(full_path)).expect("can not remove directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_directory_at(full_path: &str) {
|
|
|
|
std::fs::create_dir(PathBuf::from(full_path)).expect("can not create directory");
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:28:55 +00:00
|
|
|
pub fn executable_path() -> PathBuf {
|
|
|
|
let mut buf = PathBuf::new();
|
2019-07-17 18:56:15 +00:00
|
|
|
buf.push("target");
|
|
|
|
buf.push("debug");
|
|
|
|
buf.push("nu");
|
|
|
|
buf
|
2019-07-16 10:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn in_directory(str: &str) -> &str {
|
|
|
|
str
|
2019-07-16 10:53:19 +00:00
|
|
|
}
|