Make the self-test script importable
authorJeremy Stanley <fungi@yuggoth.org>
Mon, 2 Jan 2017 06:06:41 +0000 (06:06 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Mon, 2 Jan 2017 06:06:41 +0000 (06:06 +0000)
In preparation for moving the self-test script to a more standard
entrypoint, make it safely importable and give it a separate
conditional main() execution.

bin/test

index 85aa3f3..d246cfe 100755 (executable)
--- a/bin/test
+++ b/bin/test
@@ -1,7 +1,4 @@
-#!/usr/bin/env python3
-"""Regression test script for the mudpy engine."""
-
-# Copyright (c) 2004-2016 Jeremy Stanley <fungi@yuggoth.org>. Permission
+# Copyright (c) 2004-2017 Jeremy Stanley <fungi@yuggoth.org>. Permission
 # to use, copy, modify, and distribute this software is granted under
 # terms provided in the LICENSE file distributed with this software.
 
@@ -195,54 +192,61 @@ dialogue = (
     (test_log_no_errors, "no errors logged"),
 )
 
-captures = ["", "", ""]
-lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
-success = True
-start = time.time()
-for luser in lusers:
-    luser.open("::1", 6669)
-for test, description in dialogue:
-    print("\nTesting %s..." % description)
-    test_start = time.time()
-    for conversant, question, answer in test:
-        print("luser%s waiting for: %s" % (conversant, question))
-        index, match, received = lusers[conversant].expect(
-            [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
-        captures[conversant] += received.decode("utf-8")
+
+def main():
+    captures = ["", "", ""]
+    lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
+    success = True
+    start = time.time()
+    for luser in lusers:
+        luser.open("::1", 6669)
+    for test, description in dialogue:
+        print("\nTesting %s..." % description)
+        test_start = time.time()
+        for conversant, question, answer in test:
+            print("luser%s waiting for: %s" % (conversant, question))
+            index, match, received = lusers[conversant].expect(
+                [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
+            captures[conversant] += received.decode("utf-8")
+            try:
+                captures[conversant] += lusers[
+                    conversant].read_very_eager().decode("utf-8")
+            except:
+                pass
+            if index is not 0:
+                print("ERROR: luser%s did not receive expected string:\n\n"
+                      "%s\n\n"
+                      "Check the end of capture_%s.log for received data."
+                      % (conversant, question, conversant))
+                success = False
+                break
+            print("luser%s sending: %s" % (conversant, answer))
+            lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
+            captures[conversant] += "%s\r\n" % answer
+        if not success:
+            break
+        print("Completed in %.3f seconds." % (time.time() - test_start))
+    duration = time.time() - start
+    print("")
+    for conversant in range(len(captures)):
         try:
             captures[conversant] += lusers[
                 conversant].read_very_eager().decode("utf-8")
         except:
             pass
-        if index is not 0:
-            print("ERROR: luser%s did not receive expected string:\n\n%s\n\n"
-                  "Check the end of capture_%s.log for received data."
-                  % (conversant, question, conversant))
-            success = False
-            break
-        print("luser%s sending: %s" % (conversant, answer))
-        lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
-        captures[conversant] += "%s\r\n" % answer
-    if not success:
-        break
-    print("Completed in %.3f seconds." % (time.time() - test_start))
-duration = time.time() - start
-print("")
-for conversant in range(len(captures)):
-    try:
-        captures[conversant] += lusers[
-            conversant].read_very_eager().decode("utf-8")
-    except:
-        pass
-    lusers[conversant].close()
-    logfile = "capture_%s.log" % conversant
-    print("Recording session %s as %s." % (conversant, logfile))
-    log = open(logfile, "w")
-    log.write(captures[conversant])
-    log.close()
-print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
-if success:
-    print("SUCCESS")
-else:
-    print("FAILURE")
-    sys.exit(1)
+        lusers[conversant].close()
+        logfile = "capture_%s.log" % conversant
+        print("Recording session %s as %s." % (conversant, logfile))
+        log = open(logfile, "w")
+        log.write(captures[conversant])
+        log.close()
+    print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
+    if success:
+        print("SUCCESS")
+    else:
+        print("FAILURE")
+        sys.exit(1)
+
+
+if __name__ == '__main__':
+    sys.exit(main())