<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/'
cfgs            = null

describe 'loadConfig', ->
  before 'register', ->
    loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse

  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 'synchronously', ->

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

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

    it 'should load wrong config file synchronously failed', ->
      loadConfig.bind(null, __dirname+'/fixture/err').should.throw('Unexpected token')

    it 'should raise error if raiseError is true and no config loaded', ->
      loadConfig.bind(null, __dirname+'/fixture/xxy', raiseError: true).should.throw('xxy Nothing Loaded')

    it 'should exclude itself synchronously', ->
      file = __dirname+'/fixture/config.json'
      result = loadConfig(file, exclude:file)
      should.not.exist result

    it 'should load config synchronously with Bom', ->
      result = loadConfig(__dirname+'/fixture/con')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/con.jon'
      result.should.be.deep.equal
        test: 123

  describe 'asynchronously', ->
    it 'should exclude itself asynchronously', (done)->
      file = __dirname+'/fixture/config.json'
      loadConfig file, exclude:file, (err, result)->
        should.not.exist result
        done(err, result)
      return

    it 'should load config asynchronously', (done)->
      loadConfig __dirname+'/fixture/config', raiseError:true, (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()
      return

    it 'should raise error if raiseError is true and no config loaded', (done)->
      loadConfig __dirname+'/fixture/xxy', raiseError: true, (err, result)->
        should.exist err
        err.should.have.property 'message', 'xxy Nothing Loaded'
        done()
      return

    it 'should load wrong config file asynchronously failed', (done)->
      loadConfig __dirname+'/fixture/err', (err, result)->
        should.exist err
        err.should.have.property 'name', 'err.json:SyntaxError'
        done()
      return

    it 'should load config asynchronously with bom', ()->
      # loadConfig __dirname+'/fixture/con', (err, result)->
      #   return done(err) if err
      loadConfig.load __dirname+'/fixture/con'
      .then (result)->
        should.exist result
        result.should.have.property '$cfgPath', __dirname+'/fixture/con.jon'
        result.should.be.deep.equal
          test: 123

  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/config')
      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.part synchronously', ->
      result = new loadConfig(__dirname+'/fixture/config.part')
      result = result.loadSync()
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.part.json'
      result.should.be.deep.equal
        part: 'hello'

    it 'should load config synchronously overwrite path', ->
      result = new loadConfig(__dirname+'/fixture/con')
      result = result.loadSync(__dirname+'/fixture/config')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal<commit_msg>test: add test constructor without path<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/'
cfgs            = null

describe 'loadConfig', ->
  before 'register', ->
    loadConfig.register ['.jsn', 'jon', '.json'], JSON.parse

  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 'synchronously', ->

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

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

    it 'should load wrong config file synchronously failed', ->
      loadConfig.bind(null, __dirname+'/fixture/err').should.throw('Unexpected token')

    it 'should raise error if raiseError is true and no config loaded', ->
      loadConfig.bind(null, __dirname+'/fixture/xxy', raiseError: true).should.throw('xxy Nothing Loaded')

    it 'should exclude itself synchronously', ->
      file = __dirname+'/fixture/config.json'
      result = loadConfig(file, exclude:file)
      should.not.exist result

    it 'should load config synchronously with Bom', ->
      result = loadConfig(__dirname+'/fixture/con')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/con.jon'
      result.should.be.deep.equal
        test: 123

  describe 'asynchronously', ->
    it 'should exclude itself asynchronously', (done)->
      file = __dirname+'/fixture/config.json'
      loadConfig file, exclude:file, (err, result)->
        should.not.exist result
        done(err, result)
      return

    it 'should load config asynchronously', (done)->
      loadConfig __dirname+'/fixture/config', raiseError:true, (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()
      return

    it 'should raise error if raiseError is true and no config loaded', (done)->
      loadConfig __dirname+'/fixture/xxy', raiseError: true, (err, result)->
        should.exist err
        err.should.have.property 'message', 'xxy Nothing Loaded'
        done()
      return

    it 'should load wrong config file asynchronously failed', (done)->
      loadConfig __dirname+'/fixture/err', (err, result)->
        should.exist err
        err.should.have.property 'name', 'err.json:SyntaxError'
        done()
      return

    it 'should load config asynchronously with bom', ()->
      # loadConfig __dirname+'/fixture/con', (err, result)->
      #   return done(err) if err
      loadConfig.load __dirname+'/fixture/con'
      .then (result)->
        should.exist result
        result.should.have.property '$cfgPath', __dirname+'/fixture/con.jon'
        result.should.be.deep.equal
          test: 123

  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 create a new Config object without path', ->
      result = new loadConfig {hi:112}
      result.should.have.property 'path', undefined
      result.should.have.ownProperty 'options'
      result.options.should.be.deep.equal {hi:112}

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

    it 'should load config synchronously overwrite path', ->
      result = new loadConfig(__dirname+'/fixture/con')
      result = result.loadSync(__dirname+'/fixture/config')
      should.exist result
      result.should.have.property '$cfgPath', __dirname+'/fixture/config.json'
      result.should.be.deep.equal
{'fraction_non_alphanumeric': 0.12873900293255133, 'fraction_numerical': 0.0033235581622678397, 'mean_word_length': 2.7600147004777655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 60, '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': '4413206', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2924, 'n_words': 742}