Exemplo n.º 1
0
def validate_contribute_json():
    t = Terminal()
    schema = get_schema()

    passed, failed, skipped = [], [], []
    for project in Bar('Validating').iter(list(get_projects())):
        if not project.repos:
            reason = 'No repos found'
            skipped.append((project, reason))
            continue

        # TODO: Handle non-github projects
        for repo in project.repos:
            if repo.startswith('https://github.com'):
                parsed = urlparse(repo)
                user, repo_name = parsed.path.strip('/').split('/')
                url = CONTRIBUTE_JSON_URL.format(user=user, repo=repo_name)

                response = requests.get(url)
                if response.status_code != requests.codes.ok:
                    reason = 'No contribute.json: {0}'.format(
                        response.status_code)
                    failed.append((project, reason))
                    continue

                contribute_json = json.loads(response.text)
                try:
                    jsonschema.validate(contribute_json, schema)
                    passed.append(project)
                except jsonschema.ValidationError as e:
                    reason = 'Invalid contribute.json: {0}'.format(e.message)
                    failed.append((project, reason))

    print t.bright_green('== Passed ==')
    for project in passed:
        print '  ' + t.underline(project.name)

    print t.bright_red('== Failed ==')
    for project, reason in failed:
        print '  {0}\n    {1}'.format(t.underline(project.name), reason)

    print t.bright_yellow('== Skipped ==')
    for project, reason in skipped:
        print '  {0}\n    {1}'.format(t.underline(project.name), reason)
Exemplo n.º 2
0
class testGroup(object):
    """TestGroup, group a number of testUnit, exec them and print the results"""
    def __init__(self,
                 name="",
                 terminal=None,
                 prefix="",
                 verbose=False,
                 align=0):
        self._tests = []
        self.name = name
        self.t = terminal
        self.prefix = prefix
        (self.t)
        if self.t == None:
            self.t = Terminal()
        self.results = []
        self.status_modules = {
            "success": 0,
            "total": 0,
            "warning": 0,
            "critical": 0,
            "failure": 0
        }
        self.success_text = self.t.green("success")
        self.failure_text = self.t.bright_red("failure")
        self.warning_text = self.t.bright_yellow("warning")
        self.critical_text = self.t.white_on_red("critical")
        self.verbose = verbose
        self.align = align

    def addTest(self, testUnit):
        self._tests.append(testUnit)
        return self

    def test(self):
        "Execute all tests, some options might exist at some point"
        module_success, module_total = 0, 0

        print(self.prefix + "+ Executing test group " +
              self.pretty_group(self.name))
        oldprefix = self.prefix
        self.prefix += "|  "
        self.results = []

        for test in self._tests:
            try:
                list_status, total, log_results = self.print_result(
                    test.test())
            except Exception as e:
                print(
                    self.t.bright_red(
                        "[ERROR] An unhandled error occured during the execution of the tests"
                    ))
                raise (e)
                # for l in e:
                #     print(l)
                list_status, total, log_results = self.print_result([])

            print(self.pretty_subtests(test.name, list_status, total))

            if list_status["success"] + list_status["warning"] == total:
                self.status_modules["success"] += 1
            self.status_modules["total"] += 1
            for log in log_results:
                print(log)
            self.results.append([self, SUCCESS_STATUS, ""])
        self.prefix = oldprefix

        print(
            self.pretty_group_result(self.status_modules,
                                     self.status_modules["total"]))
        return self.results

    def get_status(self):
        "Get the status of every module, if no test was run, should return an empty dict"
        return self.status_modules

    def print_result(self, table):
        "Get the array of success/failures and print according to the options (still none yet)"
        total = len(table)
        success = 0
        results_array = []
        nb = 0
        list_status = {"success": 0, "failure": 0, "warning": 0, "critical": 0}
        for item, status, infos in table:
            nb += 1
            if status == SUCCESS_STATUS:
                list_status["success"] += 1
            elif status == WARNING_STATUS:
                list_status["warning"] += 1
            elif status == CRITICAL_STATUS:
                list_status["critical"] += 1
            else:
                list_status["failure"] += 1
            if self.verbose or status != SUCCESS_STATUS:
                results_array.append(
                    self.pretty_result(status, nb, item, infos))
        return list_status, total, results_array

    def pretty_group_result(self, module_status, total):
        "Prettyfying the result of the batch of tests"
        bloc = self.prefix + "+ Done "
        return bloc + self.pretty_group(self.name) + self.pretty_dots(
            bloc, len(self.name)) + self.pretty_successrate(
                module_status, total)

    def pretty_name(self, item):
        "Just a pretty way of showing the name of a test"
        try:
            return item.__name__.strip("<>")
        except:
            return str(item)

    def pretty_subtests(self, name, success, total):
        "Pretty way of showing the result of the group of tests"
        bloc = self.prefix + "testing " + self.pretty_test(name)
        return bloc + self.pretty_dots(bloc) + self.pretty_successrate(
            success, total)

    def pretty_result(self, status, nb, item, infos):
        "Just a pretty way of showing the result of one test"
        bloc = self.prefix + " * " + " [" + str(nb) + "] " + self.pretty_name(
            item)
        if status == CRITICAL_STATUS:
            pbloc = self.prefix + " * " + " [" + str(
                nb) + "] " + self.t.bold_bright_red(self.pretty_name(item))
        else:
            pbloc = bloc
        return pbloc + self.pretty_dots(bloc) + self.pretty_status(
            status) + self.pretty_info(infos)

    def pretty_dots(self, bloc, padding=0):
        lenbloc = len(bloc) + padding
        if (self.align > lenbloc + 2):
            dots = "." * (self.align - lenbloc)
        else:
            dots = ".."
        return dots

    def pretty_info(self, infos):
        "Prettyfy the additional infos"
        if infos == "":
            return ""
        return self.t.italic(" (" + str(infos) + ")")

    def pretty_status(self, status):
        "Prettyfy the status of the test"
        if status == SUCCESS_STATUS:
            return self.success_text
        elif status == FAILURE_STATUS:
            return self.failure_text
        elif status == WARNING_STATUS:
            return self.warning_text
        else:
            return self.critical_text

    def pretty_group(self, name):
        "Prettify the name of the testGroup"
        return self.t.bold(name)

    def pretty_test(self, test):
        "Prettify the name of the testUnit"
        return test

    def pretty_successrate(self, success, total):
        warnings = ""
        if success["success"] == total:
            wrap = self.t.green
            txt = self.success_text
        elif success["success"] + success["warning"] == total:
            wrap = self.t.yellow
            txt = self.warning_text
            warnings = " ({} warnings)".format(str(success["warning"]))
        elif success["critical"] != 0:
            wrap = self.t.white_on_red
            txt = self.critical_text
        else:
            wrap = self.t.bright_red
            txt = self.failure_text
        return wrap(txt + " [" + str(success["success"] + success["warning"]) +
                    "/" + str(total) + "]" + warnings)

    def __str__(self):
        return self.name