xref: /aosp_15_r20/external/pdfium/PRESUBMIT_test.py (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1#!/usr/bin/env python3
2# Copyright 2020 The PDFium Authors
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
12class 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;`']),
25        MockFile('some/cpp/v8/get-current.cc', ['v8::Isolate::GetCurrent()']),
26        MockFile('some/cpp/v8/try-get-current.cc',
27                 ['v8::Isolate::TryGetCurrent()']),
28    ]
29
30    results = PRESUBMIT._CheckNoBannedFunctions(input_api, MockOutputApi())
31
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
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)
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)
50
51
52class CheckChangeOnUploadTest(unittest.TestCase):
53
54  def testCheckPngNames(self):
55    correct_paths = [
56        'test_expected.pdf.0.png',
57        'test_expected_win.pdf.1.png',
58        'test_expected_agg.pdf.3.png',
59        'test_expected_agg_linux.pdf.3.png',
60        'test_expected_skia.pdf.2.png',
61        'test_expected_skia_mac.pdf.4.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_linux_agg.pdf.0.png',  # Wrong order of keywords
70        'test4_expected_mac_skia.pdf.0.png',  # Wrong order of keywords
71        'test5_expected_useskia.pdf.0.png',  # Wrong keyword
72    ]
73    mock_input_api = MockInputApi()
74    mock_output_api = MockOutputApi()
75    mock_input_api.files = map(MockFile, correct_paths + wrong_paths)
76    errors = list(
77        map(str, PRESUBMIT._CheckPngNames(mock_input_api, mock_output_api)))
78
79    self.assertEqual(len(wrong_paths), len(errors))
80    self.assertFalse('notpng.cc' in errors[0])
81    for path, error in zip(wrong_paths, errors):
82      self.assertIn(path, error)
83
84
85if __name__ == '__main__':
86  unittest.main()
87