pre-upload: Add a check for FilePath::CharType stuff

It's safe to assume FilePath::CharType == char in places like
platform2 where we don't have to worry about Windows.

With this assumption, the code can be simplified:

- constexpr base::FilePath::CharType kRunContainersPath[] =
-     FILE_PATH_LITERAL("/run/containers");
+ constexpr char kRunContainersPath[] = "/run/containers";

BUG=chromium:870621
TEST=./pre-upload_unittest.py

Change-Id: I6c96fd3d231ba5d91f074d0c84a28b2a11528af5
Reviewed-on: https://chromium-review.googlesource.com/1163354
Commit-Ready: Satoru Takabayashi <satorux@google.com>
Tested-by: Satoru Takabayashi <satorux@google.com>
Reviewed-by: Dan Erat <derat@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index 4c7b5a3..5f87a8e 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -1441,6 +1441,27 @@
                        % (project_name, project_name))
 
 
+def _check_filepath_chartype(_project, commit):
+  """Checks that FilePath::CharType stuff is not used."""
+
+  FILEPATH_REGEXP = re.compile('|'.join(
+      [r'(?:base::)?FilePath::(?:Char|String|StringPiece)Type',
+       r'FILE_PATH_LITERAL']))
+  files = _filter_files(_get_affected_files(commit, relative=True),
+                        [r'.*\.(cc|h)$'])
+
+  errors = []
+  for afile in files:
+    for line_num, line in _get_file_diff(afile, commit):
+      m = re.search(FILEPATH_REGEXP, line)
+      if m:
+        errors.append('%s, line %s has %s' % (afile, line_num, m.group(0)))
+
+  if errors:
+    msg = 'Please assume FilePath::CharType is char (crbug.com/870621):'
+    return HookFailure(msg, errors)
+
+
 def _check_exec_files(_project, commit):
   """Make +x bits on files."""
   # List of files that should never be +x.
@@ -1575,6 +1596,7 @@
     'manifest_check': _check_manifests,
     'contribution_check': _check_change_is_contribution,
     'project_prefix_check': _check_project_prefix,
+    'filepath_chartype_check': _check_filepath_chartype,
 }
 
 
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py
index 0f36910..d97f506 100755
--- a/pre-upload_unittest.py
+++ b/pre-upload_unittest.py
@@ -189,6 +189,40 @@
         pre_upload._check_project_prefix(ProjectNamed('PROJECT'), 'COMMIT'))
 
 
+class CheckFilePathCharTypeTest(cros_test_lib.MockTestCase):
+  """Tests for _check_filepath_chartype."""
+
+  def setUp(self):
+    self.diff_mock = self.PatchObject(pre_upload, '_get_file_diff')
+
+  def testCheck(self):
+    self.PatchObject(pre_upload, '_get_affected_files', return_value=['x.cc'])
+    self.diff_mock.return_value = [
+        (1, 'base::FilePath'),                   # OK
+        (2, 'base::FilePath::CharType'),         # NG
+        (3, 'base::FilePath::StringType'),       # NG
+        (4, 'base::FilePath::StringPieceType'),  # NG
+        (5, 'FilePath::CharType'),               # NG
+        (6, 'FilePath::StringType'),             # NG
+        (7, 'FilePath::StringPieceType'),        # NG
+        (8, 'FILE_PATH_LITERAL'),                # NG
+    ]
+    failure = pre_upload._check_filepath_chartype(ProjectNamed('PROJECT'),
+                                                  'COMMIT')
+    self.assertTrue(failure)
+    self.assertEquals(
+        'Please assume FilePath::CharType is char (crbug.com/870621):',
+        failure.msg)
+    self.assertEquals(['x.cc, line 2 has base::FilePath::CharType',
+                       'x.cc, line 3 has base::FilePath::StringType',
+                       'x.cc, line 4 has base::FilePath::StringPieceType',
+                       'x.cc, line 5 has FilePath::CharType',
+                       'x.cc, line 6 has FilePath::StringType',
+                       'x.cc, line 7 has FilePath::StringPieceType',
+                       'x.cc, line 8 has FILE_PATH_LITERAL'],
+                      failure.items)
+
+
 class CheckKernelConfig(cros_test_lib.MockTestCase):
   """Tests for _kernel_configcheck."""