Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(736)

Side by Side Diff: recipe_engine/recipe_test_api.py

Issue 2707593002: Whitelist allowed test names
Patch Set: Comment code. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2016 The LUCI Authors. All rights reserved. 1 # Copyright 2016 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 import contextlib 5 import contextlib
6 import inspect 6 import inspect
7 import re
7 8
8 from collections import namedtuple, defaultdict 9 from collections import namedtuple, defaultdict
9 10
10 from .util import ModuleInjectionSite, static_call, static_wraps 11 from .util import ModuleInjectionSite, static_call, static_wraps
11 from .types import freeze 12 from .types import freeze
12 13
13 def combineify(name, dest, a, b, overwrite=False): 14 def combineify(name, dest, a, b, overwrite=False):
14 """ 15 """
15 Combines dictionary members in two objects into a third one using addition. 16 Combines dictionary members in two objects into a third one using addition.
16 17
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 ret.update(other) 180 ret.update(other)
180 return ret 181 return ret
181 182
182 def __repr__(self): 183 def __repr__(self):
183 return "ModuleTestData(%r)" % super(ModuleTestData, self).__repr__() 184 return "ModuleTestData(%r)" % super(ModuleTestData, self).__repr__()
184 185
185 186
186 PostprocessHook = namedtuple( 187 PostprocessHook = namedtuple(
187 'PostprocessHook', 'func args kwargs filename lineno') 188 'PostprocessHook', 'func args kwargs filename lineno')
188 189
190 def is_valid_test_name(name):
191 # * is there to make the match greedy, so it matches the whole word, rather
Paweł Hajdan Jr. 2017/02/21 09:53:31 nit: No need to explain regex syntax IMO.
192 # than just the first character of a name
193 match = re.match(r'(\w)*', name)
dnj 2017/02/18 00:15:29 Why not: "name.isalnum()"? Also will reject whites
dnj 2017/02/18 01:27:41 Ah boo :( Could do: re.match(r'[a-zA-Z0-9_]*') I
Paweł Hajdan Jr. 2017/02/21 09:53:31 Let's use ^, $, and +, i.e. r'^(\w)+$' Then we d
194 if not match:
195 return False
196
197 # Catches a case of a name with a space in it.
198 return len(match.group(0)) == len(name)
199
189 200
190 class TestData(BaseTestData): 201 class TestData(BaseTestData):
191 def __init__(self, name=None): 202 def __init__(self, name=None):
192 super(TestData, self).__init__() 203 super(TestData, self).__init__()
204 # Allow empty names; sometimes we create test datas to merge two test datas
205 # together.
206 if name and not is_valid_test_name(name):
207 raise ValueError("Invalid test name %r" % name)
Paweł Hajdan Jr. 2017/02/21 09:53:31 nit: Single quotes ' instead of double " Also, ex
208
193 self.name = name 209 self.name = name
194 self.properties = {} # key -> val 210 self.properties = {} # key -> val
195 self.mod_data = defaultdict(ModuleTestData) 211 self.mod_data = defaultdict(ModuleTestData)
196 self.step_data = defaultdict(StepTestData) 212 self.step_data = defaultdict(StepTestData)
197 self.depend_on_data = {} 213 self.depend_on_data = {}
198 self.expected_exception = None 214 self.expected_exception = None
199 self.post_process_hooks = [] # list(PostprocessHook) 215 self.post_process_hooks = [] # list(PostprocessHook)
200 216
201 def __add__(self, other): 217 def __add__(self, other):
202 assert isinstance(other, TestData) 218 assert isinstance(other, TestData)
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 ) 662 )
647 """ 663 """
648 ret = TestData() 664 ret = TestData()
649 try: 665 try:
650 stk = inspect.stack() 666 stk = inspect.stack()
651 _, filename, lineno, _, _, _ = stk[1] 667 _, filename, lineno, _, _, _ = stk[1]
652 finally: 668 finally:
653 del stk 669 del stk
654 ret.post_process(func, args, kwargs, filename, lineno) 670 ret.post_process(func, args, kwargs, filename, lineno)
655 return ret 671 return ret
OLDNEW
« no previous file with comments | « no previous file | recipe_engine/unittests/recipe_test_api_test.py » ('j') | recipe_engine/unittests/recipe_test_api_test.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698