| 1 |
extern crate tempfile; |
| 2 |
extern crate tar; |
| 3 |
|
| 4 |
use std::path::Path; |
| 5 |
|
| 6 |
const FILES: &'static [(&'static str, &'static str)] = &[ |
| 7 |
("Cargo.toml", r#" |
| 8 |
[package] |
| 9 |
name = "pint" |
| 10 |
version = "0.1.0" |
| 11 |
|
| 12 |
[dependencies] |
| 13 |
fomat-macros = { path = "PATH" } |
| 14 |
"#), |
| 15 |
("src/main.rs", r#" |
| 16 |
#[macro_use] extern crate fomat_macros; |
| 17 |
|
| 18 |
fn foo() { |
| 19 |
pint!("stdout"(1)); |
| 20 |
pintln!("stdout"(2)); |
| 21 |
epint!("stderr"(1)); |
| 22 |
epintln!("stderr"(2)); |
| 23 |
perr!("stderr"(1)); |
| 24 |
perrln!("stderr"(2)); |
| 25 |
} |
| 26 |
|
| 27 |
fn main() { |
| 28 |
foo(); |
| 29 |
} |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn capturing() { |
| 33 |
foo(); |
| 34 |
} |
| 35 |
"#), |
| 36 |
("src/lib.rs", r#" |
| 37 |
#[macro_use] extern crate fomat_macros; |
| 38 |
|
| 39 |
pub fn called_from_other_crate() { |
| 40 |
pintln!("nocapture"(1)); |
| 41 |
} |
| 42 |
"#), |
| 43 |
("tests/nocapture.rs", r#" |
| 44 |
extern crate pint; |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn nocapture() { |
| 48 |
pint::called_from_other_crate(); |
| 49 |
} |
| 50 |
"#), |
| 51 |
]; |
| 52 |
|
| 53 |
fn unpack_files(directory: &Path, replace_path: &str) { |
| 54 |
use tar::{Builder, Header, Archive}; |
| 55 |
|
| 56 |
let mut builder = Builder::new(Vec::new()); |
| 57 |
for &(name, data) in FILES { |
| 58 |
let data = data.replace("PATH", replace_path); |
| 59 |
let mut header = Header::new_gnu(); |
| 60 |
header.set_path(name).unwrap(); |
| 61 |
header.set_size(data.len() as u64); |
| 62 |
header.set_cksum(); |
| 63 |
builder.append(&header, data.as_bytes()).unwrap(); |
| 64 |
} |
| 65 |
|
| 66 |
let archive: &[u8] = &builder.into_inner().unwrap(); |
| 67 |
Archive::new(archive).unpack(directory) |
| 68 |
.expect("Can't unpack test crate"); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn capturing() { |
| 73 |
use std::process::Command; |
| 74 |
use std::env; |
| 75 |
use std::str::from_utf8; |
| 76 |
|
| 77 |
let rootdir = { |
| 78 |
let mut rootdir = env::current_exe().unwrap(); |
| 79 |
while rootdir.file_name() != Some("target".as_ref()) { |
| 80 |
assert!( rootdir.pop() ); |
| 81 |
} |
| 82 |
assert!( rootdir.pop() ); |
| 83 |
rootdir |
| 84 |
}; |
| 85 |
|
| 86 |
let pintdir = tempfile::Builder::new() |
| 87 |
.prefix("fomat-macros-capturing-test") |
| 88 |
.tempdir() |
| 89 |
.expect("Can't create tempdir"); |
| 90 |
unpack_files(pintdir.as_ref(), rootdir.to_str().unwrap()); |
| 91 |
|
| 92 |
assert!( |
| 93 |
Command::new("cargo").arg("build") |
| 94 |
.current_dir(&pintdir) |
| 95 |
.status().unwrap().success() |
| 96 |
); |
| 97 |
|
| 98 |
let expected_stdout = "stdout1stdout2\n"; |
| 99 |
let expected_stderr = "stderr1stderr2\nstderr1stderr2\n"; |
| 100 |
|
| 101 |
let output = Command::new("target/debug/pint") |
| 102 |
.current_dir(&pintdir) |
| 103 |
.output().unwrap(); |
| 104 |
assert!(output.status.success()); |
| 105 |
assert_eq!(from_utf8(&output.stdout).unwrap(), expected_stdout); |
| 106 |
assert_eq!(from_utf8(&output.stderr).unwrap(), expected_stderr); |
| 107 |
|
| 108 |
let output = Command::new("cargo") |
| 109 |
.arg("test") |
| 110 |
.current_dir(&pintdir) |
| 111 |
.output().unwrap(); |
| 112 |
assert!(output.status.success()); |
| 113 |
assert!(!from_utf8(&output.stdout).unwrap().contains(expected_stdout)); |
| 114 |
assert!(from_utf8(&output.stderr).unwrap().contains(expected_stderr)); |
| 115 |
assert!(from_utf8(&output.stdout).unwrap().contains("nocapture1")); |
| 116 |
|
| 117 |
let output = Command::new("cargo") |
| 118 |
.args(&["test", "--", "--nocapture"]) |
| 119 |
.current_dir(&pintdir) |
| 120 |
.output().unwrap(); |
| 121 |
assert!(output.status.success()); |
| 122 |
assert!(from_utf8(&output.stdout).unwrap().contains(expected_stdout)); |
| 123 |
assert!(from_utf8(&output.stderr).unwrap().contains(expected_stderr)); |
| 124 |
} |