Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
fbt_lib::Output
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed May 19, 2021
1 parent 1ddec94 commit e00b7e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 47 deletions.
66 changes: 21 additions & 45 deletions fbt_lib/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ pub fn main() -> Option<i32> {
"FAILED".red(),
duration,
expected,
output.status.code()
);
println!(
"stdout:\n{}\n",
std::str::from_utf8(&output.stdout).unwrap_or("failed to decode")
);
println!(
"stderr:\n{}\n",
std::str::from_utf8(&output.stderr).unwrap_or("failed to decode")
output.exit_code
);
println!("stdout:\n{}\n", &output.stdout);
println!("stderr:\n{}\n", &output.stderr);
}
Err(crate::Failure::StdoutMismatch { expected, output }) => {
any_failed = true;
Expand All @@ -78,13 +72,12 @@ pub fn main() -> Option<i32> {
"FAILED".red(),
duration,
);
let stdout = std::str::from_utf8(&output.stdout).unwrap_or("failed to decode");
println!("stdout:\n\n{}\n", stdout);
println!("stdout:\n\n{}\n", &output.stdout);
println!(
"diff:\n\n{}\n",
diffy::create_patch(
(expected.to_owned() + "\n").as_str(),
(stdout.to_owned() + "\n").as_str()
(output.stdout.clone() + "\n").as_str()
)
);
}
Expand All @@ -96,13 +89,12 @@ pub fn main() -> Option<i32> {
"FAILED".red(),
duration,
);
let stderr = std::str::from_utf8(&output.stderr).unwrap_or("failed to decode");
println!("stderr:\n\n{}\n", stderr);
println!("stderr:\n\n{}\n", &output.stderr);
println!(
"diff:\n\n{}\n",
diffy::create_patch(
(expected.to_owned() + "\n").as_str(),
(stderr.to_owned() + "\n").as_str()
(output.stderr.clone() + "\n").as_str()
)
);
}
Expand Down Expand Up @@ -248,8 +240,7 @@ pub fn test_all() -> Result<Vec<crate::Case>, crate::Error> {
}

fn test_one(global: &crate::Config, entry: std::path::PathBuf) -> crate::Case {
use std::borrow::BorrowMut;
use std::io::Write;
use std::{borrow::BorrowMut, convert::TryFrom, io::Write};

let id = entry
.file_name()
Expand Down Expand Up @@ -314,8 +305,6 @@ fn test_one(global: &crate::Config, entry: std::path::PathBuf) -> crate::Case {
fbt
};

let dir_as_string = dir.to_string_lossy().to_string();

// eprintln!("executing '{}' in {:?}", &config.cmd, &dir);
let mut child = match config.cmd().current_dir(&dir).spawn() {
Ok(c) => c,
Expand Down Expand Up @@ -346,30 +335,22 @@ fn test_one(global: &crate::Config, entry: std::path::PathBuf) -> crate::Case {
}
};

match output.status.code() {
Some(code) => {
if code != config.exit_code {
return err(crate::Failure::UnexpectedStatusCode {
expected: config.exit_code,
output,
});
}
}
None => {
return err(crate::Failure::UnexpectedStatusCode {
expected: config.exit_code,
output,
})
let output = match crate::Output::try_from(&output) {
Ok(o) => o,
Err(reason) => {
return err(crate::Failure::CantReadOutput { reason, output });
}
};

if output.exit_code != config.exit_code {
return err(crate::Failure::UnexpectedStatusCode {
expected: config.exit_code,
output,
});
}

if let Some(ref stdout) = config.stdout {
if std::str::from_utf8(&output.stdout)
.unwrap_or("")
.trim()
.replace(dir_as_string.as_str(), "")
!= stdout.trim()
{
if output.stdout != stdout.trim() {
return err(crate::Failure::StdoutMismatch {
output,
expected: stdout.trim().to_string(),
Expand All @@ -378,12 +359,7 @@ fn test_one(global: &crate::Config, entry: std::path::PathBuf) -> crate::Case {
}

if let Some(ref stderr) = config.stderr {
if std::str::from_utf8(&output.stderr)
.unwrap_or("")
.trim()
.replace(dir_as_string.as_str(), "")
!= stderr.trim()
{
if output.stderr != stderr.trim() {
return err(crate::Failure::StderrMismatch {
output,
expected: stderr.trim().to_string(),
Expand Down
42 changes: 40 additions & 2 deletions fbt_lib/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

#[derive(Debug, Default)]
pub(crate) struct Config {
pub build: Option<String>,
Expand Down Expand Up @@ -261,6 +263,38 @@ pub struct Case {
pub duration: std::time::Duration,
}

#[derive(Debug)]
pub struct Output {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}

impl TryFrom<&std::process::Output> for Output {
type Error = &'static str;

fn try_from(o: &std::process::Output) -> std::result::Result<Self, Self::Error> {
Ok(Output {
exit_code: match o.status.code() {
Some(code) => code,
None => return Err("cant read exit_code"),
},
stdout: {
std::str::from_utf8(&o.stdout)
.unwrap_or("")
.trim()
.to_string()
},
stderr: {
std::str::from_utf8(&o.stderr)
.unwrap_or("")
.trim()
.to_string()
},
})
}
}

#[derive(Debug)]
pub enum Failure {
Skipped {
Expand All @@ -283,15 +317,19 @@ pub enum Failure {
},
UnexpectedStatusCode {
expected: i32,
output: Output,
},
CantReadOutput {
output: std::process::Output,
reason: &'static str,
},
StdoutMismatch {
expected: String,
output: std::process::Output,
output: Output,
},
StderrMismatch {
expected: String,
output: std::process::Output,
output: Output,
},
DirDiffError {
error: crate::DirDiffError,
Expand Down

0 comments on commit e00b7e9

Please sign in to comment.