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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
"""configtest.py
Verify the .json and .py files in the configs/ directory are well-formed.
Further tests to verify validity would be desirable.
This is also a good example script to look at to understand mozharness.
"""
import os
import pprint
import sys
try:
import simplejson as json
except ImportError:
import json
sys.path.insert(1, os.path.dirname(sys.path[0]))
from mozharness.base.script import BaseScript
# ConfigTest {{{1
class ConfigTest(BaseScript):
config_options = [
[
[
"--test-file",
],
{
"action": "extend",
"dest": "test_files",
"help": "Specify which config files to test",
},
]
]
def __init__(self, require_config_file=False):
self.config_files = []
BaseScript.__init__(
self,
config_options=self.config_options,
all_actions=[
"list-config-files",
"test-json-configs",
"test-python-configs",
"summary",
],
default_actions=[
"test-json-configs",
"test-python-configs",
"summary",
],
require_config_file=require_config_file,
)
def query_config_files(self):
"""This query method, much like others, caches its runtime
settings in self.VAR so we don't have to figure out config_files
multiple times.
"""
if self.config_files:
return self.config_files
c = self.config
if "test_files" in c:
self.config_files = c["test_files"]
return self.config_files
self.debug(
"No --test-file(s) specified; defaulting to crawling the configs/ directory."
)
config_files = []
for root, dirs, files in os.walk(os.path.join(sys.path[0], "..", "configs")):
for name in files:
# Hardcode =P
if name.endswith(".json") or name.endswith(".py"):
if not name.startswith("test_malformed"):
config_files.append(os.path.join(root, name))
self.config_files = config_files
return self.config_files
def list_config_files(self):
"""Non-default action that is mainly here to demonstrate how
non-default actions work in a mozharness script.
"""
config_files = self.query_config_files()
for config_file in config_files:
self.info(config_file)
def test_json_configs(self):
"""Currently only "is this well-formed json?" """
config_files = self.query_config_files()
filecount = [0, 0]
for config_file in config_files:
if config_file.endswith(".json"):
filecount[0] += 1
self.info("Testing %s." % config_file)
contents = self.read_from_file(config_file, verbose=False)
try:
json.loads(contents)
except ValueError:
self.add_summary("%s is invalid json." % config_file, level="error")
self.error(pprint.pformat(sys.exc_info()[1]))
else:
self.info("Good.")
filecount[1] += 1
if filecount[0]:
self.add_summary(
"%d of %d json config files were good." % (filecount[1], filecount[0])
)
else:
self.add_summary("No json config files to test.")
def test_python_configs(self):
"""Currently only "will this give me a config dictionary?" """
config_files = self.query_config_files()
filecount = [0, 0]
for config_file in config_files:
if config_file.endswith(".py"):
filecount[0] += 1
self.info("Testing %s." % config_file)
global_dict = {}
local_dict = {}
try:
with open(config_file) as f:
exec(f.read(), global_dict, local_dict)
except Exception:
self.add_summary(
"%s is invalid python." % config_file, level="error"
)
self.error(pprint.pformat(sys.exc_info()[1]))
else:
if "config" in local_dict and isinstance(
local_dict["config"], dict
):
self.info("Good.")
filecount[1] += 1
else:
self.add_summary(
"%s is valid python, "
"but doesn't create a config dictionary." % config_file,
level="error",
)
if filecount[0]:
self.add_summary(
"%d of %d python config files were good." % (filecount[1], filecount[0])
)
else:
self.add_summary("No python config files to test.")
# __main__ {{{1
if __name__ == "__main__":
config_test = ConfigTest()
config_test.run_and_exit()
|