Skip to content

Commit

Permalink
add formatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benluiwj committed Dec 21, 2024
1 parent a3fce1e commit 5f1d31c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
3 changes: 1 addition & 2 deletions check_diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ impl Diff {
}
}

// will be used in future PRs, just added to make the compiler happy
pub struct CheckDiffRunners<F, S> {
feature_runner: F,
src_runner: S,
Expand Down Expand Up @@ -413,7 +412,7 @@ pub fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
/// repo specified with the specific configs.
pub fn check_diff(
config: Option<Vec<String>>,
runners: CheckDiffRunners<RustfmtRunner, RustfmtRunner>,
runners: CheckDiffRunners<impl CodeFormatter, impl CodeFormatter>,
repo: &Path,
) -> i32 {
let mut errors = 0;
Expand Down
60 changes: 37 additions & 23 deletions check_diff/tests/check_diff.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<String>>,
) -> Result<String, CheckDiffError> {
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<Vec<String>>,
) -> Result<String, CheckDiffError> {
let result = code.to_string() + " ";
Ok(result)
}
}

#[test]
fn search_for_files_correctly_non_nested() -> Result<(), Box<dyn std::error::Error>> {
let dir = Builder::new().tempdir_in("").unwrap();
Expand Down Expand Up @@ -45,14 +71,8 @@ fn search_for_files_correctly_nested() -> Result<(), Box<dyn std::error::Error>>
}

#[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");
Expand All @@ -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(())
}

0 comments on commit 5f1d31c

Please sign in to comment.