|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the test runner script for the 'pytest' framework. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import sys |
|
12 |
|
13 # TODO: implement 'pytest' support in PytestRunner |
|
14 |
|
15 |
|
16 class GetPluginVersionsPlugin(): |
|
17 """ |
|
18 Class implementing a pytest plugin to extract the version info of all |
|
19 installed plugins. |
|
20 """ |
|
21 def __init__(self): |
|
22 """ |
|
23 Constructor |
|
24 """ |
|
25 super().__init__() |
|
26 |
|
27 self.versions = [] |
|
28 |
|
29 def pytest_cmdline_main(self, config): |
|
30 """ |
|
31 Public method called for performing the main command line action. |
|
32 |
|
33 @param config pytest config object |
|
34 @type Config |
|
35 """ |
|
36 pluginInfo = config.pluginmanager.list_plugin_distinfo() |
|
37 if pluginInfo: |
|
38 for _plugin, dist in pluginInfo: |
|
39 self.versions.append({ |
|
40 "name": dist.project_name, |
|
41 "version": dist.version |
|
42 }) |
|
43 |
|
44 def getVersions(self): |
|
45 """ |
|
46 Public method to get the assembled list of plugin versions. |
|
47 |
|
48 @return list of collected plugin versions |
|
49 @rtype list of dict |
|
50 """ |
|
51 return self.versions |
|
52 |
|
53 |
|
54 def getVersions(): |
|
55 """ |
|
56 Function to determine the framework version and versions of all available |
|
57 plugins. |
|
58 """ |
|
59 try: |
|
60 import pytest # __IGNORE_WARNING__ |
|
61 versions = { |
|
62 "name": "pytest", |
|
63 "version": pytest.__version__, |
|
64 "plugins": [], |
|
65 } |
|
66 |
|
67 # --capture=sys needed on Windows to avoid |
|
68 # ValueError: saved filedescriptor not valid anymore |
|
69 plugin = GetPluginVersionsPlugin() |
|
70 pytest.main(['--version', '--capture=sys'], plugins=[plugin]) |
|
71 versions["plugins"] = plugin.getVersions() |
|
72 except ImportError: |
|
73 versions = {} |
|
74 |
|
75 print(json.dumps(versions)) |
|
76 sys.exit(0) |
|
77 |
|
78 |
|
79 if __name__ == '__main__': |
|
80 command = sys.argv[1] |
|
81 if command == "installed": |
|
82 try: |
|
83 import pytest # __IGNORE_WARNING__ |
|
84 sys.exit(0) |
|
85 except ImportError: |
|
86 sys.exit(1) |
|
87 |
|
88 elif command == "versions": |
|
89 getVersions() |
|
90 |
|
91 sys.exit(42) |
|
92 |
|
93 # |
|
94 # eflag: noqa = M801 |