Bug 1393242 - Make hglib tests more resilient; r?mshal draft
authorGregory Szorc <gps@mozilla.com>
Wed, 30 Aug 2017 10:42:35 -0700
changeset 657532 31067e1c7137c373c1148b933105c5b066365e9d
parent 657531 b73a8ce2cb7b7aa7678e6423929c88168be12bdd
child 657533 2bc0b96ae3784ae622f0eadbbe230fbe83c92c55
push id77552
push usergszorc@mozilla.com
push dateFri, 01 Sep 2017 16:20:16 +0000
reviewersmshal
bugs1393242
milestone57.0a1
Bug 1393242 - Make hglib tests more resilient; r?mshal These tests weren't running in automation because hglib wasn't available. An upcoming commit will vendor hglib. This exposed that the tests can fail if ui.username isn't set. In this commit, we introduce a helper function to obtain an hglib client with ui.username set. We also convert tests to use the context manager form of the client so resources are cleaned up immediately without relying on refcounting or garbage collection. MozReview-Commit-ID: HRSBDlYgqpC
python/mozbuild/mozpack/test/test_files.py
--- a/python/mozbuild/mozpack/test/test_files.py
+++ b/python/mozbuild/mozpack/test/test_files.py
@@ -1176,90 +1176,118 @@ class TestComposedFinder(MatchTestTempla
         self.assertIsInstance(self.finder.get('bar'), File)
 
 
 @unittest.skipUnless(hglib, 'hglib not available')
 class TestMercurialRevisionFinder(MatchTestTemplate, TestWithTmpDir):
     def setUp(self):
         super(TestMercurialRevisionFinder, self).setUp()
         hglib.init(self.tmpdir)
+        self._clients = []
+
+    def tearDown(self):
+        # Ensure the hg client process is closed. Otherwise, Windows
+        # may have trouble removing the repo directory because the process
+        # has an open handle on it.
+        for client in getattr(self, '_clients', []):
+            if client.server:
+                client.close()
+
+        self._clients[:] = []
+
+        super(TestMercurialRevisionFinder, self).tearDown()
+
+    def _client(self):
+        configs = (
+            'ui.username="Dummy User <dummy@example.com>"',
+        )
+
+        client = hglib.open(self.tmpdir, encoding='UTF-8',
+                            configs=configs)
+        self._clients.append(client)
+        return client
 
     def add(self, path):
-        c = hglib.open(self.tmpdir)
-        ensureParentDir(self.tmppath(path))
-        with open(self.tmppath(path), 'wb') as fh:
-            fh.write(path)
-        c.add(self.tmppath(path))
+        with self._client() as c:
+            ensureParentDir(self.tmppath(path))
+            with open(self.tmppath(path), 'wb') as fh:
+                fh.write(path)
+            c.add(self.tmppath(path))
 
     def do_check(self, pattern, result):
         do_check(self, self.finder, pattern, result)
 
     def _get_finder(self, *args, **kwargs):
-        return MercurialRevisionFinder(*args, **kwargs)
+        f = MercurialRevisionFinder(*args, **kwargs)
+        self._clients.append(f._client)
+        return f
 
     def test_default_revision(self):
         self.prepare_match_test()
-        c = hglib.open(self.tmpdir)
-        c.commit('initial commit')
+        with self._client() as c:
+            c.commit('initial commit')
+
         self.finder = self._get_finder(self.tmpdir)
         self.do_match_test()
 
         self.assertIsNone(self.finder.get('does-not-exist'))
         self.assertIsInstance(self.finder.get('bar'), MercurialFile)
 
     def test_old_revision(self):
-        c = hglib.open(self.tmpdir)
-        with open(self.tmppath('foo'), 'wb') as fh:
-            fh.write('foo initial')
-        c.add(self.tmppath('foo'))
-        c.commit('initial')
+        with self._client() as c:
+            with open(self.tmppath('foo'), 'wb') as fh:
+                fh.write('foo initial')
+            c.add(self.tmppath('foo'))
+            c.commit('initial')
 
-        with open(self.tmppath('foo'), 'wb') as fh:
-            fh.write('foo second')
-        with open(self.tmppath('bar'), 'wb') as fh:
-            fh.write('bar second')
-        c.add(self.tmppath('bar'))
-        c.commit('second')
-        # This wipes out the working directory, ensuring the finder isn't
-        # finding anything from the filesystem.
-        c.rawcommand(['update', 'null'])
+            with open(self.tmppath('foo'), 'wb') as fh:
+                fh.write('foo second')
+            with open(self.tmppath('bar'), 'wb') as fh:
+                fh.write('bar second')
+            c.add(self.tmppath('bar'))
+            c.commit('second')
+            # This wipes out the working directory, ensuring the finder isn't
+            # finding anything from the filesystem.
+            c.rawcommand(['update', 'null'])
 
         finder = self._get_finder(self.tmpdir, 0)
         f = finder.get('foo')
         self.assertEqual(f.read(), 'foo initial')
         self.assertEqual(f.read(), 'foo initial', 'read again for good measure')
         self.assertIsNone(finder.get('bar'))
 
-        finder = MercurialRevisionFinder(self.tmpdir, rev=1)
+        finder = self._get_finder(self.tmpdir, rev=1)
         f = finder.get('foo')
         self.assertEqual(f.read(), 'foo second')
         f = finder.get('bar')
         self.assertEqual(f.read(), 'bar second')
+        f = None
 
     def test_recognize_repo_paths(self):
-        c = hglib.open(self.tmpdir)
-        with open(self.tmppath('foo'), 'wb') as fh:
-            fh.write('initial')
-        c.add(self.tmppath('foo'))
-        c.commit('initial')
-        c.rawcommand(['update', 'null'])
+        with self._client() as c:
+            with open(self.tmppath('foo'), 'wb') as fh:
+                fh.write('initial')
+            c.add(self.tmppath('foo'))
+            c.commit('initial')
+            c.rawcommand(['update', 'null'])
 
         finder = self._get_finder(self.tmpdir, 0,
                                   recognize_repo_paths=True)
         with self.assertRaises(NotImplementedError):
             list(finder.find(''))
 
         with self.assertRaises(ValueError):
             finder.get('foo')
         with self.assertRaises(ValueError):
             finder.get('')
 
         f = finder.get(self.tmppath('foo'))
         self.assertIsInstance(f, MercurialFile)
         self.assertEqual(f.read(), 'initial')
+        f = None
 
 
 @unittest.skipUnless(MercurialNativeRevisionFinder, 'hgnative not available')
 class TestMercurialNativeRevisionFinder(TestMercurialRevisionFinder):
     def _get_finder(self, *args, **kwargs):
         return MercurialNativeRevisionFinder(*args, **kwargs)