mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-05 17:02:32 -05:00
[Trivia] Validate custom trivia file upload using schema (#4659)
* Add custom trivia list schema validation and test * Address review * Improve error formatting in trivia list test Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
This commit is contained in:
@@ -1,33 +1,27 @@
|
||||
import textwrap
|
||||
|
||||
import yaml
|
||||
from schema import SchemaError
|
||||
|
||||
|
||||
def test_trivia_lists():
|
||||
from redbot.cogs.trivia import get_core_lists
|
||||
from redbot.cogs.trivia import InvalidListError, get_core_lists, get_list
|
||||
|
||||
list_names = get_core_lists()
|
||||
assert list_names
|
||||
problem_lists = []
|
||||
for l in list_names:
|
||||
with l.open(encoding="utf-8") as f:
|
||||
try:
|
||||
dict_ = yaml.safe_load(f)
|
||||
except yaml.error.YAMLError as e:
|
||||
problem_lists.append((l.stem, "YAML error:\n{!s}".format(e)))
|
||||
try:
|
||||
get_list(l)
|
||||
except InvalidListError as exc:
|
||||
e = exc.__cause__
|
||||
if isinstance(e, SchemaError):
|
||||
problem_lists.append((l.stem, f"SCHEMA error:\n{e!s}"))
|
||||
else:
|
||||
for key in list(dict_.keys()):
|
||||
if key == "CONFIG":
|
||||
if not isinstance(dict_[key], dict):
|
||||
problem_lists.append((l.stem, "CONFIG is not a dict"))
|
||||
elif key == "AUTHOR":
|
||||
if not isinstance(dict_[key], str):
|
||||
problem_lists.append((l.stem, "AUTHOR is not a string"))
|
||||
else:
|
||||
if not isinstance(dict_[key], list):
|
||||
problem_lists.append(
|
||||
(l.stem, "The answers for '{}' are not a list".format(key))
|
||||
)
|
||||
problem_lists.append((l.stem, f"YAML error:\n{e!s}"))
|
||||
|
||||
if problem_lists:
|
||||
msg = ""
|
||||
for l in problem_lists:
|
||||
msg += "{}: {}\n".format(l[0], l[1])
|
||||
for name, error in problem_lists:
|
||||
msg += f"- {name}:\n{textwrap.indent(error, ' ')}"
|
||||
raise TypeError("The following lists contain errors:\n" + msg)
|
||||
|
||||
Reference in New Issue
Block a user