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

//! 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;

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 = test_utils::TestFileHelper::new::<EndOfLine>()
        .get_test_file_paths()
        .filter(|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]
        })
        .map(|path| path.file_name().unwrap().to_owned())
        .sorted()
        .collect_vec();
    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.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: {}",
                    error.path().display()
                );
                let file_end_of_lines =
                    test_utils::end_of_line::get_end_of_line_from_file_name(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: {}",
                    error.path().display()
                );
            }
            _ => panic!(
                "The check error is not about end of lines: {}",
                error.path().display()
            ),
        }
    }
}

/// 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(),
    )
}