|
| 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 | +} |
0 commit comments