pyEDAA.Reports.CLI.Unittesting

pyEDAA/Reports/CLI/Unittesting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from argparse import Namespace
from pathlib  import Path
from typing   import List, Tuple, Type

from pyTooling.MetaClasses                    import ExtendedType
from pyTooling.Attributes.ArgParse            import CommandHandler
from pyTooling.Attributes.ArgParse.ValuedFlag import LongValuedFlag

from pyEDAA.Reports.Unittesting       import UnittestException, TestsuiteKind, TestsuiteSummary, Testsuite, Testcase
from pyEDAA.Reports.Unittesting       import Document, MergedTestsuiteSummary
from pyEDAA.Reports.Unittesting.JUnit import JUnitReaderMode, TestsuiteSummary as ju_TestsuiteSummary


class UnittestingHandlers(metaclass=ExtendedType, mixin=True):
	@CommandHandler("unittest", help="Transform unit testing results.", description="Merge and/or transform unit testing results.")
	@LongValuedFlag("--name", dest="name", metaName='Name', optional=True, help="Top-level unit testing summary name.")
	@LongValuedFlag("--input", dest="input", metaName='format:JUnit File', optional=True, help="Unit testing summary file (XML).")
	@LongValuedFlag("--merge", dest="merge", metaName='format:JUnit File', optional=True, help="Unit testing summary file (XML).")
	@LongValuedFlag("--pytest", dest="pytest", metaName='cleanup;cleanup', optional=True, help="Remove pytest overhead.")
	@LongValuedFlag("--render", dest="render", metaName='format', optional=True, help="Render unit testing results to <format>.")
	@LongValuedFlag("--output", dest="output", metaName='format:JUnit File', help="Processed unit testing summary file (XML).")
	def HandleUnittest(self, args: Namespace) -> None:
		"""Handle program calls with command ``unittest``."""
		self._PrintHeadline()

		returnCode = 0
		if (args.input is None) and (args.merge is None):
			self.WriteError(f"Either option '--input=<Format>:<JUnitFilePattern>' or '--merge=<Format>:<JUnitFilePattern>' is missing.")
			returnCode = 3

		if returnCode != 0:
			self.Exit(returnCode)

		testsuiteSummaryName = args.name if args.name is not None else "TestsuiteSummary"
		merged = MergedTestsuiteSummary(testsuiteSummaryName)

		if args.input is not None:
			self.WriteNormal(f"Reading unit test input file ...")
			openTask = args.input
			try:
				document = self._open(openTask)
			except UnittestException as ex:
				self.WriteFatal(ex, immediateExit=False)
				for note in ex.__notes__:
					self.WriteNormal(f"           {note}")
				self.Exit()

			merged.Merge(document.ToTestsuiteSummary())

		if args.merge is not None:
			mergeTasks: Tuple[str, ...] = (args.merge, )
			for mergeTask in mergeTasks:
				self._merge(merged, mergeTask)

		self.WriteNormal(f"Aggregating unit test metrics ...")
		merged.Aggregate()

		self.WriteNormal(f"Flattening data structures to a single dimension ...")
		result = merged.ToTestsuiteSummary()

		if args.pytest is not None:
			self._processPyTest(result, args.pytest)

		if args.render is not None and self.Verbose:
			self.WriteVerbose("*" * self.Width)

			if args.render == "tree":
				tree = result.ToTree()
				self.WriteVerbose(tree.Render(), appendLinebreak=False)

			self.WriteVerbose("*" * self.Width)

		if args.output is not None:
			outputs = (args.output, )
			for output in outputs:
				self._output(result, output)

		self.ExitOnPreviousErrors()

	def _open(self, task: str) -> ju_TestsuiteSummary:
		parts = task.split(":")
		if (length := len(parts)) == 1:
			raise UnittestException(f"Syntax error: '{task}'")
		elif length == 2:
			dialect, dataFormat = (x.lower() for x in parts[0].split("-"))
			globPattern = parts[1]
			foundFiles = [f for f in Path.cwd().glob(globPattern)]
			if (length := len(foundFiles)) != 1:
				raise UnittestException(f"Found {length} files for pattern '{globPattern}'.") from FileNotFoundError(str(Path.cwd() / globPattern))

			file = foundFiles[0]

			if dataFormat == "junit":
				if dialect == "ant":
					from pyEDAA.Reports.Unittesting.JUnit.AntJUnit4 import Document

					documentClass = Document
				elif dialect == "any":
					from pyEDAA.Reports.Unittesting.JUnit import Document

					documentClass = Document
				elif dialect == "ctest":
					from pyEDAA.Reports.Unittesting.JUnit.CTestJUnit import Document

					documentClass = Document
				elif dialect == "gtest":
					from pyEDAA.Reports.Unittesting.JUnit.GoogleTestJUnit import Document

					documentClass = Document
				elif dialect == "pytest":
					from pyEDAA.Reports.Unittesting.JUnit.PyTestJUnit import Document

					documentClass = Document
				else:
					raise UnittestException(f"Unsupported JUnit XML dialect for input: '{dataFormat}-{dialect}'")

				self.WriteVerbose(f"  Reading {file}")
				return documentClass(file, parse=True)
			else:
				raise UnittestException(f"Unsupported unit testing report dataFormat for input: '{dataFormat}'")
		else:
			raise UnittestException(f"Syntax error: '{task}'")

	def _merge(self, testsuiteSummary: MergedTestsuiteSummary, task: str) -> None:
		parts = task.split(":")
		if (length := len(parts)) == 1:
			self.WriteError(f"Syntax error: '{task}'")
		elif length == 2:
			dialect, dataFormat = (x.lower() for x in parts[0].split("-"))
			globPattern = parts[1]

			foundFiles = tuple(f for f in Path.cwd().glob(globPattern))
			if len(foundFiles) == 0:
				self.WriteWarning(f"Found no matching files for pattern '{Path.cwd()}/{globPattern}'")
				return

			if dataFormat == "junit":
				if dialect == "ant":
					from pyEDAA.Reports.Unittesting.JUnit.AntJUnit4 import Document

					self._mergeJUnit(testsuiteSummary, Document, foundFiles, "Ant+JUnit4")
				elif dialect == "any":
					from pyEDAA.Reports.Unittesting.JUnit import Document

					self._mergeJUnit(testsuiteSummary, Document, foundFiles, "Any-JUnit")
				elif dialect == "ctest":
					from pyEDAA.Reports.Unittesting.JUnit.CTestJUnit import Document

					self._mergeJUnit(testsuiteSummary, Document, foundFiles, "CTest-JUnit")
				elif dialect == "gtest":
					from pyEDAA.Reports.Unittesting.JUnit.GoogleTestJUnit import Document

					self._mergeJUnit(testsuiteSummary, Document, foundFiles, "GoogleTest-JUnit")
				elif dialect == "pytest":
					from pyEDAA.Reports.Unittesting.JUnit.PyTestJUnit import Document

					self._mergeJUnit(testsuiteSummary, Document, foundFiles, "pyTest-JUnit")
				else:
					self.WriteError(f"Unsupported JUnit XML dialect for merging: '{dataFormat}-{dialect}'")
			else:
				self.WriteError(f"Unsupported unit testing report dataFormat for merging: '{dataFormat}'")
		else:
			self.WriteError(f"Syntax error: '{task}'")

	def _mergeJUnit(self, testsuiteSummary: MergedTestsuiteSummary, documentClass: Type[Document], foundFiles: Tuple[Path, ...], dialect: str) -> None:
		self.WriteNormal(f"Reading {len(foundFiles)} {dialect} unit test summary files ...")

		junitDocuments: List[documentClass] = []
		for file in foundFiles:
			self.WriteVerbose(f"  Reading {file}")
			try:
				junitDocuments.append(documentClass(file, analyzeAndConvert=True, readerMode=JUnitReaderMode.DecoupleTestsuiteHierarchyAndTestcaseClassName))
			except UnittestException as ex:
				self.WriteError(ex)

		if len(junitDocuments) == 0:
			self.WriteCritical(f"None of the {dialect} files were successfully read.")
			return

		self.WriteNormal(f"Merging unit test summary files into a single data model ...")
		for summary in junitDocuments:
			self.WriteVerbose(f"  merging {summary.Path}")
			testsuiteSummary.Merge(summary.ToTestsuiteSummary())

	def _processPyTest(self, testsuiteSummary: TestsuiteSummary, cleanups: str) -> None:
		self.WriteNormal(f"Simplifying unit testing reports created by pytest ...")

		for cleanup in cleanups.split(";"):
			parts = cleanup.split(":")
			if (l := len(parts)) == 1:
				if cleanup.lower() == "rewrite-dunder-init":
					self._processPyTest_RewiteDunderInit(testsuiteSummary)
				else:
					self.WriteError(f"Unsupported cleanup action for pytest: '{cleanup}'")
			elif l >= 2:
				command = parts[0].lower()
				if command == "reduce-depth":
					for path in parts[1:]:
						self._processPyTest_ReduceDepth(testsuiteSummary, path)
				elif command == "split":
					for path in parts[1:]:
						self._processPyTest_SplitTestsuite(testsuiteSummary, path)
				else:
					self.WriteError(f"Unsupported cleanup action for pytest: '{parts[0]}'")
			else:
				self.WriteError(f"Syntax error: '{cleanup}'")

	def _processPyTest_RewiteDunderInit(self, testsuiteSummary: TestsuiteSummary) -> None:
		self.WriteVerbose(f"  Rewriting '__init__' in classnames to actual Python package names")

		def processTestsuite(suite: Testsuite) -> None:
			testsuites: Tuple[Testsuite, ...] = tuple(ts for ts in suite.Testsuites.values())
			for testsuite in testsuites:                # type: Testsuite
				if testsuite.Name != "__init__":
					processTestsuite(testsuite)
					continue

				for ts in testsuite.Testsuites.values():  # type: Testsuite
					ts._parent = None
					suite.AddTestsuite(ts)

				for tc in testsuite.Testcases.values():   # type: Testcase
					tc._parent = None
					suite.AddTestcase(tc)

				del suite._testsuites["__init__"]

		processTestsuite(testsuiteSummary)

	def _processPyTest_ReduceDepth(self, testsuiteSummary: TestsuiteSummary, path: str) -> None:
		self.WriteVerbose(f"  Reducing path depth of testsuite '{path}'")
		cleanups = []
		suite = testsuiteSummary
		message = f"    Walking: {suite._name}"
		for element in path.split("."):
			if element in suite._testsuites:
				suite = suite._testsuites[element]
				message += f" -> {suite._name}"
			else:
				self.WriteDebug(f"    Skipping: {path}")
				suite = None
				break

		if suite is None:
			return

		self.WriteDebug(message)
		cleanups.append(suite)

		self.WriteDebug(f"    Moving testsuites ...")
		for ts in suite._testsuites.values():
			self.WriteDebug(f"      {ts._name} -> {testsuiteSummary._name}")
			ts._parent = None
			ts._kind = TestsuiteKind.Logical
			testsuiteSummary.AddTestsuite(ts)

		self.WriteDebug(f"    Deleting empty testsuites ...")
		for clean in cleanups:
			suite = clean
			while suite is not testsuiteSummary:
				name = suite._name
				suite = suite._parent
				if name in suite._testsuites:
					self.WriteDebug(f"      delete '{name}'")
					del suite._testsuites[name]
				else:
					self.WriteDebug(f"      skipping '{name}'")
					break

	def _processPyTest_SplitTestsuite(self, testsuiteSummary: TestsuiteSummary, path: str) -> None:
		self.WriteVerbose(f"  Splitting testsuite '{path}'")
		if path not in testsuiteSummary.Testsuites:
			self.WriteError(f"Path '{path}' not found")
			return

		cleanups = []
		parentTestsuite = testsuiteSummary
		workingTestsuite = parentTestsuite.Testsuites[path]
		for testsuite in workingTestsuite.Testsuites.values():
			self.WriteDebug(f"    Moving {testsuite.Name} to {parentTestsuite.Name}")

			testsuiteName = testsuite._name
			parentTestsuite.Testsuites[testsuiteName] = testsuite
			testsuite._parent = parentTestsuite

			cleanups.append(testsuiteName)

		for cleanup in cleanups:
			del workingTestsuite.Testsuites[cleanup]

		if len(workingTestsuite.Testsuites) == 0 and len(workingTestsuite.Testcases) == 0:
			self.WriteVerbose(f"  Removing empty testsuite '{path}'")
			del parentTestsuite.Testsuites[path]

	def _output(self, testsuiteSummary: TestsuiteSummary, task: str):
		parts = task.split(":")
		if (l := len(parts)) == 1:
			self.WriteError(f"Syntax error: '{task}'")
		elif l == 2:
			dialect, format = (x.lower() for x in parts[0].split("-"))
			outputFile = Path(parts[1])
			if format == "junit":
				if dialect == "ant":
					from pyEDAA.Reports.Unittesting.JUnit.AntJUnit4 import Document, UnittestException

					self._outputJUnit(testsuiteSummary, Document, outputFile, "Ant+JUnit4")
				elif dialect == "ctest":
					from pyEDAA.Reports.Unittesting.JUnit.CTestJUnit import Document, UnittestException

					self._outputJUnit(testsuiteSummary, Document, outputFile, "CTest-JUnit")
				elif dialect == "gtest":
					from pyEDAA.Reports.Unittesting.JUnit.GoogleTestJUnit import Document, UnittestException

					self._outputJUnit(testsuiteSummary, Document, outputFile, "GoogleTest-JUnit")
				elif dialect == "pytest":
					from pyEDAA.Reports.Unittesting.JUnit.PyTestJUnit import Document, UnittestException

					self._outputJUnit(testsuiteSummary, Document, outputFile, "pyTest-JUnit")
				else:
					self.WriteError(f"Unsupported JUnit XML dialect for writing: '{format}-{dialect}'")
			else:
				self.WriteError(f"Unsupported unit testing report format for writing: '{format}'")
		else:
			self.WriteError(f"Syntax error: '{task}'")

	def _outputJUnit(self, testsuiteSummary: TestsuiteSummary, documentClass: Type[Document], file: Path, dialect: str):
		self.WriteNormal(f"Writing merged unit test summaries to file ...")
		self.WriteVerbose(f"  Common Data Model -> OUT ({dialect}): {file}")

		junitDocument = documentClass.FromTestsuiteSummary(file, testsuiteSummary)
		try:
			junitDocument.Write(regenerate=True, overwrite=True)
		except UnittestException as ex:
			self.WriteError(str(ex))
			if ex.__cause__ is not None:
				self.WriteError(f"  {ex.__cause__}")

		self.WriteNormal(f"Output written to '{file}' in {dialect} format.")