From 11809c336797b6f508502a4507ddbf785039e558 Mon Sep 17 00:00:00 2001
From: Daira Hopwood <daira@jacaranda.org>
Date: Mon, 8 Sep 2014 22:45:24 +0100
Subject: [PATCH] Add a --coverage option to 'python setup.py test' and 'python
 setup.py trial'. refs #1698

Signed-off-by: Daira Hopwood <daira@jacaranda.org>
---
 setup.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/setup.py b/setup.py
index d5b4b4e2..04e54c20 100644
--- a/setup.py
+++ b/setup.py
@@ -153,6 +153,7 @@ class Trial(Command):
                      ("reporter=", None, "The reporter to use for this test run."),
                      ("suite=", "s", "Specify the test suite."),
                      ("quiet", None, "Don't display version numbers and paths of Tahoe dependencies."),
+                     ("coverage", "c", "Collect branch coverage information."),
                    ]
 
     def initialize_options(self):
@@ -162,12 +163,35 @@ class Trial(Command):
         self.reporter = None
         self.suite = "allmydata"
         self.quiet = False
+        self.coverage = False
 
     def finalize_options(self):
         pass
 
     def run(self):
         args = [sys.executable, os.path.join('bin', 'tahoe')]
+
+        if self.coverage:
+            from errno import ENOENT
+            coverage_cmd = 'coverage'
+            try:
+                subprocess.call([coverage_cmd, 'help'])
+            except OSError as e:
+                if e.errno != ENOENT:
+                    raise
+                coverage_cmd = 'python-coverage'
+                try:
+                    rc = subprocess.call([coverage_cmd, 'help'])
+                except OSError as e:
+                    if e.errno != ENOENT:
+                        raise
+                    print >>sys.stderr
+                    print >>sys.stderr, "Couldn't find the command 'coverage' nor 'python-coverage'."
+                    print >>sys.stderr, "coverage can be installed using 'pip install coverage', or on Debian-based systems, 'apt-get install python-coverage'."
+                    sys.exit(1)
+
+            args += ['@' + coverage_cmd, 'run', '--branch', '--source=src/allmydata', '@tahoe']
+
         if not self.quiet:
             args.append('--version-and-path')
         args += ['debug', 'trial']
-- 
2.45.2