Add a presubmit check for ARC compile guards in new .mm files for iOS.
Implements a new presubmit check that verifies that newly created .mm
files have a proper compile guard.
For example:
$ touch ios/asdf.mm
$ git commit -am "asdf" && git cl upload
Running presubmit upload checks ...
** Presubmit ERRORS **
Found new Objective-C implementation file without compile guard. Please use the following compile guard:
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
ios/asdf.mm
Bug: 762461
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ice720c8ddf5db2f11b12e56c0877619d612fa9ef
Reviewed-on: https://chromium-review.googlesource.com/844696
Commit-Queue: Stepan Khapugin <[email protected]>
Reviewed-by: Sylvain Defresne <[email protected]>
Cr-Commit-Position: refs/heads/master@{#526292}
diff --git a/ios/PRESUBMIT_test.py b/ios/PRESUBMIT_test.py
index e50ff3d..69c9e91 100644
--- a/ios/PRESUBMIT_test.py
+++ b/ios/PRESUBMIT_test.py
@@ -8,9 +8,57 @@
import PRESUBMIT
+ARC_COMPILE_GUARD = '''\
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+'''
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import PRESUBMIT_test_mocks
+class CheckARCCompilationGuardTest(unittest.TestCase):
+ """Test the _CheckARCCompilationGuard presubmit check."""
+
+ def testGoodImplementationFiles(self):
+ """Test that .m and .mm files with a guard don't raise any errors."""
+ text = "foobar \n" + ARC_COMPILE_GUARD
+ mock_input = PRESUBMIT_test_mocks.MockInputApi()
+ mock_input.files = [
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', text),
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', text),
+ ]
+ mock_output = PRESUBMIT_test_mocks.MockOutputApi()
+ errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
+ self.assertEqual(len(errors), 0)
+
+ def testBadImplementationFiles(self):
+ """Test that .m and .mm files without a guard raise an error."""
+ text = "foobar \n"
+ mock_input = PRESUBMIT_test_mocks.MockInputApi()
+ mock_input.files = [
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', text),
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.m', text),
+ ]
+ mock_output = PRESUBMIT_test_mocks.MockOutputApi()
+ errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
+ self.assertEqual(len(errors), 1)
+ self.assertEqual('error', errors[0].type)
+ self.assertTrue('ios/path/foo_controller.m' in errors[0].message)
+ self.assertTrue('ios/path/foo_controller.mm' in errors[0].message)
+
+ def testOtherFiles(self):
+ """Test that other files without a guard don't raise errors."""
+ text = "foobar \n"
+ mock_input = PRESUBMIT_test_mocks.MockInputApi()
+ mock_input.files = [
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.h', text),
+ PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.cc', text),
+ PRESUBMIT_test_mocks.MockFile('ios/path/BUILD.gn', text),
+ ]
+ mock_output = PRESUBMIT_test_mocks.MockOutputApi()
+ errors = PRESUBMIT._CheckARCCompilationGuard(mock_input, mock_output)
+ self.assertEqual(len(errors), 0)
class CheckTODOFormatTest(unittest.TestCase):
"""Test the _CheckBugInToDo presubmit check."""