📄 console.py
字号:
# -*- coding: utf-8 -*-# # Copyright (C) 2004-2008 Edgewall Software# All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://trac.edgewall.org/wiki/TracLicense.## This software consists of voluntary contributions made by many# individuals. For the exact contribution history, see the revision# history and logs, available at http://trac.edgewall.org/log/.## Author: Tim Moloney <t.moloney@verizon.net>import ConfigParserimport difflibimport osimport reimport shleximport sysimport timeimport unittestimport tracebackfrom StringIO import StringIOfrom trac.config import Configurationfrom trac.env import Environmentfrom trac.admin import consolefrom trac.test import InMemoryDatabasefrom trac.util.datefmt import get_date_format_hintSTRIP_TRAILING_SPACE = re.compile(r'( +)$', re.MULTILINE)def load_expected_results(file, pattern): """Reads the file, named file, which contains test results separated by the regular expression pattern. The test results are returned as a dictionary. """ expected = {} compiled_pattern = re.compile(pattern) f = open(file, 'r') for line in f: line = line.rstrip().decode('utf-8') match = compiled_pattern.search(line) if match: test = match.groups()[0] expected[test] = '' else: expected[test] += line + '\n' f.close() return expectedclass InMemoryEnvironment(Environment): """ A subclass of Environment that keeps its' DB in memory. """ def get_db_cnx(self): if not hasattr(self, '_db'): self._db = InMemoryDatabase() return self._db def create(self, db_str=None): pass def verify(self): return True def setup_log(self): from trac.log import logger_factory self.log = logger_factory('null') def is_component_enabled(self, cls): return cls.__module__.startswith('trac.') and \ cls.__module__.find('.tests.') == -1 def setup_config(self, load_defaults=None): self.config = Configuration(None) def save_config(self): passclass TracadminTestCase(unittest.TestCase): """ Tests the output of trac-admin and is meant to be used with .../trac/tests.py. """ expected_results = load_expected_results(os.path.join(os.path.split(__file__)[0], 'console-tests.txt'), '===== (test_[^ ]+) =====') def setUp(self): self.env = InMemoryEnvironment('', create=True) self.db = self.env.get_db_cnx() self._admin = console.TracAdmin() self._admin.env_set('', self.env) # Set test date to 11th Jan 2004 self._test_date = '2004-01-11' def tearDown(self): self.env = None def _execute(self, cmd, strip_trailing_space=True, expect_exception=False): _err = sys.stderr _out = sys.stdout try: sys.stderr = sys.stdout = out = StringIO() setattr(out, 'encoding', 'utf-8') # fake output encoding retval = None try: retval = self._admin.onecmd(cmd) except SystemExit, e: pass sys.stderr = _err sys.stdout = _out value = out.getvalue() if isinstance(value, str): # reverse what print_listing did value = value.decode('utf-8') if strip_trailing_space: return retval, STRIP_TRAILING_SPACE.sub('', value) else: return retval, value except Exception, e: sys.stderr = _err sys.stdout = _out if expect_exception: tb = traceback.format_exc() message = tb.splitlines()[-1] + '\n' return -1, message raise # Help test def test_help_ok(self): """ Tests the 'help' command in trac-admin. Since the 'help' command has no command arguments, it is hard to call it incorrectly. As a result, there is only this one test. """ from trac import __version__ test_name = sys._getframe().f_code.co_name d = {'version': __version__, 'date_format_hint': get_date_format_hint()} expected_results = self.expected_results[test_name] % d rv, output = self._execute('help') self.assertEqual(0, rv) # Create a useful delta between the output and the expected output output_lines = ['%s\n' % x for x in output.split('\n')] expected_lines = ['%s\n' % x for x in expected_results.split('\n')] output_diff = ''.join(list( difflib.unified_diff(expected_lines, output_lines) )) failure_message = "%r != %r\n" % (output, expected_results) + output_diff self.assertEqual(expected_results, output, failure_message) # Permission tests def test_permission_list_ok(self): """ Tests the 'permission list' command in trac-admin. Since this command has no command arguments, it is hard to call it incorrectly. As a result, there is only this one test. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('permission list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_permission_add_one_action_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add one permission and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('permission add test_user WIKI_VIEW') rv, output = self._execute('permission list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_permission_add_multiple_actions_ok(self): """ Tests the 'permission add' command in trac-admin. This particular test passes valid arguments to add multiple permissions and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('permission add test_user LOG_VIEW FILE_VIEW') rv, output = self._execute('permission list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_permission_remove_one_action_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove one permission and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('permission remove anonymous TICKET_MODIFY') rv, output = self._execute('permission list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_permission_remove_multiple_actions_ok(self): """ Tests the 'permission remove' command in trac-admin. This particular test passes valid arguments to remove multiple permission and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('permission remove anonymous WIKI_CREATE WIKI_MODIFY') rv, output = self._execute('permission list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) # Component tests def test_component_list_ok(self): """ Tests the 'component list' command in trac-admin. Since this command has no command arguments, it is hard to call it incorrectly. As a result, there is only this one test. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_add_ok(self): """ Tests the 'component add' command in trac-admin. This particular test passes valid arguments and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('component add new_component new_user') rv, output = self._execute('component list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_add_error_already_exists(self): """ Tests the 'component add' command in trac-admin. This particular test passes a component name that already exists and checks for an error message. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component add component1 new_user', expect_exception=True) self.assertEqual(-1, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_rename_ok(self): """ Tests the 'component rename' command in trac-admin. This particular test passes valid arguments and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('component rename component1 changed_name') rv, output = self._execute('component list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_rename_error_bad_component(self): """ Tests the 'component rename' command in trac-admin. This particular test tries to rename a component that does not exist. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component rename bad_component changed_name') self.assertEqual(2, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_rename_error_bad_new_name(self): """ Tests the 'component rename' command in trac-admin. This particular test tries to rename a component to a name that already exists. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component rename component1 component2', expect_exception=True) self.assertEqual(-1, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_chown_ok(self): """ Tests the 'component chown' command in trac-admin. This particular test passes valid arguments and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('component chown component2 changed_owner') rv, output = self._execute('component list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_chown_error_bad_component(self): """ Tests the 'component chown' command in trac-admin. This particular test tries to change the owner of a component that does not exist. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component chown bad_component changed_owner') self.assertEqual(2, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_remove_ok(self): """ Tests the 'component remove' command in trac-admin. This particular test passes a valid argument and checks for success. """ test_name = sys._getframe().f_code.co_name self._execute('component remove component1') rv, output = self._execute('component list') self.assertEqual(0, rv) self.assertEqual(self.expected_results[test_name], output) def test_component_remove_error_bad_component(self): """ Tests the 'component remove' command in trac-admin. This particular test tries to remove a component that does not exist. """ test_name = sys._getframe().f_code.co_name rv, output = self._execute('component remove bad_component')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -