3 # This is the open-adventure dungeon text coverage report generator. It
4 # consumes a YAML description of the dungeon and determines whether the
5 # various strings contained are present within the test check files.
7 # The default HTML output is appropriate for use with Gitlab CI.
8 # You can override it with a command-line argument.
16 YAML_PATH = "../adventure.yaml"
17 HTML_TEMPLATE_PATH = "coverage_dungeon.html.tpl"
18 DEFAULT_HTML_OUTPUT_PATH = "../coverage/adventure.yaml.html"
20 STDOUT_REPORT_CATEGORY = " {name:.<19}: {percent:5.1f}% covered ({covered} of {total})\n"
22 HTML_SUMMARY_ROW = '''
24 <td class="headerItem"><a href="#{name}">{name}:</a></td>
25 <td class="headerCovTableEntry">{total}</td>
26 <td class="headerCovTableEntry">{covered}</td>
27 <td class="headerCovTableEntry">{percent:.1f}%</td>
31 HTML_CATEGORY_SECTION = '''
39 HTML_CATEGORY_HEADER = '''
41 <td class="tableHead" width="60%" colspan="{colspan}">{label}</td>
46 HTML_CATEGORY_HEADER_CELL = '<td class="tableHead" width="15%">{}</td>\n'
48 HTML_CATEGORY_COVERAGE_CELL = '<td class="{}"> </td>\n'
50 HTML_CATEGORY_ROW = '''
52 <td class="coverFile" colspan="{colspan}">{id}</td>
57 def search(needle, haystack):
58 # Search for needle in haystack, first escaping needle for regex, then
59 # replacing %s, %d, etc. with regex wildcards, so the variable messages
60 # within the dungeon definition will actually match
61 needle = re.escape(needle) \
62 .replace("\\n", "\n") \
63 .replace("\\t", "\t") \
64 .replace("\%S", ".*") \
65 .replace("\%s", ".*") \
66 .replace("\%d", ".*") \
69 return re.search(needle, haystack)
71 def obj_coverage(objects, text, report):
72 # objects have multiple descriptions based on state
73 for i, objouter in enumerate(objects):
74 (obj_name, obj) = objouter
75 if obj["descriptions"]:
76 for j, desc in enumerate(obj["descriptions"]):
77 name = "{}[{}]".format(obj_name, j)
78 if name not in report["messages"]:
79 report["messages"][name] = {"covered" : False}
81 if report["messages"][name]["covered"] != True:
82 if desc == None or desc == '' or search(desc, text):
83 report["messages"][name]["covered"] = True
84 report["covered"] += 1
86 def loc_coverage(locations, text, report):
87 # locations have a long and a short description, that each have to
88 # be checked seperately
89 for name, loc in locations:
90 if name not in report["messages"]:
91 report["messages"][name] = {"long" : False, "short": False}
93 if report["messages"][name]["long"] != True:
94 if loc["description"]["long"] == None or loc["description"]["long"] == '' or search(loc["description"]["long"], text):
95 report["messages"][name]["long"] = True
96 report["covered"] += 1
97 if report["messages"][name]["short"] != True:
98 if loc["description"]["short"] == None or loc["description"]["short"] == '' or search(loc["description"]["short"], text):
99 report["messages"][name]["short"] = True
100 report["covered"] += 1
102 def hint_coverage(obituaries, text, report):
103 # hints have a "question" where the hint is offered, followed
104 # by the actual hint if the player requests it
105 for i, hintouter in enumerate(obituaries):
106 hint = hintouter["hint"]
108 if name not in report["messages"]:
109 report["messages"][name] = {"question" : False, "hint": False}
111 if report["messages"][name]["question"] != True and search(hint["question"], text):
112 report["messages"][name]["question"] = True
113 report["covered"] += 1
114 if report["messages"][name]["hint"] != True and search(hint["hint"], text):
115 report["messages"][name]["hint"] = True
116 report["covered"] += 1
118 def obit_coverage(obituaries, text, report):
119 # obituaries have a "query" where it asks the player for a resurrection,
120 # followed by a snarky comment if the player says yes
121 for name, obit in enumerate(obituaries):
122 if name not in report["messages"]:
123 report["messages"][name] = {"query" : False, "yes_response": False}
125 if report["messages"][name]["query"] != True and search(obit["query"], text):
126 report["messages"][name]["query"] = True
127 report["covered"] += 1
128 if report["messages"][name]["yes_response"] != True and search(obit["yes_response"], text):
129 report["messages"][name]["yes_response"] = True
130 report["covered"] += 1
132 def threshold_coverage(classes, text, report):
133 # works for class thresholds and turn threshold, which have a "message"
135 for name, item in enumerate(classes):
136 if name not in report["messages"]:
137 report["messages"][name] = {"covered" : "False"}
139 if report["messages"][name]["covered"] != True:
140 if item["message"] == None or item["message"] == "NO_MESSAGE" or search(item["message"], text):
141 report["messages"][name]["covered"] = True
142 report["covered"] += 1
144 def arb_coverage(arb_msgs, text, report):
145 for name, message in arb_msgs:
146 if name not in report["messages"]:
147 report["messages"][name] = {"covered" : False}
149 if report["messages"][name]["covered"] != True:
150 if message == None or search(message, text):
151 report["messages"][name]["covered"] = True
152 report["covered"] += 1
154 def specials_actions_coverage(items, text, report):
155 # works for actions or specials
156 for name, item in items:
157 if name not in report["messages"]:
158 report["messages"][name] = {"covered" : False}
160 if report["messages"][name]["covered"] != True:
161 if item["message"] == None or item["message"] == "NO_MESSAGE" or search(item["message"], text):
162 report["messages"][name]["covered"] = True
163 report["covered"] += 1
165 def coverage_report(db, check_file_contents):
166 # Create report for each catagory, including total items, number of items
167 # covered, and a list of the covered messages
169 for name in db.keys():
170 # initialize each catagory
172 "name" : name, # convenience for string formatting
178 # search for each message in ever test check file
179 for chk in check_file_contents:
180 arb_coverage(db["arbitrary_messages"], chk, report["arbitrary_messages"])
181 hint_coverage(db["hints"], chk, report["hints"])
182 loc_coverage(db["locations"], chk, report["locations"])
183 obit_coverage(db["obituaries"], chk, report["obituaries"])
184 obj_coverage(db["objects"], chk, report["objects"])
185 specials_actions_coverage(db["actions"], chk, report["actions"])
186 specials_actions_coverage(db["specials"], chk, report["specials"])
187 threshold_coverage(db["classes"], chk, report["classes"])
188 threshold_coverage(db["turn_thresholds"], chk, report["turn_thresholds"])
192 if __name__ == "__main__":
195 with open(YAML_PATH, "r") as f:
198 print('ERROR: could not load {} ({}})'.format(YAML_PATH, e.strerror))
201 # get contents of all the check files
202 check_file_contents = []
203 for filename in os.listdir(TEST_DIR):
204 if filename.endswith(".chk"):
205 with open(filename, "r") as f:
206 check_file_contents.append(f.read())
208 # run coverage analysis report on dungeon database
209 report = coverage_report(db, check_file_contents)
211 # render report output
214 summary_stdout = "adventure.yaml coverage rate:\n"
215 for name, category in sorted(report.items()):
216 # ignore categories with zero entries
217 if category["total"] > 0:
218 # Calculate percent coverage
219 category["percent"] = (category["covered"] / float(category["total"])) * 100
221 # render section header
222 cat_messages = sorted(category["messages"].items())
223 cat_keys = cat_messages[0][1].keys()
225 colspan = 10 - len(cat_keys)
227 headers_html += HTML_CATEGORY_HEADER_CELL.format(key)
228 category_html = HTML_CATEGORY_HEADER.format(colspan=colspan, label=category["name"], cells=headers_html)
230 # render message coverage row
231 for message_id, covered in cat_messages:
232 category_html_row = ""
233 for key, value in covered.items():
234 category_html_row += HTML_CATEGORY_COVERAGE_CELL.format("uncovered" if value != True else "covered")
235 category_html += HTML_CATEGORY_ROW.format(id=message_id,colspan=colspan, cells=category_html_row)
236 categories_html += HTML_CATEGORY_SECTION.format(id=name, rows=category_html)
238 # render category summaries
239 summary_stdout += STDOUT_REPORT_CATEGORY.format(**category)
240 summary_html += HTML_SUMMARY_ROW.format(**category)
242 # output some quick report stats
243 print(summary_stdout)
245 if len(sys.argv) > 1:
246 html_output_path = sys.argv[1]
248 html_output_path = DEFAULT_HTML_OUTPUT_PATH
252 with open(HTML_TEMPLATE_PATH, "r") as f:
253 # read in HTML template
254 html_template = f.read()
256 print('ERROR: reading HTML report template failed ({})'.format(e.strerror))
259 # parse template with report and write it out
261 with open(html_output_path, "w") as f:
262 f.write(html_template.format(categories=categories_html, summary=summary_html))
264 print('ERROR: writing HTML report failed ({})'.format(e.strerror))