-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.py
164 lines (145 loc) · 5.78 KB
/
run_tests.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
import argparse
import json
import os
import sys
import unittest
import git
import requests
from xmlrunner import XMLTestRunner
import utils.global_vars
from tests.page_tests import DEV_MANUAL, IBEX_MANUAL, USER_MANUAL, PageTests
from tests.shadow_mirroring_tests import ShadowReplicationTests
from utils.ignored_words import IGNORED_ITEMS
GITHUB_API_ISSUE_CALL = "https://api.github.com/repos/ISISComputingGroup/IBEX/issues?per_page=1"
def run_tests_on_pages(reports_path, pages, wiki_dir, highest_issue_num, test_class):
suite = unittest.TestSuite()
loader = unittest.TestLoader()
# Add spelling test suite a dynamic number of times with an argument of the page name.
# unittest's test loader is unable to take arguments to test classes by default so have
# to use the getTestCaseNames() syntax and explicitly add the argument ourselves.
for page in pages:
suite.addTests(
[
test_class(test, IGNORED_ITEMS, (page, pages, wiki_dir, highest_issue_num))
for test in loader.getTestCaseNames(test_class)
]
)
runner = XMLTestRunner(output=str(reports_path), stream=sys.stdout)
return runner.run(suite).wasSuccessful()
def run_all_tests(single_file, remote, folder):
"""
Runs all of the tests
Returns
True if all tests pass, else False
"""
reports_path = os.path.join(os.getcwd(), "test-reports")
if not os.path.exists(reports_path):
try:
os.mkdir(reports_path)
except OSError as e:
print("Unable to create test report folder: {}".format(e))
return [False]
return_values = []
# initialise globals, currently just string of warnings
utils.global_vars.init()
top_issue_num = int(json.loads(requests.get(GITHUB_API_ISSUE_CALL).content)[0]["number"])
if remote:
for wiki in [DEV_MANUAL, IBEX_MANUAL, USER_MANUAL]:
try:
with wiki:
pages = wiki.get_pages()
wiki_dir = wiki.get_path()
print("Running spelling tests on {}".format(wiki.name))
return_values.append(
run_tests_on_pages(
os.path.join(reports_path, wiki.name),
pages,
wiki_dir,
top_issue_num,
test_class=PageTests,
)
)
print()
except git.GitCommandError as ex:
print("FAILED to clone {}: {}".format(wiki.name, str(ex)))
print("Skipping tests\n")
return_values.append(0)
continue
print(utils.global_vars.failed_url_string)
for wiki in [DEV_MANUAL, USER_MANUAL]:
try:
with wiki:
pages = wiki.get_pages()
wiki_dir = wiki.get_path()
# Only do shadow replication tests in "remote" mode.
print("Running shadow replication tests on {}".format(wiki.name))
return_values.append(
run_tests_on_pages(
os.path.join(reports_path, wiki.name),
pages,
wiki_dir,
top_issue_num,
test_class=ShadowReplicationTests,
)
)
print()
except git.GitCommandError as ex:
print("FAILED to clone {}: {}".format(wiki.name, str(ex)))
print("Skipping tests\n")
return_values.append(0)
continue
elif single_file:
return_values.append(
run_tests_on_pages(
os.path.join(reports_path, os.path.basename(single_file)),
[single_file],
os.path.dirname(single_file),
top_issue_num,
test_class=PageTests,
)
)
elif folder:
print("Running spelling tests on folder {}".format(folder))
files = os.listdir(folder)
files_to_test = []
for f in files:
if f.endswith(".md"):
files_to_test.append(os.path.join(folder, f))
# The path is listed as an empty string as this hybrid set up ignores it
return_values.append(
run_tests_on_pages(
os.path.join(reports_path, os.path.basename(folder)),
files_to_test,
"",
top_issue_num,
test_class=PageTests,
)
)
print(utils.global_vars.failed_url_string)
return all(value for value in return_values)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="""Runs tests against the IBEX wikis""",
)
parser.add_argument("--file", required=False, type=str, default=None, help="The file to scan")
parser.add_argument(
"--remote",
required=False,
action="store_true",
default=False,
help="Scan all remote wikis (dev manual, user manual, IBEX",
)
parser.add_argument(
"--folder", required=False, type=str, default=None, help="Scan just a local folder"
)
args = parser.parse_args()
if not args.file and not args.remote and not args.folder:
raise (RuntimeError("No arguments specified"))
elif (
(args.file and args.remote) or (args.file and args.folder) or (args.remote and args.folder)
):
raise (RuntimeError("Cannot specify more than one target for the tests"))
sys.exit(0 if run_all_tests(args.file, args.remote, args.folder) else 1)
if __name__ == "__main__":
main()