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

use std::io;

use ec4rs::property::FinalNewline;
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 `insert_final_newline=true`.
#[test]
fn test_check_insert_final_newline_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::<FinalNewline, _>(|test_entry| {
            test_utils::insert_final_newline::get_needs_final_newline_from_file_name(test_entry)
        });
    assert_eq!(
        error_files, expected_error_files,
        "Detected files with missing final newline are not as expected"
    );

    // Type of error correct?
    for error in &error_list {
        match error {
            EditorConfigError::FileError(file_error) => match file_error.error_type() {
                CheckErrorType::InsertFinalNewlineError {} => {
                    // Nothing to check as the error type has not fields
                }
                _ => panic!(
                    "The check error is not about final newline: {}",
                    file_error.path().display()
                ),
            },
            e => panic!("The error is no file error: {e}"),
        }
    }
}

/// Runs the `check` command to the test files with `insert_final_newline=false`.
#[test]
fn test_check_insert_final_newline_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 `insert_final_newline` property
#[rstest]
fn test_fix_insert_final_newline(#[values(true, false)] insert_final_newline: bool) {
    let temp_dir =
        copy_test_files(insert_final_newline).expect("Copy tests files into temp dir necessary");
    integration_test_utils::run_fix_command(temp_dir.path());
}

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