diff options
author | Andoni Morales Alastruey <ylatuya@gmail.com> | 2012-05-10 14:32:48 +0200 |
---|---|---|
committer | Andoni Morales Alastruey <ylatuya@gmail.com> | 2012-05-10 14:35:31 +0200 |
commit | 57b4a128ad5827ad50d8aa0edf59c5b0f1f0fe03 (patch) | |
tree | d9c0d5aed775b79fb8ee793f2227f7c6f9aa7c05 | |
parent | c7f7ea347af9a87d7802d02f4c913d38c4d25767 (diff) |
config: clean-up and add unit tests
-rw-r--r-- | cerbero/config.py | 45 | ||||
-rw-r--r-- | test/test_cerbero_config.py | 142 |
2 files changed, 172 insertions, 15 deletions
diff --git a/cerbero/config.py b/cerbero/config.py index b7a818a..7e70ed0 100644 --- a/cerbero/config.py +++ b/cerbero/config.py @@ -69,28 +69,19 @@ class Config (object): self.load_defaults() # Next parse the main configuration file - if os.path.exists(DEFAULT_CONFIG_FILE): - self.parse(DEFAULT_CONFIG_FILE) - else: - msg = _('Using default configuration because %s is missing') % \ - DEFAULT_CONFIG_FILE - m.warning(msg) + self._load_main_config() # Next, if a config file is provided use it to override the settings # from the main configuration file - if filename is not None: - if os.path.exists(filename): - self.parse(filename) - self.filename = DEFAULT_CONFIG_FILE - else: - raise ConfigurationError(_("Configuration file %s doesn't " - "exsits") % filename) + self._load_cmd_config(filename) + # Next, load the platform configuration self._load_platform_config() # Finally fill the missing gaps in the config self._load_last_defaults() + # And validate properties self.validate_properties() self.setup_env() @@ -152,13 +143,21 @@ class Config (object): if self.platform == Platform.LINUX: xdgdatadir += ":/usr/share:/usr/local/share" + ldflags = '-L%s ' % libdir + if ldflags not in os.environ.get('LDFLAGS', ''): + ldflags += os.environ.get('LDFLAGS', '') + + path = os.environ.get('PATH', '') + if bindir not in path: + path = self._join_path(bindir, path) + # Most of these variables are extracted from jhbuild env = {'LD_LIBRARY_PATH': libdir, - 'LDFLAGS': '-L%s %s' % (libdir, os.environ.get('LDFLAGS', '')), + 'LDFLAGS': ldflags, 'C_INCLUDE_PATH': includedir, 'CPLUS_INCLUDE_PATH': includedir, 'DYLD_FALLBACK_LIBRARY_PATH': libdir, - 'PATH': self._join_path(bindir, os.environ.get('PATH', '')), + 'PATH': path, 'MANPATH': manpathdir, 'INFOPATH': infopathdir, 'PKG_CONFIG_PATH': '%s' % pkgconfigdatadir, @@ -202,6 +201,22 @@ class Config (object): separator = ':' return "%s%s%s" % (path1, separator, path2) + def _load_main_config(self): + if os.path.exists(DEFAULT_CONFIG_FILE): + self.parse(DEFAULT_CONFIG_FILE) + else: + msg = _('Using default configuration because %s is missing') % \ + DEFAULT_CONFIG_FILE + m.warning(msg) + + def _load_cmd_config(self, filename): + if filename is not None: + if os.path.exists(filename): + self.parse(filename) + self.filename = DEFAULT_CONFIG_FILE + else: + raise ConfigurationError(_("Configuration file %s doesn't " + "exists") % filename) def _load_platform_config(self): platform_config = os.path.join(self.environ_dir, '%s.config' % self.target_platform) diff --git a/test/test_cerbero_config.py b/test/test_cerbero_config.py new file mode 100644 index 0000000..adcc99c --- /dev/null +++ b/test/test_cerbero_config.py @@ -0,0 +1,142 @@ +# cerbero - a multi-platform build system for Open Source software +# Copyright (C) 2012 Andoni Morales Alastruey <ylatuya@gmail.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +import os +import sys +import tempfile +import unittest + +from cerbero import config as cconfig +from cerbero.errors import FatalError +from cerbero.utils import system_info +Config = cconfig.Config + + +class LinuxPackagesTest(unittest.TestCase): + + def setUp(self): + os.environ[cconfig.CERBERO_UNINSTALLED]='1' + + def _checkLoadConfig(self, config, func, filename, properties): + with open(filename, 'w+') as f: + for p in properties: + f.write('%s="test"\n' % p) + func() + for p in properties: + self.assertEquals(getattr(config, p), 'test') + + def testAllPropsInitializedNone(self): + config = Config(load=False) + for p in config._properties: + self.assertIsNone(getattr(config, p)) + + def testLoadDefaults(self): + config = Config(load=False) + config.load_defaults() + platform, arch, distro, distro_version, num_of_cpus = system_info() + data_dir = os.path.join(os.path.dirname(__file__), '..', 'data') + data_dir = os.path.abspath(data_dir) + props = { + 'platform': platform, + 'target_platform': platform, + 'distro': distro, + 'distro_version': distro_version, + 'target_distro': distro, + 'target_distro_version': distro_version, + 'arch': arch, + 'target_arch': arch, + 'num_of_cpus': num_of_cpus, + 'host': None, + 'build': None, + 'target': None, + 'prefix': None, + 'sources': None, + 'local_sources': None, + 'lib_suffix': '', + 'cache_file': None, + 'toolchain_prefix': None, + 'install_dir': None, + 'packages_prefix': None, + 'data_dir': data_dir, + 'environ_dir': config._relative_path('config'), + 'recipes_dir': config._relative_path('recipes'), + 'packages_dir': config._relative_path('packages'), + 'git_root': cconfig.DEFAULT_GIT_ROOT, + 'wix_prefix': cconfig.DEFAULT_WIX_PREFIX, + 'packager': cconfig.DEFAULT_PACKAGER, + 'py_prefix': 'lib/python%s.%s' % (sys.version_info[0], + sys.version_info[1]), + 'allow_parallel_build': cconfig.DEFAULT_ALLOW_PARALLEL_BUILD, + 'use_configure_cache': False, + 'allow_system_libs': True, + } + self.assertEquals(sorted(config._properties), sorted(props.keys())) + for p, v in props.iteritems(): + self.assertEquals(getattr(config, p), v) + + def testLoadMainConfig(self): + config = Config(load=False) + + tmpconfig = tempfile.NamedTemporaryFile() + cconfig.DEFAULT_CONFIG_FILE = tmpconfig.name + + config._load_main_config() + for p in config._properties: + self.assertIsNone(getattr(config, p)) + + config.load_defaults() + self._checkLoadConfig(config, config._load_main_config, + tmpconfig.name, config._properties) + + def testLoadPlatformConfig(self): + config = Config(load=False) + tmpdir = tempfile.mkdtemp() + config.environ_dir = tmpdir + config.load_defaults() + config._load_platform_config() + platform_config = os.path.join(tmpdir, '%s.config' % + config.target_platform) + config.load_defaults() + self._checkLoadConfig(config, config._load_platform_config, + platform_config, config._properties) + + def testFindDataDir(self): + config = Config(load=False) + del os.environ[cconfig.CERBERO_UNINSTALLED] + config._check_uninstalled() + self.failUnlessRaises(FatalError, config.load_defaults) + + def testCheckUninstalled(self): + config = Config(load=False) + del os.environ[cconfig.CERBERO_UNINSTALLED] + config._check_uninstalled() + self.assertFalse(config.uninstalled) + os.environ[cconfig.CERBERO_UNINSTALLED]='1' + config._check_uninstalled() + self.assertTrue(config.uninstalled) + + def testSetupEnv(self): + config = Config(load=False) + tmpdir = tempfile.mkdtemp() + config.prefix = tmpdir + config.load_defaults() + config.setup_env() + env = config.get_env(tmpdir, os.path.join(tmpdir, 'lib'), + config.py_prefix) + for k, v in env.iteritems(): + self.assertEquals(os.environ[k], v) |