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

use std::io;

use ec4rs::property::TrimTrailingWs;
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 with `trim_trailing_whitespace=true`.
#[test]
fn test_check_trim_trailing_whitespace_true() {
    let temp_dir = copy_test_files(true).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::<TrimTrailingWs, _>(|test_entry| {
            !test_utils::trim_trailing_whitespace
                    ::get_lines_with_trailing_whitespace_from_file_name(test_entry).is_empty()
        });
    assert_eq!(
        error_files, expected_error_files,
        "Detected files with trailing whitespace 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::TrimTrailingWhitespaceError {
                    lines_with_trailing_whitespace,
                } => {
                    let actual_lines_with_trailing_whitespace= test_utils::trim_trailing_whitespace
                        ::get_lines_with_trailing_whitespace_from_file_name(file_error.path()).len();
                    assert_eq!(
                        &actual_lines_with_trailing_whitespace,
                        lines_with_trailing_whitespace,
                        "Wrong number of lines with trailing whitespace found for {}",
                        file_error.path().display()
                    );
                }
                _ => panic!(
                    "The check error is not about trailing whitespace: {}",
                    file_error.path().display()
                ),
            },
            e => panic!("The error is no file error: {e}"),
        }
    }
}

/// Runs the `check` command to the test files with `trim_trailing_whitespace=false`.
#[test]
fn test_check_trim_trailing_whitespace_false() {
    let temp_dir = copy_test_files(false).expect("Copy tests files into temp dir necessary");
    let result = ecformat::check(&integration_test_utils::get_command_args(temp_dir.path()));
    if let Err(error) = result {
        panic!("Errors found: {error}")
    }
}

/// Runs the `fix` command to the test files for the `trim_trailing_whitespace` property
#[rstest]
fn test_fix_trim_trailing_whitespace(#[values(true, false)] trim_trailing_whitespace: bool) {
    let temp_dir = copy_test_files(trim_trailing_whitespace)
        .expect("Copy tests files into temp dir necessary");
    integration_test_utils::run_fix_command(temp_dir.path());
}

fn copy_test_files(trim_trailing_whitespace: bool) -> io::Result<TempDir> {
    integration_test_utils::copy_test_files(
        test_utils::TestFileHelper::new::<TrimTrailingWs>(),
        trim_trailing_whitespace.to_string(),
    )
}