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

Cosmetic clean-up of the pytest fixtures in tests/unit/conftest.py #70

Merged
Merged
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
59 changes: 36 additions & 23 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
"""tests/unit/conftest.py: pytest fixtures for unit-testing functions in the xen-bugtool script"""
"""tests/unit/conftest.py: pytest fixtures for unit-testing xen-bugtool

Introduction to fixtures:
https://docs.pytest.org/en/8.0.x/fixture.html

How to use fixtures:
https://docs.pytest.org/en/8.0.x/how-to/fixtures.html

The full documentation on fixtures:
https://docs.pytest.org/en/8.0.x/reference/fixtures.html
"""

from __future__ import print_function

import os
Expand All @@ -10,61 +21,58 @@

@pytest.fixture(scope="function")
def builtins():
"""
Pytest fixture to return the name of the built-in module.
The built-in module provides the built-in functions, exceptions, etc.
It is needed to replace built-in functions like open() with a new mock.

:returns (str): The name of the built-in module.
"""
if sys.version_info < (3,):
return "__builtin__" # pragma: no cover
return "builtins"
"""Provide the builtins module name for Python 2.x and Python 3.x"""
return "__builtin__" if sys.version_info < (3,) else "builtins"


@pytest.fixture(scope="session")
def testdir():
"""Test fixture to get the directory of the unit test for finding other files relative to it"""
"""Test fixture to provide the directory of the dom0 template directory"""
return os.path.dirname(__file__)


@pytest.fixture(scope="session")
def dom0_template(testdir):
"""Test fixture to get the directory of the dom0 template and adding it's /usr/sbin to the PATH"""
dom0_root_dir = testdir + "/../integration/dom0-template"
return dom0_root_dir
"""Test fixture to get the directory of the dom0 template"""
return testdir + "/../integration/dom0-template"


@pytest.fixture(scope="session")
def imported_bugtool(testdir, dom0_template):
"""Test fixture to import the xen-bugtool script as a module for executing unit tests on functions"""
"""Fixture to provide the xen-bugtool script as a module for unit tests"""

# This uses the deprecated imp module because it needs also to run with Python2.7 for now:
def import_from_file(module_name, file_path):
if sys.version_info.major == 2: # pragma: no cover
import imp # pylint: disable=deprecated-module # pyright: ignore[reportMissingImports]
# Python 2.7, use the deprecated imp module (no alternative)
# pylint: disable-next=deprecated-module
import imp # pyright: ignore[reportMissingImports]

return imp.load_source(module_name, file_path)
module = imp.load_source(module_name, file_path)

# Prevent other code from importing the same module again:
sys.modules[module_name] = module
return module
else:
# Py3.11+: Importing Python source code from a script without the .py extension:
# https://gist.github.com/bernhardkaindl/1aaa04ea925fdc36c40d031491957fd3:
from importlib import machinery, util

loader = machinery.SourceFileLoader(module_name, file_path)
spec = util.spec_from_loader(module_name, loader)
assert spec
assert spec.loader
module = util.module_from_spec(spec)
# Probably a good idea to add manually imported module stored in sys.modules

# Prevent other code from importing the same module again:
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module

bugtool = import_from_file("xen-bugtool", testdir + "/../../xen-bugtool")
bugtool.ProcOutput.debug = True

# Prepend tests/mocks to force the use of our mock xen.lowlevel.xc module:
sys.path.insert(0, testdir + "/../mocks")
os.environ["PATH"] = dom0_template + "/usr/sbin" # for modinfo, mdadm, etc

return bugtool


Expand All @@ -74,7 +82,12 @@ def bugtool(imported_bugtool):
# Init import_bugtool.data, so each unit test function gets it pristine:
imported_bugtool.data = {}
sys.argv = ["xen-bugtool", "--unlimited"]
return imported_bugtool

yield imported_bugtool # provide the bugtool to the test function

# Cleanup the bugtool data dict after each test as tests may modify it:
imported_bugtool.data = {}
sys.argv = ["xen-bugtool", "--unlimited"]


@pytest.fixture(scope="function")
Expand Down
Loading