From b681b0ec75cf2ba6b6f8ef219c32f019358696e6 Mon Sep 17 00:00:00 2001 From: Vo Minh Thu Date: Wed, 1 Jun 2011 13:50:55 +0200 Subject: [PATCH] [ADD] tools/config: started tests. bzr revid: vmt@openerp.com-20110601115055-idoid8p0pnkfci38 --- openerp/tools/config.py | 6 ++-- openerp/tools/test-config-values | 25 +++++++++++++ openerp/tools/test-config-values-00.conf | 3 ++ openerp/tools/test_config.py | 46 ++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) create mode 100755 openerp/tools/test-config-values create mode 100644 openerp/tools/test-config-values-00.conf create mode 100644 openerp/tools/test_config.py diff --git a/openerp/tools/config.py b/openerp/tools/config.py index 65f6726febc..9a59235773a 100644 --- a/openerp/tools/config.py +++ b/openerp/tools/config.py @@ -258,8 +258,10 @@ class configmanager(object): self.parse_config() - def parse_config(self, args=[]): - opt, args = self.parser.parse_args() + def parse_config(self, args=None): + if args is None: + args = [] + opt, args = self.parser.parse_args(args) def die(cond, msg): if cond: diff --git a/openerp/tools/test-config-values b/openerp/tools/test-config-values new file mode 100755 index 00000000000..43991e8a21d --- /dev/null +++ b/openerp/tools/test-config-values @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import logging +import os +import signal +import sys +import threading +import traceback +import time + +import openerp + +openerp.tools.config.parse_config(sys.argv[1:]) +config = openerp.tools.config + +for name in [ + 'db_name', + 'addons_path', + 'demo', + 'osv_memory_count_limit', + 'osv_memory_age_limit', + ]: + print "%s: %s - %s" % (name, config[name], type(config[name])) + diff --git a/openerp/tools/test-config-values-00.conf b/openerp/tools/test-config-values-00.conf new file mode 100644 index 00000000000..1d0bd7419fe --- /dev/null +++ b/openerp/tools/test-config-values-00.conf @@ -0,0 +1,3 @@ +[options] +osv_memory_count_limit = 5 +osv_memory_age_limit = 3.4 diff --git a/openerp/tools/test_config.py b/openerp/tools/test_config.py new file mode 100644 index 00000000000..256e7ba4c32 --- /dev/null +++ b/openerp/tools/test_config.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +""" Tests for the configuration file/command-line arguments. """ + +# This test should be run from its directory. + +# TODO A configmanager object cannot parse multiple times a config file +# and/or the command line, preventing to 'reload' a configuration. + +import os + +import config + +config_file_00 = os.path.join(os.path.dirname(__file__),'test-config-values-00.conf') + +# 1. No config file, no command-line arguments (a.k.a. default values) + +conf = config.configmanager() +conf.parse_config() + +assert conf['osv_memory_age_limit'] == 1.0 +assert os.path.join(conf['root_path'], 'addons') == conf['addons_path'] + +# 2. No config file, some command-line arguments + +conf = config.configmanager() +# mess with the optparse.Option definition to allow an invalid path +conf.casts['addons_path'].action = 'store' +conf.parse_config(['--addons-path=/xyz/dont-exist', '--osv-memory-age-limit=2.3']) + +assert conf['osv_memory_age_limit'] == 2.3 +assert conf['addons_path'] == '/xyz/dont-exist' + +# 3. Config file, no command-line arguments + +conf = config.configmanager() +conf.parse_config(['-c', config_file_00]) + +assert conf['osv_memory_age_limit'] == 3.4 + +# 4. Config file, and command-line arguments + +conf = config.configmanager() +conf.parse_config(['-c', config_file_00, '--osv-memory-age-limit=2.3']) + +assert conf['osv_memory_age_limit'] == 2.3