diff --git a/check_diff/src/lib.rs b/check_diff/src/lib.rs index 9d3cc906a93..d3f1dfa7278 100644 --- a/check_diff/src/lib.rs +++ b/check_diff/src/lib.rs @@ -114,7 +114,7 @@ impl RustfmtRunner { // code: Code to run the binary on // config: Any additional configuration options to pass to rustfmt // - fn format_code<'a>( + pub fn format_code<'a>( &self, code: &'a str, config: &Option>, @@ -346,7 +346,7 @@ pub fn compile_rustfmt( }); } -fn search_for_rs_files(repo: &Path) -> impl Iterator { +pub fn search_for_rs_files(repo: &Path) -> impl Iterator { return WalkDir::new(repo).into_iter().filter_map(|e| match e.ok() { Some(entry) => { let path = entry.path(); diff --git a/check_diff/tests/check_diff.rs b/check_diff/tests/check_diff.rs new file mode 100644 index 00000000000..3531508f229 --- /dev/null +++ b/check_diff/tests/check_diff.rs @@ -0,0 +1,80 @@ +use check_diff::{check_diff, compile_rustfmt, search_for_rs_files, CheckDiffError}; +use std::fs::File; +use tempfile::Builder; + +#[test] +fn search_for_files_correctly_non_nested() -> Result<(), Box> { + let dir = Builder::new().tempdir_in("").unwrap(); + let file_path = dir.path().join("test.rs"); + let _tmp_file = File::create(file_path)?; + + let iter = search_for_rs_files(dir.path()); + + let mut count = 0; + for _ in iter { + count += 1; + } + + assert_eq!(count, 1); + + Ok(()) +} + +#[test] +fn search_for_files_correctly_nested() -> Result<(), Box> { + let dir = Builder::new().tempdir_in("").unwrap(); + let file_path = dir.path().join("test.rs"); + let _tmp_file = File::create(file_path)?; + + let nested_dir = Builder::new().tempdir_in(dir.path()).unwrap(); + let nested_file_path = nested_dir.path().join("nested.rs"); + let _ = File::create(nested_file_path)?; + + let iter = search_for_rs_files(dir.path()); + + let mut count = 0; + for _ in iter { + count += 1; + } + + assert_eq!(count, 2); + + Ok(()) +} + +#[test] +fn check_diff_test() -> Result<(), CheckDiffError> { + let tmp_dir = Builder::new().tempdir_in("").unwrap(); + let runners = compile_rustfmt( + tmp_dir.path(), + "https://github.com/rust-lang/rustfmt".to_string(), + "rustfmt-1.4.32".to_string(), + None, + )?; + + let dir = Builder::new().tempdir_in("").unwrap(); + let file_path = dir.path().join("test.rs"); + let _tmp_file = File::create(file_path)?; + + let errors = check_diff(None, runners, dir.path()); + assert_eq!(errors, 0); + Ok(()) +} + +#[test] +fn format_simple_code() -> Result<(), CheckDiffError> { + let tmp_dir = Builder::new().tempdir_in("").unwrap(); + let runners = compile_rustfmt( + tmp_dir.path(), + "https://github.com/rust-lang/rustfmt".to_string(), + "rustfmt-1.4.32".to_string(), + None, + )?; + + let output = runners + .src_runner + .format_code("fn main() {}", &None)?; + assert_eq!(output, "fn main() {}\n".to_string()); + + Ok(()) +} diff --git a/check_diff/tests/diffy.rs b/check_diff/tests/diffy.rs deleted file mode 100644 index 03157c8f487..00000000000 --- a/check_diff/tests/diffy.rs +++ /dev/null @@ -1,22 +0,0 @@ -use diffy::{self, create_patch}; - -#[test] -fn diffy_test_diff() { - let original = "The quick brown fox jumps over the lazy dog"; - let modified = "The quick brown fox jumps over the LAZY dog"; - - let patch = create_patch(original, modified); - // diffy uses hunks which indicates the lines that are different - assert_eq!(patch.hunks().is_empty(), false); - // hence regardless, patch.to_string() will never be empty - assert_eq!(patch.to_string().is_empty(), false); -} - -#[test] -fn diffy_test_no_diff() { - let original = "The quick brown fox jumps over the lazy dog"; - - let patch = create_patch(original, original); - assert_eq!(patch.hunks().is_empty(), true); - assert_eq!(patch.to_string().is_empty(), false); -}