Add tests for 'ForTesting' presubmit check
The root PRESUBMIT.py contains
_CheckNoProductionCodeUsingTestOnlyFunctions which checks against
including a call to a 'for testing only' method in production code.
That check has not been tested. This CL adds two tests for it.
Additionally, the CL also fixes two issues in the presubmit tests:
* FilterSourceFile in PRESUBMIT_test_mocks.py did not match the
production version from tools/depot_tools/presubmit_support.py: the
production requires the filename to be in the whitelist, while the
mock version was OK with just not being in the blacklist. The CL
modifies the mock version to match the production.
* The test for _CheckAndroidTestJUnitInheritance did not adhere to
the whitelisted filename pattern (*Test.java). This CL changes the
names of the fake files to match the pattern.
Bug: 821981
Change-Id: I65edf07ddb2ae26ad7d08ceb7cf4d51b482b5e56
Reviewed-on: https://chromium-review.googlesource.com/966605
Reviewed-by: Daniel Cheng <[email protected]>
Commit-Queue: Vaclav Brozek <[email protected]>
Cr-Commit-Position: refs/heads/master@{#543783}
diff --git a/PRESUBMIT_test.py b/PRESUBMIT_test.py
index b83d58ad..5aa17a1 100755
--- a/PRESUBMIT_test.py
+++ b/PRESUBMIT_test.py
@@ -1009,11 +1009,11 @@
'public class IncorrectTest extends TestCase {',
'}',
]),
- MockAffectedFile('IncorrectTestWithInterface.java', [
+ MockAffectedFile('IncorrectWithInterfaceTest.java', [
'public class Test implements X extends BaseClass {',
'}',
]),
- MockAffectedFile('IncorrectTestMultiLine.java', [
+ MockAffectedFile('IncorrectMultiLineTest.java', [
'public class Test implements X, Y, Z',
' extends TestBase {',
'}',
@@ -1029,11 +1029,11 @@
% (3, len(msgs[0].items), msgs[0].items))
self.assertTrue('IncorrectTest.java:1' in msgs[0].items,
'IncorrectTest not found in errors')
- self.assertTrue('IncorrectTestWithInterface.java:1'
+ self.assertTrue('IncorrectWithInterfaceTest.java:1'
in msgs[0].items,
- 'IncorrectTestWithInterface not found in errors')
- self.assertTrue('IncorrectTestMultiLine.java:2' in msgs[0].items,
- 'IncorrectTestMultiLine not found in errors')
+ 'IncorrectWithInterfaceTest not found in errors')
+ self.assertTrue('IncorrectMultiLineTest.java:2' in msgs[0].items,
+ 'IncorrectMultiLineTest not found in errors')
class LogUsageTest(unittest.TestCase):
@@ -1488,5 +1488,38 @@
self.assertTrue('some/mac/file.mm' not in errors[0].message)
+class NoProductionCodeUsingTestOnlyFunctions(unittest.TestCase):
+ def testTruePositives(self):
+ mock_input_api = MockInputApi()
+ mock_input_api.files = [
+ MockFile('some/path/foo.cc', ['foo_for_testing();']),
+ MockFile('some/path/foo.mm', ['FooForTesting();']),
+ MockFile('some/path/foo.cxx', ['FooForTests();']),
+ MockFile('some/path/foo.cpp', ['foo_for_test();']),
+ ]
+
+ results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctions(
+ mock_input_api, MockOutputApi())
+ self.assertEqual(1, len(results))
+ self.assertEqual(4, len(results[0].items))
+ self.assertTrue('foo.cc' in results[0].items[0])
+ self.assertTrue('foo.mm' in results[0].items[1])
+ self.assertTrue('foo.cxx' in results[0].items[2])
+ self.assertTrue('foo.cpp' in results[0].items[3])
+
+ def testFalsePositives(self):
+ mock_input_api = MockInputApi()
+ mock_input_api.files = [
+ MockFile('some/path/foo.h', ['foo_for_testing();']),
+ MockFile('some/path/foo.mm', ['FooForTesting() {']),
+ MockFile('some/path/foo.cc', ['::FooForTests();']),
+ MockFile('some/path/foo.cpp', ['// foo_for_test();']),
+ ]
+
+ results = PRESUBMIT._CheckNoProductionCodeUsingTestOnlyFunctions(
+ mock_input_api, MockOutputApi())
+ self.assertEqual(0, len(results))
+
+
if __name__ == '__main__':
unittest.main()