tests: add assertExists/assertNotExists helpers
authorMike Frysinger <vapier@gentoo.org>
Wed, 16 Oct 2013 20:51:56 +0000 (16:51 -0400)
committerMike Frysinger <vapier@gentoo.org>
Wed, 16 Oct 2013 20:53:41 +0000 (16:53 -0400)
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.

pym/portage/tests/__init__.py
pym/portage/tests/ebuild/test_config.py

index c0ad112ab8fc7634bc9efb1410ea1451c30700f3..890851b9dacef069b1e2a54c93ff27f83d88a0a6 100644 (file)
@@ -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
index 275663c8956af304c6a3677332b3fa6824addd43..7941bf21f3cd5044ac1d89cc162f8f783cc7fe07 100644 (file)
@@ -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)