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

Commit

Permalink
Merge pull request #15 from fifthtry/output_dir
Browse files Browse the repository at this point in the history
output checking
  • Loading branch information
amitu authored May 13, 2021
2 parents f7ee382 + 010290f commit 630cede
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fbt"
version = "0.1.4"
version = "0.1.5"
authors = ["Sitesh <[email protected]>", "Amit Upadhyay <[email protected]>"]
edition = "2018"
description = "folder based testing tool (library)"
Expand All @@ -11,7 +11,7 @@ homepage = "https://www.fifthtry.com/fifthtry/fbt/"
members = ["fbt_lib"]

[dependencies]
# fbt-lib = { version = "0.1.4" }
# fbt-lib = { version = "0.1.5" }
fbt-lib = { path = "fbt_lib" }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion fbt_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fbt-lib"
version = "0.1.4"
version = "0.1.5"
authors = ["Sitesh <[email protected]>", "Amit Upadhyay <[email protected]>"]
edition = "2018"
description = "folder based testing tool (library)"
Expand Down
174 changes: 85 additions & 89 deletions fbt_lib/src/dir_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,98 +37,94 @@ pub enum DirDiff {
}

pub(crate) fn diff<A: AsRef<std::path::Path>, B: AsRef<std::path::Path>>(
_a_base: A,
_b_base: B,
a_base: A,
b_base: B,
) -> Result<Option<DirDiff>, DirDiffError> {
todo!("not yet implemented")

// let mut a_walker = walk_dir(a_base)?;
// let mut b_walker = walk_dir(b_base)?;
//
// loop {
// match (a_walker.next(), b_walker.next()) {
// (Some(a), Some(b)) => {
// // first lets check the depth:
// // a > b: UnexpectedFileFound or UnexpectedFolderFound else
// // b > a: ExpectedFileMissing or ExpectedFolderMissing
//
// // if file names dont match how to find if we got a new entry
// // on left or extra entry on right? how do people actually
// // calculate diff?
//
// // then check file type
//
// // finally check file content if its a file
//
// // TODO: this is dummy code to test stuff
// let a = a?;
// let b = b?;
// eprintln!("a: {:?}", a);
// eprintln!("b: {:?}", b);
//
// // TODO: this is fully buggy! loop never loops.
// let found: std::path::PathBuf = b.path().into();
// return Ok(Some(if found.is_dir() {
// DirDiff::UnexpectedFolderFound { found }
// } else {
// DirDiff::UnexpectedFileFound { found }
// }));
// }
// (None, Some(b)) => {
// // we have something in b, but a is done, lets iterate over all
// // entries in b, and put them in UnexpectedFileFound and
// // UnexpectedFolderFound
// let found: std::path::PathBuf = b?.path().into();
// return Ok(Some(if found.is_dir() {
// DirDiff::UnexpectedFolderFound { found }
// } else {
// DirDiff::UnexpectedFileFound { found }
// }));
// }
// (Some(a), None) => {
// // we have something in a, but b is done, lets iterate over all
// // entries in a, and put them in ExpectedFileMissing and
// // ExpectedFolderMissing
// let expected: std::path::PathBuf = a?.path().into();
// return Ok(Some(if expected.is_dir() {
// DirDiff::ExpectedFolderMissing { expected }
// } else {
// DirDiff::ExpectedFileMissing { expected }
// }));
// }
// (None, None) => break,
// }
// }
//
// Ok(None)
let mut a_walker = walk_dir(a_base)?;
let mut b_walker = walk_dir(b_base)?;

loop {
match (a_walker.next(), b_walker.next()) {
(Some(a), Some(b)) => {
// first lets check the depth:
// a > b: UnexpectedFileFound or UnexpectedFolderFound else
// b > a: ExpectedFileMissing or ExpectedFolderMissing

// if file names dont match how to find if we got a new entry
// on left or extra entry on right? how do people actually
// calculate diff?

// then check file type

// finally check file content if its a file

// TODO: this is dummy code to test stuff
let a = a?;
let b = b?;

let found: std::path::PathBuf = b.path().into();

if a.file_name() != b.file_name() {
return Ok(Some(if found.is_dir() {
DirDiff::UnexpectedFolderFound { found }
} else {
DirDiff::UnexpectedFileFound { found }
}));
}

let a_content = std::fs::read_to_string(a.path())?;
let b_content = std::fs::read_to_string(b.path())?;
if a_content != b_content {
return Ok(Some(DirDiff::ContentMismatch {
expected: a_content,
found: b_content,
file: found,
}));
}
}
(None, Some(b)) => {
// we have something in b, but a is done, lets iterate over all
// entries in b, and put them in UnexpectedFileFound and
// UnexpectedFolderFound
let found: std::path::PathBuf = b?.path().into();
return Ok(Some(if found.is_dir() {
DirDiff::UnexpectedFolderFound { found }
} else {
DirDiff::UnexpectedFileFound { found }
}));
}
(Some(a), None) => {
// we have something in a, but b is done, lets iterate over all
// entries in a, and put them in ExpectedFileMissing and
// ExpectedFolderMissing
let expected: std::path::PathBuf = a?.path().into();
return Ok(Some(if expected.is_dir() {
DirDiff::ExpectedFolderMissing { expected }
} else {
DirDiff::ExpectedFileMissing { expected }
}));
}
(None, None) => break,
}
}

Ok(None)
}

