use std::{
ffi::OsString,
fs, io,
path::{Path, PathBuf},
};
use ec4rs::PropertyKey;
use itertools::Itertools;
use tempfile::{TempDir, tempdir};
use ecformat::{
cli::{CommandArgs, EditorConfigArgs, IgnoreArgs},
errors::{CheckErrorList, EditorConfigError},
};
#[path = "../../src/test_utils.rs"]
pub mod test_utils;
use test_utils::TestFileHelper;
#[allow(dead_code)] 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)
}
#[allow(dead_code)] pub fn copy_test_editorconfigs(test_file_helper: TestFileHelper) -> io::Result<TempDir> {
let target_dir = tempdir()?;
for test_entry in test_file_helper.get_test_editorconfig_paths() {
let editorconfig_name = test_entry.file_stem().unwrap();
let mut target = PathBuf::from(target_dir.path());
target.push(editorconfig_name);
fs::create_dir(&target)?;
fs::copy(&test_entry, target.join(".editorconfig"))?;
fs::copy(&test_entry, target.join("EditorConfig_applies_to.txt"))?;
}
Ok(target_dir)
}
#[allow(dead_code)] pub fn check_error_list_as_file_names(error_list: &CheckErrorList) -> Vec<OsString> {
error_list
.into_iter()
.map(|ce| {
match ce {
EditorConfigError::FileError(file_error) => {
file_error.path().file_name().unwrap().to_owned()
}
EditorConfigError::ConfigError(config_error) => {
let mut name = config_error
.path()
.parent()
.unwrap()
.file_name()
.unwrap()
.to_owned();
name.push(".editorconfig");
name
}
}
})
.sorted()
.collect()
}
#[allow(dead_code)] pub fn get_filtered_test_file_names<T: PropertyKey, P>(predicate: P) -> Vec<OsString>
where
P: FnMut(&PathBuf) -> bool,
{
test_utils::TestFileHelper::new::<T>()
.get_test_file_paths()
.filter(predicate)
.map(|path| path.file_name().unwrap().to_owned())
.sorted()
.collect()
}
#[allow(dead_code)] pub fn get_filtered_test_editorconfig_names<T: PropertyKey, P>(predicate: P) -> Vec<OsString>
where
P: FnMut(&PathBuf) -> bool,
{
test_utils::TestFileHelper::new::<T>()
.get_test_editorconfig_paths()
.filter(predicate)
.map(|path| path.file_name().unwrap().to_owned())
.sorted()
.collect()
}
pub fn get_command_args(target: &Path) -> CommandArgs {
CommandArgs::new(
Some(target),
EditorConfigArgs {
charset: true,
end_of_line: true,
trim_trailing_whitespace: true,
insert_final_newline: true,
indentation_handling: true,
spelling_language: true,
},
IgnoreArgs {
hidden: false, git_settings: true,
ignore_file: None,
},
)
}
#[allow(dead_code)] 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")
}
#[allow(dead_code)] 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");
}