blob: 33023d7b4bebd6be84291c6424e3ecf3d7c24c64 [file] [log] [blame]
Laurent Chavey47eeccc2020-10-14 10:09:41 +09001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Unit tests for blocked_terms.txt and unblocked_terms.txt.
8
9 Implements unit tests for the blocked terms and unblocked terms
10 regex processed by pre-upload.py.
11"""
12
13from __future__ import print_function
14
15import os
16import sys
17
18
19# pylint: disable=W0212
Laurent Chavey48b7ef32020-11-10 20:01:10 +090020# We access private members of the pre_upload module.
Laurent Chavey47eeccc2020-10-14 10:09:41 +090021
22# Make sure we can find the chromite paths.
23sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
24 '..', '..'))
25
26# The sys.path monkey patching confuses the linter.
27# pylint: disable=wrong-import-position
28from chromite.lib import cros_test_lib
Laurent Chavey47eeccc2020-10-14 10:09:41 +090029
30
31assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
32
33
34pre_upload = __import__('pre-upload')
35
36
37class CheckFilesTest(cros_test_lib.MockTestCase, cros_test_lib.TempDirTestCase):
38 """Tests for blocked and unblocked files."""
39
40 DIFF = 'diff'
41 MATCH = 'match'
Laurent Chavey48b7ef32020-11-10 20:01:10 +090042 common_file_terms = None
43 unblocked_file_terms = None
44 hooks_dir = None
Laurent Chavey47eeccc2020-10-14 10:09:41 +090045
Laurent Chavey48b7ef32020-11-10 20:01:10 +090046 @classmethod
47 def setUpClass(cls):
Laurent Chavey625fbeb2020-11-11 16:17:56 +090048 """Initialize the class instance mocks."""
49 cls.hooks_dir = pre_upload._get_hooks_dir()
50 cls.common_file_terms = pre_upload._read_terms_file(
51 os.path.join(cls.hooks_dir, pre_upload.BLOCKED_TERMS_FILE))
52 cls.unblocked_file_terms = pre_upload._read_terms_file(
53 os.path.join(cls.hooks_dir, pre_upload.UNBLOCKED_TERMS_FILE))
Laurent Chavey47eeccc2020-10-14 10:09:41 +090054
Laurent Chavey48b7ef32020-11-10 20:01:10 +090055 def setUp(self):
56 """Initialize the test instance mockers."""
57 pre_upload.CACHE.clear()
Laurent Chavey47eeccc2020-10-14 10:09:41 +090058 self.PatchObject(pre_upload, '_get_affected_files',
59 return_value=['x.ebuild'])
60 self.PatchObject(pre_upload, '_filter_files', return_value=['x.ebuild'])
Laurent Chavey48b7ef32020-11-10 20:01:10 +090061
62 self.rf_mock = self.PatchObject(pre_upload, '_read_terms_file')
Laurent Chavey47eeccc2020-10-14 10:09:41 +090063 self.diff_mock = self.PatchObject(pre_upload, '_get_file_diff')
64 self.desc_mock = self.PatchObject(pre_upload, '_get_commit_desc')
65 self.project = pre_upload.Project(name='PROJECT', dir='./', remote=None)
Laurent Chavey48b7ef32020-11-10 20:01:10 +090066 self.hooks_dir_mock = self.PatchObject(pre_upload, '_get_hooks_dir')
Laurent Chavey625fbeb2020-11-11 16:17:56 +090067 self.hooks_dir_mock.return_value = self.hooks_dir
Laurent Chavey47eeccc2020-10-14 10:09:41 +090068
69 def CheckKeyword(self, test):
Laurent Chaveybb3debd2020-11-04 02:14:50 +090070 """Test a particular keyword.
71
72 Args:
73 test: { DIFF: [(int, 'line to test keyword against'), ],
74 MATCH: number of matched terms or None, }
75 """
Laurent Chavey48b7ef32020-11-10 20:01:10 +090076 def _check_keyword(unblocked):
Laurent Chavey47eeccc2020-10-14 10:09:41 +090077 self.desc_mock.return_value = 'Commit message'
78 self.diff_mock.return_value = test[self.DIFF]
79 failures = pre_upload._check_keywords(self.project, 'COMMIT', ())
80 if test[self.MATCH] and not unblocked:
81 self.assertNotEqual(failures, [])
Laurent Chavey48b7ef32020-11-10 20:01:10 +090082 self.assertEqual(test[self.MATCH], len(failures[0].items),
83 msg=failures[0].items)
Laurent Chavey47eeccc2020-10-14 10:09:41 +090084 else:
85 self.assertEqual(failures, [])
86
Laurent Chaveybb3debd2020-11-04 02:14:50 +090087 # Check blocked terms.
Daisuke Nojiri062a0a92021-02-14 15:38:16 -080088 self.rf_mock.side_effect = [self.common_file_terms, set(), set()]
Laurent Chavey48b7ef32020-11-10 20:01:10 +090089 _check_keyword(unblocked=False)
Laurent Chavey47eeccc2020-10-14 10:09:41 +090090
Laurent Chaveybb3debd2020-11-04 02:14:50 +090091 # Check unblocked terms.
Laurent Chavey625fbeb2020-11-11 16:17:56 +090092 self.rf_mock.side_effect = [self.common_file_terms,
Daisuke Nojiri062a0a92021-02-14 15:38:16 -080093 self.unblocked_file_terms,
Laurent Chavey625fbeb2020-11-11 16:17:56 +090094 self.unblocked_file_terms]
Laurent Chavey48b7ef32020-11-10 20:01:10 +090095 _check_keyword(unblocked=True)
96
97 def check_lines(self, blocked_lines, unblocked_lines):
98 """Check each lines for blocked or unblocked terms."""
99 self.CheckKeyword({self.DIFF: blocked_lines,
100 self.MATCH: len(blocked_lines)})
101 self.CheckKeyword({self.DIFF: unblocked_lines, self.MATCH: 0})
Laurent Chavey47eeccc2020-10-14 10:09:41 +0900102
103 def test_mitm_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900104 """Test mitm term."""
105 self.check_lines([
106 # blocked
107 (1, 'blocked mitm '),
108 (2, 'blocked (mitm)'),
109 (3, 'blocked .mitm'),
110 (4, 'blocked MITM'),
111 (5, 'blocked mitm1'),
112 ], [
113 # unblocked
114 (11, 'unblocked (commitment)'),
115 (12, 'unblocked DailyLimitMins'),
116 ])
Laurent Chaveybb3debd2020-11-04 02:14:50 +0900117
118 def test_sane_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900119 """Test sane term."""
120 self.check_lines([
121 # blocked
122 (1, 'blocked sane '),
123 (2, 'blocked (sane)'),
124 (3, 'blocked .sane'),
125 (4, 'blocked SANE'),
126 (5, 'blocked sane1'),
127 (6, 'blocked insane'),
128 (7, 'blocked .insane'),
129 ], [
130 # unblocked
131 (11, 'unblocked asanEnabled'),
132 ])
Laurent Chavey47eeccc2020-10-14 10:09:41 +0900133
Bernie Thompson495976a2020-11-05 17:24:14 -0800134 def test_whitelist_keyword(self):
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900135 """Test whitelist term."""
136 self.check_lines([
137 # blocked
138 (1, 'blocked white list '),
139 (2, 'blocked white-list'),
140 (3, 'blocked whitelist'),
141 (4, 'blocked _whitelist'),
142 (5, 'blocked whitelist1'),
143 (6, 'blocked whitelisted'),
144 ], [
145 # unblocked
146 (11, 'unblocked white dog list'),
147 ])
Bernie Thompson495976a2020-11-05 17:24:14 -0800148
Laurent Chavey48b7ef32020-11-10 20:01:10 +0900149 def test_sanity_keyword(self):
150 """Test sanity term."""
151 self.check_lines([
152 # blocked
153 (1, 'blocked sanity '),
154 (2, 'blocked (sanity)'),
155 (3, 'blocked struct.sanity'),
156 (4, 'blocked SANITY'),
157 (5, 'blocked AndroidSanityCheck'),
158 ], [
159 # unblocked
160 ])
Bernie Thompson495976a2020-11-05 17:24:14 -0800161
Laurent Chavey47eeccc2020-10-14 10:09:41 +0900162
163if __name__ == '__main__':
Laurent Chavey625fbeb2020-11-11 16:17:56 +0900164 cros_test_lib.main(module=__name__)