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

Unified Diff: recipe_engine/stream.py

Issue 2792333003: Assert validity of stream parameters. (Closed)
Patch Set: fix spelling Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | recipe_engine/unittests/stream_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: recipe_engine/stream.py
diff --git a/recipe_engine/stream.py b/recipe_engine/stream.py
index 831e69b0e9bfab75ca31cbae0cfe30f152bf4f26..5420bdc8091aab12120eb4176a2b89138c4a8606 100644
--- a/recipe_engine/stream.py
+++ b/recipe_engine/stream.py
@@ -46,7 +46,14 @@ def output_iter(stream, it):
stream.write_line(line)
+def _check(cond, msg, **kwargs):
+ """Runtime assertion used for parameter correctness."""
+ if not cond:
+ raise ValueError('Failed assertion: %s %r' % (msg, kwargs))
+
+
class StreamEngine(object):
+
class Stream(object):
def write_line(self, line):
raise NotImplementedError()
@@ -93,6 +100,8 @@ class StreamEngine(object):
def make_step_stream(self, name, **kwargs):
"""Shorthand for creating a step stream from a step configuration dict."""
+ _check('\n' not in name, 'Stream name must not contain a newline.',
+ name=name)
kwargs['name'] = name
return self.new_step_stream(recipe_api.StepConfig.create(**kwargs))
@@ -314,7 +323,7 @@ class StreamEngineInvariants(StreamEngine):
self._status = 'SUCCESS'
def write_line(self, line):
- assert '\n' not in line
+ assert '\n' not in line, 'Individual line must not contain a newline.'
assert self._open
def close(self):
@@ -328,19 +337,30 @@ class StreamEngineInvariants(StreamEngine):
assert self._open
assert log_name not in self._logs, 'Log %s already exists in step %s' % (
log_name, self._step_name)
+
+ _check('\n' not in log_name, 'Log name must not contain a newline.',
+ log_name=log_name)
ret = self._engine.LogStream(self, log_name)
self._logs[log_name] = ret
return ret
def add_step_text(self, text):
- pass
+ _check('\n' not in text, 'Step text must not contain a newline.',
+ text=text)
def add_step_summary_text(self, text):
- pass
+ _check('\n' not in text, 'Step summary text must not contain a newline.',
+ text=text)
def add_step_link(self, name, url):
- assert isinstance(name, basestring), 'Link name %s is not a string' % name
- assert isinstance(url, basestring), 'Link url %s is not a string' % url
+ _check(isinstance(name, basestring), 'Link name is not a string',
+ name=name)
+ _check('\n' not in name, 'Link name must not contain a newline.',
+ name=name)
+ _check(isinstance(url, basestring),
+ 'Link URL is not a string', url=url)
+ _check('\n' not in url, 'Link URL must not contain a newline.',
+ url=url)
def set_step_status(self, status):
assert status in ('SUCCESS', 'WARNING', 'FAILURE', 'EXCEPTION')
@@ -351,11 +371,16 @@ class StreamEngineInvariants(StreamEngine):
self._status = status
def set_build_property(self, key, value):
- pass
+ _check('\n' not in key, 'Property key must not contain a newline.',
+ key=key)
+ _check('\n' not in value, 'Property value must not contain a newline.',
+ value=value)
+ json.loads(value) # value must be a valid JSON object.
def trigger(self, spec):
- assert '\n' not in spec # Spec must fit on one line.
- json.loads(spec) # Spec must be a valid json object.
+ _check('\n' not in spec, 'Spec must not contain a newline.',
+ spec=spec)
+ json.loads(spec) # Spec must be a valid JSON object.
class LogStream(StreamEngine.Stream):
def __init__(self, step_stream, log_name):
@@ -364,7 +389,7 @@ class StreamEngineInvariants(StreamEngine):
self._open = True
def write_line(self, line):
- assert '\n' not in line
+ assert '\n' not in line, 'Individual line must not contain a newline.'
assert self._step_stream._open
assert self._open
« no previous file with comments | « no previous file | recipe_engine/unittests/stream_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698