<commit_before>'use strict'

const _ = require('lodash')
const spawn = require('cross-spawn')
const gutil = require('gulp-util')
const path = require('path')
const through = require('through2')

const PLUGIN_NAME = 'gulp-elm-test'

// some of the following code is based on https://github.com/sun-zheng-an/gulp-shell/blob/4de040ab0b96d05aba5d6e054b61f4a3ab3227e6/index.js, Copyright (c) 2014 <NAME>, under the MIT license
function normalizeOptions(options) {
    const pathToBin = path.join(process.cwd(), 'node_modules', '.bin');
    const pathName = /^win/.test(process.platform) ? 'Path' : 'PATH';
    const newPath = pathToBin + path.delimiter + process.env[pathName];

    return _.extend({
        elmTest: 'elm-test',
        args: [],
        verbose: false,
        errorMessage: 'Running tests for `<%= file.path %>` failed with exit code <%= error.code %>',
        quiet: false,
        cwd: process.cwd(),
        env: _.extend(process.env, _.fromPairs([[pathName, newPath]]), options ? options.env : undefined),
    }, options);
}

function runCommand(command, args, options, file, done) {
    const allArgs = (args || []).concat(options.args);
    if (options.verbose) {
        gutil.log(gutil.colors.cyan(command + ' ' + allArgs.join(' ')));
    }

    let stdout = null;
    const child = spawn(command, allArgs, {
        env: options.env,
        cwd: options.cwd,
        timeout: options.timeout,
        stdio: [
            'pipe',
            options.quiet ? 'pipe' : process.stdout,
            process.stderr,
        ],
    }).on('close', (code) => {
        if (code !== 0) {
            const errorContext = {
                command: command,
                file: file,
                error: { code: code, },
            };

            if (options.quiet) {
                // if output wasn't already display, show since there was a failed test
                gutil.log(stdout.toString());
            }

            done({ message: gutil.template(options.errorMessage, errorContext), });
        } else {
            done();
        }
    });

    if (options.quiet) {
        // buffer output in case there's an error
        stdout = new Buffer(0);
        child.stdout.on('data', (chunk) => { stdout = Buffer.concat([ stdout, new Buffer(chunk), ]); });
    }
}

function shell(options) {
    options = normalizeOptions(options);

    const stream = through.obj(function (file, unused, done) {
        const self = this;

        runCommand(options.elmTest, [ file.path, '--yes', ], options, file, function (error) {
            if (error) {
                self.emit('error', new gutil.PluginError({
                    plugin: PLUGIN_NAME,
                    message: error.message,
                }));<commit_msg>Always pass relative path to elm-test (#6)<commit_after>'use strict'

const _ = require('lodash')
const spawn = require('cross-spawn')
const gutil = require('gulp-util')
const path = require('path')
const through = require('through2')

const PLUGIN_NAME = 'gulp-elm-test'

// some of the following code is based on https://github.com/sun-zheng-an/gulp-shell/blob/4de040ab0b96d05aba5d6e054b61f4a3ab3227e6/index.js, Copyright (c) 2014 <NAME>, under the MIT license
function normalizeOptions(options) {
    const pathToBin = path.join(process.cwd(), 'node_modules', '.bin');
    const pathName = /^win/.test(process.platform) ? 'Path' : 'PATH';
    const newPath = pathToBin + path.delimiter + process.env[pathName];

    return _.extend({
        elmTest: 'elm-test',
        args: [],
        verbose: false,
        errorMessage: 'Running tests for `<%= file.path %>` failed with exit code <%= error.code %>',
        quiet: false,
        cwd: process.cwd(),
        env: _.extend(process.env, _.fromPairs([[pathName, newPath]]), options ? options.env : undefined),
    }, options);
}

function runCommand(command, args, options, file, done) {
    const allArgs = (args || []).concat(options.args);
    if (options.verbose) {
        gutil.log(gutil.colors.cyan(command + ' ' + allArgs.join(' ')));
    }

    let stdout = null;
    const child = spawn(command, allArgs, {
        env: options.env,
        cwd: options.cwd,
        timeout: options.timeout,
        stdio: [
            'pipe',
            options.quiet ? 'pipe' : process.stdout,
            process.stderr,
        ],
    }).on('close', (code) => {
        if (code !== 0) {
            const errorContext = {
                command: command,
                file: file,
                error: { code: code, },
            };

            if (options.quiet) {
                // if output wasn't already display, show since there was a failed test
                gutil.log(stdout.toString());
            }

            done({ message: gutil.template(options.errorMessage, errorContext), });
        } else {
            done();
        }
    });

    if (options.quiet) {
        // buffer output in case there's an error
        stdout = new Buffer(0);
        child.stdout.on('data', (chunk) => { stdout = Buffer.concat([ stdout, new Buffer(chunk), ]); });
    }
}

function shell(options) {
    options = normalizeOptions(options);

    const stream = through.obj(function (file, unused, done) {
        const self = this;
        const absoluteFilePath = path.isAbsolute(file.path) ? path.relative(process.cwd(), file.path) : file.path;

        runCommand(options.elmTest, [ absoluteFilePath, '--yes', ], options, file, function (error) {
            if (error) {
                self.emit('error', new gutil.PluginError({
                    plugin: PLUGIN_NAME,
                    message: error.message,
                }));
{'fraction_non_alphanumeric': 0.14374006710224263, 'fraction_numerical': 0.010771675790217199, 'mean_word_length': 2.225512528473804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '2570665', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1647, 'n_words': 408}