Skip to content

Commit c6ca5e5

Browse files
committed
Create _is_env_set_and_non_empty
Signed-off-by: Stavros Ntentos <[email protected]>
1 parent 14b242f commit c6ca5e5

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pylint/lint/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import contextlib
8+
import os
89
import platform
910
import sys
1011
import traceback
@@ -133,3 +134,8 @@ def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
133134
yield
134135
finally:
135136
sys.path[:] = original
137+
138+
139+
def _is_env_set_and_non_empty(env_var: str) -> bool:
140+
"""Checks if env_var is set and non-empty."""
141+
return os.environ.get(env_var) not in ["", None]

tests/lint/test_utils.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
import unittest.mock
66
from pathlib import Path, PosixPath
7+
from typing import Any
78

89
import pytest
910

1011
from pylint.constants import full_version
11-
from pylint.lint.utils import get_fatal_error_message, prepare_crash_report
12+
from pylint.lint.utils import (
13+
_is_env_set_and_non_empty,
14+
get_fatal_error_message,
15+
prepare_crash_report,
16+
)
1217
from pylint.testutils._run import _Run as Run
1318

1419

@@ -56,3 +61,31 @@ def test_issue_template_on_fatal_errors(capsys: pytest.CaptureFixture) -> None:
5661
assert "Fatal error while checking" in captured.out
5762
assert "Please open an issue" in captured.out
5863
assert "Traceback" in captured.err
64+
65+
66+
@pytest.mark.parametrize(
67+
"value, expected",
68+
[
69+
(None, False),
70+
("", False),
71+
(0, True),
72+
(1, True),
73+
(2, True),
74+
(False, True),
75+
("no", True),
76+
("off", True),
77+
("on", True),
78+
(True, True),
79+
("yes", True),
80+
],
81+
ids=repr,
82+
)
83+
def test_is_env_set_and_non_empty(
84+
monkeypatch: pytest.MonkeyPatch, value: Any, expected: bool
85+
) -> None:
86+
"""Test the function returns True if the environment variable is set and non-empty."""
87+
env_var = "TEST_VAR"
88+
if value is not None:
89+
monkeypatch.setenv(env_var, str(value))
90+
91+
assert _is_env_set_and_non_empty(env_var) == expected

0 commit comments

Comments
 (0)