~james-w/udd/management-commands

« back to all changes in this revision

Viewing changes to udd/tests/test_config.py

  • Committer: James Westby
  • Date: 2011-12-13 21:09:23 UTC
  • mfrom: (557.1.1 drop_email_failures)
  • Revision ID: james.westby@canonical.com-20111213210923-tfrirlx3xbwmi70u
Merged drop_email_failures into management-commands.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
from bzrlib import (
4
4
    config,
5
5
    osutils,
6
 
    tests,
 
6
    tests as bzr_tests,
7
7
    )
8
8
 
9
9
 
10
10
from udd import (
11
11
    iconfig,
 
12
    tests,
12
13
    )
13
 
from udd.paths import paths
14
 
 
15
 
 
16
 
class TestConfigPath(tests.TestCaseInTempDir):
17
 
 
18
 
    def setUp(self):
19
 
        super(TestConfigPath, self).setUp()
20
 
        current_dir = paths.root_dir
21
 
        self.addCleanup(paths.set_root_dir, current_dir)
22
 
        paths.set_root_dir(self.test_dir)
23
 
 
24
 
    def test_iconfig_file_name(self):
25
 
        os.mkdir('config')
26
 
        with open('config/pkgimport.conf', 'w'):
27
 
            pass
28
 
        conf_path = iconfig.pkgimportconfig_file_name()
29
 
        self.assertEquals(os.path.join(paths.root_dir, 'config'),
30
 
                          osutils.dirname(conf_path))
31
 
        self.assertEndsWith(conf_path, 'pkgimport.conf')
32
 
 
33
 
    def test_iconfig_file_name_old_path(self):
34
 
        conf_path = iconfig.pkgimportconfig_file_name()
35
 
        self.assertEquals(paths.root_dir, osutils.dirname(conf_path))
36
 
        self.assertEndsWith(conf_path, 'pkgimport.conf')
37
 
 
38
 
 
39
 
class TestPkgimportconfig(tests.TestCaseInTempDir):
40
 
 
41
 
    def setUp(self):
42
 
        super(TestPkgimportconfig, self).setUp()
43
 
        self.conf_file_name = osutils.pathjoin(self.test_dir, 'pkgimport.conf')
44
 
 
45
 
    def test_simple_option(self):
46
 
        conf = iconfig.PkgimportConfig.from_string(
47
 
            '''pkgimport.base_dir=/foo/bar''',
48
 
            file_name=self.conf_file_name, save=True)
49
 
        self.assertEquals('/foo/bar', conf.get('pkgimport.base_dir'))
50
 
 
51
 
 
52
 
class TestDefaultPkgimportconfig(tests.TestCase):
53
 
 
54
 
    def setUp(self):
55
 
        super(TestDefaultPkgimportconfig, self).setUp()
56
 
        self.conf = iconfig.PkgimportConfig()
57
 
 
58
 
    def test_default_config(self):
59
 
        self.assertEquals('pkgimport', self.conf.config_id())
60
 
        self.assertEquals(iconfig.pkgimportconfig_file_name(),
61
 
                          self.conf.file_name)
62
 
 
63
 
    def test_default_basedir(self):
64
 
        self.assertEquals('/srv/package-import.canonical.com/new',
65
 
                          self.conf.get('pkgimport.base_dir'))
66
 
 
67
 
    def assertOptiondIsDefined(self, name):
68
 
        self.assertIsNot(None, self.conf.get(name))
69
 
 
70
 
    def test_required_options(self):
71
 
        self.assertOptiondIsDefined('pkgimport.base_dir')
72
 
        self.assertOptiondIsDefined('pkgimport.log_dir')
73
 
        self.assertOptiondIsDefined('pkgimport.driver.log_dir')
74
 
        self.assertOptiondIsDefined('pkgimport.driver.log.debug')
75
 
        self.assertOptiondIsDefined('pkgimport.driver.log.progress')
76
 
        self.assertOptiondIsDefined('pkgimport.packages.log_dir')
77
 
 
78
 
 
79
 
class TestConfigStack(tests.TestCase):
80
 
 
81
 
    def test_compatibility(self):
82
 
        self.assertRaises(AssertionError, iconfig.ConfigStack, [object()])
83
 
 
84
 
    def test_single_config_get(self):
85
 
        conf = config.IniBasedConfig.from_string('foo=bar')
86
 
        conf_stack = iconfig.ConfigStack([conf])
87
 
        self.assertEquals('bar', conf_stack.get('foo'))
