diff mbox

[pacman-dev] pactest: add --review option

Message ID 20170327123330.25737-1-andrew.gregory.8@gmail.com
State Accepted, archived
Headers show

Commit Message

Andrew Gregory March 27, 2017, 12:33 p.m. UTC
Opens the test file(s), test output, and any log files in the test
environment in an editor after the tests run for review.  Simplifies
debugging tests by avoiding the need to use --keep-root and manually
opening the relevant files.  The editor used can be set with --editor or
$EDITOR, falling back to vim.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
---
 test/pacman/pactest.py | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

Comments

Allan McRae April 4, 2017, 1:55 a.m. UTC | #1
On 27/03/17 22:33, Andrew Gregory wrote:
> Opens the test file(s), test output, and any log files in the test
> environment in an editor after the tests run for review.  Simplifies
> debugging tests by avoiding the need to use --keep-root and manually
> opening the relevant files.  The editor used can be set with --editor or
> $EDITOR, falling back to vim.
> 
> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>

Looks useful.  Thanks,

A
diff mbox

Patch

diff --git a/test/pacman/pactest.py b/test/pacman/pactest.py
index db61233d..b5f6d4d7 100755
--- a/test/pacman/pactest.py
+++ b/test/pacman/pactest.py
@@ -23,6 +23,8 @@ 
 import shutil
 import sys
 import tempfile
+import glob
+import subprocess
 
 import pmenv
 import tap
@@ -31,6 +33,30 @@ 
 __author__ = "Aurelien FORET"
 __version__ = "0.4"
 
+# writer to send output to multiple destinations simultaneously
+class MultiWriter():
+    def __init__(self, *outputs):
+        self.outputs = outputs
+
+    def write(self, message):
+        for op in self.outputs:
+            op.write(message)
+
+# duplicate stdout/stderr to a temporary file
+class OutputSaver():
+    def __init__(self):
+        self.save_file = tempfile.NamedTemporaryFile(prefix='pactest-output-')
+
+    def __enter__(self):
+        sys.stdout = MultiWriter(sys.stdout, self.save_file)
+        sys.stderr = MultiWriter(sys.stderr, self.save_file)
+        return self.save_file
+
+    def __exit__(self, type, value, traceback):
+        sys.stdout = sys.__stdout__
+        sys.stderr = sys.__stderr__
+        self.save_file.flush()
+
 def create_parser():
     usage = "usage: %prog [options] <path/to/testfile.py>..."
     description = "Runs automated tests on the pacman binary. Tests are " \
@@ -71,6 +97,12 @@  def create_parser():
     parser.add_option("--ldconfig", type = "string",
                       dest = "ldconfig", default = "/sbin/ldconfig",
                       help = "specify path to ldconfig")
+    parser.add_option("--review", action = "store_true",
+                      dest = "review", default = False,
+                      help = "review test files, test output, and saved logs")
+    parser.add_option("--editor", action = "store",
+                      dest = "editor", default = os.getenv('EDITOR', 'vim'),
+                      help = "editor to use for viewing files")
     return parser
 
 
@@ -114,7 +146,14 @@  def create_parser():
         sys.exit(2)
 
     # run tests
-    env.run()
+    if not opts.review:
+        env.run()
+    else:
+        # save output in tempfile for review
+        with OutputSaver() as save_file:
+            env.run()
+        files = [save_file.name] + args + glob.glob(root_path + "/var/log/*")
+        subprocess.call([opts.editor] + files)
 
     if not opts.keeproot:
         shutil.rmtree(root_path)