Skip to content

Commit

Permalink
✨ [+feature] Added InitializeStreamCapabilities for testing (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrownell authored May 30, 2024
2 parents 955d2f0 + 7f2d71e commit ced9263
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
10 changes: 7 additions & 3 deletions Build.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,21 @@ def list_commands(self, *args, **kwargs): # pylint: disable=unused-argument
# ----------------------------------------------------------------------
this_dir = PathEx.EnsureDir(Path(__file__).parent)
src_dir = PathEx.EnsureDir(this_dir / "src" / "dbrownell_Common")
tests_dir = PathEx.EnsureDir(this_dir / "tests")


# ----------------------------------------------------------------------
Black = RepoBuildTools.BlackFuncFactory(this_dir, app)
Pylint = RepoBuildTools.PylintFuncFactory(src_dir, app)
Pytest = RepoBuildTools.PytestFuncFactory(
tests_dir, "dbrownell_Common", app, default_min_coverage=80.0
this_dir,
"dbrownell_Common",
app,
default_min_coverage=80.0,
)
UpdateVersion = RepoBuildTools.UpdateVersionFuncFactory(
src_dir.parent, src_dir / "__init__.py", app
src_dir.parent,
src_dir / "__init__.py",
app,
)
Package = RepoBuildTools.PackageFuncFactory(this_dir, app)
Publish = RepoBuildTools.PublishFuncFactory(this_dir, app)
Expand Down
29 changes: 25 additions & 4 deletions src/dbrownell_Common/TestHelpers/StreamTestHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Test helpers for content found in ../Streams"""

import re
import sys

from io import StringIO
from typing import Generator, Match, Optional
Expand Down Expand Up @@ -47,7 +48,7 @@ def Replace(
else:
replace_func = lambda _: "<scrubbed duration>"

return re.sub(
content = re.sub(
r"""(?#
Hours )(?P<hours>\d+)(?#
sep )\:(?#
Expand All @@ -59,6 +60,11 @@ def Replace(
content,
)

# Remove any trailing whitespace
content = "\n".join(line.rstrip() for line in content.split("\n"))

return content


# ----------------------------------------------------------------------
def GenerateDoneManagerAndContent(
Expand Down Expand Up @@ -118,12 +124,27 @@ def GenerateDoneManagerAndContent(
keep_seconds=keep_duration_seconds,
)

# Remove any trailing whitespace
content = "\n".join(line.rstrip() for line in content.split("\n"))

assert expected_result is None or final_result == expected_result, (
expected_result,
final_result,
content,
)
yield content


# ----------------------------------------------------------------------
def InitializeStreamCapabilities(
stream=sys.stdout,
) -> None:
"""Ensure that the provided stream outputs consistently within a variety of different environments."""

if not hasattr(
stream,
Capabilities._EMBEDDED_CAPABILITIES_ATTRIBUTE_NAME, # pylint: disable=protected-access
):
Capabilities(
columns=120,
supports_colors=True,
stream=stream,
no_column_warning=True,
)
9 changes: 9 additions & 0 deletions tests/TestHelpers/StreamTestHelpers_UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,12 @@ def Impl(
DONE! (0, <scrubbed duration>)
""",
)


# ----------------------------------------------------------------------
def test_InitializeStreamCapabilites():
# It should be safe to call this multiple times
InitializeStreamCapabilities()
InitializeStreamCapabilities()

assert True

0 comments on commit ced9263

Please sign in to comment.