forked from amidaware/community-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_json.py
75 lines (57 loc) · 2.23 KB
/
test_json.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
import json
import os
def _check_for_duplicate_keys(items):
tmp = {}
for k, v in items:
if k in tmp:
raise ValueError(f"Duplicated key detected: {k}")
else:
tmp[k] = v
return tmp
def test_community_script_json_file():
valid_shells = ["powershell", "python", "cmd", "shell"]
valid_os = ["windows", "linux", "darwin"]
with open("community_scripts.json") as f:
info = json.load(f, object_pairs_hook=_check_for_duplicate_keys)
guids = []
for script in info:
fn: str = script["filename"]
assert os.path.exists(os.path.join("scripts", fn))
assert script["filename"]
assert script["name"]
assert script["description"]
assert script["shell"]
assert script["shell"] in valid_shells
if fn.endswith(".ps1"):
assert script["shell"] == "powershell"
elif fn.endswith(".bat"):
assert script["shell"] == "cmd"
elif fn.endswith(".py"):
assert script["shell"] == "python"
if "args" in script.keys():
assert isinstance(script["args"], list)
# allows strings as long as they can be type casted to int
if "default_timeout" in script.keys():
assert isinstance(int(script["default_timeout"]), int)
# check supported platforms
if "supported_platforms" in script.keys():
assert isinstance(script["supported_platforms"], list)
for i in script["supported_platforms"]:
assert i in valid_os
assert "guid" in script.keys()
guids.append(script["guid"])
# check guids are unique
assert len(guids) == len(set(guids))
def test_community_script_has_jsonfile_entry():
with open(os.path.join("community_scripts.json")) as f:
info = json.load(f)
filenames = [i["filename"] for i in info]
with os.scandir("scripts") as it:
for f in it:
if not f.name.startswith(".") and f.is_file():
assert f.name in filenames
def test_script_filenames_do_not_contain_spaces():
with open(os.path.join("community_scripts.json")) as f:
info = json.load(f)
for script in info:
assert " " not in script["filename"]