|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +import xml.dom.minidom as xml |
| 5 | + |
| 6 | +from _canary.util.filesystem import mkdirp |
| 7 | +from _canary.util.filesystem import working_dir |
| 8 | + |
| 9 | + |
| 10 | +def test_cdash_labels_for_subproject(tmpdir): |
| 11 | + """Test the plugin 'canary_cdash_labels_for_subproject' |
| 12 | +
|
| 13 | + The canary_cdash_labels_for_subproject allows adding subproject labels for the entire test |
| 14 | + session |
| 15 | + """ |
| 16 | + with working_dir(tmpdir.strpath, create=True): |
| 17 | + setup_cdash_labels_for_subproject() |
| 18 | + run_the_thing_and_check() |
| 19 | + |
| 20 | + |
| 21 | +def test_cdash_subproject_label(tmpdir): |
| 22 | + """Test the plugin 'canary_cdash_subroject_label' |
| 23 | +
|
| 24 | + This plugin allows tests to define their CDash subproject label. If the subproject label is |
| 25 | + not included in the test case's keywords, it is added |
| 26 | +
|
| 27 | + """ |
| 28 | + with working_dir(tmpdir.strpath, create=True): |
| 29 | + setup_cdash_subproject_label() |
| 30 | + run_the_thing_and_check() |
| 31 | + |
| 32 | + |
| 33 | +def run_the_thing_and_check(): |
| 34 | + env = dict(os.environ) |
| 35 | + env["PYTHONPATH"] = os.getcwd() |
| 36 | + subprocess.run([f"{sys.prefix}/bin/canary", "-p", "baz", "run", "."], env=env) |
| 37 | + subprocess.run( |
| 38 | + [f"{sys.prefix}/bin/canary", "-C", "TestResults", "report", "cdash", "create"], env=env |
| 39 | + ) |
| 40 | + file = "TestResults/CDASH/Test-0.xml" |
| 41 | + doc = xml.parse(open(file)) |
| 42 | + names = get_subproject_labels(doc) |
| 43 | + assert sorted(names) == ["baz", "foo"] |
| 44 | + names = get_test_labels(doc) |
| 45 | + assert sorted(names) == ["baz", "foo"] |
| 46 | + |
| 47 | + |
| 48 | +def setup_cdash_labels_for_subproject(): |
| 49 | + mkdirp("baz") |
| 50 | + with open("baz/__init__.py", "w") as fh: |
| 51 | + fh.write("""\ |
| 52 | +import canary |
| 53 | +@canary.hookimpl |
| 54 | +def canary_cdash_labels_for_subproject(): |
| 55 | + return ['foo', 'baz'] |
| 56 | +""") |
| 57 | + with open("baz.pyt", "w") as fh: |
| 58 | + fh.write("""\ |
| 59 | +import sys |
| 60 | +import canary |
| 61 | +canary.directives.keywords('baz') |
| 62 | +def test(): |
| 63 | + return 0 |
| 64 | +if __name__ == '__main__': |
| 65 | + sys.exit(test()) |
| 66 | +""") |
| 67 | + with open("foo.pyt", "w") as fh: |
| 68 | + fh.write("""\ |
| 69 | +import sys |
| 70 | +import canary |
| 71 | +canary.directives.keywords('foo') |
| 72 | +def test(): |
| 73 | + return 0 |
| 74 | +if __name__ == '__main__': |
| 75 | + sys.exit(test()) |
| 76 | +""") |
| 77 | + |
| 78 | + |
| 79 | +def setup_cdash_subproject_label(): |
| 80 | + """Test the plugin 'canary_cdash_subroject_label' |
| 81 | +
|
| 82 | + This plugin allows tests to define their CDash subproject label. If the subproject label is |
| 83 | + not included in the test case's keywords, it is added |
| 84 | +
|
| 85 | + """ |
| 86 | + mkdirp("baz") |
| 87 | + with open("baz/__init__.py", "w") as fh: |
| 88 | + fh.write("""\ |
| 89 | +import canary |
| 90 | +@canary.hookimpl |
| 91 | +def canary_cdash_subproject_label(case): |
| 92 | + return case.family |
| 93 | +""") |
| 94 | + with open("baz.pyt", "w") as fh: |
| 95 | + fh.write("""\ |
| 96 | +import sys |
| 97 | +import canary |
| 98 | +def test(): |
| 99 | + return 0 |
| 100 | +if __name__ == '__main__': |
| 101 | + sys.exit(test()) |
| 102 | +""") |
| 103 | + with open("foo.pyt", "w") as fh: |
| 104 | + fh.write("""\ |
| 105 | +import sys |
| 106 | +import canary |
| 107 | +def test(): |
| 108 | + return 0 |
| 109 | +if __name__ == '__main__': |
| 110 | + sys.exit(test()) |
| 111 | +""") |
| 112 | + |
| 113 | + |
| 114 | +def get_test_labels(doc): |
| 115 | + names = [] |
| 116 | + for el1 in doc.getElementsByTagName("Testing"): |
| 117 | + for el2 in el1.getElementsByTagName("Test"): |
| 118 | + for el3 in el2.getElementsByTagName("Labels"): |
| 119 | + for el4 in el3.getElementsByTagName("Label"): |
| 120 | + names.append(el4.childNodes[0].nodeValue) |
| 121 | + return names |
| 122 | + |
| 123 | + |
| 124 | +def get_subproject_labels(doc): |
| 125 | + return [el.getAttribute("name") for el in doc.getElementsByTagName("Subproject")] |
0 commit comments