gclient_scm: Fix tests on windows

Change-Id: I649bee199e52ecbd66467cfaf850a7a57e2eedf6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1874506
Reviewed-by: Anthony Polito <apolito@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/tests/gclient_scm_test.py b/tests/gclient_scm_test.py
index e5f1fcd..9b53902 100755
--- a/tests/gclient_scm_test.py
+++ b/tests/gclient_scm_test.py
@@ -7,7 +7,9 @@
 
 # pylint: disable=E1103
 
-from shutil import rmtree
+from __future__ import unicode_literals
+
+
 from subprocess import Popen, PIPE, STDOUT
 
 import json
@@ -30,9 +32,13 @@
 from testing_support import test_case_utils
 
 import gclient_scm
+import gclient_utils
 import git_cache
 import subprocess2
 
+
+GIT = 'git' if sys.platform != 'win32' else 'git.bat'
+
 # Disable global git cache
 git_cache.Mirror.SetCachePath(None)
 
@@ -168,26 +174,26 @@
   def CreateGitRepo(git_import, path):
     """Do it for real."""
     try:
-      Popen(['git', 'init', '-q'], stdout=PIPE, stderr=STDOUT,
+      Popen([GIT, 'init', '-q'], stdout=PIPE, stderr=STDOUT,
             cwd=path).communicate()
     except OSError:
       # git is not available, skip this test.
       return False
-    Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE,
+    Popen([GIT, 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate(input=git_import.encode())
-    Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT,
+    Popen([GIT, 'checkout', '-q'], stdout=PIPE, stderr=STDOUT,
         cwd=path).communicate()
-    Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
+    Popen([GIT, 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate()
-    Popen(['git', 'checkout', '-b', 'new', 'origin/master', '-q'], stdout=PIPE,
+    Popen([GIT, 'checkout', '-b', 'new', 'origin/master', '-q'], stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate()
-    Popen(['git', 'push', 'origin', 'origin/origin:origin/master', '-q'],
+    Popen([GIT, 'push', 'origin', 'origin/origin:origin/master', '-q'],
         stdout=PIPE, stderr=STDOUT, cwd=path).communicate()
-    Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE,
+    Popen([GIT, 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate()
-    Popen(['git', 'config', 'user.email', 'someuser@chromium.org'], stdout=PIPE,
+    Popen([GIT, 'config', 'user.email', 'someuser@chromium.org'], stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate()
-    Popen(['git', 'config', 'user.name', 'Some User'], stdout=PIPE,
+    Popen([GIT, 'config', 'user.name', 'Some User'], stdout=PIPE,
         stderr=STDOUT, cwd=path).communicate()
     return True
 
@@ -212,7 +218,7 @@
                staticmethod(lambda : True)).start()
     mock.patch('sys.stdout', StringIO()).start()
     self.addCleanup(mock.patch.stopall)
-    self.addCleanup(lambda: rmtree(self.root_dir))
+    self.addCleanup(gclient_utils.rmtree, self.root_dir)
 
 
 class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
@@ -282,7 +288,7 @@
     file_path = join(self.base_path, 'c')
     with open(file_path, 'w') as f:
       f.writelines('new\n')
-    Popen(['git', 'add', 'c'], stdout=PIPE,
+    Popen([GIT, 'add', 'c'], stdout=PIPE,
           stderr=STDOUT, cwd=self.base_path).communicate()
     file_list = []
     scm.revert(options, self.args, file_list)
@@ -363,8 +369,9 @@
     # parents instead.
     self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
                      'd2e35c10ac24d6c621e14a1fcadceb533155627d')
-    self.assertEqual(scm._Capture(['rev-parse', 'HEAD^1']), rev)
-    self.assertEqual(scm._Capture(['rev-parse', 'HEAD^2']),
+    parent = 'HEAD^' if sys.platform != 'win32' else 'HEAD^^'
+    self.assertEqual(scm._Capture(['rev-parse', parent + '1']), rev)
+    self.assertEqual(scm._Capture(['rev-parse', parent + '2']),
                      scm._Capture(['rev-parse', 'origin/master']))
     sys.stdout.close()
 
@@ -387,7 +394,8 @@
     # parent instead.
     self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
                      'd2e35c10ac24d6c621e14a1fcadceb533155627d')
-    self.assertEqual(scm._Capture(['rev-parse', 'HEAD^']),
+    parent = 'HEAD^' if sys.platform != 'win32' else 'HEAD^^'
+    self.assertEqual(scm._Capture(['rev-parse', parent + '1']),
                      scm._Capture(['rev-parse', 'origin/master']))
     sys.stdout.close()
 
@@ -547,41 +555,6 @@
     rev_info = scm.revinfo(options, (), None)
     self.assertEqual(rev_info, '069c602044c5388d2d15c3f875b057c852003458')
 
-  def testMirrorPushUrl(self):
-    if not self.enabled:
-      return
-    fakes = fake_repos.FakeRepos()
-    fakes.set_up_git()
-    self.url = fakes.git_base + 'repo_1'
-    self.root_dir = fakes.root_dir
-    self.addCleanup(fake_repos.FakeRepos.tear_down_git, fakes)
-
-    mirror = tempfile.mkdtemp()
-    self.addCleanup(rmtree, mirror)
-
-    # This should never happen, but if it does, it'd render the other assertions
-    # in this test meaningless.
-    self.assertFalse(self.url.startswith(mirror))
-
-    git_cache.Mirror.SetCachePath(mirror)
-    self.addCleanup(git_cache.Mirror.SetCachePath, None)
-
-    options = self.Options()
-    scm = gclient_scm.GitWrapper(self.url, self.root_dir, self.relpath)
-    self.assertIsNotNone(scm._GetMirror(self.url, options))
-    scm.update(options, (), [])
-
-    fetch_url = scm._Capture(['remote', 'get-url', 'origin'])
-    self.assertTrue(
-        fetch_url.startswith(mirror),
-        msg='\n'.join([
-            'Repository fetch url should be in the git cache mirror directory.',
-            '  fetch_url: %s' % fetch_url,
-            '  mirror:    %s' % mirror]))
-    push_url = scm._Capture(['remote', 'get-url', '--push', 'origin'])
-    self.assertEqual(push_url, self.url)
-    sys.stdout.close()
-
 
 class ManagedGitWrapperTestCaseMock(unittest.TestCase):
   class OptionsObject(object):
@@ -736,6 +709,8 @@
     options = self.Options()
 
     origin_root_dir = self.root_dir
+    self.addCleanup(gclient_utils.rmtree, origin_root_dir)
+
     self.root_dir = tempfile.mkdtemp()
     self.relpath = '.'
     self.base_path = join(self.root_dir, self.relpath)
@@ -758,7 +733,6 @@
     self.checkInStdout(
       'Checked out refs/remotes/origin/master to a detached HEAD')
 
-    rmtree(origin_root_dir)
 
   def testUpdateCloneOnCommit(self):
     if not self.enabled:
@@ -766,6 +740,8 @@
     options = self.Options()
 
     origin_root_dir = self.root_dir
+    self.addCleanup(gclient_utils.rmtree, origin_root_dir)
+
     self.root_dir = tempfile.mkdtemp()
     self.relpath = '.'
     self.base_path = join(self.root_dir, self.relpath)
@@ -790,14 +766,14 @@
     self.checkInStdout(
       'Checked out a7142dc9f0009350b96a11f372b6ea658592aa95 to a detached HEAD')
 
-    rmtree(origin_root_dir)
-
   def testUpdateCloneOnBranch(self):
     if not self.enabled:
       return
     options = self.Options()
 
     origin_root_dir = self.root_dir
+    self.addCleanup(gclient_utils.rmtree, origin_root_dir)
+
     self.root_dir = tempfile.mkdtemp()
     self.relpath = '.'
     self.base_path = join(self.root_dir, self.relpath)
@@ -823,14 +799,14 @@
         'Checked out 9a51244740b25fa2ded5252ca00a3178d3f665a9 '
         'to a detached HEAD')
 
-    rmtree(origin_root_dir)
-
   def testUpdateCloneOnFetchedRemoteBranch(self):
     if not self.enabled:
       return
     options = self.Options()
 
     origin_root_dir = self.root_dir
+    self.addCleanup(gclient_utils.rmtree, origin_root_dir)
+
     self.root_dir = tempfile.mkdtemp()
     self.relpath = '.'
     self.base_path = join(self.root_dir, self.relpath)
@@ -855,14 +831,14 @@
     self.checkInStdout(
       'Checked out refs/remotes/origin/feature to a detached HEAD')
 
-    rmtree(origin_root_dir)
-
   def testUpdateCloneOnTrueRemoteBranch(self):
     if not self.enabled:
       return
     options = self.Options()
 
     origin_root_dir = self.root_dir
+    self.addCleanup(gclient_utils.rmtree, origin_root_dir)
+
     self.root_dir = tempfile.mkdtemp()
     self.relpath = '.'
     self.base_path = join(self.root_dir, self.relpath)
@@ -894,8 +870,6 @@
     self.checkInStdout(
       'Checked out refs/remotes/origin/feature to a detached HEAD')
 
-    rmtree(origin_root_dir)
-
   def testUpdateUpdate(self):
     if not self.enabled:
       return
@@ -933,10 +907,8 @@
     mock.patch('gclient_scm.CipdRoot.clobber').start()
     mock.patch('gclient_scm.CipdRoot.ensure').start()
     self.addCleanup(mock.patch.stopall)
-
-  def tearDown(self):
-    rmtree(self._cipd_root_dir)
-    rmtree(self._workdir)
+    self.addCleanup(gclient_utils.rmtree, self._cipd_root_dir)
+    self.addCleanup(gclient_utils.rmtree, self._workdir)
 
   def createScmWithPackageThatSatisfies(self, condition):
     return gclient_scm.CipdWrapper(
@@ -1061,11 +1033,13 @@
     self.options = BaseGitWrapperTestCase.OptionsObject()
     self.url = self.git_base + 'repo_1'
     self.mirror = None
+    mock.patch('sys.stdout').start()
+    self.addCleanup(mock.patch.stopall)
 
   def setUpMirror(self):
     self.mirror = tempfile.mkdtemp()
     git_cache.Mirror.SetCachePath(self.mirror)
-    self.addCleanup(rmtree, self.mirror)
+    self.addCleanup(gclient_utils.rmtree, self.mirror)
     self.addCleanup(git_cache.Mirror.SetCachePath, None)
 
   def assertCommits(self, commits):
@@ -1108,6 +1082,25 @@
     self.setUpMirror()
     self.testCanSyncToGerritChange()
 
+  def testMirrorPushUrl(self):
+    self.setUpMirror()
+
+    scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
+    file_list = []
+    self.assertIsNotNone(scm._GetMirror(self.url, self.options))
+
+    scm.update(self.options, None, file_list)
+
+    fetch_url = scm._Capture(['remote', 'get-url', 'origin'])
+    self.assertTrue(
+        fetch_url.startswith(self.mirror),
+        msg='\n'.join([
+            'Repository fetch url should be in the git cache mirror directory.',
+            '  fetch_url: %s' % fetch_url,
+            '  mirror:    %s' % self.mirror]))
+    push_url = scm._Capture(['remote', 'get-url', '--push', 'origin'])
+    self.assertEqual(push_url, self.url)
+
   def testAppliesPatchOnTopOfMasterByDefault(self):
     """Test the default case, where we apply a patch on top of master."""
     scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')