Skip to content

Commit 5f1d31c

Browse files
committed
add formatter tests
1 parent a3fce1e commit 5f1d31c

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

check_diff/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ impl Diff {
8080
}
8181
}
8282

83-
// will be used in future PRs, just added to make the compiler happy
8483
pub struct CheckDiffRunners<F, S> {
8584
feature_runner: F,
8685
src_runner: S,
@@ -413,7 +412,7 @@ pub fn search_for_rs_files(repo: &Path) -> impl Iterator<Item = PathBuf> {
413412
/// repo specified with the specific configs.
414413
pub fn check_diff(
415414
config: Option<Vec<String>>,
416-
runners: CheckDiffRunners<RustfmtRunner, RustfmtRunner>,
415+
runners: CheckDiffRunners<impl CodeFormatter, impl CodeFormatter>,
417416
repo: &Path,
418417
) -> i32 {
419418
let mut errors = 0;

check_diff/tests/check_diff.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
use check_diff::{
2-
CheckDiffError, CheckDiffRunners, check_diff, compile_rustfmt, search_for_rs_files,
2+
CheckDiffError, CheckDiffRunners, CodeFormatter, check_diff, search_for_rs_files,
33
};
44
use std::fs::File;
55
use tempfile::Builder;
66

7+
struct DoNothingFormatter;
8+
9+
impl CodeFormatter for DoNothingFormatter {
10+
fn format_code<'a>(
11+
&self,
12+
_code: &'a str,
13+
_config: &Option<Vec<String>>,
14+
) -> Result<String, CheckDiffError> {
15+
Ok(String::new())
16+
}
17+
}
18+
19+
/// Formatter that adds a white space to the end of the codd
20+
struct AddWhiteSpaceFormatter;
21+
22+
impl CodeFormatter for AddWhiteSpaceFormatter {
23+
fn format_code<'a>(
24+
&self,
25+
code: &'a str,
26+
_config: &Option<Vec<String>>,
27+
) -> Result<String, CheckDiffError> {
28+
let result = code.to_string() + " ";
29+
Ok(result)
30+
}
31+
}
32+
733
#[test]
834
fn search_for_files_correctly_non_nested() -> Result<(), Box<dyn std::error::Error>> {
935
let dir = Builder::new().tempdir_in("").unwrap();
@@ -45,14 +71,8 @@ fn search_for_files_correctly_nested() -> Result<(), Box<dyn std::error::Error>>
4571
}
4672

4773
#[test]
48-
fn check_diff_test() -> Result<(), CheckDiffError> {
49-
let tmp_dir = Builder::new().tempdir_in("").unwrap();
50-
let runners = compile_rustfmt(
51-
tmp_dir.path(),
52-
"https://github.com/rust-lang/rustfmt".to_string(),
53-
"rustfmt-1.4.32".to_string(),
54-
None,
55-
)?;
74+
fn check_diff_test_no_formatting_difference() -> Result<(), CheckDiffError> {
75+
let runners = CheckDiffRunners::new(DoNothingFormatter, DoNothingFormatter);
5676

5777
let dir = Builder::new().tempdir_in("").unwrap();
5878
let file_path = dir.path().join("test.rs");
@@ -64,19 +84,13 @@ fn check_diff_test() -> Result<(), CheckDiffError> {
6484
}
6585

6686
#[test]
67-
fn format_simple_code() -> Result<(), CheckDiffError> {
68-
let tmp_dir = Builder::new().tempdir_in("").unwrap();
69-
let runners = compile_rustfmt(
70-
tmp_dir.path(),
71-
"https://github.com/rust-lang/rustfmt".to_string(),
72-
"rustfmt-1.4.32".to_string(),
73-
None,
74-
)?;
75-
76-
//let output = runners
77-
// .src_runner
78-
// .format_code("fn main() {}", &None)?;
79-
//assert_eq!(output, "fn main() {}\n".to_string());
80-
//
87+
fn check_diff_test_formatting_difference() -> Result<(), CheckDiffError> {
88+
let runners = CheckDiffRunners::new(DoNothingFormatter, AddWhiteSpaceFormatter);
89+
let dir = Builder::new().tempdir_in("").unwrap();
90+
let file_path = dir.path().join("test.rs");
91+
let _tmp_file = File::create(file_path)?;
92+
93+
let errors = check_diff(None, runners, dir.path());
94+
assert_ne!(errors, 0);
8195
Ok(())
8296
}

0 commit comments

Comments
 (0)