Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing tests for ANSI formatting and NO_COLOR #14

Open
ericcornelissen opened this issue Apr 29, 2023 · 0 comments
Open

Missing tests for ANSI formatting and NO_COLOR #14

ericcornelissen opened this issue Apr 29, 2023 · 0 comments
Labels
test Changes to automated tests

Comments

@ericcornelissen
Copy link
Owner

ericcornelissen commented Apr 29, 2023

Test Improvements

Caused by #13
Relates to #5, #16

Summary

The original no_colors.rs test did not function correctly (removing/commenting out the line cmd.env("NO_COLOR", "1"); did not change test outcomes). A new test suite should be created to validate the behavior of the CLI when it comes to ANSI styling and various conditions that affect it (such as the NO_COLOR environment variable).

Suggestion

The following general test outline is recommended based on current knowledge (tests/ansi_test.rs):

// SPDX-License-Identifier: Apache-2.0

//! Test suite focussed on testing that ANSI is or isn't used in stdout/stderr based on options to
//! suppress ansi-formatting.

pub mod common;

use crate::common::TestResult;

use predicates::prelude::*;

#[test]
fn example_test_with_color() -> TestResult {
    with_color(|mut cmd, _test_dir| {
        cmd.arg("--help").assert().stdout(has_ansi());

        Ok(())
    })
}

#[test]
fn example_test_without_color() -> TestResult {
    without_color(|mut cmd, _test_dir| {
        cmd.arg("--help").assert().stdout(no_ansi()).stderr(no_ansi());

        Ok(())
    })
}

/// Run a test with ANSI styling enabled.
///
/// See also [`common::with_test_dir`].
fn with_color<C>(callback: C) -> TestResult
where
    C: FnOnce(assert_cmd::Command, &assert_fs::TempDir) -> TestResult,
{
    common::with_test_dir(|cmd, test_dir| callback(cmd, test_dir))
}

/// Run a test with the `NO_COLOR` environment variable set.
///
/// See also [`common::with_test_dir`].
fn without_color<C>(callback: C) -> TestResult
where
    C: FnOnce(assert_cmd::Command, &assert_fs::TempDir) -> TestResult,
{
    common::with_test_dir(|mut cmd, test_dir| {
        cmd.env("NO_COLOR", "1");
        callback(cmd, test_dir)
    })
}

/// Create a predicate that evaluates to `true` if a given string contains ANSI characters.
fn has_ansi() -> predicates::str::RegexPredicate {
    // This regular expression is based on based on (MIT licensed):
    // <https://github.com/TheLarkInn/ansi-regex>
    const ANSI_REGEX: &str = r"\x1b\[([\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e])";

    predicates::str::is_match(ANSI_REGEX).expect("static regular expression should always be valid")
}

/// Create a predicate that evaluates to `true` if a given string does not contain ANSI characters.
fn no_ansi() -> predicates::boolean::NotPredicate<predicates::str::RegexPredicate, str> {
    has_ansi().not()
}
@ericcornelissen ericcornelissen added the test Changes to automated tests label Apr 29, 2023
@ericcornelissen ericcornelissen changed the title The no_color.rs test is incorrect Missing tests for ANSI formatting and NO_COLOR Apr 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
test Changes to automated tests
Projects
None yet
Development

No branches or pull requests

1 participant