2 # ***** BEGIN LICENSE BLOCK *****
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 # You can obtain one at http://mozilla.org/MPL/2.0/.
6 # ***** END LICENSE BLOCK *****
9 Verify the .json and .py files in the configs/ directory are well-formed.
10 Further tests to verify validity would be desirable.
12 This is also a good example script to look at to understand mozharness.
20 import simplejson
as json
24 sys
.path
.insert(1, os
.path
.dirname(sys
.path
[0]))
26 from mozharness
.base
.script
import BaseScript
30 class ConfigTest(BaseScript
):
39 "help": "Specify which config files to test",
44 def __init__(self
, require_config_file
=False):
45 self
.config_files
= []
48 config_options
=self
.config_options
,
52 "test-python-configs",
57 "test-python-configs",
60 require_config_file
=require_config_file
,
63 def query_config_files(self
):
64 """This query method, much like others, caches its runtime
65 settings in self.VAR so we don't have to figure out config_files
69 return self
.config_files
72 self
.config_files
= c
["test_files"]
73 return self
.config_files
75 "No --test-file(s) specified; defaulting to crawling the configs/ directory."
78 for root
, dirs
, files
in os
.walk(os
.path
.join(sys
.path
[0], "..", "configs")):
81 if name
.endswith(".json") or name
.endswith(".py"):
82 if not name
.startswith("test_malformed"):
83 config_files
.append(os
.path
.join(root
, name
))
84 self
.config_files
= config_files
85 return self
.config_files
87 def list_config_files(self
):
88 """Non-default action that is mainly here to demonstrate how
89 non-default actions work in a mozharness script.
91 config_files
= self
.query_config_files()
92 for config_file
in config_files
:
93 self
.info(config_file
)
95 def test_json_configs(self
):
96 """Currently only "is this well-formed json?" """
97 config_files
= self
.query_config_files()
99 for config_file
in config_files
:
100 if config_file
.endswith(".json"):
102 self
.info("Testing %s." % config_file
)
103 contents
= self
.read_from_file(config_file
, verbose
=False)
107 self
.add_summary("%s is invalid json." % config_file
, level
="error")
108 self
.error(pprint
.pformat(sys
.exc_info()[1]))
114 "%d of %d json config files were good." % (filecount
[1], filecount
[0])
117 self
.add_summary("No json config files to test.")
119 def test_python_configs(self
):
120 """Currently only "will this give me a config dictionary?" """
121 config_files
= self
.query_config_files()
123 for config_file
in config_files
:
124 if config_file
.endswith(".py"):
126 self
.info("Testing %s." % config_file
)
130 with
open(config_file
, "r") as f
:
131 exec(f
.read(), global_dict
, local_dict
)
134 "%s is invalid python." % config_file
, level
="error"
136 self
.error(pprint
.pformat(sys
.exc_info()[1]))
138 if "config" in local_dict
and isinstance(
139 local_dict
["config"], dict
145 "%s is valid python, "
146 "but doesn't create a config dictionary." % config_file
,
151 "%d of %d python config files were good." % (filecount
[1], filecount
[0])
154 self
.add_summary("No python config files to test.")
158 if __name__
== "__main__":
159 config_test
= ConfigTest()
160 config_test
.run_and_exit()