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 `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 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 `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 =
        integration_test_utils::get_filtered_test_file_names::<Charset, _>(|test_entry| {
            let entry_charset = test_utils::charset::get_charset_from_file_name(test_entry);
            entry_charset != Ok(charset)
        });
    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 {
            EditorConfigError::FileError(file_error) => match file_error.error_type() {
                CheckErrorType::CharsetError {
                    actual_charset,
                    expected_charset,
                } => {
                    assert_eq!(
                        &charset,
                        expected_charset,
                        "Expected charset wrong: {}",
                        file_error.path().display()
                    );
                    let file_charset = file_error.path().file_stem().unwrap().to_str().unwrap();
                    assert_eq!(
                        file_charset,
                        actual_charset,
                        "Actual charset wrong: {}",
                        file_error.path().display()
                    );
                }
                _ => panic!(
                    "The check error is not about charset: {}",
                    file_error.path().display()
                ),
            },
            e => panic!("The error is no file error: {e}"),
        }
    }
}

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