예제 #1
0
class TestConfig(object):
    """ Test the operation of Config class. """
    def setup(self):
        self.config_file = os.path.join(tc.STAGING, 'pakit.yml')
        self.config = Config(self.config_file)

    def teardown(self):
        tc.delete_it(self.config_file)

    def test_get_default(self):
        assert self.config.get('pakit.paths.prefixxx', 42) == 42

    def test_get_raises(self):
        with pytest.raises(KeyError):
            assert self.config['pakit.paths.prefixxx']

    def test_get_missing_key(self):
        self.config.write()
        sub.call(['sed', '-i', '-e', '/command\\|timeout/d',
                  self.config.filename])
        with open(self.config.filename) as fin:
            lines = fin.readlines()
            print(lines)
            assert 'command:' not in lines
            assert 'timeout:' not in lines
        config = Config(self.config_file)
        assert config.get('pakit.command.timeout') == 120

    def test_path_to(self):
        expect = pakit.conf.TEMPLATE['pakit']['paths']['link']
        assert self.config.path_to('link') == expect

    def test_path_to_raises(self):
        with pytest.raises(KeyError):
            self.config.path_to('bad_key')

    def test_opts_for(self):
        """ Requires the testing pakit.yml. """
        config_file = os.path.join(os.path.dirname(__file__), 'pakit.yml')
        config = Config(config_file)
        opts = config.opts_for('ag')
        assert opts.get('repo') == 'unstable'
        assert opts.get('prefix') == '/tmp/test_pakit/builds'

    def test_reset(self):
        self.config['pakit.paths.prefix'] = 22
        self.config.reset()
        assert self.config.get('pakit.paths.prefix') == '/tmp/pakit/builds'
예제 #2
0
파일: test_conf.py 프로젝트: kostyll/pakit
class TestConfig(object):
    """ Test the operation of Config class. """
    def setup(self):
        self.config_file = os.path.join(tc.STAGING, 'pakit.yml')
        self.config = Config(self.config_file)

    def teardown(self):
        tc.delete_it(self.config_file)

    def test__str__(self):
        print()
        print(str(self.config))
        expect = """Config File: PLACE0
Contents:
{
  "pakit": {
    "command": {
      "timeout": 120
    },
    "defaults": {
      "repo": "stable"
    },
    "log": {
      "enabled": true,
      "file": "/tmp/pakit/main.log",
      "level": "debug"
    },
    "paths": {
      "link": "/tmp/pakit/links",
      "prefix": "/tmp/pakit/builds",
      "recipes": "PLACE1",
      "source": "/tmp/pakit/src"
    },
    "recipe": {
      "update_interval": 86400,
      "uris": [
        {
          "uri": "https://github.com/pakit/base_recipes"
        },
        {
          "uri": "user_recipes"
        }
      ]
    }
  }
}"""

        expect = expect.replace('PLACE0', self.config.filename)
        expect = expect.replace('PLACE1', os.path.expanduser('~/.pakit'))
        print(expect)
        assert str(self.config) == expect

    def test__contains__(self):
        assert 'pakit' in self.config
        assert 'pakit.paths' in self.config
        assert 'pakit.paths.prefix' in self.config
        assert 'beaver' not in self.config
        assert 'canada.beaver' not in self.config

    def test_get(self):
        assert self.config.get('pakit.paths.prefix') == '/tmp/pakit/builds'

    def test_get_missing_key(self):
        self.config.write()
        sub.call(['sed', '-i', '-e', '/command\\|timeout/d',
                  self.config.filename])
        with open(self.config.filename) as fin:
            lines = fin.readlines()
            assert 'command:' not in lines
            assert 'timeout:' not in lines
        config = Config(self.config_file)
        assert config.get('pakit.command.timeout') == 120

    def test_path_to(self):
        expect = pakit.conf.TEMPLATE['pakit']['paths']['link']
        assert self.config.path_to('link') == expect

    def test_path_to_raises(self):
        with pytest.raises(KeyError):
            self.config.path_to('bad_key')

    def test_get_raises(self):
        with pytest.raises(KeyError):
            self.config.get('pakit.paths.prefixxx')

    def test_opts_for(self):
        """ Requires the testing pakit.yml. """
        config_file = os.path.join(os.path.dirname(__file__), 'pakit.yml')
        config = Config(config_file)
        opts = config.opts_for('ag')
        assert opts.get('repo') == 'unstable'
        assert opts.get('prefix') == '/tmp/test_pakit/builds'

    def test_set(self):
        self.config.set('pakit.paths.prefix', '/dev/null')
        assert self.config.get('pakit.paths.prefix') == '/dev/null'

        self.config.set('hello.world', True)
        assert self.config.get('hello.world') is True

    def test_write(self):
        self.config.set('pakit.paths.install', 22)
        self.config.write()
        self.config.read()
        assert self.config.get('pakit.paths.install') == 22

    def test_reset(self):
        self.config.set('pakit.paths.prefix', 22)
        self.config.reset()
        assert self.config.get('pakit.paths.prefix') == '/tmp/pakit/builds'