depot_tools: Restore appending title before change description.

On an initial upload, git-cl upload -t <title> should append <title>
before the change description.

This broke because options.squash is set to a default value very late
into code execution, so its value was not available when computing the
initial description.

options.squash is now set when parsing the options, early into code
execution.

Bug: 1064905
Change-Id: I04a18c572dd5b04a3013d39f71bc43d026ea85d3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2122324
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/tests/git_cl_test.py b/tests/git_cl_test.py
index 721719f..521a097 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -658,6 +658,7 @@
     super(TestGitCl, self).setUp()
     self.calls = []
     self._calls_done = []
+    self.failed = False
     mock.patch('sys.stdout', StringIO()).start()
     mock.patch(
         'git_cl.time_time',
@@ -734,7 +735,8 @@
 
   def tearDown(self):
     try:
-      self.assertEqual([], self.calls)
+      if not self.failed:
+        self.assertEqual([], self.calls)
     except AssertionError:
       calls = ''.join('  %s\n' % str(call) for call in self.calls[:5])
       if len(self.calls) > 5:
@@ -771,6 +773,7 @@
           (prior_calls, len(self._calls_done), expected_args,
            len(self._calls_done), args, following_calls))
 
+      self.failed = True
       self.fail('@%d\n'
                 '  Expected: %r\n'
                 '  Actual:   %r\n'
@@ -1164,6 +1167,7 @@
       final_description=None,
       gitcookies_exists=True,
       force=False,
+      log_description=None,
       edit_description=None,
       fetched_description=None):
     """Generic gerrit upload test framework."""
@@ -1211,7 +1215,8 @@
               lambda path: self._mocked_call(['os.path.isfile', path])).start()
     mock.patch('git_cl.Changelist.GitSanityChecks', return_value=True).start()
     mock.patch(
-        'git_cl._create_description_from_log', return_value=description).start()
+        'git_cl._create_description_from_log',
+        return_value=log_description or description).start()
     mock.patch(
         'git_cl.Changelist._AddChangeIdToCommitMessage',
         return_value=post_amend_description or description).start()
@@ -1319,7 +1324,7 @@
         short_hostname='other',
         change_id='I123456789')
 
-  def test_gerrit_patchset_title_special_chars(self):
+  def test_gerrit_patchset_title_special_chars_nosquash(self):
     self._run_gerrit_upload_test(
         ['-f', '-t', 'We\'ll escape ^_ ^ special chars...@{u}'],
         'desc ✔\n\nBUG=\n\nChange-Id: I123456789',
@@ -1398,6 +1403,16 @@
         squash=True,
         change_id='123456789')
 
+  def test_gerrit_upload_squash_first_title(self):
+    self._run_gerrit_upload_test(
+        ['-f', '-t', 'title'],
+        'title\n\ndesc\n\nChange-Id: 123456789',
+        [],
+        force=True,
+        squash=True,
+        log_description='desc',
+        change_id='123456789')
+
   def test_gerrit_upload_squash_first_with_labels(self):
     self._run_gerrit_upload_test(
         ['--squash', '--cq-dry-run', '--enable-auto-submit'],
@@ -3469,6 +3484,10 @@
     mock.patch('git_cl._fetch_tryjobs').start()
     mock.patch('git_cl._trigger_tryjobs', return_value={}).start()
     mock.patch('git_cl.Changelist.CMDUpload', return_value=0).start()
+    mock.patch('git_cl.Settings.GetRoot', return_value='').start()
+    mock.patch(
+        'git_cl.Settings.GetSquashGerritUploads',
+        return_value=True).start()
     self.addCleanup(mock.patch.stopall)
 
   def testWarmUpChangeDetailCache(self):