# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 2014-04-20 07:23:06
# Node ID 00e5f5b9fc903d05fbf54bbb04015d619480b0f8
# Parent  ff4ce72cc8d6407ca0298aa1091bd4bc98b0dbba

run-tests: move test discovery logic into a function

The new function is easily monkeypatchable. This facilitates more
advanced test discovery by 3rd parties such as extensions.

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -1026,6 +1026,25 @@ class TestRunner(object):
         self.abort = [False]
         self._createdfiles = []
 
+    def findtests(self, args):
+        """Finds possible test files from arguments.
+
+        If you wish to inject custom tests into the test harness, this would
+        be a good function to monkeypatch or override in a derived class.
+        """
+        if not args:
+            if self.options.changed:
+                proc = Popen4('hg st --rev "%s" -man0 .' %
+                              self.options.changed, None, 0)
+                stdout, stderr = proc.communicate()
+                args = stdout.strip('\0').split('\0')
+            else:
+                args = os.listdir('.')
+
+        return [t for t in args
+                if os.path.basename(t).startswith('test-')
+                    and (t.endswith('.py') or t.endswith('.t'))]
+
     def runtests(self, tests):
         try:
             if self.inst:
@@ -1322,18 +1341,7 @@ def main(args, parser=None):
 
     checktools()
 
-    if not args:
-        if options.changed:
-            proc = Popen4('hg st --rev "%s" -man0 .' % options.changed,
-                          None, 0)
-            stdout, stderr = proc.communicate()
-            args = stdout.strip('\0').split('\0')
-        else:
-            args = os.listdir(".")
-
-    tests = [t for t in args
-             if os.path.basename(t).startswith("test-")
-                 and (t.endswith(".py") or t.endswith(".t"))]
+    tests = runner.findtests(args)
 
     if options.random:
         random.shuffle(tests)