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

Added OverrideDryrun #34

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
History
=======

5.2.0 (2024-09-18)
------------------

* Added ``OverrideDryrun`` context manager


5.1.1 (2024-08-12)
------------------

Expand Down
4 changes: 2 additions & 2 deletions src/runez/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from runez.program import check_pid, is_executable, make_executable, PsInfo, run, shell, which
from runez.serialize import from_json, read_json, represented_json, save_json, Serializable
from runez.system import abort, abort_if, AdaptedProperty, cached_property, uncolored, Undefined, UNSET, wcswidth
from runez.system import Anchored, CaptureOutput, CurrentFolder, Slotted, TempArgv, TrackedOutput
from runez.system import Anchored, CaptureOutput, CurrentFolder, OverrideDryrun, Slotted, TempArgv, TrackedOutput
from runez.system import capped, decode, DEV, flattened, joined, quoted, resolved_path, short, stringified, SYS_INFO
from runez.system import first_line, get_version, is_basetype, is_iterable, ltattr

Expand All @@ -39,7 +39,7 @@
"check_pid", "is_executable", "make_executable", "PsInfo", "run", "shell", "which",
"from_json", "read_json", "represented_json", "save_json", "Serializable",
"abort", "abort_if", "AdaptedProperty", "cached_property", "uncolored", "Undefined", "UNSET", "wcswidth",
"Anchored", "CaptureOutput", "CurrentFolder", "Slotted", "TempArgv", "TrackedOutput",
"Anchored", "CaptureOutput", "CurrentFolder", "OverrideDryrun", "Slotted", "TempArgv", "TrackedOutput",
"capped", "decode", "DEV", "flattened", "joined", "quoted", "resolved_path", "short", "stringified", "SYS_INFO",
"first_line", "get_version", "is_basetype", "is_iterable", "ltattr"
]
Expand Down
14 changes: 14 additions & 0 deletions src/runez/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,20 @@ def __exit__(self, *_):
Anchored.pop(self.destination)


class OverrideDryrun:
"""Context manager to temporarily override dryrun mode"""

def __init__(self, dryrun):
self.dryrun = dryrun

def __enter__(self):
self.prior = _R.set_dryrun(self.dryrun)
return self.prior

def __exit__(self, *_):
_R.set_dryrun(self.prior)


class TrackedOutput:
"""Track captured output"""

Expand Down
18 changes: 18 additions & 0 deletions tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,24 @@ def test_system():
assert str(runez.system.PlatformInfo("Darwin 20.5.0 x86_64 i386")) == "Darwin/20.5.0; x86_64 i386"
assert str(runez.system.PlatformInfo("Linux 5.10.25 x86_64 x86_64")) == "Linux/5.10.25; x86_64"

assert runez.DRYRUN is False
with runez.OverrideDryrun(True) as prior1:
assert runez.DRYRUN is True
assert prior1 is False
with runez.OverrideDryrun(False) as prior2:
# Flip-flopping OK
assert runez.DRYRUN is False
assert prior2 is True

with runez.OverrideDryrun(False) as prior3:
# Overriding twice with the same value OK
assert runez.DRYRUN is False
assert prior3 is False

assert runez.DRYRUN is True

assert runez.DRYRUN is False

ct = runez.DEV.current_test()
assert ct
assert not ct.is_main
Expand Down
Loading