<commit_before>'use strict';

var assert = require('assert'),
    util = require('util'),
    Q = require('q'),
    promiseIt = require('../testHelpers').promiseIt,
    behaviors = require('../../src/models/behaviors'),
    Logger = require('../fakes/fakeLogger'),
    fs = require('fs');

describe('behaviors', function () {
    describe('#shellTransform', function () {
        promiseIt('should not execute during dry run', function () {
            var request = { isDryRun: true },
                responsePromise = Q({ data: 'ORIGINAL' }),
                command = 'echo Should not reach here',
                logger = Logger.create();

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'ORIGINAL' });
            });
        });

        promiseIt('should return output of command', function () {
            var request = {},
                responsePromise = Q({ data: 'ORIGINAL' }),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.log('%s', JSON.stringify({ data: 'CHANGED' }));
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'CHANGED' });
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should pass request and response to shell command', function () {
            var request = { data: 'FROM REQUEST' },
                responsePromise = Q({ data: 'UNCHANGED', requestData: '' }),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    var shellRequest = JSON.parse(process.argv[2]),
                        shellResponse = JSON.parse(process.argv[3]);

                    shellResponse.requestData = shellRequest.data;
                    console.log(JSON.stringify(shellResponse));
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'UNCHANGED', requestData: 'FROM REQUEST' });
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should reject promise if file does not exist', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'fileDoesNotExist',
                logger = Logger.create();

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('not found') >= 0, error);
            });
        });

        promiseIt('should reject if command returned non-zero status code', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.error('BOOM!!!');
                    process.exit(1);
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('Command failed') >= 0, error);
                assert.ok(error.indexOf('BOOM!!!') >= 0, error);
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should reject if command does not return valid JSON', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.log('This is not JSON');
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('Shell command returned invalid JSON') >= 0, error);
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });
    });
});
<commit_msg>Trying to fix Windows shell tests https://github.com/bbyars/mountebank/issues/171<commit_after>'use strict';

var assert = require('assert'),
    util = require('util'),
    Q = require('q'),
    promiseIt = require('../testHelpers').promiseIt,
    behaviors = require('../../src/models/behaviors'),
    Logger = require('../fakes/fakeLogger'),
    fs = require('fs');

describe('behaviors', function () {
    describe('#shellTransform', function () {
        promiseIt('should not execute during dry run', function () {
            var request = { isDryRun: true },
                responsePromise = Q({ data: 'ORIGINAL' }),
                command = 'echo Should not reach here',
                logger = Logger.create();

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'ORIGINAL' });
            });
        });

        promiseIt('should return output of command', function () {
            var request = {},
                responsePromise = Q({ data: 'ORIGINAL' }),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.log('%s', JSON.stringify({ data: 'CHANGED' }));
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'CHANGED' });
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should pass request and response to shell command', function () {
            var request = { data: 'FROM REQUEST' },
                responsePromise = Q({ data: 'UNCHANGED', requestData: '' }),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    // The replace of quotes only matters on Windows due to shell differences
                    var shellRequest = JSON.parse(process.argv[2].replace("'", '')),
                        shellResponse = JSON.parse(process.argv[3].replace("'", ''));

                    shellResponse.requestData = shellRequest.data;
                    console.log(JSON.stringify(shellResponse));
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function (response) {
                assert.deepEqual(response, { data: 'UNCHANGED', requestData: 'FROM REQUEST' });
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should reject promise if file does not exist', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'fileDoesNotExist',
                logger = Logger.create();

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('not found') >= 0, error);
            });
        });

        promiseIt('should reject if command returned non-zero status code', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.error('BOOM!!!');
                    process.exit(1);
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('Command failed') >= 0, error);
                assert.ok(error.indexOf('BOOM!!!') >= 0, error);
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });

        promiseIt('should reject if command does not return valid JSON', function () {
            var request = {},
                responsePromise = Q({}),
                command = 'node shellTransformTest.js',
                logger = Logger.create(),
                shellFn = function exec () {
                    console.log('This is not JSON');
                };

            fs.writeFileSync('shellTransformTest.js', util.format('%s\nexec();', shellFn.toString()));

            return behaviors.shellTransform(request, responsePromise, command, logger).then(function () {
                assert.fail('Promise resolved, should have been rejected');
            }, function (error) {
                assert.ok(error.indexOf('Shell command returned invalid JSON') >= 0, error);
            }).finally(function () {
                fs.unlinkSync('shellTransformTest.js');
            });
        });
    });
});

{'fraction_non_alphanumeric': 0.13763197586727, 'fraction_numerical': 0.0016025641025641025, 'mean_word_length': 1.9073718827075912, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '5422443', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2491, 'n_words': 570}