ecformat 0.1.1

command line tool to keep files correct in respect of your EditorConfig
Documentation
// SPDX-FileCopyrightText: Contributors to ecformat project <https://codeberg.org/BaumiCoder/ecformat>
//
// SPDX-License-Identifier: BlueOak-1.0.0

//! Module with helper functions only for integration tests.

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;

/// Copy the test files and the EditorConfig into the returned temporary directory
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)
}

/// Extracts the file names of the errors in an error list.
/// The file names are sorted to have a stable order, suitable for comparisons.
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()
}

/// Default command arguments for Integration tests
pub fn get_command_args(target: &Path) -> CommandArgs {
    CommandArgs::new(
        Some(target),
        EditorConfigArgs {
            charset: true,
            end_of_line: true,
        },
        IgnoreArgs {
            hidden: false, // ignore .editorconfig file itself
            git_settings: true,
            ignore_file: None,
        },
    )
}

/// Runs the `check` command on the given target path
/// and returns the list of errors found.
/// Panic if no error list was return by the `check` command.
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")
}

/// Runs the `fix` command on the given target path
/// and panic if itself or a follow `check` return an error.
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");
}