Skip to content

Commit 7e1e892

Browse files
mikethemanhugovk
andauthored
Complete coverage via added tests (#106)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 92af748 commit 7e1e892

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ omit = [
107107
"*/termcolor/__main__.py",
108108
]
109109

110+
[tool.coverage.html]
111+
show_contexts = true
112+
110113
[tool.coverage.report]
111114
# Regexes for lines to exclude from consideration
112115
exclude_also = [

tests/test_termcolor.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import io
34
import os
45

56
import pytest
@@ -279,3 +280,38 @@ def test_tty(monkeypatch: pytest.MonkeyPatch, test_isatty: bool, expected: str)
279280

280281
# Act / Assert
281282
assert colored("text", color="cyan") == expected
283+
284+
285+
def test_no_sys_stdout_fileno(monkeypatch: pytest.MonkeyPatch) -> None:
286+
"""Assert no color when sys.stdout has no file descriptor"""
287+
# Arrange
288+
monkeypatch.setattr("sys.stdout", object())
289+
290+
# Act / Assert
291+
assert colored("text", color="cyan") == "text"
292+
293+
294+
@pytest.mark.parametrize(
295+
"test_isatty, expected",
296+
[
297+
(True, "\x1b[36mtext\x1b[0m"),
298+
(False, "text"),
299+
],
300+
)
301+
def test_unsupported_operation(
302+
monkeypatch: pytest.MonkeyPatch, test_isatty: bool, expected: str
303+
) -> None:
304+
"""Assert no color when sys.stdout does not support the operation"""
305+
306+
# Arrange
307+
class MockStdout:
308+
def fileno(self) -> None:
309+
raise io.UnsupportedOperation()
310+
311+
def isatty(self) -> bool:
312+
return test_isatty
313+
314+
monkeypatch.setattr("sys.stdout", MockStdout())
315+
316+
# Act / Assert
317+
assert colored("text", color="cyan") == expected

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ commands =
1616
{envpython} -m pytest \
1717
--cov termcolor \
1818
--cov tests \
19+
--cov-context test \
1920
--cov-report html \
2021
--cov-report term \
2122
--cov-report xml \

0 commit comments

Comments
 (0)