<commit_before>import tl = require('vsts-task-lib/task')
import tr = require('vsts-task-lib/toolrunner')
import path = require('path')

let agentBuildDir = tl.getVariable('Agent.BuildDirectory');
let rootDir = tl.getPathInput('pythonroot', false, true);
let requirementFile = tl.getPathInput('reqfile', false, true);
let coverageOutput = tl.getPathInput('coveragedir', false, false);

/**
 * Activates the virtual environment created at the provided location
 * @param venvPath The path to the virtual environment
 */
function activateVenv(venvPath: string) {
    tl.debug('Activating virtual environment')

    let venvToolsPath = isWindows() ? 'Scripts' : 'bin';

    process.env['VIRTUAL_ENV'] = venvPath;
    process.env['PATH'] = path.join(venvPath, venvToolsPath) + path.delimiter + process.env['PATH'];
}

/**
 * Determines if the current operating system is Windows based
 */
function isWindows() {
    return tl.osType().match(/^Win/) !== null;
}

/**
 * Gets a ToolRunner for the Pip tool using the provided arguments
 * @param args A collection of arguments to provide to the tool
 */
function getPipTool(args: string[]): tr.ToolRunner {
    return tl.tool(tl.which('pip')).arg(args);
}

/**
 * Configures the environment for use
 */
async function configureEnvironment() {
     if (process.env['VIRTUAL_ENV'] === undefined) {
        tl.debug('Not currently in a virtual environment');

        // Define the location of the virtual environment
        let venv = path.join(agentBuildDir, 'venv', 'build');
        tl.debug('Virtual environment path set to: ' + venv);

        // Create the virtual environment
        tl.debug('Creating virtual environment');
        let pythonPath = isWindows() ? tl.which('python') : tl.which('python3');
        let venvTool = tl.tool(pythonPath).arg(['-m', 'venv', venv]);
        await venvTool.exec();

        // Activate the virtual environment
        activateVenv(venv);
    } else {
        tl.debug('Already in a virtual environment');
    }

    // Get the optional requirements file and restore if available
    if (requirementFile !== null) {
        var pipTool = getPipTool(['install', '-r', requirementFile]);
        await pipTool.exec();
    }
}

/**
 * Sets the environment up to ensure that the unittest tools are installed and available
 */
async function setupUnittestTools() {
    var pipTool: tr.ToolRunner;

    pipTool = getPipTool(['install', 'coverage']);
    await pipTool.exec();

    pipTool = getPipTool(['install', 'unittest-xml-reporting']);
    await pipTool.exec();
}

async function run() {
    tl.cd(rootDir);
    await configureEnvironment();
    await setupUnittestTools();

    let toolRunOptions: tr.IExecSyncOptions = <any> {
        silent: true
    };

    let coverageToolPath = tl.which('coverage');

    // Execute the unit tests
    let unittestTool = tl.tool(coverageToolPath).arg(['run', '-m', 'xmlrunner', 'discover']);
    unittestTool.execSync(toolRunOptions);

    // Generate the coverage output files
    let coverageOutputPath = path.join(coverageOutput, 'coverage.xml');
    var coverageTool = tl.tool(coverageToolPath).arg(['xml', '-o', coverageOutputPath]);
    coverageTool.execSync();

    // Generate the coverage reports
    let coverageHtmlPath = path.join(coverageOutput, 'htmlcov');
    coverageTool = tl.tool(coverageToolPath).arg(['html', '-d', coverageHtmlPath]);
    coverageTool.execSync();
}

run();<commit_msg>remove the existing output location if it exists<commit_after>import tl = require('vsts-task-lib/task')
import tr = require('vsts-task-lib/toolrunner')
import path = require('path')

let agentBuildDir = tl.getVariable('Agent.BuildDirectory');
let rootDir = tl.getPathInput('pythonroot', false, true);
let requirementFile = tl.getPathInput('reqfile', false, true);
let coverageOutput = tl.getPathInput('coveragedir', false, false);

/**
 * Activates the virtual environment created at the provided location
 * @param venvPath The path to the virtual environment
 */
function activateVenv(venvPath: string) {
    tl.debug('Activating virtual environment')

    let venvToolsPath = isWindows() ? 'Scripts' : 'bin';

    process.env['VIRTUAL_ENV'] = venvPath;
    process.env['PATH'] = path.join(venvPath, venvToolsPath) + path.delimiter + process.env['PATH'];
}

/**
 * Determines if the current operating system is Windows based
 */
function isWindows() {
    return tl.osType().match(/^Win/) !== null;
}

/**
 * Gets a ToolRunner for the Pip tool using the provided arguments
 * @param args A collection of arguments to provide to the tool
 */
function getPipTool(args: string[]): tr.ToolRunner {
    return tl.tool(tl.which('pip')).arg(args);
}

/**
 * Configures the environment for use
 */
async function configureEnvironment() {
     if (process.env['VIRTUAL_ENV'] === undefined) {
        tl.debug('Not currently in a virtual environment');

        // Define the location of the virtual environment
        let venv = path.join(agentBuildDir, 'venv', 'build');
        tl.debug('Virtual environment path set to: ' + venv);

        // Create the virtual environment
        tl.debug('Creating virtual environment');
        let pythonPath = isWindows() ? tl.which('python') : tl.which('python3');
        let venvTool = tl.tool(pythonPath).arg(['-m', 'venv', venv]);
        await venvTool.exec();

        // Activate the virtual environment
        activateVenv(venv);
    } else {
        tl.debug('Already in a virtual environment');
    }

    // Get the optional requirements file and restore if available
    if (requirementFile !== null) {
        var pipTool = getPipTool(['install', '-r', requirementFile]);
        await pipTool.exec();
    }
}

/**
 * Sets the environment up to ensure that the unittest tools are installed and available
 */
async function setupUnittestTools() {
    var pipTool: tr.ToolRunner;

    pipTool = getPipTool(['install', 'coverage']);
    await pipTool.exec();

    pipTool = getPipTool(['install', 'unittest-xml-reporting']);
    await pipTool.exec();
}

async function run() {
    tl.cd(rootDir);
    await configureEnvironment();
    await setupUnittestTools();

    let toolRunOptions: tr.IExecSyncOptions = <any> {
        silent: true
    };

    let coverageToolPath = tl.which('coverage');

    // Execute the unit tests
    let unittestTool = tl.tool(coverageToolPath).arg(['run', '-m', 'xmlrunner', 'discover']);
    unittestTool.execSync(toolRunOptions);

    // Remove the output path if it already exists to ensure that old artefacts are not persisted
    if (tl.exist(coverageOutput)) {
        tl.rmRF(coverageOutput);
    }

    // Generate the coverage output files
    let coverageOutputPath = path.join(coverageOutput, 'coverage.xml');
    var coverageTool = tl.tool(coverageToolPath).arg(['xml', '-o', coverageOutputPath]);
    coverageTool.execSync();

    // Generate the coverage reports
    let coverageHtmlPath = path.join(coverageOutput, 'htmlcov');
    coverageTool = tl.tool(coverageToolPath).arg(['html', '-d', coverageHtmlPath]);
    coverageTool.execSync();
}

run();
{'fraction_non_alphanumeric': 0.13377595705608136, 'fraction_numerical': 0.00028252578047746857, 'mean_word_length': 3.685638649900728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1150120', 'n_tokens_mistral': 2077, 'n_tokens_neox': 2042, 'n_words': 575}