# Path: tests/_constants.py
# _CONTEXT = Context()
# 
# _CONTEXT_STRATEGY = strategies.just(_CONTEXT)
# 
# _SUBSYSTEM_STRATEGY = device_strategy().map(lambda x: x.subsystem)
# 
# def _UDEV_TEST(version, node=None):  # pylint: disable=invalid-name
#     fmt_str = "%s: udev version must be at least %s, is %s"
#     return pytest.mark.skipif(
#         _UDEV_VERSION < version, reason=fmt_str % (node, version, _UDEV_VERSION)
#     )
# 
# def device_strategy(require_existing=True, filter_func=lambda x: True):
#     """
#     Strategy that yields filtered devices.
# 
#     The devices are filtered before being sampled to reduce the number
#     of health failures.
# 
#     :param bool require_existing: at the very last, verify existance
#     :param filter_func: a function to be used as a filter
#     :type filter_func: Device -> bool
#     """
#     strategy = strategies.sampled_from(
#         [x for x in _CONTEXT.list_devices() if filter_func(x)]
#     )
# 
#     if require_existing:
#         strategy = strategy.filter(_check_device)
# 
#     return strategy
# 
# Path: tests/utils/misc.py
# def failed_health_check_wrapper(func):
#     """
#     If the test fails a health check, calls skip().
#     """
# 
#     @wraps(func)
#     def the_func(*args):
#         """
#         Catch a hypothesis FailedHealthCheck exception and log it as a skip.
#         """
#         try:
#             func(*args)
#         except FailedHealthCheck:
#             pytest.skip(
#                 "failed health check for %s() (%s: %s)"
#                 % (
#                     func.__code__.co_name,
#                     func.__code__.co_filename,
#                     func.__code__.co_firstlineno,
#                 )
#             )
# 
#     return the_func


import os
import stat
import pytest
from hypothesis import assume, given, settings
from pyudev import (
    DeviceNotFoundAtPathError,
    DeviceNotFoundByFileError,
    DeviceNotFoundByNameError,
    DeviceNotFoundByNumberError,
    DeviceNotFoundInEnvironmentError,
    Devices,
)
from .._constants import (
    _CONTEXT,
    _CONTEXT_STRATEGY,
    _SUBSYSTEM_STRATEGY,
    _UDEV_TEST,
    device_strategy,
)
from ..utils import failed_health_check_wrapper
            assert error.device_number == a_device.device_number

    def test_from_device_number_invalid_type(self):
        """
        Verify that a non-existant subsystem always results in an exception.
        """
        with pytest.raises(DeviceNotFoundByNumberError):
            Devices.from_device_number(_CONTEXT, "foobar", 100)

    @failed_health_check_wrapper
    @given(
        _CONTEXT_STRATEGY,
        device_strategy(filter_func=lambda x: x.device_node is not None),
    )
    @settings(max_examples=5)
    def test_from_device_file(self, a_context, a_device):
        """
        Verify that from_device_file() yields correct device.
        """
        device = Devices.from_device_file(a_context, a_device.device_node)
        assert a_device == device

    def test_from_device_file_no_device_file(self, tmpdir):
        """
        Verify that a file that is not a device file will cause an exception
        to be raised.
        """
        filename = tmpdir.join("test")
        filename.ensure(file=True)
        with pytest.raises(DeviceNotFoundByFileError):

# Path: tests/_constants.py
# _CONTEXT = Context()
# 
# _CONTEXT_STRATEGY = strategies.just(_CONTEXT)
# 
# _SUBSYSTEM_STRATEGY = device_strategy().map(lambda x: x.subsystem)
# 
# def _UDEV_TEST(version, node=None):  # pylint: disable=invalid-name
#     fmt_str = "%s: udev version must be at least %s, is %s"
#     return pytest.mark.skipif(
#         _UDEV_VERSION < version, reason=fmt_str % (node, version, _UDEV_VERSION)
#     )
# 
# def device_strategy(require_existing=True, filter_func=lambda x: True):
#     """
#     Strategy that yields filtered devices.
# 
#     The devices are filtered before being sampled to reduce the number
#     of health failures.
# 
#     :param bool require_existing: at the very last, verify existance
#     :param filter_func: a function to be used as a filter
#     :type filter_func: Device -> bool
#     """
#     strategy = strategies.sampled_from(
#         [x for x in _CONTEXT.list_devices() if filter_func(x)]
#     )
# 
#     if require_existing:
#         strategy = strategy.filter(_check_device)
# 
#     return strategy
# 
# Path: tests/utils/misc.py
# def failed_health_check_wrapper(func):
#     """
#     If the test fails a health check, calls skip().
#     """
# 
#     @wraps(func)
#     def the_func(*args):
#         """
#         Catch a hypothesis FailedHealthCheck exception and log it as a skip.
#         """
#         try:
#             func(*args)
#         except FailedHealthCheck:
#             pytest.skip(
#                 "failed health check for %s() (%s: %s)"
#                 % (
#                     func.__code__.co_name,
#                     func.__code__.co_filename,
#                     func.__code__.co_firstlineno,
#                 )
#             )
# 
#     return the_func


# Path: tests/_device_tests/_devices_tests.py
import os
import stat
import pytest
from hypothesis import assume, given, settings
from pyudev import (
    DeviceNotFoundAtPathError,
    DeviceNotFoundByFileError,
    DeviceNotFoundByNameError,
    DeviceNotFoundByNumberError,
    DeviceNotFoundInEnvironmentError,
    Devices,
)
from .._constants import (
    _CONTEXT,
    _CONTEXT_STRATEGY,
    _SUBSYSTEM_STRATEGY,
    _UDEV_TEST,
    device_strategy,
)
from ..utils import failed_health_check_wrapper

            assert error.device_number == a_device.device_number

    def test_from_device_number_invalid_type(self):
        """
        Verify that a non-existant subsystem always results in an exception.
        """
        with pytest.raises(DeviceNotFoundByNumberError):
            Devices.from_device_number(_CONTEXT, "foobar", 100)

    @failed_health_check_wrapper
    @given(
        _CONTEXT_STRATEGY,
        device_strategy(filter_func=lambda x: x.device_node is not None),
    )
    @settings(max_examples=5)
    def test_from_device_file(self, a_context, a_device):
        """
        Verify that from_device_file() yields correct device.
        """
        device = Devices.from_device_file(a_context, a_device.device_node)
        assert a_device == device

    def test_from_device_file_no_device_file(self, tmpdir):
        """
        Verify that a file that is not a device file will cause an exception
        to be raised.
        """
        filename = tmpdir.join("test")
        filename.ensure(file=True)
        with pytest.raises(DeviceNotFoundByFileError):

            Devices.from_device_file(_CONTEXT, str(filename))