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 function
//! [`fix_indentations`](super::super::IndentationHandler::fix_indentations)

use std::fs;

use ec4rs::{
    PropertyKey,
    property::{self, Charset},
};
use line_ending::LineEnding;

use super::super::IndentationHandler;
use crate::test_utils::{TestFileHelper, indentation::get_infos_from_file_name};

/// Test on all files from [`TestFileHelper::get_test_file_paths`] for the `indent_style` property,
/// which should contain files where the `indent_style` property is violated.
/// The pattern for the file names is described at
/// [`get_infos_from_file_name`](crate::test_utils::indentation::get_infos_from_file_name).
#[test]
fn test_fix_indentations_on_test_files_for_indent_style() {
    test_fix_indentations_on_test_files::<property::IndentStyle>();
}

/// Test on all files from [`TestFileHelper::get_test_file_paths`] for the `indent_size` property,
/// which should contain files where the `indent_size` property is violated.
/// The pattern for the file names is described at
/// [`get_infos_from_file_name`](crate::test_utils::indentation::get_infos_from_file_name).
#[test]
fn test_fix_indentations_on_test_files_for_indent_size() {
    test_fix_indentations_on_test_files::<property::IndentSize>();
}

fn test_fix_indentations_on_test_files<P: PropertyKey>() {
    for test_entry in TestFileHelper::new::<P>().get_test_file_paths() {
        let (indent_style, indent_size, tab_width, actual_lines_with_wrong_indentation) =
            get_infos_from_file_name(&test_entry);
        let handler = IndentationHandler {
            charset: Charset::Utf8, // charset not relevant for the tested methods
            line_ending: LineEnding::from_current_platform(), // git handles line endings
            indent_style: Some(indent_style),
            indent_size: Some(indent_size),
            tab_width: Some(tab_width),
        };
        let expect_change = !actual_lines_with_wrong_indentation.is_empty();
        let mut content = fs::read_to_string(&test_entry).expect("Test file needs to be readable");
        let content_orginal = content.clone();
        let changed = handler.fix_indentations(&mut content);

        assert_eq!(
            changed,
            content != content_orginal,
            "The return value has to indicate if the content was changed or not"
        );
        assert_eq!(
            changed, expect_change,
            "The fix has to change if and only if there are lines with wrong indentation"
        );
        if changed {
            assert!(
                handler
                    .get_lines_with_wrong_indentation(&content)
                    .next()
                    .is_none(),
                "There are still wrong indented lines after the fix",
            )
        }
    }
}