Skip to content

Commit

Permalink
Move test fixtures into conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrw committed Jan 9, 2025
1 parent 8f2dbeb commit 4352888
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pythondap/python/pythondap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def main():
parser.add_argument("-n", "--configuration")
args = parser.parse_args()

ns = DebugSession(
ns = DebugSession( # noqa: F841
breakpoints=args.breakpoint,
file=args.file,
config_path=args.launch_configuration,
Expand Down
56 changes: 56 additions & 0 deletions pythondap/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from contextlib import contextmanager
import copy
import io
import json
from os import PathLike
import shutil
import socket
import subprocess as sp
Expand All @@ -8,6 +12,8 @@

import pytest

from pythondap.session import DebugSession


# vendored from LocalStack
def get_free_tcp_port(blocklist: list[int] | None = None) -> int:
Expand Down Expand Up @@ -84,3 +90,53 @@ def execute(sleep_time: int = 5):
child.kill()

yield execute


LAUNCH_TEMPLATE = {
"version": "0.2.0",
"configurations": [
{
"name": "PLACEHOLDER",
"type": "debugpy",
"request": "launch",
"program": "PLACEHOLDER",
"justMyCode": False,
},
],
}


@pytest.fixture
def write_config():
def inner(outfile: io.StringIO, script_path: PathLike, config_name: str):
template = copy.deepcopy(LAUNCH_TEMPLATE)
template["configurations"][0]["name"] = config_name
template["configurations"][0]["program"] = str(script_path)
json.dump(template, outfile)

return inner


@pytest.fixture
def environment(run_server, tmp_path, write_config):
@contextmanager
def inner(python_code: str, breakpoints: list[int]):
script = tmp_path.joinpath("script.py")
with script.open("w") as outfile:
outfile.write(python_code)

config_path = tmp_path.joinpath("launch.json")
config_name = "Launch"
with config_path.open("w") as outfile:
write_config(outfile, script, config_name)

debugger = DebugSession(
breakpoints=breakpoints,
file=str(script),
config_path=str(config_path),
config_name=config_name,
program=script,
)
yield debugger

yield inner
60 changes: 0 additions & 60 deletions pythondap/tests/test_hooks.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,9 @@
from contextlib import contextmanager
import copy
import io
import json
from os import PathLike

import pytest

from pythondap.session import DebugSession


PYTHON_CODE = """a = 10
b = 20
a = a + b
print(a)
"""

LAUNCH_TEMPLATE = {
"version": "0.2.0",
"configurations": [
{
"name": "PLACEHOLDER",
"type": "debugpy",
"request": "launch",
"program": "PLACEHOLDER",
"justMyCode": False,
},
],
}


@pytest.fixture
def write_config():
def inner(outfile: io.StringIO, script_path: PathLike, config_name: str):
template = copy.deepcopy(LAUNCH_TEMPLATE)
template["configurations"][0]["name"] = config_name
template["configurations"][0]["program"] = str(script_path)
json.dump(template, outfile)

return inner


@pytest.fixture
def environment(run_server, tmp_path, write_config):
@contextmanager
def inner(python_code: str, breakpoints: list[int]):
script = tmp_path.joinpath("script.py")
with script.open("w") as outfile:
outfile.write(python_code)

config_path = tmp_path.joinpath("launch.json")
config_name = "Launch"
with config_path.open("w") as outfile:
write_config(outfile, script, config_name)

debugger = DebugSession(
breakpoints=breakpoints,
file=str(script),
config_path=str(config_path),
config_name=config_name,
program=script,
)
yield debugger

yield inner


def test_execute(environment):
with environment(PYTHON_CODE, breakpoints=[0]) as env:
Expand Down

0 comments on commit 4352888

Please sign in to comment.