Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Switch testcases to werkzeug format.

  • Loading branch information...
commit 690e6042ed5d46b0818dab9115255938a527f64a 1 parent 2961ee8
@tony authored
View
95 run-tests.py
@@ -1,96 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-"""Test runner for tmuxp project. ``$ ./run-tests.py --help`` for more."""
-from __future__ import absolute_import, division, print_function, with_statement
-
-try:
- import unittest2 as unittest
-except ImportError: # Python 2.7
- import unittest
-import sys
-import os
-import argparse
-
-tmux_path = sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
-if tmux_path not in sys.path:
- sys.path.insert(0, tmux_path)
-
-
-def main(verbosity=2, failfast=False):
- """Run TestSuite in new tmux session. Exit with code 0 if success."""
-
- suites = unittest.TestLoader().discover('tmuxp.testsuite', pattern="test_*.py")
- result = unittest.TextTestRunner(
- verbosity=verbosity, failfast=failfast).run(suites)
- if result.wasSuccessful():
- sys.exit(0)
- else:
- sys.exit(1)
-
-if __name__ == '__main__':
- parser = argparse.ArgumentParser(
- description='''\
- Run tests suite for tmuxp. With no arguments, runs all test suites in tmuxp.testsuite.
-
- Default usage:
- $ ./run-tests.py
- ''',
- formatter_class=argparse.RawTextHelpFormatter
- )
- parser.add_argument(
- '--tests',
- nargs='*',
- default=None,
- help='''\
- Test individual, TestCase or TestSuites, or multiple. Example for test_config TestSuite:
-
- by TestSuite (module):
- $ ./run-tests.py test_config
-
- by TestCase:
- $ ./run-tests.py test_config.ImportExportTest
- individual tests:
- $ ./run-tests.py test_config.ImportExportTest.test_export_json
-
- Multiple can be separated by spaces:
- $ ./run-tests.py test_config.ImportExportTest.test_export_json \\
- test_config.ImportExportTest.test_window
-
- ./run-tests will automatically assume the package namespace ``tmuxp.testsuite``.
-
- $ ./run-tests.py test_config.ImportExportTest
-
- is the same as:
-
- $ ./run-tests.py tmuxp.testsuite.test_config.ImportExportTest
- '''
- )
- parser.add_argument('-l', '--log-level', dest='log_level', default='INFO',
- help='Log level')
- parser.add_argument(
- '-v', '--verbosity', dest='verbosity', type=int, default=2,
- help='unittest verbosity level')
- parser.add_argument(
- '-F', '--failfast', dest='failfast', action='store_true',
-
- help='Stop on first test failure. failfast=True')
- args = parser.parse_args()
-
- verbosity = args.verbosity
-
- import logging
- logging.getLogger('tmuxp.testsuite').setLevel(args.log_level.upper())
-
- if 'help' in args:
- parser.print_help()
- if args.tests and len(args.tests) > int(0):
- for arg in args.tests:
- if not arg.startswith('tmuxp.testsuite'):
- loc = args.tests.index(arg)
- args.tests[loc] = 'tmuxp.testsuite.%s' % arg
- suites = unittest.TestLoader().loadTestsFromNames(args.tests)
- result = unittest.TextTestRunner(
- verbosity=verbosity, failfast=args.failfast).run(suites)
- else:
- main(verbosity=verbosity, failfast=args.failfast)
+from tmuxp.testsuite import main
+main()
View
9 tmuxp/_compat.py
@@ -19,6 +19,7 @@
from io import StringIO, BytesIO
import pickle
+ import configparser
izip = zip
imap = map
@@ -31,6 +32,8 @@
import urllib.parse as urllib
import urllib.parse as urlparse
+ exec('def reraise(tp, value, tb=None):\n raise(tp, value, tb)')
+
console_encoding = sys.__stdout__.encoding
def console_to_str(s):
@@ -54,6 +57,7 @@ def console_to_str(s):
from cStringIO import StringIO as BytesIO
from StringIO import StringIO
import cPickle as pickle
+ import ConfigParser as configparser
from itertools import izip, imap
range_type = xrange
@@ -67,5 +71,10 @@ def console_to_str(s):
def console_to_str(s):
return s.decode('utf_8')
+ def reraise(tp, value, tb=None):
+ if value.__traceback__ is not tb:
+ raise(value.with_traceback(tb))
+ raise value
+
number_types = integer_types + (float,)
View
218 tmuxp/testsuite/__init__.py
@@ -5,18 +5,23 @@
~~~~~~~~~~~
"""
-
from __future__ import absolute_import, division, print_function, \
with_statement, unicode_literals
-from ..server import Server
-t = Server()
-t.socket_name = 'tmuxp_test'
+import logging
+import unittest
+import sys
from .. import log
-import logging
+from .._compat import string_types, PY2, reraise
+from ..server import Server
+
+import pkgutil
+
logger = logging.getLogger()
+t = Server()
+t.socket_name = 'tmuxp_test'
if not logger.handlers:
channel = logging.StreamHandler()
@@ -30,11 +35,202 @@
testsuite_logger.setLevel('INFO')
-def suite():
- """Return TestSuite."""
+class ImportStringError(ImportError):
+ """Provides information about a failed :func:`import_string` attempt."""
+
+ #: String in dotted notation that failed to be imported.
+ import_name = None
+ #: Wrapped exception.
+ exception = None
+
+ def __init__(self, import_name, exception):
+ self.import_name = import_name
+ self.exception = exception
+
+ msg = (
+ 'import_string() failed for %r. Possible reasons are:\n\n'
+ '- missing __init__.py in a package;\n'
+ '- package or module path not included in sys.path;\n'
+ '- duplicated package or module name taking precedence in '
+ 'sys.path;\n'
+ '- missing module, class, function or variable;\n\n'
+ 'Debugged import:\n\n%s\n\n'
+ 'Original exception:\n\n%s: %s')
+
+ name = ''
+ tracked = []
+ for part in import_name.replace(':', '.').split('.'):
+ name += (name and '.') + part
+ imported = import_string(name, silent=True)
+ if imported:
+ tracked.append((name, getattr(imported, '__file__', None)))
+ else:
+ track = ['- %r found in %r.' % (n, i) for n, i in tracked]
+ track.append('- %r not found.' % name)
+ msg = msg % (import_name, '\n'.join(track),
+ exception.__class__.__name__, str(exception))
+ break
+
+ ImportError.__init__(self, msg)
+
+ def __repr__(self):
+ return '<%s(%r, %r)>' % (self.__class__.__name__, self.import_name,
+ self.exception)
+
+
+def import_string(import_name, silent=False):
+ """Imports an object based on a string. This is useful if you want to
+ use import paths as endpoints or something similar. An import path can
+ be specified either in dotted notation (``xml.sax.saxutils.escape``)
+ or with a colon as object delimiter (``xml.sax.saxutils:escape``).
+
+ If `silent` is True the return value will be `None` if the import fails.
+
+ :param import_name: the dotted name for the object to import.
+ :param silent: if set to `True` import errors are ignored and
+ `None` is returned instead.
+ :return: imported object
+ """
+ #XXX: py3 review needed
+ assert isinstance(import_name, string_types)
+ # force the import name to automatically convert to strings
+ import_name = str(import_name)
try:
- import unittest2 as unittest
- except ImportError: # Python 2.7
- import unittest
+ if ':' in import_name:
+ module, obj = import_name.split(':', 1)
+ elif '.' in import_name:
+ module, obj = import_name.rsplit('.', 1)
+ else:
+ return __import__(import_name)
+ # __import__ is not able to handle unicode strings in the fromlist
+ # if the module is a package
+ if PY2 and isinstance(obj, unicode):
+ obj = obj.encode('utf-8')
+ try:
+ return getattr(__import__(module, None, None, [obj]), obj)
+ except (ImportError, AttributeError):
+ # support importing modules not yet set up by the parent module
+ # (or package for that matter)
+ modname = module + '.' + obj
+ __import__(modname)
+ return sys.modules[modname]
+ except ImportError as e:
+ if not silent:
+ reraise(
+ ImportStringError,
+ ImportStringError(import_name, e),
+ sys.exc_info()[2])
+
+
+def find_modules(import_path, include_packages=False, recursive=False):
+ """Find all the modules below a package. This can be useful to
+ automatically import all views / controllers so that their metaclasses /
+ function decorators have a chance to register themselves on the
+ application.
+
+ Packages are not returned unless `include_packages` is `True`. This can
+ also recursively list modules but in that case it will import all the
+ packages to get the correct load path of that module.
+
+ :param import_name: the dotted name for the package to find child modules.
+ :param include_packages: set to `True` if packages should be returned, too.
+ :param recursive: set to `True` if recursion should happen.
+ :return: generator
+ """
+ module = import_string(import_path)
+ path = getattr(module, '__path__', None)
+ if path is None:
+ raise ValueError('%r is not a package' % import_path)
+ basename = module.__name__ + '.'
+ for importer, modname, ispkg in pkgutil.iter_modules(path):
+ modname = basename + modname
+ if ispkg:
+ if include_packages:
+ yield modname
+ if recursive:
+ for item in find_modules(modname, include_packages, True):
+ yield item
+ else:
+ yield modname
+
+
+def iter_suites(package):
+ """Yields all testsuites."""
+ for module in find_modules(package, include_packages=True):
+ mod = __import__(module, fromlist=['*'])
+ if hasattr(mod, 'suite'):
+ yield mod.suite()
+
+
+def find_all_tests(suite):
+ """Yields all the tests and their names from a given suite."""
+ suites = [suite]
+ while suites:
+ s = suites.pop()
+ try:
+ suites.extend(s)
+ except TypeError:
+ yield s, '%s.%s.%s' % (
+ s.__class__.__module__,
+ s.__class__.__name__,
+ s._testMethodName
+ )
+
+
+class BetterLoader(unittest.TestLoader):
+ """A nicer loader that solves two problems. First of all we are setting
+ up tests from different sources and we're doing this programmatically
+ which breaks the default loading logic so this is required anyways.
+ Secondly this loader has a nicer interpolation for test names than the
+ default one so you can just do ``run-tests.py ViewTestCase`` and it
+ will work.
+ """
- return unittest.TestLoader().discover('.', pattern="test_*.py")
+ def getRootSuite(self):
+ return suite()
+
+ def loadTestsFromName(self, name, module=None):
+ root = self.getRootSuite()
+ if name == 'suite':
+ return root
+
+ all_tests = []
+ for testcase, testname in find_all_tests(root):
+ if testname == name or \
+ testname.endswith('.' + name) or \
+ ('.' + name + '.') in testname or \
+ testname.startswith(name + '.'):
+ all_tests.append(testcase)
+
+ if not all_tests:
+ raise LookupError('could not find test case for "%s"' % name)
+
+ if len(all_tests) == 1:
+ return all_tests[0]
+ rv = unittest.TestSuite()
+ for test in all_tests:
+ rv.addTest(test)
+ return rv
+
+
+def suite():
+ """A testsuite that has all the Flask tests. You can use this
+ function to integrate the Flask tests into your own testsuite
+ in case you want to test that monkeypatches to Flask do not
+ break it.
+ """
+ suite = unittest.TestSuite()
+ for other_suite in iter_suites(__name__):
+ suite.addTest(other_suite)
+ return suite
+
+
+def main():
+ """Runs the testsuite as command line application."""
+ try:
+ unittest.main(testLoader=BetterLoader(), defaultTest='suite')
+ except Exception:
+ import sys
+ import traceback
+ traceback.print_exc()
+ sys.exit(1)
View
8 tmuxp/testsuite/test_cli.py
@@ -13,6 +13,7 @@
import shutil
import tempfile
import logging
+import unittest
import kaptan
@@ -179,3 +180,10 @@ def tearDownClass(cls):
}
]
}
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(FindConfigsTest))
+ suite.addTest(unittest.makeSuite(StartupTest))
+ return suite
View
14 tmuxp/testsuite/test_config.py
@@ -13,6 +13,7 @@
import shutil
import tempfile
import logging
+import unittest
import kaptan
@@ -1067,3 +1068,16 @@ def test_no_window_name(self):
with self.assertRaisesRegexp(exc.ConfigError, 'missing "window_name"'):
config.validate_schema(sconfig)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(ConfigBlankPanes))
+ suite.addTest(unittest.makeSuite(ConfigConsistency))
+ suite.addTest(unittest.makeSuite(ExpandTest))
+ suite.addTest(unittest.makeSuite(ImportExportTest))
+ suite.addTest(unittest.makeSuite(InlineTest))
+ suite.addTest(unittest.makeSuite(ShellCommandBeforeTest))
+ suite.addTest(unittest.makeSuite(ShellCommandBeforeSession))
+ suite.addTest(unittest.makeSuite(TrickleRelativeStartDirectory))
+ return suite
View
11 tmuxp/testsuite/test_config_teamocil.py
@@ -11,6 +11,7 @@
import os
import logging
+import unittest
import kaptan
@@ -730,3 +731,13 @@ def test_config_to_dict(self):
""" this configuration contains multiple sessions in a single file.
tmuxp can split them into files, proceed?
"""
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TeamocilTest))
+ suite.addTest(unittest.makeSuite(Teamocil2Test))
+ suite.addTest(unittest.makeSuite(Teamocil3Test))
+ suite.addTest(unittest.makeSuite(Teamocil4Test))
+ suite.addTest(unittest.makeSuite(TeamocilLayoutsTest))
+ return suite
View
9 tmuxp/testsuite/test_config_tmuxinator.py
@@ -11,6 +11,7 @@
import os
import logging
+import unittest
import kaptan
@@ -475,3 +476,11 @@ def test_config_to_dict(self):
config.import_tmuxinator(self.tmuxinator_dict),
self.tmuxp_dict
)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TmuxinatorTest))
+ suite.addTest(unittest.makeSuite(TmuxinatorDeprecationsTest))
+ suite.addTest(unittest.makeSuite(TmuxinatoriSampleTest))
+ return suite
View
7 tmuxp/testsuite/test_pane.py
@@ -9,6 +9,7 @@
from __future__ import absolute_import, division, print_function, \
with_statement, unicode_literals
+import unittest
import logging
from . import t
@@ -60,3 +61,9 @@ def test_set_width(self):
self.assertEqual(int(pane1['pane_width']), 10)
pane1.reset()
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(ResizeTest))
+ return suite
View
8 tmuxp/testsuite/test_server.py
@@ -10,6 +10,8 @@
with_statement, unicode_literals
from random import randint
+
+import unittest
import logging
from .. import Server
@@ -66,3 +68,9 @@ def test_88_colors(self):
self.assertIn('-8', proc.cmd)
self.assertNotIn('-2', proc.cmd)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(ServerTest))
+ return suite
View
10 tmuxp/testsuite/test_session.py
@@ -10,6 +10,8 @@
with_statement, unicode_literals
from random import randint
+
+import unittest
import logging
from .. import Session, Window, Pane
@@ -139,3 +141,11 @@ def test_set_option_bad(self):
"""Session.set_option raises ValueError for bad option key."""
with self.assertRaises(ValueError):
self.session.set_option('afewewfew', 43)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(Options))
+ suite.addTest(unittest.makeSuite(SessionNewTest))
+ suite.addTest(unittest.makeSuite(SessionTest))
+ return suite
View
7 tmuxp/testsuite/test_tmuxobject.py
@@ -11,6 +11,7 @@
import random
import logging
+import unittest
from .. import Pane, Window, Session
from . import t
@@ -206,3 +207,9 @@ def test_getById(self):
self.assertIsNone(window.getById(
'%' + str(random.randint(50000, 90000))
))
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TmuxObjectTest))
+ return suite
View
15 tmuxp/testsuite/test_window.py
@@ -10,6 +10,7 @@
with_statement, unicode_literals
import logging
+import unittest
from .. import Pane, Window, Server
from . import t
@@ -212,3 +213,17 @@ def test_set_window_option_bad(self):
with self.assertRaises(ValueError):
window.set_window_option('afewewfew', 43)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(KillWindow))
+ suite.addTest(unittest.makeSuite(NewTest))
+ suite.addTest(unittest.makeSuite(NewTest2))
+ suite.addTest(unittest.makeSuite(NewTest3))
+ suite.addTest(unittest.makeSuite(NewTest4))
+ suite.addTest(unittest.makeSuite(Options))
+ suite.addTest(unittest.makeSuite(RenameTest))
+ suite.addTest(unittest.makeSuite(RenameSpacesTest))
+ suite.addTest(unittest.makeSuite(SelectTest))
+ return suite
View
15 tmuxp/testsuite/test_workspacebuilder.py
@@ -12,6 +12,7 @@
import os
import sys
import logging
+import unittest
import time
import kaptan
@@ -524,3 +525,17 @@ def test_window_index(self):
for window, wconf in builder.iter_create_windows(self.session):
expected_index = name_index_map[window['window_name']]
self.assertEqual(int(window['window_index']), expected_index)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(BlankPaneTest))
+ suite.addTest(unittest.makeSuite(FocusAndPaneIndexTest))
+ suite.addTest(unittest.makeSuite(PaneOrderingTest))
+ suite.addTest(unittest.makeSuite(StartDirectoryTest))
+ suite.addTest(unittest.makeSuite(ThreePaneTest))
+ suite.addTest(unittest.makeSuite(TwoPaneTest))
+ suite.addTest(unittest.makeSuite(WindowAutomaticRename))
+ suite.addTest(unittest.makeSuite(WindowIndexTest))
+ suite.addTest(unittest.makeSuite(WindowOptions))
+ return suite
View
7 tmuxp/testsuite/test_workspacefreezer.py
@@ -11,6 +11,7 @@
import os
import logging
+import unittest
import time
import kaptan
@@ -85,3 +86,9 @@ def test_freeze_config(self):
default_flow_style=False,
safe=True
)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(FreezeTest))
+ return suite
Please sign in to comment.
Something went wrong with that request. Please try again.