88
 
 
89
 
    def test_get_first_definition(self):
90
 
        conf1 = config.IniBasedConfig.from_string('foo=bar')
91
 
        conf2 = config.IniBasedConfig.from_string('foo=baz')
92
 
        conf_stack = iconfig.ConfigStack([conf1, conf2])
93
 
        self.assertEquals('bar', conf_stack.get('foo'))
94
 
 
95
 
 
96
 
class TestIconfig(tests.TestCaseInTempDir):
97
 
 
98
 
    def test_location_override_default(self):
99
 
        lconf = config.LocationConfig('.')
100
 
        lconf.set_user_option('pkgimport.base_dir', 'foo')
101
 
        conf = iconfig.Iconfig('.')
102
 
        self.assertEquals('foo', conf.get('pkgimport.base_dir'))
103
 
 
104
 
class TestPackageStack(tests.TestCaseWithTransport):
105
 
 
106
 
    def setUp(self):
107
 
        super(TestPackageStack, self).setUp()
108
 
        # Since iconfig states that pkgimport.conf is part of the sources (or a
109
 
        # directory below), we can't directly use this file for testing.
110
 
        # Instead, we override _root_dir to be local to the test
111
 
        self.overrideAttr(iconfig, '_root_dir', self.test_dir)
112
 
 
113
 
    def _set_store_content(self, store, content=None):
114
 
        # content may be unicode but the file content should be utf8
115
 
        content = content.encode('utf-8')
116
 
        store._load_from_string(content)
117
 
        store.save()
 
14
 
 
15
 
 
16
class TestPackageStack(tests.TestCaseWithConfig):
118
17
 
119
18
    def test_package_option(self):
120
19
        self._set_store_content(iconfig.PkgimportStore(self.get_transport()),
130
29
                                '[mypackage]\nfoo={bar}\nbar=baz')
131
30
        stack = iconfig.PackageStack('mypackage')
132
31
        self.assertEquals('baz', stack.get('foo'))
 
32
 
 
33
 
 
34
class TestImporterStack(tests.TestCaseWithConfig):
 
35
 
 
36
    def test_subdir_takes_priority(self):
 
37
        self._set_store_content(iconfig.PkgimportStore(self.get_transport()),
 
38
                                'foo=regular\n')
 
39
        os.mkdir('config')
 
40
        with open('config/pkgimport.conf', 'w') as f:
 
41
            f.write('foo=subdir\n')
 
42
        stack = iconfig.ImporterStack()
 
43
        self.assertEquals('subdir', stack.get('foo'))
 
44
 
 
45
    def test_common_option(self):
 
46
        self._set_store_content(iconfig.PkgimportStore(self.get_transport()),
 
47
                                'foo=bar\n')
 
48
        stack = iconfig.ImporterStack()
 
49
        self.assertEquals('bar', stack.get('foo'))
 
50
 
 
51
    def test_overridden_in_locations(self):
 
52
        self._set_store_content(config.LocationStore(),
 
53
                                '''[%s]\nfoo = location\n''' % self.test_dir)
 
54
        stack = iconfig.ImporterStack()
 
55
        self.assertEquals('location', stack.get('foo'))
 
56
 
 
57
    def test_expanded_option_for_importer(self):
 
58
        self._set_store_content(iconfig.PkgimportStore(self.get_transport()),
 
59
                                'foo={bar}\nbar=baz')
 
60
        stack = iconfig.ImporterStack()
 
61
        self.assertEquals('baz', stack.get('foo'))
 
62
 
 
63
 
 
64
class TestDefaultConfigStack(bzr_tests.TestCaseWithTransport):
 
65
    """Test some significant options in the default configuration.
 
66
 
 
67
    No need to test them all, most of them are built on the same model.
 
68
    """
 
69
 
 
70
    def setUp(self):
 
71
        super(TestDefaultConfigStack, self).setUp()
 
72
        self.conf = iconfig.ImporterStack()
 
73
 
 
74
    # 'test_base_dir' is a TestCaseInTempDir attribute so it can't be used as a
 
75
    # test method name :-)
 
76
    def test_the_base_dir(self):
 
77
        self.assertEquals('/srv/package-import.canonical.com/new',
 
78
                          self.conf.get('pi.base_dir'))
 
79
 
 
80
    def test_log_dir(self):
 
81
        self.assertEquals('/srv/package-import.canonical.com/new/logs',
 
82
                          self.conf.get('pi.log_dir'))