2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 # You can obtain one at http://mozilla.org/MPL/2.0/.
7 Verify the .json and .py files in the configs/ directory are well-formed.
8 Further tests to verify validity would be desirable.
10 This is also a good example script to look at to understand mozharness.
18 import simplejson
as json
22 sys
.path
.insert(1, os
.path
.dirname(sys
.path
[0]))
24 from mozharness
.base
.script
import BaseScript
28 class ConfigTest(BaseScript
):
37 "help": "Specify which config files to test",
42 def __init__(self
, require_config_file
=False):
43 self
.config_files
= []
46 config_options
=self
.config_options
,
50 "test-python-configs",
55 "test-python-configs",
58 require_config_file
=require_config_file
,
61 def query_config_files(self
):
62 """This query method, much like others, caches its runtime
63 settings in self.VAR so we don't have to figure out config_files
67 return self
.config_files
70 self
.config_files
= c
["test_files"]
71 return self
.config_files
73 "No --test-file(s) specified; defaulting to crawling the configs/ directory."
76 for root
, dirs
, files
in os
.walk(os
.path
.join(sys
.path
[0], "..", "configs")):
79 if name
.endswith(".json") or name
.endswith(".py"):
80 if not name
.startswith("test_malformed"):
81 config_files
.append(os
.path
.join(root
, name
))
82 self
.config_files
= config_files
83 return self
.config_files
85 def list_config_files(self
):
86 """Non-default action that is mainly here to demonstrate how
87 non-default actions work in a mozharness script.
89 config_files
= self
.query_config_files()
90 for config_file
in config_files
:
91 self
.info(config_file
)
93 def test_json_configs(self
):
94 """Currently only "is this well-formed json?" """
95 config_files
= self
.query_config_files()
97 for config_file
in config_files
:
98 if config_file
.endswith(".json"):
100 self
.info("Testing %s." % config_file
)
101 contents
= self
.read_from_file(config_file
, verbose
=False)
105 self
.add_summary("%s is invalid json." % config_file
, level
="error")
106 self
.error(pprint
.pformat(sys
.exc_info()[1]))
112 "%d of %d json config files were good." % (filecount
[1], filecount
[0])
115 self
.add_summary("No json config files to test.")
117 def test_python_configs(self
):
118 """Currently only "will this give me a config dictionary?" """
119 config_files
= self
.query_config_files()
121 for config_file
in config_files
:
122 if config_file
.endswith(".py"):
124 self
.info("Testing %s." % config_file
)
128 with
open(config_file
, "r") as f
:
129 exec(f
.read(), global_dict
, local_dict
)
132 "%s is invalid python." % config_file
, level
="error"
134 self
.error(pprint
.pformat(sys
.exc_info()[1]))
136 if "config" in local_dict
and isinstance(
137 local_dict
["config"], dict
143 "%s is valid python, "
144 "but doesn't create a config dictionary." % config_file
,
149 "%d of %d python config files were good." % (filecount
[1], filecount
[0])
152 self
.add_summary("No python config files to test.")
156 if __name__
== "__main__":
157 config_test
= ConfigTest()
158 config_test
.run_and_exit()