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 function [`set_charset_of_file`]

use std::fs;

use ec4rs::property::Charset;
use rstest::rstest;
use tempfile::NamedTempFile;

use super::super::CharsetHandler;
use crate::test_utils::TestFileHelper;

/// Test on all files from [`get_test_file_paths`].
#[rstest]
fn test_set_charset_of_file_success(
    #[values(
        Charset::Utf8,
        Charset::Latin1,
        Charset::Utf16Le,
        Charset::Utf16Be,
        Charset::Utf8Bom
    )]
    expected_charset: Charset,
) {
    let handler = CharsetHandler {
        charset: expected_charset,
    };
    for test_entry in TestFileHelper::new::<Charset>().get_test_file_paths() {
        let test_file = NamedTempFile::new().expect("Tempfile needs to be possible");
        let test_file_path = test_file.path();

        fs::copy(&test_entry, test_file_path).expect("Copy to temp file is necessary");
        assert!(
            handler
                .set_charset_of_file(test_file_path)
                .expect("No IO error at setting charset"),
            "Charset needs to be determinable"
        );
        let actual_charset = CharsetHandler::get_charset_from_file(test_file_path)
            .expect("No IO error as getting charset");
        assert_eq!(
            actual_charset,
            Some(Ok(expected_charset)),
            "Charset needs to be correct after setting it"
        );
    }
}