11use check_diff:: {
2- CheckDiffError , CheckDiffRunners , check_diff , compile_rustfmt , search_for_rs_files,
2+ CheckDiffError , CheckDiffRunners , CodeFormatter , check_diff , search_for_rs_files,
33} ;
44use std:: fs:: File ;
55use 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]
834fn 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