pre-upload: Use _get_file_content in blank_line_check

The original design of blank_line_check uses the current content
of the file. This causes

1. Error when there is a CL removing the file which was changed in the
previous CL.
2. False negative when the latter CL deleted the blank line, while the
previous CL still had blank line.

Use _get_file_content to retrieve the content of the file in each
commit, so that the two issues above are resolved.

BUG=none
TEST=`repo upload` on CL:3139536 fails
TEST=./pre-upload_unittest.py

Change-Id: I267b3f8f267c6f53049065b9111aa5e75965b067
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/repohooks/+/3138163
Commit-Queue: Kuan-Yu Chen <edisonhello@google.com>
Tested-by: Kuan-Yu Chen <edisonhello@google.com>
Auto-Submit: Kuan-Yu Chen <edisonhello@google.com>
Reviewed-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-by: Alex Klein <saklein@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index c20b322..e7dfe21 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -517,8 +517,8 @@
                         excluded + COMMON_EXCLUDED_PATHS)
   errors = []
   for afile in files:
-    with open(afile, 'r') as f:
-      last_bytes = f.read()[-2:]
+    file_content = _get_file_content(afile, commit)
+    last_bytes = file_content[-2:]
     if last_bytes == '\n\n':
       errors.append(afile)
   if errors:
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py
index 44755d7..6dbc0af 100755
--- a/pre-upload_unittest.py
+++ b/pre-upload_unittest.py
@@ -245,14 +245,15 @@
   }
 
   @classmethod
-  def _open_side_effect(cls, name, *_args, **_kwargs):
-    return mock.mock_open(read_data=cls.MOCK_FILE_CONTENT[name])()
+  def _get_file_content(cls, path, _commit):
+    return cls.MOCK_FILE_CONTENT[path]
 
   def _run_check(self, files):
     self.PatchObject(pre_upload, '_get_affected_files', return_value=files)
-    with mock.patch('builtins.open', side_effect=self._open_side_effect):
-      failure = pre_upload._check_no_extra_blank_lines(
-          ProjectNamed('PROJECT'), 'COMMIT')
+    self.PatchObject(pre_upload, '_get_file_content',
+                     new=self._get_file_content)
+    failure = pre_upload._check_no_extra_blank_lines(
+        ProjectNamed('PROJECT'), 'COMMIT')
     return failure
 
   def test_good_cases(self):