use std::io;
use ec4rs::property::TrimTrailingWs;
use rstest::rstest;
use tempfile::TempDir;
use ecformat::errors::{CheckErrorType, EditorConfigError};
mod integration_test_utils;
use integration_test_utils::test_utils;
#[test]
fn test_check_trim_trailing_whitespace_true() {
let temp_dir = copy_test_files(true).expect("Copy tests files into temp dir necessary");
let error_list = integration_test_utils::run_check_command(temp_dir.path());
let error_files = integration_test_utils::check_error_list_as_file_names(&error_list);
let expected_error_files =
integration_test_utils::get_filtered_test_file_names::<TrimTrailingWs, _>(|test_entry| {
!test_utils::trim_trailing_whitespace
::get_lines_with_trailing_whitespace_from_file_name(test_entry).is_empty()
});
assert_eq!(
error_files, expected_error_files,
"Detected files with trailing whitespace are not as expected"
);
for error in &error_list {
match error {
EditorConfigError::FileError(file_error) => match file_error.error_type() {
CheckErrorType::TrimTrailingWhitespaceError {
lines_with_trailing_whitespace,
} => {
let actual_lines_with_trailing_whitespace= test_utils::trim_trailing_whitespace
::get_lines_with_trailing_whitespace_from_file_name(file_error.path()).len();
assert_eq!(
&actual_lines_with_trailing_whitespace,
lines_with_trailing_whitespace,
"Wrong number of lines with trailing whitespace found for {}",
file_error.path().display()
);
}
_ => panic!(
"The check error is not about trailing whitespace: {}",
file_error.path().display()
),
},
e => panic!("The error is no file error: {e}"),
}
}
}
#[test]
fn test_check_trim_trailing_whitespace_false() {
let temp_dir = copy_test_files(false).expect("Copy tests files into temp dir necessary");
let result = ecformat::check(&integration_test_utils::get_command_args(temp_dir.path()));
if let Err(error) = result {
panic!("Errors found: {error}")
}
}
#[rstest]
fn test_fix_trim_trailing_whitespace(#[values(true, false)] trim_trailing_whitespace: bool) {
let temp_dir = copy_test_files(trim_trailing_whitespace)
.expect("Copy tests files into temp dir necessary");
integration_test_utils::run_fix_command(temp_dir.path());
}
fn copy_test_files(trim_trailing_whitespace: bool) -> io::Result<TempDir> {
integration_test_utils::copy_test_files(
test_utils::TestFileHelper::new::<TrimTrailingWs>(),
trim_trailing_whitespace.to_string(),
)
}