// fn walk_dir<P: AsRef<std::path::Path>>(path: P) -> Result<walkdir::IntoIter, std::io::Error> {
// let mut walkdir = walkdir::WalkDir::new(path)
// .sort_by(compare_by_file_name)
// .into_iter();
// if let Some(Err(e)) = walkdir.next() {
// Err(e.into())
// } else {
// Ok(walkdir)
// }
// }

// fn compare_by_file_name(a: &walkdir::DirEntry, b: &walkdir::DirEntry) -> std::cmp::Ordering {
// a.file_name().cmp(&b.file_name())
// }

// fn read_to_vec<P: AsRef<Path>>(file: P) -> Result<Vec<u8>, std::io::Error> {
// use std::fs::File;
// use std::io::Read;
//
// let mut data = Vec::new();
// let mut file = File::open(file.as_ref())?;
//
// file.read_to_end(&mut data)?;
//
// Ok(data)
// }
fn walk_dir<P: AsRef<std::path::Path>>(path: P) -> Result<walkdir::IntoIter, std::io::Error> {
let mut walkdir = walkdir::WalkDir::new(path)
.sort_by(compare_by_file_name)
.into_iter();
if let Some(Err(e)) = walkdir.next() {
Err(e.into())
} else {
Ok(walkdir)
}
}

fn compare_by_file_name(a: &walkdir::DirEntry, b: &walkdir::DirEntry) -> std::cmp::Ordering {
a.file_name().cmp(&b.file_name())
}

impl From<std::io::Error> for DirDiffError {
fn from(e: std::io::Error) -> DirDiffError {
Expand Down
44 changes: 44 additions & 0 deletions fbt_lib/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,50 @@ pub fn main() -> Option<i32> {
)
);
}
Err(crate::Failure::OutputMismatch { diff }) => {
any_failed = true;
match diff {
crate::DirDiff::ContentMismatch {
found,
expected,
file,
} => {
println!(
"{}: {} {} (output content mismatch: {})",
case.id.blue(),
"FAILED".red(),
duration,
file.to_str().unwrap_or("cant-read-filename"),
);
println!("found:\n\n{}\n", found.as_str());
println!(
"diff:\n\n{}\n",
diffy::create_patch(
(expected.to_owned() + "\n").as_str(),
(found.to_owned() + "\n").as_str()
)
);
}
crate::DirDiff::UnexpectedFileFound { found } => {
println!(
"{}: {} {} (extra file found: {})",
case.id.blue(),
"FAILED".red(),
duration,
found.to_str().unwrap_or("cant-read-filename"),
);
}
_ => {
println!(
"{}: {} {} (output mismatch: {:?})",
case.id.blue(),
"FAILED".red(),
duration,
diff
);
}
}
}
Err(e) => {
any_failed = true;
println!(
Expand Down
20 changes: 18 additions & 2 deletions tests/03_failing_fbt_tests/cmd.p1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,22 @@ diff:
+hello there


02_output_mismatch: SKIPPED (output not yet implemented)
02_output_mismatch: FAILED (output content mismatch: ./tests/02_output_mismatch/output/foo.txt)
found:

hello world2


diff:

\--- original
+++ modified
@@ -1,2 +1,2 @@
-hello world
+hello world2



03_stderr_mismatch: FAILED (stderr mismatch)
stderr:

Expand All @@ -35,4 +50,5 @@ diff:
+hello there



04_extra_file: FAILED (extra file found: ./tests/04_extra_file/output/bar.txt)
05_expected_file_missing: SKIPPED (the output is incorrect)
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-- fbt:
cmd: echo hello world > foo.txt
skip: output not yet implemented
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello world
hello world2
2 changes: 2 additions & 0 deletions tests/03_failing_fbt_tests/input/tests/04_extra_file/cmd.p1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- fbt:
cmd: echo hello world > foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar bar dekho
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- fbt:
cmd: echo hello world > foo.txt && echo bar bar dekho > bar.txt
skip: the output is incorrect
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
4 changes: 2 additions & 2 deletions tests/06_version/cmd.p1
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ We will have to keep updating this file every time we release a new version.

-- stdout:

fbt: 0.1.4
fbt: 0.1.4
fbt: 0.1.5
fbt: 0.1.5
2 changes: 2 additions & 0 deletions tests/07_valid_output/cmd.p1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- fbt:
cmd: echo hello world > foo.txt
1 change: 1 addition & 0 deletions tests/07_valid_output/output/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world

0 comments on commit 630cede

Please sign in to comment.