Skip to content

Commit 28ce3b9

Browse files
committed
add UT for check_diff and formatting
1 parent 960dc5e commit 28ce3b9

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

check_diff/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl RustfmtRunner {
114114
// code: Code to run the binary on
115115
// config: Any additional configuration options to pass to rustfmt
116116
//
117-
fn format_code<'a>(
117+
pub fn format_code<'a>(
118118
&self,
119119
code: &'a str,
120120
config: &Option<Vec<String>>,
@@ -346,7 +346,7 @@ pub fn compile_rustfmt(
346346
});
347347
}
348348

349-
fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
349+
pub fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
350350
return WalkDir::new(repo).into_iter().filter_map(|e| match e.ok() {
351351
Some(entry) => {
352352
let path = entry.path();

check_diff/tests/check_diff.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use check_diff::{check_diff, compile_rustfmt, search_for_rs_files, CheckDiffError};
2+
use std::fs::File;
3+
use tempfile::Builder;
4+
5+
#[test]
6+
fn search_for_files_correctly_non_nested() -> Result<(), Box<dyn std::error::Error>> {
7+
let dir = Builder::new().tempdir_in("").unwrap();
8+
let file_path = dir.path().join("test.rs");
9+
let _tmp_file = File::create(file_path)?;
10+
11+
let iter = search_for_rs_files(dir.path());
12+
13+
let mut count = 0;
14+
for _ in iter {
15+
count += 1;
16+
}
17+
18+
assert_eq!(count, 1);
19+
20+
Ok(())
21+
}
22+
23+
#[test]
24+
fn search_for_files_correctly_nested() -> Result<(), Box<dyn std::error::Error>> {
25+
let dir = Builder::new().tempdir_in("").unwrap();
26+
let file_path = dir.path().join("test.rs");
27+
let _tmp_file = File::create(file_path)?;
28+
29+
let nested_dir = Builder::new().tempdir_in(dir.path()).unwrap();
30+
let nested_file_path = nested_dir.path().join("nested.rs");
31+
let _ = File::create(nested_file_path)?;
32+
33+
let iter = search_for_rs_files(dir.path());
34+
35+
let mut count = 0;
36+
for _ in iter {
37+
count += 1;
38+
}
39+
40+
assert_eq!(count, 2);
41+
42+
Ok(())
43+
}
44+
45+
#[test]
46+
fn check_diff_test() -> Result<(), CheckDiffError> {
47+
let tmp_dir = Builder::new().tempdir_in("").unwrap();
48+
let runners = compile_rustfmt(
49+
tmp_dir.path(),
50+
"https://github.com/rust-lang/rustfmt".to_string(),
51+
"rustfmt-1.4.32".to_string(),
52+
None,
53+
)?;
54+
55+
let dir = Builder::new().tempdir_in("").unwrap();
56+
let file_path = dir.path().join("test.rs");
57+
let _tmp_file = File::create(file_path)?;
58+
59+
let errors = check_diff(None, runners, dir.path());
60+
assert_eq!(errors, 0);
61+
Ok(())
62+
}
63+
64+
#[test]
65+
fn format_simple_code() -> Result<(), CheckDiffError> {
66+
let tmp_dir = Builder::new().tempdir_in("").unwrap();
67+
let runners = compile_rustfmt(
68+
tmp_dir.path(),
69+
"https://github.com/rust-lang/rustfmt".to_string(),
70+
"rustfmt-1.4.32".to_string(),
71+
None,
72+
)?;
73+
74+
let output = runners
75+
.src_runner
76+
.format_code("fn main() {}", &None)?;
77+
assert_eq!(output, "fn main() {}\n".to_string());
78+
79+
Ok(())
80+
}

check_diff/tests/diffy.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)