Commits
1 1 | ######################################################################################################################## |
2 2 | ############################################ Imports ############################################# |
3 3 | ######################################################################################################################## |
4 4 | |
5 5 | import os, sys, re, json, unittest, shlex |
6 6 | import argparse, subprocess, traceback |
7 7 | import shutil, datetime, platform |
8 + | import socket |
9 + | import xml.etree.ElementTree as ET |
10 + | import signal |
8 11 | |
9 12 | default_timeout = 1800 |
10 13 | sys.path.insert(0,'') |
11 14 | |
12 15 | #### PYTEST IMPORT |
13 16 | HAVE_PYTEST = True |
14 17 | try: |
15 18 | import pytest |
16 19 | except ImportError: |
17 20 | HAVE_PYTEST = False |
233 236 | return tests |
234 237 | |
235 238 | def getname(testfile): |
236 239 | '''Get the test name from the command-line |
237 240 | Ex: from test_clean[test1], returns test_clean''' |
238 241 | n0 = testfile.rfind('[') |
239 242 | n1 = testfile.rfind(']') |
240 243 | if n0 != -1: |
241 244 | return testfile[:n0] |
242 245 | |
243 - | def update_xml(filename): |
244 - | import xml.etree.ElementTree as ET |
246 + | def write_xml(name, runtime, testname, classname, fMessage, filename, result): |
247 + | e = datetime.datetime.now() |
248 + | timestamp = e.strftime('%Y-%m-%dT%H:%M:%S.%f') |
249 + | #string="""<?xml version='1.0' encoding='UTF-8'?> |
250 + | #<testsuites><testsuite name="'{}'" errors="0" failures="1" skipped="0" tests="1" time="{}" timestamp="{}" hostname="{}"><testcase classname="{}" name="{}" time="0.001"><failure message="{}"</failure></testcase></testsuite></testsuites>""".format(name, runtime, timestamp,socket.gethostname(), testname, classname, fMessage) |
251 + | |
252 + | #f = open(filename, "w") |
253 + | #f.write(string) |
254 + | #f.close() |
255 + | |
256 + | data = ET.Element('testsuites') |
257 + | |
258 + | # Adding a subtag named `Opening` |
259 + | # inside our root tag |
260 + | element1 = ET.SubElement(data, 'testsuite') |
261 + | element1.set('name', "'{}'".format(name)) |
262 + | element1.set('errors', "0") |
263 + | element1.set('failures', "1") |
264 + | element1.set('skipped', "0") |
265 + | element1.set('tests', "1") |
266 + | element1.set('time', "0.01") |
267 + | element1.set('timestamp', timestamp) |
268 + | element1.set('hostname', socket.gethostname()) |
269 + | |
270 + | s_elem1 = ET.SubElement(element1, 'testcase') |
271 + | s_elem1.set('classname', "{}.class".format(name)) |
272 + | s_elem1.set('name', "{}".format(name)) |
273 + | s_elem1.set('time', "0.01") |
274 + | |
275 + | ss_elem1 = ET.SubElement(s_elem1, 'failure') |
276 + | ss_elem1.set('message', fMessage) |
277 + | ss_elem1.text = fMessage |
278 + | """ |
279 + | # Adding subtags under the `Opening` |
280 + | # subtag |
281 + | s_elem1 = ET.SubElement(element1, 'E4') |
282 + | |
283 + | s_elem2 = ET.SubElement(element1, 'D4') |
284 + | |
285 + | # Adding attributes to the tags under |
286 + | # `items` |
287 + | s_elem1.set('type', 'Accepted') |
288 + | s_elem2.set('type', 'Declined') |
289 + | |
290 + | # Adding text between the `E4` and `D5` |
291 + | # subtag |
292 + | s_elem1.text = "King's Gambit Accepted" |
293 + | s_elem2.text = "Queen's Gambit Declined" |
294 + | """ |
295 + | # Converting the xml data to byte object, |
296 + | # for allowing flushing data to file |
297 + | # stream |
298 + | b_xml = ET.tostring(data) |
299 + | |
300 + | # Opening a file under the name `items2.xml`, |
301 + | # with operation mode `wb` (write + binary) |
302 + | with open(filename, "wb") as f: |
303 + | f.write(b_xml) |
304 + | |
305 + | |
306 + | def update_xml(filename, result, name="", runtime="", testname="", classname="", fMessage=""): |
307 + | |
308 + | if not os.path.isfile(filename): |
309 + | try: fMessage = signal.strsignal(abs(result.returncode)) |
310 + | except: fMessage = signal.Signals(abs(result.returncode)).name |
311 + | print("Nose File Not Generated. Generating.") |
312 + | write_xml(name, runtime, testname, classname, fMessage, filename, result) |
313 + | |
245 314 | xmlTree = ET.parse(filename) |
315 + | |
246 316 | rootElement = xmlTree.getroot() |
247 317 | for element in rootElement.iter(): |
248 318 | if element.tag == 'testcase': |
249 319 | testname = element.attrib['name'] |
250 320 | testscript = element.attrib['classname'].split(".")[0] |
251 321 | testclass = element.attrib['classname'].split(".")[1] |
252 322 | #print(name,testscript,testclass) |
253 323 | element.set("classname",testscript) |
254 324 | element.set("name",'.'.join([testclass,testname])) |
255 325 | xmlTree.write(filename,encoding='UTF-8',xml_declaration=True) |
256 326 | |
327 + | |
257 328 | class casa_test: |
258 329 | def __init__(self, |
259 330 | name, |
260 331 | path, |
261 332 | test_group=None, |
262 333 | test_type=None, |
263 334 | maintainer=None, |
264 335 | email=None, |
265 336 | options=None, |
266 337 | comment=None, |
516 587 | write_conftest_osx(conf_name) |
517 588 | else: |
518 589 | write_conftest_linux(conf_name) |
519 590 | |
520 591 | def run_cmd(cmd): |
521 592 | try: |
522 593 | from casampi.MPIEnvironment import MPIEnvironment |
523 594 | if MPIEnvironment.is_mpi_enabled: |
524 595 | pytest.main(cmd) |
525 596 | else: |
526 - | subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd , env={**os.environ}) |
597 + | result = subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd , env={**os.environ}) |
527 598 | except: |
528 - | subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd, env={**os.environ}) |
599 + | result = subprocess.run([sys.executable,"-m","pytest"] + pytest_args + cmd, env={**os.environ}) |
600 + | |
601 + | return result |
529 602 | |
530 603 | def setup_and_run(cmd,workdir, workpath, dirname, DRY_RUN ): |
531 604 | # https://docs.pytest.org/en/stable/usage.html |
532 605 | cmd = ["--verbose"] + ["-ra"] + ["--tb=short"] + cmd |
533 606 | |
534 607 | if DRY_RUN: |
535 608 | cmd = ["--collect-only"] + cmd |
536 609 | |
537 610 | if not os.path.isdir(workpath + '/xml/{}/'.format(dirname)): |
538 611 | os.makedirs(workpath + '/xml/{}/'.format(dirname)) |
541 614 | if len(os.listdir(workpath)) < 1: # If only the XML dir was created |
542 615 | print("No Tests to Run") |
543 616 | sys.exit() |
544 617 | else: |
545 618 | myworkdir = os.getcwd() |
546 619 | os.chdir(workdir + "{}/".format(dirname)) |
547 620 | print("Test Directory: {}\n".format(os.getcwd())) |
548 621 | print("Running Command: pytest " + " ".join(str(x) for x in cmd)) |
549 622 | write_pytestini(os.path.join(os.getcwd(),"pytest.ini"),dirname) |
550 623 | write_conftest(os.path.join(os.getcwd(),"conftest.py")) |
551 - | run_cmd(cmd) |
552 - | update_xml(xmlfile) |
624 + | result = run_cmd(cmd) |
625 + | update_xml(xmlfile, result, name= os.getcwd().split("/")[-1]) |
553 626 | os.remove(os.path.join(os.getcwd(),"conftest.py")) |
554 627 | os.remove(os.path.join(os.getcwd(),"pytest.ini")) |
555 628 | os.chdir(myworkdir) |
556 629 | ######################################################################################################################## |
557 630 | ############################################## Run ############################################### |
558 631 | ######################################################################################################################## |
559 632 | |
560 633 | def run(testnames, branch=None, merge_target=None, DRY_RUN=False): |
561 634 | |
562 635 | if not HAVE_PYTEST: |