]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
misc/build_helpers/run_trial.py: check that the root from which the module we are...
authordavid-sarah <david-sarah@jacaranda.org>
Mon, 2 Aug 2010 04:55:35 +0000 (21:55 -0700)
committerdavid-sarah <david-sarah@jacaranda.org>
Mon, 2 Aug 2010 04:55:35 +0000 (21:55 -0700)
misc/build_helpers/run_trial.py
src/allmydata/test/test_runner.py

index 4d06a5d0628685870fd209921038ff9b6044b761..eb9c63a438ad5b259257b95f5d5306f9e6786796 100644 (file)
@@ -1 +1,55 @@
-from twisted.scripts.trial import run; run()
\ No newline at end of file
+#!/usr/bin/env python
+
+import os, sys, re
+
+modulename = None
+for i in xrange(1, len(sys.argv)):
+    if not sys.argv[i].startswith('-'):
+        modulename = sys.argv[i]
+        break
+
+if modulename is None:
+    raise AssertionError("no test module specified")
+
+__import__(modulename)
+srcfile = sys.modules[modulename].__file__
+srcdir = os.path.dirname(os.path.realpath(srcfile))
+for i in modulename.split('.'):
+    srcdir = os.path.dirname(srcdir)
+
+if os.path.normcase(srcdir).endswith('.egg'):
+    srcdir = os.path.dirname(srcdir)
+elif os.path.normcase(os.path.basename(srcdir)) == 'site-packages':
+    srcdir = os.path.dirname(srcdir)
+    if re.search(r'python.+\..+', os.path.normcase(os.path.basename(srcdir))):
+        srcdir = os.path.dirname(srcdir)
+    if os.path.normcase(os.path.basename(srcdir)) == 'lib':
+        srcdir = os.path.dirname(srcdir)
+
+srcdir = os.path.normcase(os.path.normpath(srcdir))
+cwd = os.path.normcase(os.path.normpath(os.getcwd()))
+
+same = (srcdir == cwd)
+if not same:
+    try:
+        same = os.path.samefile(srcdir, cwd)
+    except AttributeError, e:
+        e  # hush pyflakes
+
+if not same:
+    msg = ("We seem to be testing the code at %r\n"
+           "(according to the source filename %r),\n"
+           "but expected to be testing the code at %r.\n"
+           % (srcdir, srcfile, cwd))
+    if (not isinstance(cwd, unicode) and
+        cwd.decode(sys.getfilesystemencoding(), 'replace') != os.path.normcase(os.path.normpath(os.getcwdu()))):
+        msg += ("However, this may be a false alarm because the current directory path\n"
+                "is not representable in the filesystem encoding. This script needs to be\n"
+                "run from the source directory to be tested, at a non-Unicode path.")
+    else:
+        msg += "This script needs to be run from the source directory to be tested."
+
+    raise AssertionError(msg)
+
+from twisted.scripts.trial import run
+run()
\ No newline at end of file
index 6e85a20bccb8d6ced2594aa21e053e92a941f6ef..81a44a393b916ead2903b39f914378d6fe8216c4 100644 (file)
@@ -13,7 +13,11 @@ from allmydata.scripts import runner
 from allmydata.test import common_util
 import allmydata
 
-bintahoe = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(allmydata.__file__))), 'bin', 'tahoe')
+srcfile = allmydata.__file__
+srcdir = os.path.dirname(os.path.dirname(os.path.realpath(srcfile)))
+rootdir = os.path.dirname(srcdir)
+
+bintahoe = os.path.join(rootdir, 'bin', 'tahoe')
 if sys.platform == "win32":
     bintahoe += ".pyscript"
 
@@ -33,6 +37,33 @@ class SkipMixin:
 
 
 class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
+    def test_the_right_code(self):
+        cwd = os.getcwd()
+        root_from_cwd = os.path.normcase(os.path.normpath(os.path.join(cwd, "..")))
+        root_from_test = os.path.normcase(os.path.normpath(rootdir))
+
+        same = (root_from_cwd == root_from_test)
+        if not same:
+            try:
+                same = os.path.samefile(root_from_cwd, root_from_test)
+            except AttributeError, e:
+                e  # hush pyflakes
+
+        if not same:
+            msg = ("We seem to be testing the code at %r,\n"
+                   "(according to the source filename %r),\n"
+                   "but expected to be testing the code at %r.\n"
+                   % (root_from_test, srcfile, root_from_cwd))
+            if (not isinstance(cwd, unicode) and
+                cwd.decode(get_filesystem_encoding(), 'replace') != os.path.normcase(os.path.normpath(os.getcwdu()))):
+                msg += ("However, this may be a false alarm because the current directory path\n"
+                        "is not representable in the filesystem encoding. Please run the tests\n"
+                        "from the root of the Tahoe-LAFS distribution at a non-Unicode path.")
+                raise unittest.SkipTest(msg)
+            else:
+                msg += "Please run the tests from the root of the Tahoe-LAFS distribution."
+                self.fail(msg)
+
     def test_path(self):
         self.skip_if_cannot_run_bintahoe()
         d = utils.getProcessOutputAndValue(bintahoe, args=["--version-and-path"], env=os.environ)
@@ -41,10 +72,13 @@ class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
             self.failUnlessEqual(rc_or_sig, 0, str(res))
 
             # Fail unless the package is *this* version *and* was loaded from *this* source directory.
-            ad = os.path.dirname(os.path.dirname(os.path.realpath(allmydata.__file__)))
-            required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, allmydata.__version__, ad)
+            required_ver_and_path = "%s: %s (%s)" % (allmydata.__appname__, allmydata.__version__, srcdir)
             self.failUnless(out.startswith(required_ver_and_path),
                             str((out, err, rc_or_sig, required_ver_and_path)))
+
+            self.failIfEqual(allmydata.__version__, "unknown",
+                             "We don't know our version, because this distribution didn't come "
+                             "with a _version.py and 'setup.py darcsver' hasn't been run.")
         d.addCallback(_cb)
         return d
 
@@ -61,7 +95,7 @@ class BinTahoe(common_util.SignalMixin, unittest.TestCase, SkipMixin):
         d = utils.getProcessOutputAndValue(bintahoe, args=[tricky_arg], env=os.environ)
         def _cb(res):
             out, err, rc_or_sig = res
-            self.failUnlessEqual(rc_or_sig, 1, str((out, err, rc_or_sig)))
+            self.failUnlessEqual(rc_or_sig, 1, str(res))
             self.failUnlessIn("Unknown command: "+tricky_out, out)
         d.addCallback(_cb)
         return d