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 [`get_editorconfig_files`]

use std::slice;

use itertools::Itertools;

use crate::{cli::IgnoreArgs, test_utils};

use super::super::get_editorconfig_files;

#[test]
fn test_get_editorconfig_files_defaults() {
    let repo = test_utils::get_repo();
    let args = IgnoreArgs {
        hidden: true,
        git_settings: true,
        ignore_file: None,
    };
    let config_files = get_editorconfig_files(slice::from_ref(&repo), &args);
    let mut vscode = false;
    for cf in config_files {
        let f = cf
            .expect("file in the iterator should not be an error")
            .path;

        assert!(
            !f.starts_with(repo.join(".git")),
            "A file of the .git directory is considered: {}",
            f.display()
        );
        if f.starts_with(repo.join(".vscode")) {
            vscode = true;
        }
        assert_eq!(
            f.file_name().unwrap(),
            ".editorconfig",
            "Another file than a .editorconfig file is included: {}",
            f.display()
        );
    }
    assert!(
        vscode,
        "no EditorConfig from .vscode (a hidden directory) in the editorconfig files"
    );
}

/// Test with the boolean options different from in the default.
#[test]
fn test_get_editorconfig_files_bool_options() {
    let repo = test_utils::get_repo();
    let args = IgnoreArgs {
        hidden: false,
        git_settings: false,
        ignore_file: None,
    };
    let config_files = get_editorconfig_files(slice::from_ref(&repo), &args);
    for cf in config_files {
        let f = cf
            .expect("file in the iterator should not be an error")
            .path;

        assert!(
            !f.starts_with(repo.join(".git")),
            "files in .git directory (a hidden directory) in editorconfig files: {}",
            f.display()
        );
        assert!(
            !f.starts_with(repo.join(".vscode")),
            "files in .vscode directory (a hidden directory) in editorconfig files: {}",
            f.display()
        );
        assert_eq!(
            f.file_name().unwrap(),
            ".editorconfig",
            "Another file than a .editorconfig file is included: {}",
            f.display()
        );
    }
}

/// Test multiple target files and folders
#[test]
fn test_get_editorconfig_files_multiple_targets() {
    let repo = test_utils::get_repo();
    let license_directory = repo.join("LICENSES");
    // The `.editorconfig` of tests/resources has a root = true
    // which implies that the `.editorconfig` of the repo root should not be included.
    let test_resouces_file = repo.join("tests").join("resources").join("README.md");
    let targets = vec![license_directory.clone(), test_resouces_file.clone()];
    let args = IgnoreArgs {
        hidden: true,
        git_settings: true,
        ignore_file: None,
    };
    let config_files = get_editorconfig_files(&targets, &args);
    let expected_paths = vec![
        license_directory,
        test_resouces_file.parent().unwrap().to_owned(),
    ]
    .into_iter()
    .map(|dir| dir.join(".editorconfig").canonicalize().unwrap())
    .sorted()
    .collect_vec();
    let paths = config_files
        .map(|cf| {
            cf.expect("file in the iterator should not be an error")
                .path
                .canonicalize()
                .unwrap()
        })
        .sorted()
        .collect_vec();

    assert_eq!(
        paths, expected_paths,
        "The found EditorConfigs are not as expected."
    )
}