From 5f1d31c902350453d54f5dba25f947dc7e4dc831 Mon Sep 17 00:00:00 2001 From: benluiwj Date: Sat, 21 Dec 2024 11:23:49 +0800 Subject: [PATCH] add formatter tests --- check_diff/src/lib.rs | 3 +- check_diff/tests/check_diff.rs | 60 +++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/check_diff/src/lib.rs b/check_diff/src/lib.rs index 1a01b9f5325..80f5d1803a1 100644 --- a/check_diff/src/lib.rs +++ b/check_diff/src/lib.rs @@ -80,7 +80,6 @@ impl Diff { } } -// will be used in future PRs, just added to make the compiler happy pub struct CheckDiffRunners { feature_runner: F, src_runner: S, @@ -413,7 +412,7 @@ pub fn search_for_rs_files(repo: &Path) -> impl Iterator { /// repo specified with the specific configs. pub fn check_diff( config: Option>, - runners: CheckDiffRunners, + runners: CheckDiffRunners, repo: &Path, ) -> i32 { let mut errors = 0; diff --git a/check_diff/tests/check_diff.rs b/check_diff/tests/check_diff.rs index 9cc0637c81e..17297c13043 100644 --- a/check_diff/tests/check_diff.rs +++ b/check_diff/tests/check_diff.rs @@ -1,9 +1,35 @@ use check_diff::{ - CheckDiffError, CheckDiffRunners, check_diff, compile_rustfmt, search_for_rs_files, + CheckDiffError, CheckDiffRunners, CodeFormatter, check_diff, search_for_rs_files, }; use std::fs::File; use tempfile::Builder; +struct DoNothingFormatter; + +impl CodeFormatter for DoNothingFormatter { + fn format_code<'a>( + &self, + _code: &'a str, + _config: &Option>, + ) -> Result { + Ok(String::new()) + } +} + +/// Formatter that adds a white space to the end of the codd +struct AddWhiteSpaceFormatter; + +impl CodeFormatter for AddWhiteSpaceFormatter { + fn format_code<'a>( + &self, + code: &'a str, + _config: &Option>, + ) -> Result { + let result = code.to_string() + " "; + Ok(result) + } +} + #[test] fn search_for_files_correctly_non_nested() -> Result<(), Box> { let dir = Builder::new().tempdir_in("").unwrap(); @@ -45,14 +71,8 @@ fn search_for_files_correctly_nested() -> Result<(), Box> } #[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, - )?; +fn check_diff_test_no_formatting_difference() -> Result<(), CheckDiffError> { + let runners = CheckDiffRunners::new(DoNothingFormatter, DoNothingFormatter); let dir = Builder::new().tempdir_in("").unwrap(); let file_path = dir.path().join("test.rs"); @@ -64,19 +84,13 @@ fn check_diff_test() -> Result<(), CheckDiffError> { } #[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()); - // +fn check_diff_test_formatting_difference() -> Result<(), CheckDiffError> { + let runners = CheckDiffRunners::new(DoNothingFormatter, AddWhiteSpaceFormatter); + 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_ne!(errors, 0); Ok(()) }