From: Mike Frysinger Date: Wed, 16 Oct 2013 20:51:56 +0000 (-0400) Subject: tests: add assertExists/assertNotExists helpers X-Git-Tag: v2.2.8~54 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=81c3a7c87b29f67c2a573d51cca8b38f462f6757;p=portage.git tests: add assertExists/assertNotExists helpers This makes it easy to assert the state of paths and when things fail, get a good error message in the process (without having to dig into temp dirs by hand). This has largely been cribbed from ChromiumOS's chromite module. --- diff --git a/pym/portage/tests/__init__.py b/pym/portage/tests/__init__.py index c0ad112ab..890851b9d 100644 --- a/pym/portage/tests/__init__.py +++ b/pym/portage/tests/__init__.py @@ -237,6 +237,27 @@ class TestCase(unittest.TestCase): else: excName = str(excClass) raise self.failureException("%s not raised: %s" % (excName, msg)) + def assertExists(self, path): + """Make sure |path| exists""" + if not os.path.exists(path): + msg = ['path is missing: %s' % (path,)] + while path != '/': + path = os.path.dirname(path) + if not path: + # If we're given something like "foo", abort once we get to "". + break + result = os.path.exists(path) + msg.append('\tos.path.exists(%s): %s' % (path, result)) + if result: + msg.append('\tcontents: %r' % os.listdir(path)) + break + raise self.failureException('\n'.join(msg)) + + def assertNotExists(self, path): + """Make sure |path| does not exist""" + if os.path.exists(path): + raise self.failureException('path exists when it should not: %s' % path) + class TextTestRunner(unittest.TextTestRunner): """ We subclass unittest.TextTestRunner to output SKIP for tests that fail but are skippable diff --git a/pym/portage/tests/ebuild/test_config.py b/pym/portage/tests/ebuild/test_config.py index 275663c89..7941bf21f 100644 --- a/pym/portage/tests/ebuild/test_config.py +++ b/pym/portage/tests/ebuild/test_config.py @@ -250,7 +250,7 @@ class ConfigTestCase(TestCase): "new_repo_config.thin_manifest != True") new_manifest_file = os.path.join(new_repo_config.location, "dev-libs", "A", "Manifest") - self.assertEqual(os.path.exists(new_manifest_file), False) + self.assertNotExists(new_manifest_file) new_manifest_file = os.path.join(new_repo_config.location, "dev-libs", "B", "Manifest") f = open(new_manifest_file)