use std::{
ffi::OsStr,
fs, io,
path::{Path, PathBuf},
};
use itertools::Itertools;
use tempfile::{TempDir, tempdir};
use ecformat::{
cli::{CommandArgs, EditorConfigArgs, IgnoreArgs},
errors::CheckErrorList,
};
#[path = "../../src/test_utils.rs"]
pub mod test_utils;
use test_utils::TestFileHelper;
pub fn copy_test_files(
test_file_helper: TestFileHelper,
editorconfig_name: String,
) -> io::Result<TempDir> {
let target_dir = tempdir()?;
for test_entry in test_file_helper.get_test_file_paths() {
let file_name = test_entry.file_name().unwrap();
let mut target_file = PathBuf::from(target_dir.path());
target_file.push(file_name);
fs::copy(test_entry, target_file)?;
}
let mut editorconfig = PathBuf::from(target_dir.path());
let mut editorconfig_template = test_file_helper.get_test_dir();
editorconfig.push(".editorconfig");
editorconfig_template.push(editorconfig_name + ".editorconfig");
fs::copy(editorconfig_template, editorconfig)?;
Ok(target_dir)
}
pub fn check_error_list_as_file_names(error_list: &CheckErrorList) -> Vec<&OsStr> {
error_list
.into_iter()
.map(|ce| ce.path().file_name().unwrap())
.sorted()
.collect()
}
pub fn get_command_args(target: &Path) -> CommandArgs {
CommandArgs::new(
Some(target),
EditorConfigArgs {
charset: true,
end_of_line: true,
},
IgnoreArgs {
hidden: false, git_settings: true,
ignore_file: None,
},
)
}
pub fn run_check_command(target: &Path) -> CheckErrorList {
let err =
ecformat::check(&get_command_args(target)).expect_err("check command has to find errors");
err.downcast::<CheckErrorList>()
.expect("list of check error expected")
}
pub fn run_fix_command(target: &Path) {
let args = get_command_args(target);
ecformat::fix(&args).expect("fix command should not have any error");
ecformat::check(&args).expect("check command after fix should not find any error");
}