gclient_utils: Add temporary_file method.

Useful when passing information to a subcommand via a temporary file.

Bug: 1051631
Change-Id: I0b8deda921effd24a37109544e1e7ca22e00ba4e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2068942
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 4c18182..8378ee3 100755
--- a/tests/git_cl_test.py
+++ b/tests/git_cl_test.py
@@ -32,7 +32,9 @@
 # We have to disable monitoring before importing git_cl.
 metrics.DISABLE_METRICS_COLLECTION = True
 
+import contextlib
 import clang_format
+import gclient_utils
 import gerrit_util
 import git_cl
 import git_common
@@ -55,26 +57,14 @@
 CERR1 = callError(1)
 
 
-def MakeNamedTemporaryFileMock(test, expected_content):
-  class NamedTemporaryFileMock(object):
-    def __init__(self, *args, **kwargs):
-      self.name = '/tmp/named'
-      self.expected_content = expected_content.encode('utf-8', 'replace')
+class TemporaryFileMock(object):
+  def __init__(self):
+    self.suffix = 0
 
-    def __enter__(self):
-      return self
-
-    def __exit__(self, _type, _value, _tb):
-      pass
-
-    def write(self, content):
-      if self.expected_content:
-        test.assertEqual(self.expected_content, content)
-
-    def close(self):
-      pass
-
-  return NamedTemporaryFileMock
+  @contextlib.contextmanager
+  def __call__(self):
+    self.suffix += 1
+    yield '/tmp/fake-temp' + str(self.suffix)
 
 
 class ChangelistMock(object):
@@ -897,8 +887,9 @@
       calls += [
         ((['git', 'rev-parse', 'HEAD:'],),  # `HEAD:` means HEAD's tree hash.
          '0123456789abcdef'),
+        ((['FileWrite', '/tmp/fake-temp1', description],), None),
         ((['git', 'commit-tree', '0123456789abcdef', '-p', parent,
-           '-F', '/tmp/named'],),
+           '-F', '/tmp/fake-temp1'],),
          ref_to_push),
       ]
     else:
@@ -1178,9 +1169,7 @@
         change_id=change_id)
     if fetched_status != 'ABANDONED':
       mock.patch(
-          'tempfile.NamedTemporaryFile',
-          MakeNamedTemporaryFileMock(
-              self, expected_content=description)).start()
+          'gclient_utils.temporary_file', TemporaryFileMock()).start()
       mock.patch('os.remove', return_value=True).start()
       self.calls += self._gerrit_upload_calls(
           description, reviewers, squash,