Skip to content
Snippets Groups Projects
Select Git revision
  • fc6d4fa91811a655caef035cc9cc6b8994d5c08c
  • master default protected
  • dependabot/pip/wheel-0.38.1
  • dependabot/pip/certifi-2022.12.7
  • dependabot/pip/urllib3-1.26.5
  • dependabot/pip/py-1.10.0
  • dependabot/pip/pygments-2.7.4
  • dependabot/pip/cryptography-3.3.2
  • dependabot/pip/bleach-3.3.0
  • 0.9
10 results

versioneer.py

Blame
  • index.test.js 4.26 KiB
    const fs = require('fs').promises
    
    const Config = require('./index')
    
    let jsonDefaults = {
      setting:"defaultvalue",
      another: {
        setting:"avalue"
      }
    }
    
    let jsonLocals = {
      setting:"localvalue",
      another: {
        more:"stuff"
      },
      even: {
        deeper: {
          key: 'sodeep'
        }
      }
    }
    
    describe('config', function() {
    
      it('should only have values from config.defaults.json', async function() {
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        let config = new Config()
        await fs.unlink('config.defaults.json')
    
        expect(config.setting).toBe('defaultvalue')
        expect(config.another.setting).toBe('avalue')
      })
    
      it('should only have values from config.json', async function() {
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
        let config = new Config()
        await fs.unlink('config.json')
    
        expect(config.setting).toBe('localvalue')
        expect(config.another.more).toBe('stuff')
      })
    
      it('should have both values with preference to config.json', async function() {
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
        let config = new Config()
        await fs.unlink('config.json')
        await fs.unlink('config.defaults.json')
    
        expect(config.setting).toBe('localvalue')
        expect(config.another.more).toBe('stuff')
        expect(config.another.setting).toBe('avalue')
      })
    
      it('should respect a given basepath', async function() {
        await fs.mkdir('tmp')
        await fs.writeFile('tmp/config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('tmp/config.json', JSON.stringify(jsonLocals))
        let config = new Config('tmp')
        await fs.unlink('tmp/config.json')
        await fs.unlink('tmp/config.defaults.json')
        await fs.rmdir('tmp')
    
        expect(config.setting).toBe('localvalue')
        expect(config.another.more).toBe('stuff')
        expect(config.another.setting).toBe('avalue')
      })
    
      it('should have all values with preference to env', async function() {
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
        
        process.env['SETTING'] = 'overwritten-by-env'
        process.env['ANOTHER_MORE'] = 'false'
        process.env['EVEN_DEEPER_KEY'] = 'true'
        
        let config = new Config()
    
        await fs.unlink('config.json')
        await fs.unlink('config.defaults.json')
    
        delete process.env['SETTING']
        delete process.env['ANOTHER_MORE']
        delete process.env['EVEN_DEEPER_KEY']
    
        expect(config.setting).toBe('overwritten-by-env')
        expect(config.another.more).toBe(false)
        expect(config.even.deeper.key).toBe(true)
        expect(config.another.setting).toBe('avalue')
      })
    
      it('should read in a file when given', async function() {
        jsonLocals.setting = 'file:file.txt'
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
        await fs.writeFile('file.txt', 'value-from-file')
    
        let config = new Config()
    
        await fs.unlink('config.json')
        await fs.unlink('config.defaults.json')
        await fs.unlink('file.txt')
        jsonLocals.setting = 'localvalue'
    
        expect(config.setting).toBe('value-from-file')
      })
      it('should show a message and keep the setting when file given but not found', async function() {
        jsonLocals.setting = 'file:file.txt'
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
    
        let config = new Config()
    
        await fs.unlink('config.json')
        await fs.unlink('config.defaults.json')
        jsonLocals.setting = 'localvalue'
    
        expect(config.setting).toBe('file:file.txt')
      })
    
      it('should reload if asked', async function() {
        await fs.writeFile('config.defaults.json', JSON.stringify(jsonDefaults))
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
    
        let config = new Config()
    
        expect(config.setting).toBe('localvalue')
    
        jsonLocals.setting = 'reloaded-value'
        await fs.writeFile('config.json', JSON.stringify(jsonLocals))
    
        config.reload()
    
        await fs.unlink('config.json')
        await fs.unlink('config.defaults.json')
        jsonLocals.setting = 'localvalue'
    
        expect(config.setting).toBe('reloaded-value')
      })
    })