<commit_before>#!/usr/bin/env python
"""
Test driver
Runs off plain text files similar to how PHP's test harness works
"""
import subprocess

def run(args):
    """
    Runs a command and returns stdout output, throws exception on failed run.
    Uses python's 2.7 check_output if available otherwise uses something else
    """

    try:
        getattr(subprocess, 'check_output')
        has_check_output = True
    except AttributeError:
        has_check_output = False

    if has_check_output:
        # 2.7
        return subprocess.check_output(args)
    else:
        # not 2.7
        aprocess = subprocess.Popen(args,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
        (stdoutdata, stderrdata) = aprocess.communicate()
        if aprocess.returncode != 0:
            raise Exception("Test died!: " + stderrdata)
        return stdoutdata

def run_test(name, data, valgrind=False):
    """
    runs a test
    """

    if valgrind:
        args = ['valgrind', '--gen-suppressions=no', '--read-var-info=yes',
                '--leak-check=full', '--error-exitcode=1',
                '--track-origins=yes', './sqli', data[1]]
        actual = run(args)
    else:
        actual = run(['./sqli', data[1]])

    if actual.strip() != data[2].strip():
        print "not ok: " + name
        print "EXPECTED: \n" + data[2]
        print "GOT: \n" + actual
    else:
        print "ok: " + name

def read_test(arg):
    """
    Read a test file and split into components
    """

    fd = open(arg, 'r')
    state = None
    info = {
        '--TEST--': '',
        '--INPUT--': '',
        '--EXPECTED--': ''
        }

    for line in fd:
        line = line.rstrip()
        if line in ('--TEST--', '--INPUT--', '--EXPECTED--'):
            state = line
        elif state:
            info[state] += line + '\n'

    # remove last newline from input
    info['--INPUT--'] = info['--INPUT--'][0:-1]

    return (info['--TEST--'], info['--INPUT--'], info['--EXPECTED--'])

if __name__ == '__main__':
    import sys
<commit_msg>more nits<commit_after>#!/usr/bin/env python
"""
Test driver
Runs off plain text files similar to how PHP's test harness works
"""
import subprocess

def run(args):
    """
    Runs a command and returns stdout output, throws exception on failed run.
    Uses python's 2.7 check_output if available otherwise uses something else
    """

    try:
        getattr(subprocess, 'check_output')
        has_check_output = True
    except AttributeError:
        has_check_output = False

    if has_check_output:
        # 2.7
        return subprocess.check_output(args)
    else:
        # not 2.7
        aprocess = subprocess.Popen(args,
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
        (stdoutdata, stderrdata) = aprocess.communicate()
        if aprocess.returncode != 0:
            raise Exception("Test died!: " + stderrdata)
        return stdoutdata

def run_test(test_name, data, valgrind=False):
    """
    runs a test
    """

    if valgrind:
        args = ['valgrind', '--gen-suppressions=no', '--read-var-info=yes',
                '--leak-check=full', '--error-exitcode=1',
                '--track-origins=yes', './sqli', data[1]]
        actual = run(args)
    else:
        actual = run(['./sqli', data[1]])

    if actual.strip() != data[2].strip():
        print "not ok: " + test_name
        print "EXPECTED: \n" + data[2]
        print "GOT: \n" + actual
    else:
        print "ok: " + test_name

def read_test(arg):
    """
    Read a test file and split into components
    """

    fd = open(arg, 'r')
    state = None
    info = {
        '--TEST--': '',
        '--INPUT--': '',
        '--EXPECTED--': ''
        }

    for line in fd:
        line = line.rstrip()
        if line in ('--TEST--', '--INPUT--', '--EXPECTED--'):
            state = line
        elif state:
            info[state] += line + '\n'

    # remove last newline from input
    info['--INPUT--'] = info['--INPUT--'][0:-1]

    return (info['--TEST--'], info['--INPUT--'], info['--EXPECTED--'])

if __name__ == '__main__':
    import sys

{'fraction_non_alphanumeric': 0.15348288075560804, 'fraction_numerical': 0.006611570247933884, 'mean_word_length': 1.8353413654618473, 'pattern_counts': {'":': 0, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1598266', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1318, 'n_words': 345}