<commit_before>chai            = require 'chai'
sinon           = require 'sinon'
sinonChai       = require 'sinon-chai'
should          = chai.should()
expect          = chai.expect
assert          = chai.assert
chai.use(sinonChai)


loadConfig      = require '../src/'
fs              = require 'fs'
fs.path         = require('path.js/lib/path').path
cfgs            = null
loadConfig.setFileSystem fs


describe 'loadConfig', ->
  before ->
    result = loadConfig.addConfig ['index', 'readme', 'config']
    assert.equal result, 3

  it 'should add a config file name', ->
    old = loadConfig::files
    loadConfig::files = []
    result = loadConfig.addConfig 'index'
    assert.equal result, 1
    loadConfig::files.should.have.length 1
    result = loadConfig.addConfig 'index'
    assert.equal result, 0
    loadConfig::files.should.have.length 1
    result = loadConfig::files.pop()
    assert.equal result, 'index'
    loadConfig::files = old

  it 'should add multi config file names', ->
    assert.deepEqual loadConfig::files, ['index', 'readme', 'config']

  it 'should register a config format', ->
    cfgs = loadConfig.register 'json', JSON.parse
    should.exist cfgs
    cfgs.should.have.property '.json', JSON.parse
    delete cfgs['.json']

  it 'should register a config format with multi extnames', ->
    cfgs = loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse
    should.exist cfgs
    cfgs.should.be.deep.equal
      '.jsn': JSON.parse
      '.jon': JSON.parse
      '.json': JSON.parse

  describe 'load', ->
    before ->
      loadConfig::files = ['index', 'readme', 'config']
      loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse

    it 'should load config synchronously', ->
      result = loadConfig(__dirname+'/fixture')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal
        str: 'hello'


    it 'should load config asynchronously', (done)->
      loadConfig __dirname+'/fixture/', (err, result)->
        return done(err) if err
        should.exist result
        result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
        result.should.be.deep.equal
          str: 'hello'
        done()

  describe 'object usage', ->
    it 'should create a new Config object', ->
      result = new loadConfig 'path1', {hi:112}
      result.should.have.property 'path', 'path1'
      result.should.have.ownProperty 'options'
      result.options.should.be.deep.equal {hi:112}

    it 'should load config synchronously', ->
      result = new loadConfig(__dirname+'/fixture/')
      result = result.loadSync()
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal
        str: 'hello'

    it 'should load config synchronously with nothinh loaded', ->
      result = new loadConfig(__dirname+'/fixture/saaw')
      result = result.loadSync(encoding:'utf8')
      should.not.exist result

    it 'should load config synchronously overwrite path', ->
      result = new loadConfig(__dirname+'/fixture/con')
      result = result.loadSync(__dirname+'/fixture')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'<commit_msg>chore(test): update test promise<commit_after>chai            = require 'chai'
sinon           = require 'sinon'
sinonChai       = require 'sinon-chai'
should          = chai.should()
expect          = chai.expect
assert          = chai.assert
chai.use(sinonChai)


loadConfig      = require '../src/'
fs              = require 'fs'
fs.path         = require('path.js/lib/path').path
cfgs            = null
loadConfig.setFileSystem fs


describe 'loadConfig', ->
  before ->
    result = loadConfig.addConfig ['index', 'readme', 'config']
    assert.equal result, 3

  it 'should add a config file name', ->
    old = loadConfig::files
    loadConfig::files = []
    result = loadConfig.addConfig 'index'
    assert.equal result, 1
    loadConfig::files.should.have.length 1
    result = loadConfig.addConfig 'index'
    assert.equal result, 0
    loadConfig::files.should.have.length 1
    result = loadConfig::files.pop()
    assert.equal result, 'index'
    loadConfig::files = old

  it 'should add multi config file names', ->
    assert.deepEqual loadConfig::files, ['index', 'readme', 'config']

  it 'should register a config format', ->
    cfgs = loadConfig.register 'json', JSON.parse
    should.exist cfgs
    cfgs.should.have.property '.json', JSON.parse
    delete cfgs['.json']

  it 'should register a config format with multi extnames', ->
    cfgs = loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse
    should.exist cfgs
    cfgs.should.be.deep.equal
      '.jsn': JSON.parse
      '.jon': JSON.parse
      '.json': JSON.parse

  describe 'load', ->
    before ->
      loadConfig::files = ['index', 'readme', 'config']
      loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse

    it 'should load config synchronously', ->
      result = loadConfig(__dirname+'/fixture')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal
        str: 'hello'


    it 'should load config asynchronously', (done)->
      loadConfig __dirname+'/fixture/', (err, result)->
        return done(err) if err
        should.exist result
        result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
        result.should.be.deep.equal
          str: 'hello'
        done()
    it 'should load config asynchronously promise', ()->
      loadConfig.load __dirname+'/fixture/'
      .then (result)->
        should.exist result
        result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
        result.should.be.deep.equal
          str: 'hello'

  describe 'object usage', ->
    it 'should create a new Config object', ->
      result = new loadConfig 'path1', {hi:112}
      result.should.have.property 'path', 'path1'
      result.should.have.ownProperty 'options'
      result.options.should.be.deep.equal {hi:112}

    it 'should load config synchronously', ->
      result = new loadConfig(__dirname+'/fixture/')
      result = result.loadSync()
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal
        str: 'hello'

    it 'should load config synchronously with nothinh loaded', ->
      result = new loadConfig(__dirname+'/fixture/saaw')
      result = result.loadSync(encoding:'utf8')
      should.not.exist result

    it 'should load config synchronously overwrite path', ->
      result = new loadConfig(__dirname+'/fixture/con')
      result = result.loadSync(__dirname+'/fixture')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
{'fraction_non_alphanumeric': 0.13398645728281228, 'fraction_numerical': 0.004034000864428756, 'mean_word_length': 2.736275565123789, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '3489818', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2024, 'n_words': 514}