1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
# Copyright 2025 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
import tempfile
import unittest
from unittest import mock
import PRESUBMIT
class MockInputApi:
def __init__(self):
self.os_path = os.path
self.change = mock.Mock()
@staticmethod
def PresubmitLocalPath():
return os.path.dirname(__file__)
class MockOutputApi:
@staticmethod
def PresubmitError(message, items=None):
# Using a tuple to make it easy to compare.
return ('Error', message, items)
@staticmethod
def PresubmitResult(message, items=None):
# Using a tuple to make it easy to compare.
return ('Result', message, items)
class UndeclaredFeaturesTest(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.repo_root = self.temp_dir.name
self.mock_input_api = MockInputApi()
self.mock_input_api.change.RepositoryRoot.return_value = self.repo_root
self.mock_output_api = MockOutputApi()
def tearDown(self):
self.temp_dir.cleanup()
# Ensure find_features is reloaded for the next test or test suite
# by removing it from the module cache. This prevents test state from
# leaking.
if 'find_features' in sys.modules:
del sys.modules['find_features']
def _create_file_in_repo(self, path, content):
full_path = os.path.join(self.repo_root, path)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, 'w', encoding='utf-8') as f:
f.write(content)
def testAllFeaturesOnChangedLinesAreDeclared(self):
# Declare a variety of features using both 2- and 3-argument macros.
self._create_file_in_repo(
'components/feature_a.cc',
'BASE_FEATURE(kFeatureA, base::FEATURE_ENABLED_BY_DEFAULT);')
self._create_file_in_repo(
'chrome/feature_b.cc', 'BASE_FEATURE(kFeatureB, "FeatureB", '
'base::FEATURE_ENABLED_BY_DEFAULT);')
self._create_file_in_repo(
'components/feature_c.cc', 'BASE_FEATURE(kFeatureC, "FeatureC",\n'
' base::FEATURE_ENABLED_BY_DEFAULT);')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureA', 'FeatureB'],
}]
}],
'Study2_Unaffected': [{
'experiments': [{
'name': 'group2',
'enable_features': ['UndeclaredFeature'],
}]
}]
}
# The check should only trigger on features in changed lines.
# FeatureA and FeatureB are declared, so this should pass.
# UndeclaredFeature is not on a changed line, so it's ignored.
changed_lines = [(1, '"enable_features": ["FeatureA", "FeatureB"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testUndeclaredFeatureOnChangedLine(self):
self._create_file_in_repo(
'components/feature_a.cc',
'BASE_FEATURE(kFeatureA, base::FEATURE_ENABLED_BY_DEFAULT);')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureA', 'UndeclaredFeature']
}]
}]
}
changed_lines = [(1, '"enable_features": ["UndeclaredFeature"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(len(result), 1)
self.assertEqual(result[0][0], 'Result')
self.assertIn('UndeclaredFeature', str(result[0]))
self.assertIn('Study1', str(result[0]))
def testFeatureWithMacroParametersOnDifferentLines(self):
self._create_file_in_repo(
'components/feature_d.cc', 'BASE_FEATURE(kFeatureD,\n'
' "FeatureD",\n'
' base::FEATURE_ENABLED_BY_DEFAULT);')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureD']
}]
}]
}
changed_lines = [(1, '"enable_features": ["FeatureD"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testTwoParameterMacro(self):
self._create_file_in_repo(
'components/feature_g.cc',
'BASE_FEATURE(kFeatureG, base::FEATURE_ENABLED_BY_DEFAULT);')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureG']
}]
}]
}
changed_lines = [(1, '"enable_features": ["FeatureG"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testTwoParameterMacroWithParametersOnDifferentLines(self):
self._create_file_in_repo(
'components/feature_h.cc', 'BASE_FEATURE(kFeatureH,\n'
' base::FEATURE_ENABLED_BY_DEFAULT);')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureH']
}]
}]
}
changed_lines = [(1, '"enable_features": ["FeatureH"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testFeatureWithPreprocessorDirectives(self):
self._create_file_in_repo(
'components/feature_f.cc', 'BASE_FEATURE(kFeatureF,\n'
' "FeatureF",\n'
'#if BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)\n'
' base::FEATURE_ENABLED_BY_DEFAULT\n'
'#else\n'
' base::FEATURE_DISABLED_BY_DEFAULT\n'
'#endif\n'
');')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureF']
}]
}]
}
changed_lines = [(1, '"enable_features": ["FeatureF"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testTwoParameterMacroWithPreprocessorDirectives(self):
self._create_file_in_repo(
'components/feature_k.cc', 'BASE_FEATURE(kFeatureK,\n'
'#if BUILDFLAG(IS_WIN)\n'
' base::FEATURE_ENABLED_BY_DEFAULT\n'
'#else\n'
' base::FEATURE_DISABLED_BY_DEFAULT\n'
'#endif\n'
');')
json_data = {
'Study1': [{
'experiments': [{
'name': 'group1',
'enable_features': ['FeatureK']
}]
}]
}
changed_lines = [(1, '"enable_features": ["FeatureK"]')]
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, json_data,
changed_lines)
self.assertEqual(result, [])
def testNoFeaturesDeclaredInRepo(self):
# No files created, so no features will be found, which is an error
# condition.
result = PRESUBMIT.CheckUndeclaredFeatures(self.mock_input_api,
self.mock_output_api, {}, [])
self.assertEqual(len(result), 1)
self.assertEqual(result[0][0], 'Error')
self.assertIn('unable to find any declared flags', result[0][1])
if __name__ == '__main__':
unittest.main()
|