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

Side by Side Diff: unittests/errors_test.py

Issue 1570333002: 'recipes.py isolate' command to build an isolate of the current package's toolchain (Closed) Base URL: git@github.com:luci/recipes-py.git@master
Patch Set: Review comments Created 4 years, 11 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 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import os
7 import shutil
8 import subprocess
9 import tempfile
10 import unittest
11
12 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13
14 class RecipeRepo(object):
15 def __init__(self):
16 self._root = tempfile.mkdtemp()
17 os.makedirs(os.path.join(self._root, 'infra', 'config'))
18 self._recipes_cfg = os.path.join(
19 self._root, 'infra', 'config', 'recipes.cfg')
20 with open(self._recipes_cfg, 'w') as fh:
21 fh.write("""
22 api_version: 1
23 project_id: "testproj"
24 deps {
25 project_id: "recipe_engine"
26 url: "%s"
27 branch: "master"
28 revision: "HEAD"
29 }
30 """ % ROOT_DIR)
31 self._recipes_dir = os.path.join(self._root, 'recipes')
32 os.mkdir(self._recipes_dir)
33 self._modules_dir = os.path.join(self._root, 'recipe_modules')
34 os.mkdir(self._modules_dir)
35
36 def make_recipe(self, recipe, contents):
37 with open(os.path.join(self._recipes_dir, '%s.py' % recipe), 'w') as fh:
38 fh.write(contents)
39
40 def make_module(self, name, init_contents, api_contents):
41 module_root = os.path.join(self._modules_dir, name)
42 os.mkdir(module_root)
43 with open(os.path.join(module_root, '__init__.py'), 'w') as fh:
44 fh.write(init_contents)
45 with open(os.path.join(module_root, 'api.py'), 'w') as fh:
46 fh.write(api_contents)
47
48 @property
49 def recipes_cmd(self):
50 return [
51 os.path.join(ROOT_DIR, 'recipes.py'),
52 '--package', self._recipes_cfg,
53 '-O', 'recipe_engine=%s' % ROOT_DIR]
54
55 def __enter__(self):
56 return self
57
58 def __exit__(self, *_):
59 shutil.rmtree(self._root)
60
61 class ErrorsTest(unittest.TestCase):
62 def _test_cmd(self, repo, cmd, asserts, retcode=0):
63 subp = subprocess.Popen(
64 repo.recipes_cmd + cmd,
65 stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE)
67 stdout, stderr = subp.communicate()
68 if asserts:
69 asserts(stdout, stderr)
70 self.assertEqual(subp.returncode, retcode)
71
72 def test_missing_dependency(self):
73 with RecipeRepo() as repo:
74 repo.make_recipe('foo', """
75 DEPS = ['aint_no_thang']
76 """)
77 subp = subprocess.Popen(
78 repo.recipes_cmd + ['run', 'foo'],
79 stdout=subprocess.PIPE)
80 stdout, _ = subp.communicate()
81 self.assertRegexpMatches(stdout,
82 r'aint_no_thang does not exist[^\n]*while loading recipe foo')
83 self.assertEqual(subp.returncode, 2)
84
85 def test_missing_module_dependency(self):
86 with RecipeRepo() as repo:
87 repo.make_recipe('foo', 'DEPS = ["le_module"]')
88 repo.make_module('le_module', 'DEPS = ["love"]', '')
89 subp = subprocess.Popen(
90 repo.recipes_cmd + ['run', 'foo'],
91 stdout=subprocess.PIPE)
92 stdout, _ = subp.communicate()
93 self.assertRegexpMatches(stdout,
94 r'love does not exist[^\n]*'
95 r'while loading recipe module \S*le_module[^\n]*'
96 r'while loading recipe foo')
97 self.assertEqual(subp.returncode, 2)
98
99 def test_no_such_recipe(self):
100 with RecipeRepo() as repo:
101 subp = subprocess.Popen(
102 repo.recipes_cmd + ['run', 'nooope'],
103 stdout=subprocess.PIPE)
104 stdout, _ = subp.communicate()
105 self.assertRegexpMatches(stdout, r'No such recipe: nooope')
106 self.assertEqual(subp.returncode, 2)
107
108 def test_syntax_error(self):
109 with RecipeRepo() as repo:
110 repo.make_recipe('foo', """
111 DEPS = [ (sic)
112 """)
113
114 def assert_syntaxerror(stdout, stderr):
115 self.assertRegexpMatches(stdout + stderr, r'SyntaxError')
116
117 self._test_cmd(repo, ['simulation_test', 'test', 'foo'],
118 asserts=assert_syntaxerror, retcode=1)
119 self._test_cmd(repo, ['simulation_test', 'train', 'foo'],
120 asserts=assert_syntaxerror, retcode=1)
121 self._test_cmd(repo, ['run', 'foo'],
122 asserts=assert_syntaxerror, retcode=1)
123
124 def test_missing_path(self):
125 with RecipeRepo() as repo:
126 repo.make_recipe('missing_path', """
127 DEPS = ['recipe_engine/step', 'recipe_engine/path']
128
129 def RunSteps(api):
130 api.step('do it, joe', ['echo', 'JOE'], cwd=api.path['bippityboppityboo'])
131
132 def GenTests(api):
133 yield api.test('basic')
134 """)
135 def assert_keyerror(stdout, stderr):
136 self.assertRegexpMatches(
137 stdout + stderr, r"KeyError: 'Unknown path: bippityboppityboo'")
138
139 self._test_cmd(repo, ['simulation_test', 'train', 'missing_path'],
140 asserts=assert_keyerror, retcode=1)
141 self._test_cmd(repo, ['simulation_test', 'test', 'missing_path'],
142 asserts=assert_keyerror, retcode=1)
143 self._test_cmd(repo, ['run', 'missing_path'],
144 asserts=assert_keyerror, retcode=255)
145
146 def test_engine_failure(self):
147 with RecipeRepo() as repo:
148 repo.make_recipe('print_step_error', """
149 DEPS = ['recipe_engine/step']
150
151 from recipe_engine import step_runner
152
153 def bad_print_step(self, step_stream, step, env):
154 raise Exception("Buh buh buh buh bad to the bone")
155
156 def RunSteps(api):
157 step_runner.SubprocessStepRunner._print_step = bad_print_step
158 try:
159 api.step('Be good', ['echo', 'Sunshine, lollipops, and rainbows'])
160 finally:
161 api.step.active_result.presentation.status = 'WARNING'
162 """)
163 self._test_cmd(repo, ['run', 'print_step_error'],
164 asserts=lambda stdout, stderr: self.assertRegexpMatches(
165 stdout + stderr,
166 r'(?s)Recipe engine bug.*Buh buh buh buh bad to the bone'),
167 retcode=2)
168
169 def test_unconsumed_assertion(self):
170 # There was a regression where unconsumed exceptions would not be detected
171 # if the exception was AssertionError.
172
173 with RecipeRepo() as repo:
174 repo.make_recipe('unconsumed_assertion', """
175 DEPS = []
176
177 def RunSteps(api):
178 pass
179
180 def GenTests(api):
181 yield api.test('basic') + api.expect_exception('AssertionError')
182 """)
183 self._test_cmd(repo, ['simulation_test', 'train', 'unconsumed_assertion'],
184 asserts=lambda stdout, stderr: self.assertRegexpMatches(
185 stdout + stderr, 'Unconsumed'),
186 retcode=1)
187
188 if __name__ == '__main__':
189 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698