From a13a41fbaf4fd22170f5dc3716b9569b232df966 Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Tue, 13 Jan 2015 23:17:18 +0100 Subject: [PATCH] qubes/tests: fix testrunner dependency on being run in specific directory --- qubes/tests/__init__.py | 33 +++++++++++++++++++++++++++++++++ qubes/tests/init.py | 7 +++++-- qubes/tests/run.py | 2 +- qubes/tests/vm/init.py | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/qubes/tests/__init__.py b/qubes/tests/__init__.py index d1b63f20..06ae7ac7 100644 --- a/qubes/tests/__init__.py +++ b/qubes/tests/__init__.py @@ -1,6 +1,8 @@ #!/usr/bin/python -O import collections +import os +import subprocess import unittest import lxml.etree @@ -12,6 +14,9 @@ import qubes.events #: :py:obj:`True` if running in dom0, :py:obj:`False` otherwise in_dom0 = False +#: :py:obj:`False` if outside of git repo, path to root of the directory otherwise +in_git = False + try: import libvirt libvirt.openReadOnly(qubes.config.defaults['libvirt_uri']).close() @@ -20,6 +25,15 @@ try: except libvirt.libvirtError: pass +try: + in_git = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).strip() +except subprocess.CalledProcessError: + # git returned nonzero, we are outside git repo + pass +except OSError: + # command not found; let's assume we're outside + pass + def skipUnlessDom0(test_item): '''Decorator that skips test outside dom0. @@ -31,6 +45,16 @@ def skipUnlessDom0(test_item): return unittest.skipUnless(in_dom0, 'outside dom0')(test_item) +def skipUnlessGit(test_item): + '''Decorator that skips test outside git repo. + + There are very few tests that an be run only in git. One example is + correctness of example code that won't get included in RPM. + ''' + + return unittest.skipUnless(in_git, 'outside git tree')(test_item) + + class TestEmitter(qubes.events.Emitter): '''Dummy event emitter which records events fired on it. @@ -160,6 +184,15 @@ class QubesTestCase(unittest.TestCase): relaxng = lxml.etree.RelaxNG(relaxng) elif file is not None and schema is None: + if not os.path.isabs(file): + basedirs = ['/usr/share/doc/qubes/relaxng'] + if in_git: + basedirs.insert(0, os.path.join(in_git, 'relaxng')) + for basedir in basedirs: + abspath = os.path.join(basedir, file) + if os.path.exists(abspath): + file = abspath + break relaxng = lxml.etree.RelaxNG(file=file) else: diff --git a/qubes/tests/init.py b/qubes/tests/init.py index ba2519e5..ddd211d8 100644 --- a/qubes/tests/init.py +++ b/qubes/tests/init.py @@ -1,5 +1,6 @@ #!/usr/bin/python2 -O +import os import sys import unittest @@ -321,7 +322,9 @@ class TC_30_VMCollection(qubes.tests.QubesTestCase): class TC_90_Qubes(qubes.tests.QubesTestCase): + @qubes.tests.skipUnlessGit def test_900_example_xml_in_doc(self): self.assertXMLIsValid( - lxml.etree.parse(open('../../doc/example.xml', 'rb')), - '../../relaxng/qubes.rng') + lxml.etree.parse(open( + os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')), + 'qubes.rng') diff --git a/qubes/tests/run.py b/qubes/tests/run.py index ab301408..b5b16ad9 100755 --- a/qubes/tests/run.py +++ b/qubes/tests/run.py @@ -12,7 +12,7 @@ test_order = [ 'qubes.tests.init' ] -sys.path.insert(0, '../../') +sys.path.insert(1, '../../') class ANSIColor(dict): def __init__(self): diff --git a/qubes/tests/vm/init.py b/qubes/tests/vm/init.py index 06b28374..c966f5f1 100644 --- a/qubes/tests/vm/init.py +++ b/qubes/tests/vm/init.py @@ -131,7 +131,7 @@ class TC_10_BaseVM(qubes.tests.QubesTestCase): 'disabledservice': False, }) - self.assertXMLIsValid(vm.__xml__(), '../../relaxng/domain.rng') + self.assertXMLIsValid(vm.__xml__(), 'domain.rng') def test_001_BaseVM_nxproperty(self): xml = lxml.etree.XML('''