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

Take the FORCE_COLOR and NO_COLOR environment variables into account #9128

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Create _is_env_set_and_non_empty
Signed-off-by: Stavros Ntentos <133706+stdedos@users.noreply.github.com>
  • Loading branch information
stdedos committed Jan 10, 2025
commit c6ca5e554e6aa1989fe02ef4017ae535d29a6e8f
6 changes: 6 additions & 0 deletions pylint/lint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import contextlib
import os
import platform
import sys
import traceback
Expand Down Expand Up @@ -133,3 +134,8 @@ def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
yield
finally:
sys.path[:] = original


def _is_env_set_and_non_empty(env_var: str) -> bool:
"""Checks if env_var is set and non-empty."""
return os.environ.get(env_var) not in ["", None]
35 changes: 34 additions & 1 deletion tests/lint/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

import unittest.mock
from pathlib import Path, PosixPath
from typing import Any

import pytest

from pylint.constants import full_version
from pylint.lint.utils import get_fatal_error_message, prepare_crash_report
from pylint.lint.utils import (
_is_env_set_and_non_empty,
get_fatal_error_message,
prepare_crash_report,
)
from pylint.testutils._run import _Run as Run


Expand Down Expand Up @@ -56,3 +61,31 @@ def test_issue_template_on_fatal_errors(capsys: pytest.CaptureFixture) -> None:
assert "Fatal error while checking" in captured.out
assert "Please open an issue" in captured.out
assert "Traceback" in captured.err


@pytest.mark.parametrize(
"value, expected",
[
(None, False),
("", False),
(0, True),
(1, True),
(2, True),
(False, True),
("no", True),
("off", True),
("on", True),
(True, True),
("yes", True),
],
ids=repr,
)
def test_is_env_set_and_non_empty(
monkeypatch: pytest.MonkeyPatch, value: Any, expected: bool
) -> None:
"""Test the function returns True if the environment variable is set and non-empty."""
env_var = "TEST_VAR"
if value is not None:
monkeypatch.setenv(env_var, str(value))

assert _is_env_set_and_non_empty(env_var) == expected