eric7/Testing/Interfaces/PytestRunner.py

Mon, 16 May 2022 19:46:51 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 16 May 2022 19:46:51 +0200
branch
unittest
changeset 9066
a219ade50f7c
parent 9065
eric7/Unittest/Interfaces/PytestRunner.py@39405e6eba20
child 9089
b48a6d0f6309
permissions
-rw-r--r--

Performed some refactoring to avoid possible name clashes on case-insensitive systems.

# -*- coding: utf-8 -*-

# Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the test runner script for the 'pytest' framework.
"""

import json
import sys

# TODO: implement 'pytest' support in PytestRunner


class GetPluginVersionsPlugin():
    """
    Class implementing a pytest plugin to extract the version info of all
    installed plugins.
    """
    def __init__(self):
        """
        Constructor
        """
        super().__init__()
        
        self.versions = []
    
    def pytest_cmdline_main(self, config):
        """
        Public method called for performing the main command line action.
        
        @param config pytest config object
        @type Config
        """
        pluginInfo = config.pluginmanager.list_plugin_distinfo()
        if pluginInfo:
            for _plugin, dist in pluginInfo:
                self.versions.append({
                    "name": dist.project_name,
                    "version": dist.version
                })
    
    def getVersions(self):
        """
        Public method to get the assembled list of plugin versions.
        
        @return list of collected plugin versions
        @rtype list of dict
        """
        return self.versions


def getVersions():
    """
    Function to determine the framework version and versions of all available
    plugins.
    """
    try:
        import pytest               # __IGNORE_WARNING__
        versions = {
            "name": "pytest",
            "version": pytest.__version__,
            "plugins": [],
        }
        
        # --capture=sys needed on Windows to avoid
        # ValueError: saved filedescriptor not valid anymore
        plugin = GetPluginVersionsPlugin()
        pytest.main(['--version', '--capture=sys'], plugins=[plugin])
        versions["plugins"] = plugin.getVersions()
    except ImportError:
        versions = {}
    
    print(json.dumps(versions))
    sys.exit(0)


if __name__ == '__main__':
    command = sys.argv[1]
    if command == "installed":
        try:
            import pytest           # __IGNORE_WARNING__
            sys.exit(0)
        except ImportError:
            sys.exit(1)
    
    elif command == "versions":
        getVersions()
    
    sys.exit(42)

#
# eflag: noqa = M801

eric ide

mercurial