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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
extern crate tempfile;
extern crate tar;
use std::path::Path;
use std::path::PathBuf;
const FILES: &'static [(&'static str, &'static str)] = &[
("Cargo.toml", r#"
[package]
name = "pint"
version = "0.1.0"
[dependencies]
fomat-macros = { path = "PATH" }
"#),
("src/main.rs", r#"
#[macro_use] extern crate fomat_macros;
fn foo() {
pint!("stdout"(1));
pintln!("stdout"(2));
epint!("stderr"(1));
epintln!("stderr"(2));
perr!("stderr"(1));
perrln!("stderr"(2));
}
fn main() {
foo();
}
#[test]
fn capturing() {
foo();
}
"#),
("src/lib.rs", r#"
#[macro_use] extern crate fomat_macros;
pub fn called_from_other_crate() {
pintln!("nocapture"(1));
}
"#),
("tests/nocapture.rs", r#"
extern crate pint;
#[test]
fn nocapture() {
pint::called_from_other_crate();
}
"#),
];
fn unpack_files(directory: &Path, replace_path: &str) {
use tar::{Builder, Header, Archive};
let mut builder = Builder::new(Vec::new());
for &(name, data) in FILES {
let data = data.replace("PATH", replace_path);
let mut header = Header::new_gnu();
header.set_path(name).unwrap();
header.set_size(data.len() as u64);
header.set_cksum();
builder.append(&header, data.as_bytes()).unwrap();
}
let archive: &[u8] = &builder.into_inner().unwrap();
Archive::new(archive).unpack(directory)
.expect("Can't unpack test crate");
}
#[test]
fn capturing() {
use std::process::Command;
use std::env;
use std::str::from_utf8;
let rootdir = {
let mut rootdir = env::current_exe().unwrap();
while rootdir.file_name() != Some("target".as_ref()) {
assert!( rootdir.pop() );
}
assert!( rootdir.pop() );
rootdir
};
let pintdir = tempfile::Builder::new().prefix("fomat-macros-capturing-test").tempdir()
.expect("Can't create tempdir");
unpack_files(pintdir.as_ref(), /*rootdir.to_str().unwrap()*/&env::var("CARGO_MANIFEST_DIR").unwrap());
assert!(
Command::new("cargo").arg("build")
.env_remove("CARGO_TARGET_DIR")
.current_dir(&pintdir)
.status().unwrap().success()
);
let expected_stdout = "stdout1stdout2\n";
let expected_stderr = "stderr1stderr2\nstderr1stderr2\n";
/*use std::io::stdout;
use std::io::Write;
let output = Command::new("find")
.current_dir(&pintdir)
.output().unwrap();
stdout().write_all(&output.stdout);
for (key, value) in env::vars() {
println!("{key}: {value}");
}*/
let pintexe;
if pintdir.as_ref().join("target/debug/pint").exists() {
pintexe = PathBuf::from("target/debug/pint");
} else if pintdir.as_ref().join("target").join(env::var("DEB_TARGET_RUST_TYPE").unwrap_or("___envar_not_found___".to_string())).join("debug/pint").exists() {
pintexe = Path::new("target").join(env::var("DEB_TARGET_RUST_TYPE").unwrap()).join("debug/pint");
} else {
panic!("can't find pint exuctable");
}
let output = Command::new(pintexe)
.current_dir(&pintdir)
.output().unwrap();
assert!(output.status.success());
assert_eq!(from_utf8(&output.stdout).unwrap(), expected_stdout);
assert_eq!(from_utf8(&output.stderr).unwrap(), expected_stderr);
let output = Command::new("cargo")
.arg("test")
.current_dir(&pintdir)
.output().unwrap();
assert!(output.status.success());
assert!(!from_utf8(&output.stdout).unwrap().contains(expected_stdout));
assert!(from_utf8(&output.stderr).unwrap().contains(expected_stderr));
assert!(from_utf8(&output.stdout).unwrap().contains("nocapture1"));
let output = Command::new("cargo")
.args(&["test", "--", "--nocapture"])
.current_dir(&pintdir)
.output().unwrap();
assert!(output.status.success());
assert!(from_utf8(&output.stdout).unwrap().contains(expected_stdout));
assert!(from_utf8(&output.stderr).unwrap().contains(expected_stderr));
}
|