Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stdout stream #22

Merged
merged 5 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
target
**/*.rs.bk
Cargo.lock
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
termcolor = "1.0"
toml = "0.5"

[dev-dependencies]
derive_mac = { path = "tests/stdout/derive_mac" }
12 changes: 12 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ pub(crate) fn output(warnings: &str, output: &Output) {
}
}

pub(crate) fn fail_output(exit: bool, output: &normalize::Variations) {
let stdout = output.stdout();
let color = if !exit { Yellow } else { Red };

if !stdout.is_empty() {
term::bold_color(color);
println!("STDOUT:");
snippet(color, &normalize::trim(stdout));
println!();
}
}

pub(crate) fn warnings(warnings: &str) {
if warnings.is_empty() {
return;
Expand Down
32 changes: 22 additions & 10 deletions src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,47 @@ pub fn trim<S: AsRef<[u8]>>(output: S) -> String {
normalized
}

pub fn diagnostics(output: Vec<u8>) -> Variations {
let mut from_bytes = String::from_utf8_lossy(&output).to_string();
from_bytes = from_bytes.replace("\r\n", "\n");
pub fn diagnostics(output: &std::process::Output) -> Variations {
let mut err_from_bytes = String::from_utf8_lossy(&output.stderr).to_string();
err_from_bytes = err_from_bytes.replace("\r\n", "\n");

let variations = [Basic, StripCouldNotCompile]
let out_from_bytes = String::from_utf8_lossy(&output.stdout);
let variations_out = out_from_bytes.replace("\r\n", "\n");

let variations_err = [Basic, StripCouldNotCompile]
.iter()
.map(|normalization| apply(&from_bytes, *normalization))
.map(|normalization| apply(&err_from_bytes, *normalization))
.collect();

Variations { variations }
Variations {
variations_err,
variations_out,
}
}

pub struct Variations {
variations: Vec<String>,
variations_err: Vec<String>,
variations_out: String,
}

impl Variations {
pub fn map<F: FnMut(String) -> String>(self, f: F) -> Self {
Variations {
variations: self.variations.into_iter().map(f).collect(),
variations_err: self.variations_err.into_iter().map(f).collect(),
variations_out: self.variations_out,
}
}

pub fn preferred(&self) -> &str {
self.variations.last().unwrap()
self.variations_err.last().unwrap()
}

pub fn any<F: FnMut(&str) -> bool>(&self, mut f: F) -> bool {
self.variations.iter().any(|stderr| f(stderr))
self.variations_err.iter().any(|stderr| f(stderr))
}

pub fn stdout(&self) -> &str {
self.variations_out.as_str()
}
}

Expand Down
22 changes: 17 additions & 5 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ impl Test {

let output = cargo::build_test(project, name)?;
let success = output.status.success();
let stderr = normalize::diagnostics(output.stderr).map(|stderr| {

let stdio = normalize::diagnostics(&output).map(|stderr| {
stderr
.replace(&name.0, "$CRATE")
.replace(project.source_dir.to_string_lossy().as_ref(), "$DIR")
Expand All @@ -209,7 +210,7 @@ impl Test {
Expected::CompileFail => Test::check_compile_fail,
};

check(self, project, name, success, stderr)
check(self, project, name, success, stdio)
}

fn check_pass(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect check_compile_fail to print the stdout also if there was any.

Expand All @@ -220,15 +221,23 @@ impl Test {
variations: Variations,
) -> Result<()> {
let preferred = variations.preferred();
// from build output (for proc-macro output)
let build_stdout = variations.stdout();

if !success {
message::failed_to_build(preferred);
return Err(Error::CargoFail);
}

let output = cargo::run_test(project, name)?;
message::output(preferred, &output);
let mut output = cargo::run_test(project, name)?;
output.stdout = format!(
"{}\n{}",
build_stdout,
String::from_utf8_lossy(&output.stdout)
)
.into_bytes();

message::output(preferred, &output);
if output.status.success() {
Ok(())
} else {
Expand All @@ -247,10 +256,11 @@ impl Test {

if success {
message::should_not_have_compiled();
message::fail_output(success, &variations);
message::warnings(preferred);
return Err(Error::ShouldNotHaveCompiled);
}

let stderr_path = self.path.with_extension("stderr");

if !stderr_path.exists() {
Expand All @@ -272,6 +282,7 @@ impl Test {
fs::write(stderr_path, preferred).map_err(Error::WriteStderr)?;
}
}
message::fail_output(success, &variations);
return Ok(());
}

Expand All @@ -281,6 +292,7 @@ impl Test {

if variations.any(|stderr| expected == stderr) {
message::ok();

return Ok(());
}

Expand Down
7 changes: 7 additions & 0 deletions tests/stdout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[test]
fn test() {
let t = trybuild::TestCases::new();
t.pass("tests/stdout/print-pass.rs");
t.compile_fail("tests/stdout/print-fail.rs");
t.compile_fail("tests/stdout/run-fail.rs");
}
11 changes: 11 additions & 0 deletions tests/stdout/derive_mac/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "derive_mac"
version = "0.0.0"
edition = "2018"

[lib]
proc-macro = true

[dependencies]
syn = "0.15"
quote = "0.6"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure every line ends with a newline. Without that, this isn't considered a text file and it messes up line-oriented tools. You should set your editor to always do this.

16 changes: 16 additions & 0 deletions tests/stdout/derive_mac/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};
use quote::quote;

#[proc_macro_derive(Print)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident.clone();
println!("IN PROC-MACRO");

proc_macro::TokenStream::from(quote!{
impl #name {}
})
}
11 changes: 11 additions & 0 deletions tests/stdout/print-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use derive_mac::Print;

#[derive(Print)]
pub struct Field {
name: &'static str,
bitmask: u16,
}

compile_error!("ERROR");

fn main() {}
12 changes: 12 additions & 0 deletions tests/stdout/print-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use derive_mac::Print;

#[derive(Print)]
pub struct Field {
name: &'static str,
bitmask: u16,
}

fn main() {
println!("IN PRINTLN.RS");
assert!(true)
}
9 changes: 9 additions & 0 deletions tests/stdout/run-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use derive_mac::Print;

#[derive(Print)]
pub struct Field {
name: &'static str,
bitmask: u16,
}

fn main() {}