• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2024 The Chromium 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 os
7import sys
8import unittest
9
10import PRESUBMIT
11
12# Append chrome source root to import `PRESUBMIT_test_mocks.py`.
13sys.path.append(
14    os.path.dirname(
15        os.path.dirname(
16            os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))
17from PRESUBMIT_test_mocks import MockAffectedFile, MockInputApi, MockOutputApi
18
19_PARTITION_ALLOC_BASE_PATH = 'base/allocator/partition_allocator/src/'
20
21
22class PartitionAllocIncludeGuardsTest(unittest.TestCase):
23
24    def _CheckForIncludeGuardsWithMock(self, filename, lines):
25        mock_input_api = MockInputApi()
26        mock_input_api.files = [MockAffectedFile(filename, lines)]
27        mock_output_api = MockOutputApi()
28        return PRESUBMIT.CheckForIncludeGuards(mock_input_api, mock_output_api)
29
30    def testExpectedGuardNameDoesNotError(self):
31        lines = [
32            '#ifndef PARTITION_ALLOC_RANDOM_H_',
33            '#define PARTITION_ALLOC_RANDOM_H_',
34            '#endif  // PARTITION_ALLOC_RANDOM_H_'
35        ]
36        errors = self._CheckForIncludeGuardsWithMock(
37            _PARTITION_ALLOC_BASE_PATH + 'partition_alloc/random.h', lines)
38        self.assertEqual(0, len(errors))
39
40    def testMissingGuardErrors(self):
41        lines = []
42        errors = self._CheckForIncludeGuardsWithMock(
43            _PARTITION_ALLOC_BASE_PATH + 'partition_alloc/random.h', lines)
44        self.assertEqual(1, len(errors))
45        self.assertIn('Missing include guard', errors[0].message)
46        self.assertIn('Recommended name: PARTITION_ALLOC_RANDOM_H_',
47                      errors[0].message)
48
49    def testMissingGuardInNonHeaderFileDoesNotError(self):
50        lines = []
51        errors = self._CheckForIncludeGuardsWithMock(
52            _PARTITION_ALLOC_BASE_PATH + 'partition_alloc/random.cc', lines)
53        self.assertEqual(0, len(errors))
54
55    def testGuardNotCoveringWholeFileErrors(self):
56        lines = [
57            '#ifndef PARTITION_ALLOC_RANDOM_H_',
58            '#define PARTITION_ALLOC_RANDOM_H_',
59            '#endif  // PARTITION_ALLOC_RANDOM_H_',
60            'int oh_i_forgot_to_guard_this;'
61        ]
62        errors = self._CheckForIncludeGuardsWithMock(
63            _PARTITION_ALLOC_BASE_PATH + 'partition_alloc/random.h', lines)
64        self.assertEqual(1, len(errors))
65        self.assertIn('not covering the whole file', errors[0].message)
66
67    def testMissingDefineInGuardErrors(self):
68        lines = [
69            '#ifndef PARTITION_ALLOC_RANDOM_H_',
70            'int somehow_put_here;'
71            '#define PARTITION_ALLOC_RANDOM_H_',
72            '#endif  // PARTITION_ALLOC_RANDOM_H_',
73        ]
74        errors = self._CheckForIncludeGuardsWithMock(
75            _PARTITION_ALLOC_BASE_PATH + 'partition_alloc/random.h', lines)
76        self.assertEqual(1, len(errors))
77        self.assertIn(
78            'Missing "#define PARTITION_ALLOC_RANDOM_H_" for include guard',
79            errors[0].message)
80
81
82if __name__ == '__main__':
83    unittest.main()
84