git cl: Print a clear error message when we fail to find the remote url.

Bug: 914655
Change-Id: I06a3ff39d884327fd407a5c50cc9730a375bef0b
Reviewed-on: https://chromium-review.googlesource.com/c/1382974
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@chromium.org>
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 841a77a..f71a5d5 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -3050,6 +3050,64 @@
     self.assertEqual(cl.GetRemoteUrl(), url)
     self.assertEqual(cl.GetRemoteUrl(), url)  # Must be cached.
 
+  def test_get_remote_url_non_existing_mirror(self):
+    original_os_path_isdir = os.path.isdir
+    def selective_os_path_isdir_mock(path):
+      if path == '/cache/this-dir-doesnt-exist':
+        return self._mocked_call('os.path.isdir', path)
+      return original_os_path_isdir(path)
+    self.mock(os.path, 'isdir', selective_os_path_isdir_mock)
+    self.mock(logging, 'error',
+              lambda fmt, *a: self._mocked_call('logging.error', fmt % a))
+
+    self.calls = [
+      ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
+      ((['git', 'config', 'branch.master.merge'],), 'master'),
+      ((['git', 'config', 'branch.master.remote'],), 'origin'),
+      ((['git', 'config', 'remote.origin.url'],),
+       '/cache/this-dir-doesnt-exist'),
+      (('os.path.isdir', '/cache/this-dir-doesnt-exist'),
+       False),
+      (('logging.error',
+        'Remote "origin" for branch "/cache/this-dir-doesnt-exist" points to'
+        ' "master", but it doesn\'t exist.'), None),
+    ]
+    cl = git_cl.Changelist(codereview='gerrit', issue=1)
+    self.assertIsNone(cl.GetRemoteUrl())
+
+  def test_get_remote_url_misconfigured_mirror(self):
+    original_os_path_isdir = os.path.isdir
+    def selective_os_path_isdir_mock(path):
+      if path == '/cache/this-dir-exists':
+        return self._mocked_call('os.path.isdir', path)
+      return original_os_path_isdir(path)
+    self.mock(os.path, 'isdir', selective_os_path_isdir_mock)
+    self.mock(logging, 'error',
+              lambda *a: self._mocked_call('logging.error', *a))
+
+    self.calls = [
+      ((['git', 'symbolic-ref', 'HEAD'],), 'master'),
+      ((['git', 'config', 'branch.master.merge'],), 'master'),
+      ((['git', 'config', 'branch.master.remote'],), 'origin'),
+      ((['git', 'config', 'remote.origin.url'],),
+       '/cache/this-dir-exists'),
+      (('os.path.isdir', '/cache/this-dir-exists'), True),
+      # Runs in /cache/this-dir-exists.
+      ((['git', 'config', 'remote.origin.url'],), ''),
+      (('logging.error',
+        'Remote "%(remote)s" for branch "%(branch)s" points to '
+        '"%(cache_path)s", but it is misconfigured.\n'
+        '"%(cache_path)s" must be a git repo and must have a remote named '
+        '"%(remote)s" pointing to the git host.', {
+              'remote': 'origin',
+              'cache_path': '/cache/this-dir-exists',
+              'branch': 'master'}
+        ), None),
+    ]
+    cl = git_cl.Changelist(codereview='gerrit', issue=1)
+    self.assertIsNone(cl.GetRemoteUrl())
+
+
   def test_gerrit_change_identifier_with_project(self):
     self.calls = [
       ((['git', 'symbolic-ref', 'HEAD'],), 'master'),