support code coverage during tests
authorGregory Szorc <gps@mozilla.com>
Tue, 01 Jul 2014 19:53:35 -0700
changeset 358889 fbb4bd1952831fa6fa882c879e6a035fe3ff7c8e
parent 358888 533d27e684b6ffbace8127d20cf9296385a4a941
child 358890 d206980353cd571ff1ddd099fd87b741e23923f1
push id16998
push userrwood@mozilla.com
push dateMon, 02 May 2016 19:42:03 +0000
support code coverage during tests
.hgignore
create-test-environment
run-mercurial-tests.py
test-requirements.txt
--- a/.hgignore
+++ b/.hgignore
@@ -1,5 +1,6 @@
+coverage/
 venv/
 \.pyc$
 \.pyo$
 \.swp$
 ~$
--- a/create-test-environment
+++ b/create-test-environment
@@ -16,9 +16,26 @@ easy_install ReviewBoard==2.0.2
 # development mode it doesn't. So use local development node.
 mkdir -p venv/tmp
 if [ -d venv/tmp/rbmozui ]; then
   cd venv/tmp/rbmzui && git pull
 else
   git clone git@github.com:mikeconley/rbmozui.git venv/tmp/rbmozui
 fi
 
-cd venv/tmp/rbmozui && python setup.py develop
+cd venv/tmp/rbmozui;
+python setup.py develop
+cd ../../..
+
+cat > venv/bin/sitecustomize.py << EOF
+import os
+
+if os.environ.get('CODE_COVERAGE', False):
+    import uuid
+    import coverage
+
+    covpath = os.path.join(os.environ['COVERAGE_DIR'],
+        'coverage.%s' % uuid.uuid1())
+    cov = coverage.coverage(data_file=covpath, auto_data=True)
+    cov._warn_no_data = False
+    cov._warn_unimported_source = False
+    cov.start()
+EOF
--- a/run-mercurial-tests.py
+++ b/run-mercurial-tests.py
@@ -4,16 +4,18 @@
 # GNU General Public License version 2 or any later version.
 
 # This file is used to run all Mercurial-related tests in this repository.
 
 import imp
 import os
 import sys
 
+from coverage import coverage
+
 # Mercurial's run-tests.py isn't meant to be loaded as a module. We do it
 # anyway.
 HERE = os.path.dirname(os.path.abspath(__file__))
 RUNTESTS = os.path.join(HERE, 'pylib', 'mercurial-support', 'run-tests.py')
 EXTDIR = os.path.join(HERE, 'hgext')
 
 sys.path.insert(0, os.path.join(HERE, 'pylib', 'mercurial-support'))
 runtestsmod = imp.load_source('runtests', RUNTESTS)
@@ -40,11 +42,41 @@ def find_test_files():
 if __name__ == '__main__':
     if not hasattr(sys, 'real_prefix'):
         raise Exception('You are not running inside the virtualenv. Please '
                 'create one and `pip install -r test-requirements.txt`')
 
     hg = os.path.join(os.path.dirname(sys.executable), 'hg')
     sys.argv.extend(['--with-hg', hg])
 
+    coveragerc = os.path.join(HERE, '.coveragerc')
+    coverdir = os.path.join(HERE, 'coverage')
+    if not os.path.exists(coverdir):
+        os.mkdir(coverdir)
+
+    docoverage = '--cover' in sys.argv
+    sys.argv = [a for a in sys.argv if a != '--cover']
+
+    # run-tests.py's coverage options don't work for us... yet. So, we hack
+    # in code coverage manually.
+    if docoverage:
+        os.environ['COVERAGE_DIR'] = coverdir
+        os.environ['CODE_COVERAGE'] = '1'
+
     runner = runtestsmod.TestRunner()
     sys.argv.extend(find_test_files())
-    sys.exit(runner.run(sys.argv[1:]))
+
+    res = runner.run(sys.argv[1:])
+
+    if docoverage:
+        cov = coverage(data_file=os.path.join(coverdir, 'coverage'))
+        cov.combine()
+        cov.html_report(directory='coverage/html', ignore_errors=True,
+            omit=[
+                os.path.join(HERE, 'venv', '*'),
+                os.path.join(HERE, 'pylib', 'flake8', '*'),
+                os.path.join(HERE, 'pylib', 'mccabe', '*'),
+                os.path.join(HERE, 'pylib', 'mercurial-support', '*'),
+                os.path.join(HERE, 'pylib', 'pep8', '*'),
+                os.path.join(HERE, 'pylib', 'pyflakes', '*'),
+            ])
+
+    sys.exit(res)
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -2,16 +2,17 @@
 --find-links http://downloads.reviewboard.org/releases/django-evolution/0.7/
 git+git://github.com/mozilla/rbbz.git@master#egg=rbbz
 Django==1.6.5
 Markdown==2.4.1
 Pillow==2.4.0
 Pygments==1.6
 RBTools==0.6.1
 Whoosh==2.6.0
+coverage==3.7.1
 django-evolution==0.7.2
 django-haystack==2.1.0
 django-pipeline==1.3.24
 docutils==0.11
 ecdsa==0.11
 feedparser==5.1.3
 futures==2.1.6
 mercurial==3.0.1