Debian Codegraph @

tests/capturing.rs

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