From: david-sarah Date: Mon, 2 Aug 2010 04:55:35 +0000 (-0700) Subject: misc/build_helpers/run_trial.py: check that the root from which the module we are... X-Git-Tag: allmydata-tahoe-1.8.0b2~51 X-Git-Url: https://git.rkrishnan.org/reedownlee?a=commitdiff_plain;h=3af6f19cb0f02fb46430bc3bb53e1f1b7c702e3c;p=tahoe-lafs%2Ftahoe-lafs.git misc/build_helpers/run_trial.py: check that the root from which the module we are testing was loaded is the current directory. This version of the patch folds in later fixes to the logic for caculating the directories to compare, and improvements to error messages. addresses #1137 --- diff --git a/misc/build_helpers/run_trial.py b/misc/build_helpers/run_trial.py index 4d06a5d0..eb9c63a4 100644 --- a/misc/build_helpers/run_trial.py +++ b/misc/build_helpers/run_trial.py @@ -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 diff --git a/src/allmydata/test/test_runner.py b/src/allmydata/test/test_runner.py index 6e85a20b..81a44a39 100644 --- a/src/allmydata/test/test_runner.py +++ b/src/allmydata/test/test_runner.py @@ -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