<commit_before>'use strict';

var fs = require('fs'),
    nsconfig = require('../.'),
    should = require('should'),
    osenv = require('osenv'),
    cp = require('cp'),
    rimraf = require('rimraf');

function myrand() {
    return Math.ceil(Math.random() * 1000);
}

describe('Reading the config files... ', () => {

    function cpIfExists(path, inverse) {
        var ext1 = inverse ? '.temp' : '';
        var ext2 = inverse ? '' : '.temp';
        if (fs.existsSync(path+ext1)) {
            cp.sync(path + ext1, path + ext2);
        }
    }

    //backups your files
    before(() => {
        try {
            fs.mkdirSync(`${osenv.home()}/.ns`);
        } catch (e) {
            if (e.code != 'EEXIST') throw e;
        }

        cpIfExists(`${osenv.home()}/.ns/nsconfig.json`);
        cpIfExists('./nsconfig.json');

        var envBackup = Object.keys(process.env).reduce((prev, key) => {
            if (key.startsWith('NSCONF_')) prev[key] = process.env[key];
            return prev;
        }, {});

        fs.writeFileSync('nsconfigenv.json.temp', JSON.stringify(envBackup));
    });

    //restores your files
    after(() => {
        cpIfExists(`${osenv.home()}/.ns/nsconfig.json`, true);
        cpIfExists('./nsconfig.json', true);

        try {
            var envBackup = fs.readFileSync('nsconfigenv.json.temp');
            envBackup = JSON.parse(envBackup);
            Object.keys(envBackup).forEach(key => {
                process.env[key] = envBackup[key];
            }, {});
        } catch (e) {
            //ignore for now
        }

        //rimraf.sync(`${osenv.home()}/.ns/nsconfig.json.temp`);
        //rimraf.sync('./nsconfig.json.temp');
        rimraf.sync('./nsconfig.json');
        //rimraf.sync('nsconfigenv.json.temp');
    });

    beforeEach(() => {
        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`, '');
        fs.writeFileSync('./nsconfig.json', '');
        Object.keys(process.env).forEach(key => {
            if (key.startsWith('NSCONF_')) process.env[key] = '';
        });
    });

    it('reads email from global config file', () => {
        var name = `globalemail${myrand()}`;

        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`,
            JSON.stringify({email: name})
        );
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('reads email from local config file', () => {
        var name = `localemail${myrand()}`;

        fs.writeFileSync('./nsconfig.json',
            JSON.stringify({email: name})
        );
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('reads email from environment variable', () => {
        var name = `localemail${myrand()}`;
        process.env.NSCONF_EMAIL = name;
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('overrides global setting with local ones', () => {
        var nameGlobal = `globalemail${myrand()}`;
        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`,
            JSON.stringify({email: nameGlobal})
        );

        var nameLocal = `localemail${myrand()}`;
        fs.writeFileSync(`./nsconfig.json`,
            JSON.stringify({email: nameLocal})
        );

        var params = nsconfig({}, true);

        console.log(params);

        should(params).have.property('email', nameLocal);
    });

    it('use a custom required parameter. Fail on it.' , () => {

        try {
            var params = nsconfig({}, [{name: 'anyparams', required: true}]);
        } catch(e) {
            var a = 2;
        }
        var a = 0;

    });

});<commit_msg>Revert "one more test"<commit_after>'use strict';

var fs = require('fs'),
    nsconfig = require('../.'),
    should = require('should'),
    osenv = require('osenv'),
    cp = require('cp'),
    rimraf = require('rimraf');

function myrand() {
    return Math.ceil(Math.random() * 1000);
}

describe('Reading the config files... ', () => {

    function cpIfExists(path, inverse) {
        var ext1 = inverse ? '.temp' : '';
        var ext2 = inverse ? '' : '.temp';
        if (fs.existsSync(path+ext1)) {
            cp.sync(path + ext1, path + ext2);
        }
    }

    //backups your files
    before(() => {
        try {
            fs.mkdirSync(`${osenv.home()}/.ns`);
        } catch (e) {
            if (e.code != 'EEXIST') throw e;
        }

        cpIfExists(`${osenv.home()}/.ns/nsconfig.json`);
        cpIfExists('./nsconfig.json');

        var envBackup = Object.keys(process.env).reduce((prev, key) => {
            if (key.startsWith('NSCONF_')) prev[key] = process.env[key];
            return prev;
        }, {});

        fs.writeFileSync('nsconfigenv.json.temp', JSON.stringify(envBackup));
    });

    //restores your files
    after(() => {
        cpIfExists(`${osenv.home()}/.ns/nsconfig.json`, true);
        cpIfExists('./nsconfig.json', true);

        try {
            var envBackup = fs.readFileSync('nsconfigenv.json.temp');
            envBackup = JSON.parse(envBackup);
            Object.keys(envBackup).forEach(key => {
                process.env[key] = envBackup[key];
            }, {});
        } catch (e) {
            //ignore for now
        }

        //rimraf.sync(`${osenv.home()}/.ns/nsconfig.json.temp`);
        //rimraf.sync('./nsconfig.json.temp');
        rimraf.sync('./nsconfig.json');
        //rimraf.sync('nsconfigenv.json.temp');
    });

    beforeEach(() => {
        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`, '');
        fs.writeFileSync('./nsconfig.json', '');
        Object.keys(process.env).forEach(key => {
            if (key.startsWith('NSCONF_')) process.env[key] = '';
        });
    });

    it('reads email from global config file', () => {
        var name = `globalemail${myrand()}`;

        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`,
            JSON.stringify({email: name})
        );
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('reads email from local config file', () => {
        var name = `localemail${myrand()}`;

        fs.writeFileSync('./nsconfig.json',
            JSON.stringify({email: name})
        );
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('reads email from environment variable', () => {
        var name = `localemail${myrand()}`;
        process.env.NSCONF_EMAIL = name;
        var params = nsconfig({}, true);

        should(params).have.property('email', name);
    });

    it('overrides global setting with local ones', () => {
        var nameGlobal = `globalemail${myrand()}`;
        fs.writeFileSync(`${osenv.home()}/.ns/nsconfig.json`,
            JSON.stringify({email: nameGlobal})
        );

        var nameLocal = `localemail${myrand()}`;
        fs.writeFileSync(`./nsconfig.json`,
            JSON.stringify({email: nameLocal})
        );

        var params = nsconfig({}, true);

        console.log(params);

        should(params).have.property('email', nameLocal);
    });
});
{'fraction_non_alphanumeric': 0.19002507662301477, 'fraction_numerical': 0.0027862914460852605, 'mean_word_length': 2.095730918499353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '6680346', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2206, 'n_words': 382}