ecformat 0.2.0

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

//! Tests for the `end_of_line` property of EditorConfig.
//! Files for the Integration Tests here, and some unit tests
//! are in `tests/resources/end_of_line`.

use std::io;

use ec4rs::property::EndOfLine;
use itertools::Itertools;
use rstest::rstest;
use tempfile::TempDir;

use ecformat::errors::{CheckErrorType, EditorConfigError};

mod integration_test_utils;

use integration_test_utils::test_utils;

/// Runs the `check` command to the test files for the `end_of_line` property.
#[rstest]
fn test_check_end_of_line(
    #[values(EndOfLine::Lf, EndOfLine::CrLf, EndOfLine::Cr)] end_of_line: EndOfLine,
) {
    let temp_dir = copy_test_files(&end_of_line).expect("Copy tests files into temp dir necessary");
    let error_list = integration_test_utils::run_check_command(temp_dir.path());

    // All files with errors found?
    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::<EndOfLine, _>(|test_entry| {
            let entry_end_of_line =
                test_utils::end_of_line::get_end_of_line_from_file_name(test_entry);
            entry_end_of_line != vec![end_of_line]
        });
    assert_eq!(
        error_files, expected_error_files,
        "Detected files with wrong end of lines are not as expected"
    );

    // Type and fields of error correct?
    for error in &error_list {
        match error {
            EditorConfigError::FileError(file_error) => match file_error.error_type() {
                CheckErrorType::EndOfLineError {
                    expected_end_of_line,
                    wrong_end_of_lines,
                } => {
                    assert_eq!(
                        &end_of_line,
                        expected_end_of_line,
                        "Expected end of line wrong: {}",
                        file_error.path().display()
                    );
                    let file_end_of_lines =
                        test_utils::end_of_line::get_end_of_line_from_file_name(file_error.path());
                    let expected_wrong_end_of_lines = file_end_of_lines
                        .into_iter()
                        .filter(|eol| eol != expected_end_of_line)
                        .sorted_by_key(|eol| eol.to_string())
                        .collect_vec();
                    assert_eq!(
                        expected_wrong_end_of_lines,
                        wrong_end_of_lines
                            .clone()
                            .into_keys()
                            .sorted_by_key(|eol| eol.to_string())
                            .collect_vec(),
                        "Wrong end of lines not correct: {}",
                        file_error.path().display()
                    );
                }
                _ => panic!(
                    "The check error is not about end of lines: {}",
                    file_error.path().display()
                ),
            },
            e => panic!("The error is no file error: {e}"),
        }
    }
}

/// Runs the `fix` command to the test files for the `end_of_line` property
#[rstest]
fn test_fix_end_of_line(
    #[values(EndOfLine::Lf, EndOfLine::CrLf, EndOfLine::Cr)] end_of_line: EndOfLine,
) {
    let temp_dir = copy_test_files(&end_of_line).expect("Copy tests files into temp dir necessary");
    integration_test_utils::run_fix_command(temp_dir.path());
}

fn copy_test_files(end_of_line: &EndOfLine) -> io::Result<TempDir> {
    integration_test_utils::copy_test_files(
        test_utils::TestFileHelper::new::<EndOfLine>(),
        end_of_line.to_string().to_uppercase(),
    )
}