Lei Zhang | 2cc3136 | 2021-10-22 00:30:43 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 2 | # Copyright 2020 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import unittest |
| 7 | |
| 8 | import PRESUBMIT |
| 9 | from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile |
| 10 | |
| 11 | |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 12 | class BannedTypeCheckTest(unittest.TestCase): |
| 13 | |
| 14 | def testBannedCppFunctions(self): |
| 15 | input_api = MockInputApi() |
| 16 | input_api.files = [ |
| 17 | MockFile('some/cpp/problematic/file.cc', ['using namespace std;']), |
| 18 | MockFile('third_party/some/cpp/problematic/file.cc', |
| 19 | ['using namespace std;']), |
| 20 | MockFile('some/cpp/ok/file.cc', ['using std::string;']), |
| 21 | MockFile('some/cpp/nocheck/file.cc', |
| 22 | ['using namespace std; // nocheck']), |
| 23 | MockFile('some/cpp/comment/file.cc', |
| 24 | [' // A comment about `using namespace std;`']), |
Daniel Hosseinian | d765538 | 2021-11-30 18:11:39 +0000 | [diff] [blame] | 25 | MockFile('some/cpp/v8/get-current.cc', ['v8::Isolate::GetCurrent()']), |
| 26 | MockFile('some/cpp/v8/try-get-current.cc', |
| 27 | ['v8::Isolate::TryGetCurrent()']), |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 28 | ] |
| 29 | |
| 30 | results = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi()) |
| 31 | |
Daniel Hosseinian | d765538 | 2021-11-30 18:11:39 +0000 | [diff] [blame] | 32 | # There are no warnings to test, so add an empty warning to keep the test |
| 33 | # extendable for the future. This block can be removed once warnings are |
| 34 | # added. |
| 35 | self.assertEqual(1, len(results)) |
| 36 | results.insert(0, MockOutputApi().PresubmitPromptWarning('')) |
| 37 | |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 38 | # warnings are results[0], errors are results[1] |
| 39 | self.assertEqual(2, len(results)) |
| 40 | self.assertTrue('some/cpp/problematic/file.cc' in results[1].message) |
| 41 | self.assertFalse( |
| 42 | 'third_party/some/cpp/problematic/file.cc' in results[1].message) |
| 43 | self.assertFalse('some/cpp/ok/file.cc' in results[1].message) |
| 44 | self.assertFalse('some/cpp/nocheck/file.cc' in results[0].message) |
| 45 | self.assertFalse('some/cpp/nocheck/file.cc' in results[1].message) |
| 46 | self.assertFalse('some/cpp/comment/file.cc' in results[0].message) |
| 47 | self.assertFalse('some/cpp/comment/file.cc' in results[1].message) |
Daniel Hosseinian | d765538 | 2021-11-30 18:11:39 +0000 | [diff] [blame] | 48 | self.assertTrue('some/cpp/v8/get-current.cc' in results[1].message) |
| 49 | self.assertTrue('some/cpp/v8/try-get-current.cc' in results[1].message) |
Daniel Hosseinian | b3bdbd4 | 2021-10-22 17:01:14 +0000 | [diff] [blame] | 50 | |
| 51 | |
Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 52 | class CheckChangeOnUploadTest(unittest.TestCase): |
| 53 | |
| 54 | def testCheckPNGFormat(self): |
| 55 | correct_paths = [ |
| 56 | 'test_expected.pdf.0.png', |
| 57 | 'test_expected_win.pdf.1.png', |
| 58 | 'test_expected_skia.pdf.2.png', |
| 59 | 'test_expected_skiapaths.pdf.3.png', |
| 60 | 'test_expected_skia_mac.pdf.4.png', |
| 61 | 'test_expected_skiapaths_win.pdf.5.png', |
| 62 | 'notpng.cc', # Check will be skipped for non-PNG files |
| 63 | ] |
| 64 | wrong_paths = [ |
| 65 | 'expected.pdf.0.png', # Missing '_expected' |
| 66 | 'test1_expected.0.png', # Missing '.pdf' |
| 67 | 'test2_expected.pdf.png', # Missing page number |
| 68 | 'test3_expected.pdf.x.png', # Wrong character for page number |
| 69 | 'test4_expected_mac_skia.pdf.0.png', # Wrong order of keywords |
| 70 | 'test5_expected_useskia.pdf.0.png', # Wrong keyword |
| 71 | ] |
| 72 | mock_input_api = MockInputApi() |
| 73 | mock_output_api = MockOutputApi() |
| 74 | mock_input_api.files = map(MockFile, correct_paths + wrong_paths) |
Lei Zhang | 2cc3136 | 2021-10-22 00:30:43 +0000 | [diff] [blame] | 75 | errors = list( |
| 76 | map(str, PRESUBMIT._CheckPNGFormat(mock_input_api, mock_output_api))) |
Hui Yingst | 2aa0a4a | 2020-04-09 19:04:21 +0000 | [diff] [blame] | 77 | |
| 78 | self.assertEqual(len(wrong_paths), len(errors)) |
| 79 | self.assertFalse('notpng.cc' in errors[0]) |
| 80 | for path, error in zip(wrong_paths, errors): |
| 81 | self.assertIn(path, error) |
| 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | unittest.main() |