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 `charset` property of EditorConfig.
//! Files for the Integration Tests here, and some unit tests
//! are in `tests/resources/charset`.

use std::io;

use ec4rs::property::Charset;
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 `charset` property.
#[rstest]
fn test_check_charset(
    #[values(
        Charset::Utf8,
        Charset::Latin1,
        Charset::Utf16Le,
        Charset::Utf16Be,
        Charset::Utf8Bom
    )]
    charset: Charset,
) {
    let temp_dir = copy_test_files(&charset).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::<Charset>()
        .get_test_file_paths()
        .filter(|test_entry| {
            let entry_charset = test_utils::charset::get_charset_from_file_name(test_entry);
            entry_charset != Ok(charset)
        })
        .map(|path| path.file_name().unwrap().to_owned())
        .sorted()
        .collect_vec();
    assert_eq!(
        error_files, expected_error_files,
        "Detected files with different charset are not as expected"
    );

    // Type and fields of error correct?
    for error in &error_list {
        match error.error_type() {
            CheckErrorType::CharsetError {
                actual_charset,
                expected_charset,
            } => {
                assert_eq!(
                    &charset,
                    expected_charset,
                    "Expected charset wrong: {}",
                    error.path().display()
                );
                let file_charset = error.path().file_stem().unwrap().to_str().unwrap();
                assert_eq!(
                    file_charset,
                    actual_charset,
                    "Actual charset wrong: {}",
                    error.path().display()
                );
            }
            _ => panic!(
                "The check error is not about charset: {}",
                error.path().display()
            ),
        }
    }
}

/// Runs the `fix` command to the test files for the `charset` property
#[rstest]
fn test_fix_charset(
    #[values(
        Charset::Utf8,
        Charset::Latin1,
        Charset::Utf16Le,
        Charset::Utf16Be,
        Charset::Utf8Bom
    )]
    charset: Charset,
) {
    let temp_dir = copy_test_files(&charset).expect("Copy tests files into temp dir necessary");
    integration_test_utils::run_fix_command(temp_dir.path());
}

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