<commit_before>#!/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/.

import argparse
import json
import marionette_extension
import mozdevice
import mozprocess
import os
import subprocess
import sys
import tempfile
import traceback
import zipfile

from collections import OrderedDict
from mozfile import TemporaryDirectory
from mozlog.structured import reader

SUBTESTS = [
    ('cert', None),
]
subtest_opts = dict(SUBTESTS)

class TestResult:
    def __init__(self, test_name, passed, failures, errors, results_file):
        self.test_name = test_name
        self.passed = passed
        self.failures = failures
        self.errors = errors
        self.results_file = results_file

def get_test_failures(raw_log):
    '''
    Return the list of test failures contained within a structured log file.
    '''
    failures = []
    def test_status(data):
        if data['status'] == 'FAIL':
            failures.append(data)
    with open(raw_log, 'r') as f:
        reader.each_log(reader.read(f),
                               {'test_status':test_status})
    return failures

def list_tests():
    '''
    Query each subharness for the list of test groups it can run and
    yield a tuple of (subharness, test group) for each one.
    '''
    for test, _ in SUBTESTS:
        try:
            for group in subprocess.check_output([test, '--list-test-groups']).splitlines():
                yield test, group
        except subprocess.CalledProcessError:
            pass

def filter_tests(tests):
    '''
    Given a list of tests from the commandline, yield triples of
    (subsuite, options, test groups) of subsuites to run and the
    test groups to run within them.
    '''
    if not tests:
        tests = [s for s, _ in SUBTESTS]
    d = OrderedDict()
    for t in tests:
        v = t.split(":", 1)
        subsuite = v[0]
        if subsuite not in d:
            d[subsuite] = []
        if len(v) == 2:
            d[subsuite].append(v[1])
    for subsuite, test_groups in d.iteritems():
        yield subsuite, subtest_opts[subsuite], test_groups

def run_test(test_name, temp_dir, args, test_groups):
    print 'Running %s' % test_name
    result = None
    try:
        raw_log = os.path.join(temp_dir, 'structured.log')
        result_file = os.path.join(temp_dir, 'results.json')
        cmd = [test_name,
               '--version=%s' % args.version,<commit_msg>Fix a few issues<commit_after>#!/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/.

import argparse
import json
import marionette_extension
import mozdevice
import mozprocess
import os
import subprocess
import sys
import tempfile
import traceback
import zipfile

from collections import OrderedDict
from mozfile import TemporaryDirectory
from mozlog.structured import reader

SUBTESTS = OrderedDict([
    ('cert', None),
])

class TestResult:
    def __init__(self, test_name, passed, failures, errors, results_file):
        self.test_name = test_name
        self.passed = passed
        self.failures = failures
        self.errors = errors
        self.results_file = results_file

def get_test_failures(raw_log):
    '''
    Return the list of test failures contained within a structured log file.
    '''
    failures = []
    def test_status(data):
        if data['status'] == 'FAIL':
            failures.append(data)
    with open(raw_log, 'r') as f:
        reader.each_log(reader.read(f),
                               {'test_status':test_status})
    return failures

def list_tests():
    '''
    Query each subharness for the list of test groups it can run and
    yield a tuple of (subharness, test group) for each one.
    '''
    for test, _ in SUBTESTS.iteritems():
        try:
            for group in subprocess.check_output([test, '--list-test-groups']).splitlines():
                yield test, group
        except subprocess.CalledProcessError:
            pass

def filter_tests(tests):
    '''
    Given a list of tests from the commandline, yield triples of
    (subsuite, options, test groups) of subsuites to run and the
    test groups to run within them. If test groups is an empty list,
    all tests in the subsuite should be run.
    '''
    if not tests:
        tests = SUBTESTS.keys()
    d = OrderedDict()
    for t in tests:
        v = t.split(":", 1)
        subsuite = v[0]
        if subsuite not in d:
            d[subsuite] = []
        if len(v) == 2:
            #TODO: verify tests passed against possible tests?
            d[subsuite].append(v[1])
    for subsuite, test_groups in d.iteritems():
        yield subsuite, SUBTESTS[subsuite], test_groups

def run_test(test_name, temp_dir, args, test_groups):
    print 'Running %s' % test_name
    result = None
    try:
        raw_log = os.path.join(temp_dir, 'structured.log')
        result_file = os.path.join(temp_dir, 'results.json')
        cmd = [test_name,
               '--version=%s' % args.version,
{'fraction_non_alphanumeric': 0.09370229007633588, 'fraction_numerical': 0.0030534351145038168, 'mean_word_length': 2.5774744027303753, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7516522', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1558, 'n_words': 529}