[plaster] Allow any valid regex flag (#31240)
Allow any regex flag that is supported by python re
This commit is contained in:
+8
-9
@@ -259,17 +259,16 @@ class PlasterFile:
|
||||
pattern = substitution.get('re_pattern', '')
|
||||
replace = substitution.get('replace', '')
|
||||
count = substitution.get('count', 0)
|
||||
flags = substitution.get('re_flags', '')
|
||||
flags = substitution.get('re_flags', [])
|
||||
|
||||
re_flags = 0
|
||||
if 'IGNORECASE' in flags:
|
||||
re_flags |= re.IGNORECASE
|
||||
if 'MULTILINE' in flags:
|
||||
re_flags |= re.MULTILINE
|
||||
if 'DOTALL' in flags:
|
||||
re_flags |= re.DOTALL
|
||||
if 'VERBOSE' in flags:
|
||||
re_flags |= re.VERBOSE
|
||||
for flag in flags:
|
||||
# Only accept valid re flags
|
||||
if flag.isupper() and hasattr(re, flag):
|
||||
re_flags |= getattr(re, flag)
|
||||
else:
|
||||
raise ValueError(
|
||||
f'Invalid re flag specified: {flag} in {info.source}')
|
||||
|
||||
contents, num_changes = re.subn(pattern,
|
||||
replace,
|
||||
|
||||
@@ -255,6 +255,113 @@ class PlasterTest(unittest.TestCase):
|
||||
output = stderr.getvalue()
|
||||
self.assertIn(str(changed_path), output)
|
||||
|
||||
def test_regex_flags_array_works(self):
|
||||
"""Test that multiple flags in array are passed through correctly."""
|
||||
test_file_chromium = Path(
|
||||
'chrome/common/extensions/api/test_flags_array.idl')
|
||||
|
||||
# Write and commit file with mixed case content
|
||||
self.fake_chromium_src.write_and_stage_file(
|
||||
test_file_chromium, 'Content with CHROMIUM word.',
|
||||
self.fake_chromium_src.chromium)
|
||||
self.fake_chromium_src.commit('Add test_flags_array.idl',
|
||||
self.fake_chromium_src.chromium)
|
||||
|
||||
# Create a PlasterFile with multiple flags in array
|
||||
plaster_path = plaster.PLASTER_FILES_PATH / (str(test_file_chromium) +
|
||||
'.toml')
|
||||
plaster_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
plaster_path.write_text('''
|
||||
[[substitution]]
|
||||
description = 'Test multiple flags in array work'
|
||||
re_pattern = 'chromium'
|
||||
replace = 'Brave'
|
||||
re_flags = ['IGNORECASE', 'MULTILINE']
|
||||
''')
|
||||
|
||||
plaster_file = plaster.PlasterFile(plaster_path)
|
||||
plaster_file.apply()
|
||||
|
||||
# Should match uppercase CHROMIUM due to IGNORECASE flag
|
||||
result = (self.fake_chromium_src.chromium /
|
||||
test_file_chromium).read_text()
|
||||
self.assertEqual(result, 'Content with Brave word.')
|
||||
|
||||
def test_regex_flags_invalid_cases_fail(self):
|
||||
"""Test that invalid regex flags (nonexistent, lowercase, etc.) raise
|
||||
ValueError."""
|
||||
test_file_chromium = Path(
|
||||
'chrome/common/extensions/api/test_invalid_flags.idl')
|
||||
|
||||
# Write and commit file
|
||||
self.fake_chromium_src.write_and_stage_file(
|
||||
test_file_chromium, 'Content with Chromium word.',
|
||||
self.fake_chromium_src.chromium)
|
||||
self.fake_chromium_src.commit('Add test_invalid_flags.idl',
|
||||
self.fake_chromium_src.chromium)
|
||||
|
||||
# Test various invalid flag cases
|
||||
invalid_flags_cases = [
|
||||
'INVALID_FLAG', # Nonexistent flag
|
||||
'ignorecase', # Lowercase (should be IGNORECASE)
|
||||
'fake_flag', # Another nonexistent flag
|
||||
'NOTREAL' # Another invalid flag
|
||||
]
|
||||
|
||||
for invalid_flag in invalid_flags_cases:
|
||||
with self.subTest(flag=invalid_flag):
|
||||
# Create a PlasterFile with invalid flag
|
||||
plaster_path = plaster.PLASTER_FILES_PATH / (
|
||||
str(test_file_chromium) + '.toml')
|
||||
plaster_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
plaster_path.write_text(f'''
|
||||
[[substitution]]
|
||||
description = 'Test invalid flag rejection'
|
||||
re_pattern = 'Chromium'
|
||||
replace = 'Brave'
|
||||
re_flags = ['{invalid_flag}']
|
||||
''')
|
||||
|
||||
plaster_file = plaster.PlasterFile(plaster_path)
|
||||
with self.assertRaises(ValueError) as context:
|
||||
plaster_file.apply()
|
||||
|
||||
self.assertIn(f'Invalid re flag specified: {invalid_flag}',
|
||||
str(context.exception))
|
||||
|
||||
def test_regex_flags_empty_list_works(self):
|
||||
"""Test that empty flag list works (no flags applied)."""
|
||||
test_file_chromium = Path(
|
||||
'chrome/common/extensions/api/test_no_flags.idl')
|
||||
|
||||
# Write and commit file with mixed case content
|
||||
self.fake_chromium_src.write_and_stage_file(
|
||||
test_file_chromium, 'Content with CHROMIUM and chromium words.',
|
||||
self.fake_chromium_src.chromium)
|
||||
self.fake_chromium_src.commit('Add test_no_flags.idl',
|
||||
self.fake_chromium_src.chromium)
|
||||
|
||||
# Create a PlasterFile with empty flags list
|
||||
plaster_path = plaster.PLASTER_FILES_PATH / (str(test_file_chromium) +
|
||||
'.toml')
|
||||
plaster_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
plaster_path.write_text('''
|
||||
[[substitution]]
|
||||
description = 'Test empty flags list'
|
||||
re_pattern = 'chromium'
|
||||
replace = 'Brave'
|
||||
re_flags = []
|
||||
''')
|
||||
|
||||
plaster_file = plaster.PlasterFile(plaster_path)
|
||||
plaster_file.apply()
|
||||
|
||||
# Should only match lowercase 'chromium', not uppercase 'CHROMIUM'
|
||||
result = (self.fake_chromium_src.chromium /
|
||||
test_file_chromium).read_text()
|
||||
self.assertEqual(result, 'Content with CHROMIUM and Brave words.')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user