<commit_before>/*jshint unused: false */
/*jslint indent: 4 */
/*global jasmine, process, require, define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, afterLast, spyOn */
(function () {

    'use strict';

    var result, helper,
        cwd = process.cwd(),
        qPromiseFunc = function () {
            return {
                promise: 'yo! it\'s a promise',
                reject: function () {
                    return 'yo! promise rejected';
                },
                resolve: function () {
                    return 'yo! promise resolved';
                }
            };
        },
        options = {
            depth: 1,
            filters: ['../*.md']
        },
        //
        Q = require('q'),
        FS = require('fs'),
        Path = require('path'),
        sinon = require('sinon'),
        lodash = require('lodash'),
        Utils = require(Path.join(cwd, 'src', 'server', 'lib', 'utils')),
        //
        // exposed
        base = Path.join(cwd, 'src', 'server', 'lib', 'scandir'),
        Scandir = require(base),
        exec = require(base).exec,
        build = require(base).build,
        hasfiles = require(base).hasfiles;

    describe('scandir', function () {

        describe('scandir.options()', function () {
            it('returns defaults options', function () {
                helper = new Scandir();
                result = helper.options();
                expect(result.root).toEqual('.');
                expect(result.depth).toEqual(0);
                expect(result.filters).toEqual([]);
            });
        });

        describe('scandir/exec/scandir.exec', function () {

            it('returns Q.promise', function () {
                helper = new Scandir();
                sinon.stub(FS, 'stat');
                sinon.stub(helper, 'build');
                sinon.stub(Q, 'defer', qPromiseFunc);
                result = helper.exec('toto');
                expect(result).toEqual('yo! it\'s a promise');
                helper.build.restore();
                FS.stat.restore();
                Q.defer.restore();
            });

            it('returns a path/test scandir.options().root', function () {
                var promise, p, e;
                helper = new Scandir();
                sinon.stub(FS, 'stat');
                sinon.stub(helper, 'build');
                /* --- */
                p = '';
                helper.exec(p);
                expect(helper.options().root).toEqual(process.cwd());
                //
                p = '.';
                helper.exec(p);
                expect(helper.options().root).toEqual(process.cwd());
                //
                p = 'toto';
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.join(cwd, p));
                //
                p = Path.join(cwd, 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.join(cwd, 'toto'));
                //
                p = Path.join(cwd, '..', '..', 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.resolve(cwd, Path.join('..', '..', 'toto')));
                //
                p = Path.join(cwd, '..', '.', '..', 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.resolve(cwd, Path.join('..', '.', '..', 'toto')));
                /* --- */
                expect(FS.stat.called).toEqual(true);
                helper.build.restore();
                FS.stat.restore();
            });


            it('returns options with default root', function () {
                helper = new Scandir();
                var defaults = helper.options(),
                    opts = {
                        filters: ['**/*.hml'],
                        added: 'no default'
                    };
                helper.exec(opts);
                result = {
                    depth: 0,
                    added: 'no default',
                    filters: ['**/*.hml'],
                    root: 'd:\\npm\\readmepad'
                }
                expect(helper.options()).toEqual(result);
            })

            it('Q.reject', function (done) {
                var msg = 'Invalid arguments. Aborted.';
                helper = new Scandir();
                helper.exec().then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec([]).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(123).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(null).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(undefined).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
            });

            it('does not reject', function (done) {
                var path = Path.join(cwd, 'spec', 'expected', 'explore_method');<commit_msg>update tests fix scandir.exe<commit_after>/*jshint unused: false */
/*jslint indent: 4 */
/*global jasmine, process, require, define, describe, xdescribe, it, xit, expect, beforeEach, afterEach, afterLast, spyOn */
(function () {

    'use strict';

    var result, helper,
        cwd = process.cwd(),
        qPromiseFunc = function () {
            return {
                promise: 'yo! it\'s a promise',
                reject: function () {
                    return 'yo! promise rejected';
                },
                resolve: function () {
                    return 'yo! promise resolved';
                }
            };
        },
        options = {
            depth: 1,
            filters: ['../*.md']
        },
        //
        Q = require('q'),
        FS = require('fs'),
        Path = require('path'),
        sinon = require('sinon'),
        lodash = require('lodash'),
        Utils = require(Path.join(cwd, 'src', 'server', 'lib', 'utils')),
        //
        // exposed
        base = Path.join(cwd, 'src', 'server', 'lib', 'scandir'),
        Scandir = require(base),
        exec = require(base).exec,
        build = require(base).build,
        hasfiles = require(base).hasfiles;

    describe('scandir', function () {

        describe('scandir.options()', function () {
            it('returns defaults options', function () {
                helper = new Scandir();
                result = helper.options();
                expect(result.root).toEqual('.');
                expect(result.depth).toEqual(0);
                expect(result.filters).toEqual([]);
            });
        });

        describe('scandir/exec/scandir.exec', function () {

            it('returns Q.promise', function () {
                helper = new Scandir();
                sinon.stub(FS, 'stat');
                sinon.stub(helper, 'build');
                sinon.stub(Q, 'defer', qPromiseFunc);
                result = helper.exec('toto');
                expect(result).toEqual('yo! it\'s a promise');
                helper.build.restore();
                FS.stat.restore();
                Q.defer.restore();
            });

            it('returns a path/test scandir.options().root', function () {
                var promise, p, e;
                helper = new Scandir();
                sinon.stub(FS, 'stat');
                sinon.stub(helper, 'build');
                /* --- */
                p = '';
                helper.exec(p);
                expect(helper.options().root).toEqual(process.cwd());
                //
                p = '.';
                helper.exec(p);
                expect(helper.options().root).toEqual(process.cwd());
                //
                p = 'toto';
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.join(cwd, p));
                //
                p = Path.join(cwd, 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.join(cwd, 'toto'));
                //
                p = Path.join(cwd, '..', '..', 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.resolve(cwd, Path.join('..', '..', 'toto')));
                //
                p = Path.join(cwd, '..', '.', '..', 'toto');
                helper.exec(p);
                expect(helper.options().root).toEqual(Path.resolve(cwd, Path.join('..', '.', '..', 'toto')));
                /* --- */
                expect(FS.stat.called).toEqual(true);
                helper.build.restore();
                FS.stat.restore();
            });


            it('returns options with default root', function () {
                helper = new Scandir();
                var defaults = helper.options(),
                    opts = {
                        filters: ['**/*.hml'],
                        added: 'no default'
                    };
                helper.exec(opts);
                result = {
                    depth: 0,
                    root: process.cwd(),
                    added: 'no default',
                    filters: ['**/*.hml']
                }
                expect(helper.options()).toEqual(result);
            })

            it('Q.reject', function (done) {
                var msg = 'Invalid arguments. Aborted.';
                helper = new Scandir();
                helper.exec().then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec([]).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(123).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(null).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
                helper.exec(undefined).then(function (data) {}, function (err) {
                    expect(err.message).toEqual(msg);
                    done();
                });
            });

            it('does not reject', function (done) {
                var path = Path.join(cwd, 'spec', 'expected', 'explore_method');
{'fraction_non_alphanumeric': 0.15911612426035504, 'fraction_numerical': 0.0012943786982248522, 'mean_word_length': 1.2093545751633987, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4900599', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2741, 'n_words': 514}