blob: 41d2a3bfedf6b1a0a9136d2bfb82304584dbaf33 [file] [log] [blame]
Lei Zhang2cc31362021-10-22 00:30:43 +00001#!/usr/bin/env python3
Hui Yingst2aa0a4a2020-04-09 19:04:21 +00002# 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
6import unittest
7
8import PRESUBMIT
9from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile
10
11
Daniel Hosseinianb3bdbd42021-10-22 17:01:14 +000012class 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 Hosseiniand7655382021-11-30 18:11:39 +000025 MockFile('some/cpp/v8/get-current.cc', ['v8::Isolate::GetCurrent()']),
26 MockFile('some/cpp/v8/try-get-current.cc',
27 ['v8::Isolate::TryGetCurrent()']),
Daniel Hosseinianb3bdbd42021-10-22 17:01:14 +000028 ]
29
30 results = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
31
Daniel Hosseiniand7655382021-11-30 18:11:39 +000032 # 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 Hosseinianb3bdbd42021-10-22 17:01:14 +000038 # 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 Hosseiniand7655382021-11-30 18:11:39 +000048 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 Hosseinianb3bdbd42021-10-22 17:01:14 +000050
51
Hui Yingst2aa0a4a2020-04-09 19:04:21 +000052class 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 Zhang2cc31362021-10-22 00:30:43 +000075 errors = list(
76 map(str, PRESUBMIT._CheckPNGFormat(mock_input_api, mock_output_api)))
Hui Yingst2aa0a4a2020-04-09 19:04:21 +000077
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
84if __name__ == '__main__':
85 unittest.main()