#!/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 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"): ifnot 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, "r") 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.")
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.