From 47c61f5691b95a24aecb1623317e6c3d84ce8ea1 Mon Sep 17 00:00:00 2001 From: Tai Sakuma Date: Thu, 9 May 2024 09:45:06 -0400 Subject: [PATCH 1/3] Clean code --- .../plugins/ctrl/schema/mutation.py | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/nextlinegraphql/plugins/ctrl/schema/mutation.py b/nextlinegraphql/plugins/ctrl/schema/mutation.py index ce85425..a7053f1 100644 --- a/nextlinegraphql/plugins/ctrl/schema/mutation.py +++ b/nextlinegraphql/plugins/ctrl/schema/mutation.py @@ -1,26 +1,27 @@ -from typing import TYPE_CHECKING, Optional +from typing import Optional import strawberry +from nextline import Nextline from strawberry.types import Info -if TYPE_CHECKING: - from nextline import Nextline - async def mutate_exec(info: Info) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.run() return True async def mutate_run_and_continue(info: Info) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.run_and_continue() return True async def mutate_reset(info: Info, statement: Optional[str] = None) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.reset(statement=statement) return True @@ -28,25 +29,29 @@ async def mutate_reset(info: Info, statement: Optional[str] = None) -> bool: async def mutate_send_pdb_command( info: Info, command: str, prompt_no: int, trace_no: int ) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.send_pdb_command(command, prompt_no, trace_no) return True async def mutate_interrupt(info: Info) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.interrupt() return True async def mutate_terminate(info: Info) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.terminate() return True async def mutate_kill(info: Info) -> bool: - nextline: Nextline = info.context["nextline"] + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) await nextline.kill() return True From d4da8d3c354b22435b77d3e11121155f480c9eb9 Mon Sep 17 00:00:00 2001 From: Tai Sakuma Date: Thu, 9 May 2024 10:12:14 -0400 Subject: [PATCH 2/3] Add GraphQL mutation to load example script --- nextlinegraphql/plugins/ctrl/schema/mutation.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/nextlinegraphql/plugins/ctrl/schema/mutation.py b/nextlinegraphql/plugins/ctrl/schema/mutation.py index a7053f1..5031688 100644 --- a/nextlinegraphql/plugins/ctrl/schema/mutation.py +++ b/nextlinegraphql/plugins/ctrl/schema/mutation.py @@ -1,9 +1,16 @@ +from pathlib import Path from typing import Optional import strawberry from nextline import Nextline from strawberry.types import Info +from nextlinegraphql.plugins.ctrl import example_script as example_script_module + +EXAMPLE_SCRIPT_PATH = Path(example_script_module.__file__).parent / 'script.py' +assert EXAMPLE_SCRIPT_PATH.is_file() +example_script = EXAMPLE_SCRIPT_PATH.read_text() + async def mutate_exec(info: Info) -> bool: nextline = info.context['nextline'] @@ -56,6 +63,13 @@ async def mutate_kill(info: Info) -> bool: return True +async def mutate_load_example_script(info: Info) -> bool: + nextline = info.context['nextline'] + assert isinstance(nextline, Nextline) + await nextline.reset(statement=example_script) + return True + + @strawberry.type class Mutation: exec: bool = strawberry.field(resolver=mutate_exec) @@ -65,3 +79,4 @@ class Mutation: interrupt: bool = strawberry.field(resolver=mutate_interrupt) terminate: bool = strawberry.field(resolver=mutate_terminate) kill: bool = strawberry.field(resolver=mutate_kill) + load_example_script: bool = strawberry.field(resolver=mutate_load_example_script) From 382623e94cf22e0f78992361957178c7b1bca3ba Mon Sep 17 00:00:00 2001 From: Tai Sakuma Date: Thu, 9 May 2024 10:43:45 -0400 Subject: [PATCH 3/3] Add a test for loadExampleScript --- .../plugins/ctrl/graphql/__init__.py | 1 + .../graphql/mutations/LoadExampleScript.gql | 3 ++ .../mutations/test_load_example_script.py | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 nextlinegraphql/plugins/ctrl/graphql/mutations/LoadExampleScript.gql create mode 100644 tests/plugins/ctrl/schema/mutations/test_load_example_script.py diff --git a/nextlinegraphql/plugins/ctrl/graphql/__init__.py b/nextlinegraphql/plugins/ctrl/graphql/__init__.py index b32b536..78e32de 100644 --- a/nextlinegraphql/plugins/ctrl/graphql/__init__.py +++ b/nextlinegraphql/plugins/ctrl/graphql/__init__.py @@ -7,6 +7,7 @@ MUTATE_RESET = (sub / 'Reset.gql').read_text() MUTATE_SEND_PDB_COMMAND = (sub / 'SendPdbCommand.gql').read_text() MUTATE_RUN_AND_CONTINUE = (sub / 'RunAndContinue.gql').read_text() +MUTATE_LOAD_EXAMPLE_SCRIPT = (sub / 'LoadExampleScript.gql').read_text() sub = pwd / 'queries' QUERY_STATE = (sub / 'State.gql').read_text() diff --git a/nextlinegraphql/plugins/ctrl/graphql/mutations/LoadExampleScript.gql b/nextlinegraphql/plugins/ctrl/graphql/mutations/LoadExampleScript.gql new file mode 100644 index 0000000..89ae454 --- /dev/null +++ b/nextlinegraphql/plugins/ctrl/graphql/mutations/LoadExampleScript.gql @@ -0,0 +1,3 @@ +mutation LoadExampleScript { + loadExampleScript +} diff --git a/tests/plugins/ctrl/schema/mutations/test_load_example_script.py b/tests/plugins/ctrl/schema/mutations/test_load_example_script.py new file mode 100644 index 0000000..90b15e4 --- /dev/null +++ b/tests/plugins/ctrl/schema/mutations/test_load_example_script.py @@ -0,0 +1,28 @@ +from pathlib import Path + +from nextline import Nextline +from strawberry import Schema + +from nextlinegraphql.plugins.ctrl import example_script as example_script_module +from nextlinegraphql.plugins.ctrl.graphql import MUTATE_LOAD_EXAMPLE_SCRIPT +from nextlinegraphql.plugins.ctrl.schema import Mutation, Query, Subscription + +EXAMPLE_SCRIPT_PATH = Path(example_script_module.__file__).parent / 'script.py' +example_script = EXAMPLE_SCRIPT_PATH.read_text() + +INITIAL_SCRIPT = ''' +import time +time.sleep(0.001) +'''.strip() + + +async def test_query() -> None: + nextline = Nextline(INITIAL_SCRIPT) + async with nextline: + context = {'nextline': nextline} + schema = Schema(query=Query, mutation=Mutation, subscription=Subscription) + result = await schema.execute(MUTATE_LOAD_EXAMPLE_SCRIPT, context_value=context) + assert not result.errors + assert result.data + assert result.data['loadExampleScript'] is True + assert nextline.statement == example_script