<commit_before>import * as fs from 'fs';
import * as xml2js from 'xml2js';
import {TestFile, TestsToRun, TestSuite, TestFunction, FlattenedTestFunction, Tests, TestStatus, FlattenedTestSuite} from './contracts';

export enum PassCalculationFormulae {
    pytest,
    nosetests
}
interface TestSuiteResult {
    $: {
        errors: string;
        failures: string;
        name: string;
        skips: string;
        skip: string;
        tests: string;
        time: string;
    };
    testcase: TestCaseResult[];
}
interface TestCaseResult {
    $: {
        classname: string;
        file: string;
        line: string;
        name: string;
        time: string;
    };
    failure: {
        _: string;
        $: { message: string, type: string }
    }[];
    error: {
        _: string;
        $: { message: string, type: string }
    }[];
    skipped: {
        _: string;
        $: { message: string, type: string }
    }[];
}

function getSafeInt(value: string, defaultValue: any = 0): number {
    const num = parseInt(value);
    if (isNaN(num)) { return defaultValue; }
    return num;
}
export function updateResultsFromXmlLogFile(tests: Tests, outputXmlFile: string, passCalculationFormulae: PassCalculationFormulae): Promise<any> {
    return new Promise<any>((resolve, reject) => {
        fs.readFile(outputXmlFile, 'utf8', (err, data) => {
            if (err) {
                return reject(err);
            }

            xml2js.parseString(data, (err, result) => {
                if (err) {
                    return reject(err);
                }

                let testSuiteResult: TestSuiteResult = result.testsuite;
                tests.summary.errors = getSafeInt(testSuiteResult.$.errors);
                tests.summary.failures = getSafeInt(testSuiteResult.$.failures);
                tests.summary.skipped = getSafeInt(testSuiteResult.$.skips ? testSuiteResult.$.skips : testSuiteResult.$.skip);
                let testCount = getSafeInt(testSuiteResult.$.tests);

                switch (passCalculationFormulae) {
                    case PassCalculationFormulae.pytest: {
                        tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped;
                        break;
                    }
                    case PassCalculationFormulae.nosetests: {
                        tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped - tests.summary.errors;
                        break;
                    }
                    default: {
                        throw new Error("Unknown Test Pass Calculation");
                    }
                }

                if (!Array.isArray(testSuiteResult.testcase)) {<commit_msg>fix calculation of success count #239<commit_after>import * as fs from 'fs';
import * as xml2js from 'xml2js';
import {TestFile, TestsToRun, TestSuite, TestFunction, FlattenedTestFunction, Tests, TestStatus, FlattenedTestSuite} from './contracts';

export enum PassCalculationFormulae {
    pytest,
    nosetests
}
interface TestSuiteResult {
    $: {
        errors: string;
        failures: string;
        name: string;
        skips: string;
        skip: string;
        tests: string;
        time: string;
    };
    testcase: TestCaseResult[];
}
interface TestCaseResult {
    $: {
        classname: string;
        file: string;
        line: string;
        name: string;
        time: string;
    };
    failure: {
        _: string;
        $: { message: string, type: string }
    }[];
    error: {
        _: string;
        $: { message: string, type: string }
    }[];
    skipped: {
        _: string;
        $: { message: string, type: string }
    }[];
}

function getSafeInt(value: string, defaultValue: any = 0): number {
    const num = parseInt(value);
    if (isNaN(num)) { return defaultValue; }
    return num;
}
export function updateResultsFromXmlLogFile(tests: Tests, outputXmlFile: string, passCalculationFormulae: PassCalculationFormulae): Promise<any> {
    return new Promise<any>((resolve, reject) => {
        fs.readFile(outputXmlFile, 'utf8', (err, data) => {
            if (err) {
                return reject(err);
            }

            xml2js.parseString(data, (err, result) => {
                if (err) {
                    return reject(err);
                }

                let testSuiteResult: TestSuiteResult = result.testsuite;
                tests.summary.errors = getSafeInt(testSuiteResult.$.errors);
                tests.summary.failures = getSafeInt(testSuiteResult.$.failures);
                tests.summary.skipped = getSafeInt(testSuiteResult.$.skips ? testSuiteResult.$.skips : testSuiteResult.$.skip);
                let testCount = getSafeInt(testSuiteResult.$.tests);

                switch (passCalculationFormulae) {
                    case PassCalculationFormulae.pytest: {
                        tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped - tests.summary.errors;
                        break;
                    }
                    case PassCalculationFormulae.nosetests: {
                        tests.summary.passed = testCount - tests.summary.failures - tests.summary.skipped - tests.summary.errors;
                        break;
                    }
                    default: {
                        throw new Error("Unknown Test Pass Calculation");
                    }
                }

                if (!Array.isArray(testSuiteResult.testcase)) {
{'fraction_non_alphanumeric': 0.10547227740653783, 'fraction_numerical': 0.0023478417915838903, 'mean_word_length': 1.8269525267993874, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1357869', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1464, 'n_words': 324}