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

Enhance emulation tests (Sourcery refactored) #27

Open
wants to merge 1 commit into
base: enhance-emulation-tests
Choose a base branch
from
Open
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
98 changes: 43 additions & 55 deletions pytest_libiio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,37 @@ def get_filename(map, hw):


def handle_iio_emu(ctx, request, _iio_emu):
if "hw" in ctx and hasattr(_iio_emu, "auto") and _iio_emu.auto:
if _iio_emu.current_device != ctx["hw"]:
# restart with new hw
if _iio_emu.p:
print("Stopping iio-emu")
_iio_emu.stop()
elif _iio_emu.current_device:
print("Using same hardware not restarting iio-emu")

map = get_hw_map(request)
fn, dd = get_filename(map, ctx["hw"])
if not fn:
return ctx
if request.config.getoption("--emu-xml-dir"):
path = request.config.getoption("--emu-xml-dir")
exml = os.path.join(path, fn)
print("exml", exml)
else:
path = pathlib.Path(__file__).parent.absolute()
exml = os.path.join(path, "resources", "devices", fn)
if not os.path.exists(exml):
pytest.skip(f"No XML file found for hardware {ctx['hw']}")
_iio_emu.xml_path = exml
_iio_emu.current_device = ctx["hw"]
_iio_emu.data_devices = dd
print("Starting iio-emu")
_iio_emu.start()
if (
"hw" in ctx
and hasattr(_iio_emu, "auto")
and _iio_emu.auto
and _iio_emu.current_device != ctx["hw"]
):
# restart with new hw
if _iio_emu.p:
print("Stopping iio-emu")
_iio_emu.stop()
elif _iio_emu.current_device:
print("Using same hardware not restarting iio-emu")

map = get_hw_map(request)
fn, dd = get_filename(map, ctx["hw"])
if not fn:
return ctx
if request.config.getoption("--emu-xml-dir"):
path = request.config.getoption("--emu-xml-dir")
exml = os.path.join(path, fn)
print("exml", exml)
else:
path = pathlib.Path(__file__).parent.absolute()
exml = os.path.join(path, "resources", "devices", fn)
if not os.path.exists(exml):
pytest.skip(f"No XML file found for hardware {ctx['hw']}")
_iio_emu.xml_path = exml
_iio_emu.current_device = ctx["hw"]
_iio_emu.data_devices = dd
print("Starting iio-emu")
_iio_emu.start()
Comment on lines -89 to +119
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function handle_iio_emu refactored with the following changes:

return ctx


Expand Down Expand Up @@ -220,10 +224,9 @@ def single_ctx_desc(request, _contexts):
hardware = hardware if isinstance(hardware, list) else [hardware]
if not marker:
return _contexts[0]
else:
for dec in _contexts:
if dec["hw"] in marker.args[0]:
return dec
for dec in _contexts:
if dec["hw"] in marker.args[0]:
return dec
Comment on lines -223 to +229
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function single_ctx_desc refactored with the following changes:

pytest.skip("No required hardware found")


Expand Down Expand Up @@ -263,10 +266,9 @@ def _iio_emu_func(request, _contexts, _iio_emu):
hardware = hardware if isinstance(hardware, list) else [hardware]
if not marker:
return _contexts[0]
else:
for dec in _contexts:
if dec["hw"] in marker.args[0]:
return handle_iio_emu(dec, request, _iio_emu)
for dec in _contexts:
if dec["hw"] in marker.args[0]:
return handle_iio_emu(dec, request, _iio_emu)
Comment on lines -266 to +271
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _iio_emu_func refactored with the following changes:

pytest.skip("No required hardware found")


Expand Down Expand Up @@ -294,10 +296,7 @@ def _iio_emu(request):
if isinstance(field, dict) and "emulate" in field:
hw_w_emulation[hw] = field
if hw in hw_w_emulation:
devices = []
for field in map[hw]:
if isinstance(field, str):
devices.append(field)
devices = [field for field in map[hw] if isinstance(field, str)]
Comment on lines -297 to +299
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _iio_emu refactored with the following changes:

hw_w_emulation[hw]["devices"] = devices

emu = iio_emu_manager(xml_path="auto", auto=True)
Expand All @@ -317,16 +316,12 @@ def _contexts(request, _iio_emu):

if _iio_emu:
if _iio_emu.auto:
ctx_plus_hw = []
for hw in _iio_emu.hw:
ctx_plus_hw.append(
{
ctx_plus_hw = [{
"uri": _iio_emu.uri,
"type": "emu",
"devices": _iio_emu.hw[hw]["devices"],
"hw": hw,
}
)
} for hw in _iio_emu.hw]
Comment on lines -320 to +324
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _contexts refactored with the following changes:

return ctx_plus_hw
else:
uri = _iio_emu.uri
Expand Down Expand Up @@ -398,16 +393,12 @@ def lookup_hw_from_map(ctx, map):
for attr_dict in driver_or_attr["ctx_attr"]:
for attr_name in attr_dict:
# loop over found and compare to
for hw_ctx_attr in ctx_attrs:
for hw_ctx_attr, value in ctx_attrs.items():
if (
hw_ctx_attr == attr_name
and attr_dict[attr_name]
in ctx_attrs[hw_ctx_attr]
and attr_dict[attr_name] in value
):
found += 1
# Compare other attribute types ...
if attr_type == "dev_attr":
pass
Comment on lines -401 to -410
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function lookup_hw_from_map refactored with the following changes:

This removes the following comments ( why? ):

# Compare other attribute types ...

continue
# Loop over drivers
for h in hw:
Expand All @@ -429,10 +420,7 @@ def lookup_hw_from_map(ctx, map):


def find_contexts(config, map, request):
if request.config.getoption("--skip-scan"):
ctxs = None
else:
ctxs = iio.scan_contexts()
ctxs = None if request.config.getoption("--skip-scan") else iio.scan_contexts()
Comment on lines -432 to +423
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function find_contexts refactored with the following changes:

if not ctxs:
print("\nNo libiio contexts found")
return False
Expand Down