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

Side by Side Diff: recipe_modules/json/example.py

Issue 2909983003: [loader] Make recipe engine support examples/ module subdir (Closed)
Patch Set: Created 3 years, 6 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
(Empty)
1 # Copyright 2014 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file.
4
5 DEPS = [
6 'json',
7 'path',
8 'properties',
9 'python',
10 'raw_io',
11 'step',
12 ]
13
14 FULLWIDTH_Z = '\xef\xbb\xbf\xef\xbd\x9a'
15
16 def RunSteps(api):
17 step_result = api.step('echo1', ['echo', '[1, 2, 3]'],
18 stdout=api.json.output())
19 assert step_result.stdout == [1, 2, 3], step_result.stdout
20
21 # have to provide a default value for example_dumps because the
22 # 'example_tests' unittest runs all these example recipes without any input
23 # properties.
24 api.step('echo_dumps_property', [
25 'echo', api.properties.get('example_dumps', '[100]')])
26
27 # Example demonstrating the usage of step_test_data for json stdout.
28 step_result = api.step('echo2', ['echo', '[2, 3, 4]'],
29 step_test_data=lambda: api.json.test_api.output_stream([2, 3, 4]),
30 stdout=api.json.output())
31 assert step_result.stdout == [2, 3, 4]
32
33 assert api.json.is_serializable('foo')
34 assert not api.json.is_serializable(set(['foo', 'bar', 'baz']))
35
36 # Example demonstrating multiple named json output files.
37 result = api.python.inline(
38 'foo',
39 """
40 import json
41 import sys
42 with open(sys.argv[1], 'w') as f:
43 f.write(json.dumps([1, 2, 3]))
44 with open(sys.argv[2], 'w') as f:
45 f.write(json.dumps(['x', 'y', %s]))
46 """ % repr(FULLWIDTH_Z),
47 args=[api.json.output(name='1'), api.json.output(name='2')],
48 )
49 assert result.json.outputs['1'] == [1, 2, 3]
50 assert result.json.outputs['2'] == ['x', 'y', FULLWIDTH_Z]
51 assert not hasattr(result.json, 'output')
52
53 example_dict = {'x': 1, 'y': 2}
54
55 # json.input(json_data) expands to a path containing that rendered json
56 step_result = api.step('json through',
57 ['cat', api.json.input(example_dict)],
58 stdout=api.json.output(),
59 step_test_data=lambda: api.json.test_api.output_stream(example_dict))
60 assert step_result.stdout == example_dict
61
62 # json.read reads a file containing json data.
63 leak_path = api.path['tmp_base'].join('temp.json')
64 api.step('write json to file',
65 ['cat', api.json.input(example_dict)],
66 stdout=api.raw_io.output(leak_to=leak_path))
67 step_result = api.json.read(
68 'read json from file we just wrote', leak_path,
69 step_test_data=lambda: api.json.test_api.output(example_dict))
70 assert step_result.json.output == example_dict
71
72 # can leak directly to a file
73 step_result = api.step('leaking json', [
74 'python', api.resource('cool_script.py'),
75 '{"x":1,"y":2}',
76 api.json.output(leak_to=api.path['tmp_base'].join('leak.json')),
77 ])
78 assert step_result.json.output == example_dict
79
80 # invalid data gets rendered
81 step_result = api.step('invalid json', [
82 'python', api.resource('cool_script.py'),
83 '{"here is some total\ngarbage',
84 api.json.output(),
85 ])
86 assert step_result.json.output is None
87
88
89 def GenTests(api):
90 yield (
91 api.test('basic')
92 + api.properties(
93 example_dumps=api.json.dumps({
94 'hello': 'world',
95 'cool': [1, 2, 3],
96 })
97 )
98 + api.step_data('echo1', stdout=api.json.output([1, 2, 3]))
99 + api.step_data(
100 'foo',
101 api.json.output([1, 2, 3], name='1') +
102 api.json.output(['x', 'y', FULLWIDTH_Z], name='2'),
103 )
104 + api.step_data(
105 'leaking json',
106 api.json.output({'x': 1, 'y': 2}),
107 )
108 + api.step_data(
109 'invalid json',
110 api.json.invalid('{"here is some total\ngarbage'),
111 )
112 )
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698