Skip to content

Commit

Permalink
add UT for check_diff and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
benluiwj committed Nov 30, 2024
1 parent 6e8c8d6 commit 0d4f462
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
4 changes: 2 additions & 2 deletions check_diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
Expand Down Expand Up @@ -346,7 +346,7 @@ pub fn compile_rustfmt(
});
}

fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
pub fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
return WalkDir::new(repo).into_iter().filter_map(|e| match e.ok() {
Some(entry) => {
let path = entry.path();
Expand Down
80 changes: 80 additions & 0 deletions check_diff/tests/check_diff.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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(())
}
22 changes: 0 additions & 22 deletions check_diff/tests/diffy.rs

This file was deleted.

0 comments on commit 0d4f462

Please sign in to comment.