]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Added nicer test finder from Flask
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 10:51:37 +0000 (11:51 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 20 May 2013 10:51:37 +0000 (11:51 +0100)
MANIFEST.in
Makefile
jinja2/testsuite/__init__.py
run-tests.py [new file with mode: 0644]
tox.ini

index 63760bbf0947315b7bdc51e1bfb0a29b38eb43de..0d6db7c5edce27d278652270d9af54904bf151a0 100644 (file)
@@ -1,4 +1,4 @@
-include MANIFEST.in Makefile CHANGES LICENSE AUTHORS
+include MANIFEST.in Makefile CHANGES LICENSE AUTHORS run-tests.py
 recursive-include docs *
 recursive-include custom_fixers *
 recursive-include ext *
index 9a3159841393a4e1e0054df6ba80978c0717d7db..266180ef52e0c291c51694f416b35cd4f33c7f4a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 test:
-       python setup.py test
+       python run-tests.py
 
 develop:
        pip install --editable .
index 781a0e79299b2a07d8efdd385e94ba9eca921c69..f5e617b8e5953ba5051b7936b3eb816a533c2293 100644 (file)
@@ -69,6 +69,57 @@ class JinjaTestCase(unittest.TestCase):
             self.fail('Expected exception')
 
 
+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.
+    """
+
+    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():
     from jinja2.testsuite import ext, filters, tests, core_tags, \
          loader, inheritance, imports, lexnparse, security, api, \
@@ -94,3 +145,11 @@ def suite():
         suite.addTest(doctests.suite())
 
     return suite
+
+
+def main():
+    """Runs the testsuite as command line application."""
+    try:
+        unittest.main(testLoader=BetterLoader(), defaultTest='suite')
+    except Exception as e:
+        print('Error: %s' % e)
diff --git a/run-tests.py b/run-tests.py
new file mode 100644 (file)
index 0000000..f8d5024
--- /dev/null
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+import sys, os
+sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+from jinja2.testsuite import main
+main()
diff --git a/tox.ini b/tox.ini
index ffd4b8735bd675003407505951f10aff79f82c3d..4bad758e9de68b1e4b1108ffe07aa76206bff37c 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -2,4 +2,4 @@
 envlist = py26, py27, pypy, py33
 
 [testenv]
-commands = python setup.py test
+commands = python run-tests.py