Skip to content

Commit

Permalink
test trace command: execute a trace against testnet (#61)
Browse files Browse the repository at this point in the history
* generate pytest file via test trace
* test execution
* capitalize constant & use removesuffix()
  • Loading branch information
andrey-kuprianov authored Jul 28, 2022
1 parent 75fe55d commit 7186c0b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
2 changes: 1 addition & 1 deletion atomkraft/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
add_completion=False,
name="atomkraft",
no_args_is_help=True,
rich_markup_mode="rich",
)

GH_TEMPLATE = "gh:informalsystems/atomkraft"


@app.command(
no_args_is_help=True,
)
def init(
name: Path = typer.Argument(..., help="Name of new directory", show_default=False)
Expand Down
26 changes: 19 additions & 7 deletions atomkraft/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional
import typer
from .trace import test_trace

app = typer.Typer(rich_markup_mode="rich", add_completion=False)

Expand All @@ -12,20 +13,31 @@ def FileOption(help, default):
)


def RequiredFileOption(help, default):
return typer.Option(
...,
show_default=False,
help=f"{help} [grey30]\[default: set via [bold cyan]atomkraft {default}[/bold cyan]][/grey30]",
)


@app.command()
def trace(
trace: typer.FileText = FileOption("trace to execute", "model"),
# currently, require the trace to be present.
# later, there will be an option to pick up the last one from the model
trace: typer.FileText = RequiredFileOption("trace to execute", "model"),
reactor: typer.FileText = FileOption("reactor to interpret the trace", "reactor"),
keypath: str = typer.Option(
"action",
show_default=True,
help=f"Path to key used as step name, extracted from ITF states",
),
):
"""
Test blockchain by running one trace
"""
print(
f"""
Trace: {trace}
Reactor: {reactor}
"""
)

test_trace(trace.name, reactor if reactor is None else reactor.name, keypath)


@app.command()
Expand Down
57 changes: 57 additions & 0 deletions atomkraft/test/trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from os import PathLike
from atomkraft.utils.project import project_root
import os.path
from datetime import datetime
import pytest
from ..reactor.reactor import get_reactor


TRACE_TEST_STUB = """
from modelator.pytest.decorators import itf
pytest_plugins = "{0}"
@itf("{1}", keypath="{2}")
def test_trace():
print("Successfully executed trace {1}")
"""


def test_trace(trace: PathLike, reactor: PathLike, keypath: str):
"""
Test blockchain by running one trace
"""

if reactor is None:
reactor = get_reactor()

root = project_root()
if not root:
raise RuntimeError(
"could not find Atomkraft project: are you in the right directory?"
)

tests = os.path.join(root, "tests")
if not os.path.isdir(tests):
raise RuntimeError(
"Atomkraft tests directory not found: have you executed `atomkraft init`?"
)

timestamp = datetime.now().isoformat(timespec="milliseconds")
test_name = f"test_{str(trace)}_{timestamp}"
test_name = (
test_name.replace("/", "_")
.replace(".", "_")
.replace(":", "_")
.replace("-", "_")
)
test_path = os.path.join(tests, f"{test_name}.py")
with open(test_path, "w") as test:
print(f"Writing {test_name} ...")
test.write(
TRACE_TEST_STUB.format(
str(reactor).replace("/", ".").removesuffix(".py"), trace, keypath
)
)
print(f"Executing {test_name} ...")
pytest.main(["-s", test_path])

0 comments on commit 7186c0b

Please sign in to comment.