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

Side by Side Diff: recipe_engine/unittests/loader_test.py

Issue 1421843006: Add simple depends_on API. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/recipes-py@master
Patch Set: Fix small broken tests, add invoke tests, respond to nits. Created 5 years, 1 month 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 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 import os 6 import os
7 import sys 7 import sys
8 import unittest 8 import unittest
9 9
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname( 10 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(
(...skipping 28 matching lines...) Expand all
39 loader.invoke_with_properties = lambda *args, **kwargs: FakeReturn() 39 loader.invoke_with_properties = lambda *args, **kwargs: FakeReturn()
40 40
41 self.assertEqual(mocked_return, script.run(None, None)) 41 self.assertEqual(mocked_return, script.run(None, None))
42 42
43 def make_prop(**kwargs): 43 def make_prop(**kwargs):
44 name = kwargs.pop('name', "dumb_name") 44 name = kwargs.pop('name', "dumb_name")
45 return recipe_api.Property(**kwargs).bind(name, 'test', 'properties_test') 45 return recipe_api.Property(**kwargs).bind(name, 'test', 'properties_test')
46 46
47 47
48 class TestInvoke(unittest.TestCase): 48 class TestInvoke(unittest.TestCase):
49 def invoke(self, callable, all_properties, prop_defs, **kwargs): 49 def invoke(self, callable, all_properties, prop_defs, arg_names, **kwargs):
50 return loader.invoke_with_properties( 50 return loader._invoke_with_properties(
51 callable, all_properties, prop_defs, **kwargs) 51 callable, all_properties, prop_defs, arg_names, **kwargs)
52 52
53 def testInvokeFuncSimple(self): 53 def testInvokeFuncSimple(self):
54 """Simple test of invoke.""" 54 """Simple test of invoke."""
55 def func(): 55 def func():
56 pass 56 pass
57 57
58 self.assertEqual(self.invoke(func, {}, {}), None) 58 self.assertEqual(self.invoke(func, {}, {}, []), None)
59 59
60 def testInvokeFuncComplex(self): 60 def testInvokeFuncComplex(self):
61 """Tests invoke with two different properties.""" 61 """Tests invoke with two different properties."""
62 def func(a, b): # pylint: disable=unused-argument 62 def func(a, b): # pylint: disable=unused-argument
63 return a 63 return a
64 64
65 prop_defs = { 65 prop_defs = {
66 'a': make_prop(name="a"), 66 'a': make_prop(name="a"),
67 'b': make_prop(name="b"), 67 'b': make_prop(name="b"),
68 } 68 }
69 69
70 props = { 70 props = {
71 'a': 1, 71 'a': 1,
72 'b': 2, 72 'b': 2,
73 } 73 }
74 self.assertEqual(1, self.invoke(func, props, prop_defs)) 74 self.assertEqual(1, self.invoke(func, props, prop_defs, ['a', 'b']))
75 75
76 def testInvokeParamName(self): 76 def testInvokeParamName(self):
77 """Tests invoke with two different properties.""" 77 """Tests invoke with two different properties."""
78 def func(a): 78 def func(a):
79 return a 79 return a
80 80
81 prop_defs = { 81 prop_defs = {
82 'a': make_prop(name='b'), 82 'a': make_prop(name='b'),
83 } 83 }
84 84
85 props = { 85 props = {
86 'b': 2, 86 'b': 2,
87 } 87 }
88 self.assertEqual(2, self.invoke(func, props, prop_defs)) 88 self.assertEqual(2, self.invoke(func, props, prop_defs, ['a']))
89 89
90 def testInvokeClass(self): 90 def testInvokeClass(self):
91 """Tests invoking a class.""" 91 """Tests invoking a class."""
92 class test(object): 92 class test(object):
93 def __init__(self, a, b): # pylint: disable=unused-argument 93 def __init__(self, a, b): # pylint: disable=unused-argument
94 self.answer = a 94 self.answer = a
95 95
96 prop_defs = { 96 prop_defs = {
97 'a': make_prop(name="a"), 97 'a': make_prop(name="a"),
98 'b': make_prop(name="b"), 98 'b': make_prop(name="b"),
99 } 99 }
100 100
101 props = { 101 props = {
102 'a': 1, 102 'a': 1,
103 'b': 2, 103 'b': 2,
104 } 104 }
105 self.assertEqual(1, self.invoke(test, props, prop_defs).answer) 105 self.assertEqual(1, self.invoke(test, props, prop_defs, ['a', 'b']).answer)
106 106
107 def testMissingProperty(self): 107 def testMissingProperty(self):
108 """Tests that invoke raises an error when missing a property.""" 108 """Tests that invoke raises an error when missing a property."""
109 def func(a): 109 def func(a):
110 return a 110 return a
111 111
112 with self.assertRaises(recipe_api.UndefinedPropertyException): 112 with self.assertRaises(recipe_api.UndefinedPropertyException):
113 self.invoke(func, {}, {}) 113 self.invoke(func, {}, {}, ['a'])
114 114
115 def testMustBeBound(self): 115 def testMustBeBound(self):
116 """Tests that calling invoke with a non BoundProperty fails.""" 116 """Tests that calling invoke with a non BoundProperty fails."""
117 prop_defs = { 117 prop_defs = {
118 "a": recipe_api.Property() 118 "a": recipe_api.Property()
119 } 119 }
120 120
121 with self.assertRaises(ValueError): 121 with self.assertRaises(ValueError):
122 self.invoke(None, None, prop_defs) 122 self.invoke(None, None, prop_defs, ['a'])
123
124 def testInvokeArgNamesFunc(self):
125 def test_function(a, b):
126 return a
127
128 self._old_inner_invoke = loader._invoke_with_properties
129 def mock_inner_invoke(_, __, ___, names):
130 return names
131 loader._invoke_with_properties = mock_inner_invoke
luqui 2015/11/20 01:07:02 Doesn't mock provide a cleaner way of doing things
iannucci 2015/11/20 02:00:19 (yes it does)
martiniss 2015/11/23 22:05:07 much wow. Had to fix it, since we don't actually
132
133 try:
134 self.assertEqual(
135 ['a', 'b'],
136 loader.invoke_with_properties(test_function, None, None))
137 finally:
138 loader._invoke_with_properties = self._old_inner_invoke
139
140 def testInvokeArgNamesClass(self):
141 class TestClass(object):
142 def __init__(self, api, foo, bar):
143 pass
144
145 self._old_inner_invoke = loader._invoke_with_properties
146 def mock_inner_invoke(_, __, ___, names):
147 return names
148
149 loader._invoke_with_properties = mock_inner_invoke
150
151 try:
152 self.assertEqual(
153 ['api', 'foo', 'bar'],
154 loader.invoke_with_properties(TestClass, None, None))
155 finally:
156 loader._invoke_with_properties = self._old_inner_invoke
123 157
124 if __name__ == '__main__': 158 if __name__ == '__main__':
125 unittest.main() 159 unittest.main()
OLDNEW
« recipe_engine/run.py ('K') | « recipe_engine/simulation_test.py ('k') | recipes.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698