Skip to content

Commit

Permalink
Merge pull request #16 from sandialabs/add-dry-run-test
Browse files Browse the repository at this point in the history
Add dry run test
  • Loading branch information
jmgate authored Jun 10, 2024
2 parents 14a8995 + c44d6ff commit 810d6fb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
35 changes: 35 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[run]
omit =
test/*
*/.vscode/*
branch = True
dynamic_context = test_function
relative_files = True

[paths]
source = staged_script

[report]
skip_covered = False
fail_under = 100
show_missing = True
exclude_lines =
pragma: no cover
if 0:
if False:

[html]
directory = test/htmlcov
title = staged-script Coverage Report
show_contexts = True

[xml]
output = test/coverage.xml

[json]
output = test/coverage.json
pretty_print = True
show_contexts = True

[lcov]
output = test/coverage.lcov
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
strategy:
matrix:
version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false
steps:

- name: Check out the commit
Expand Down
2 changes: 0 additions & 2 deletions staged_script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
HelpFormatter,
RetryStage,
StageDuration,
lazy_property,
)

__all__ = [
"StagedScript",
"HelpFormatter",
"RetryStage",
"StageDuration",
"lazy_property",
]
__version__ = "1.0.0"
31 changes: 2 additions & 29 deletions staged_script/staged_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,6 @@
rich.traceback.install()


def lazy_property(func: Callable) -> property:
"""
A decorator to make it such that a property is lazily evaluated.
When the property is first accessed, the object will not yet have a
corresponding attribute, so the value will be computed by executing
:arg:`func`. Any time the property is accessed thereafter, the
value will just be retrieved from the object's corresponding
attribute.
Args:
func: The function used to compute the value of the property.
Returns:
The lazy property decorator.
"""
attr_name = f"_lazy_{func.__name__}"

@property # type: ignore[misc]
def _lazy_property(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, func(self))
return getattr(self, attr_name)

return _lazy_property # type: ignore[return-value]


class StageDuration(NamedTuple):
"""
The duration of a stage.
Expand Down Expand Up @@ -250,7 +223,7 @@ def _validate_stage_name(stage_name: str) -> None:
# Parse the command line arguments.
#

@lazy_property
@functools.cached_property
def parser(self) -> ArgumentParser:
"""
The base argument parser.
Expand All @@ -261,7 +234,7 @@ def parser(self) -> ArgumentParser:
.. code-block:: python
@lazy_property
@functools.cached_property
def parser(self) -> ArgumentParser:
ap = super().parser
ap.description = '''INSERT DESCRIPTION HERE'''
Expand Down
12 changes: 12 additions & 0 deletions test/test_staged_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ def test_run_override_print_commands(
assert f"Executing: {command}" in captured.out


def test_run_dry_run(
script: StagedScript, capsys: pytest.CaptureFixture
) -> None:
"""Test the :func:`run` method in dry-run mode."""
command = "echo 'dry-run mode'"
script.dry_run = True
script.run(command)
captured = capsys.readouterr()
for _ in ["The command executed would be", command]:
assert _ in captured.out


@pytest.mark.parametrize("script_success", [True, False])
@pytest.mark.parametrize(
"extras",
Expand Down

0 comments on commit 810d6fb

Please sign in to comment.