11import json
22import os
3+ import pytest
34
45
56def _check_for_duplicate_keys (items ):
@@ -12,64 +13,85 @@ def _check_for_duplicate_keys(items):
1213 return tmp
1314
1415
15- def test_community_script_json_file ():
16+ def _load_scripts ():
17+ with open ("community_scripts.json" ) as f :
18+ return json .load (f , object_pairs_hook = _check_for_duplicate_keys )
19+
20+
21+ @pytest .mark .parametrize (
22+ "script" ,
23+ _load_scripts (),
24+ ids = lambda script : script ["filename" ]
25+ )
26+ def test_community_script_json_file (script ):
1627 valid_shells = ["powershell" , "python" , "cmd" , "shell" ]
1728 valid_os = ["windows" , "linux" , "darwin" ]
1829
19- with open ("community_scripts.json" ) as f :
20- info = json .load (f , object_pairs_hook = _check_for_duplicate_keys )
21-
22- guids = []
23- for script in info :
24- fn : str = script ["filename" ]
25- assert os .path .exists (os .path .join ("scripts" , fn ))
26- assert script ["filename" ]
27- assert script ["name" ]
28- assert script ["description" ]
29- assert script ["shell" ]
30- assert script ["shell" ] in valid_shells
31-
32- if fn .endswith (".ps1" ):
33- assert script ["shell" ] == "powershell"
34- elif fn .endswith (".bat" ):
35- assert script ["shell" ] == "cmd"
36- elif fn .endswith (".py" ):
37- assert script ["shell" ] == "python"
38-
39- if "args" in script .keys ():
40- assert isinstance (script ["args" ], list )
41-
42- # allows strings as long as they can be type casted to int
43- if "default_timeout" in script .keys ():
44- assert isinstance (int (script ["default_timeout" ]), int )
45-
46- # check supported platforms
47- if "supported_platforms" in script .keys ():
48- assert isinstance (script ["supported_platforms" ], list )
49- for i in script ["supported_platforms" ]:
50- assert i in valid_os
51-
52- assert "guid" in script .keys ()
53- guids .append (script ["guid" ])
54-
55- # check guids are unique
30+ fn : str = script ["filename" ]
31+ assert os .path .exists (os .path .join ("scripts" , fn ))
32+ assert script ["filename" ]
33+ assert script ["name" ]
34+ assert script ["description" ]
35+ assert script ["shell" ]
36+ assert script ["shell" ] in valid_shells
37+
38+ if fn .endswith (".ps1" ):
39+ assert script ["shell" ] == "powershell"
40+ elif fn .endswith (".bat" ):
41+ assert script ["shell" ] == "cmd"
42+ elif fn .endswith (".py" ):
43+ assert script ["shell" ] == "python"
44+
45+ if "args" in script .keys ():
46+ assert isinstance (script ["args" ], list )
47+
48+ # allows strings as long as they can be type casted to int
49+ if "default_timeout" in script .keys ():
50+ assert isinstance (int (script ["default_timeout" ]), int )
51+
52+ # check supported platforms
53+ if "supported_platforms" in script .keys ():
54+ assert isinstance (script ["supported_platforms" ], list )
55+ for i in script ["supported_platforms" ]:
56+ assert i in valid_os
57+
58+ assert "guid" in script .keys ()
59+
60+
61+ def test_guids_are_unique ():
62+ """Test that all script GUIDs are unique"""
63+ scripts = _load_scripts ()
64+ guids = [script ["guid" ] for script in scripts ]
5665 assert len (guids ) == len (set (guids ))
5766
5867
59- def test_community_script_has_jsonfile_entry ():
60- with open (os .path .join ("community_scripts.json" )) as f :
61- info = json .load (f )
62-
63- filenames = [i ["filename" ] for i in info ]
64-
68+ def _get_script_files ():
69+ """Get all script files from the scripts directory"""
70+ files = []
6571 with os .scandir ("scripts" ) as it :
6672 for f in it :
6773 if not f .name .startswith ("." ) and f .is_file ():
68- assert f .name in filenames
74+ files .append (f .name )
75+ return files
6976
7077
71- def test_script_filenames_do_not_contain_spaces ():
78+ @pytest .mark .parametrize (
79+ "filename" ,
80+ _get_script_files (),
81+ ids = lambda filename : filename
82+ )
83+ def test_community_script_has_jsonfile_entry (filename ):
7284 with open (os .path .join ("community_scripts.json" )) as f :
7385 info = json .load (f )
74- for script in info :
75- assert " " not in script ["filename" ]
86+
87+ filenames = [i ["filename" ] for i in info ]
88+ assert filename in filenames
89+
90+
91+ @pytest .mark .parametrize (
92+ "script" ,
93+ _load_scripts (),
94+ ids = lambda script : script ["filename" ]
95+ )
96+ def test_script_filenames_do_not_contain_spaces (script ):
97+ assert " " not in script ["filename" ]
0 commit comments