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

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

Issue 2387763003: Add initial postprocess unit test thingy. (Closed)
Patch Set: Add nested statement too Created 4 years, 2 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 2016 The LUCI Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0
4 # that can be found in the LICENSE file.
5
6 import json
7 import os
8 import subprocess
9 import sys
10 import unittest
11 import copy
Michael Achenbach 2016/10/13 07:45:08 nit: order
iannucci 2016/10/13 22:03:20 Done.
12
13 from collections import OrderedDict
14
15 import test_env
16 import mock
17
18 from recipe_engine import post_process
19 from recipe_engine import checker
20
21 def mkS(name, *fields):
22 ret = {
23 'name': name,
24 'sub_a': ['thing', 'other'],
25 'sub_b': 100,
26 'sub_c': 'hi',
27 }
28 if fields:
29 return {k: v for k, v in ret.iteritems() if k in fields or k == 'name'}
30 return ret
31
32 def mkD(*steps):
33 return OrderedDict([(n, mkS(n)) for n in steps])
34
35 class TestFilter(unittest.TestCase):
36 def setUp(self):
37 self.d = mkD('a', 'b', 'b.sub', 'b.sub2')
38 self.f = post_process.Filter
39
40 def test_basic(self):
41 c = checker.Checker('<filename>', 0, lambda: None, (), {})
42 self.assertEqual(
43 self.f('a', 'b')(c, self.d), mkD('a', 'b'))
44 self.assertEqual(len(c.failed_checks), 0)
45
46 def test_built(self):
47 c = checker.Checker('<filename>', 0, lambda: None, (), {})
48 f = self.f()
49 f = f.include('b')
50 f = f.include('a')
51 self.assertEqual(f(c, self.d), mkD('a', 'b'))
52 self.assertEqual(len(c.failed_checks), 0)
53
54 def test_built_fields(self):
55 c = checker.Checker('<filename>', 0, lambda: None, (), {})
56 f = self.f()
57 f = f.include('b', 'sub_b')
58 f = f.include('a', 'sub_a')
59 self.assertEqual(f(c, self.d), OrderedDict([
60 ('a', mkS('a', 'sub_a')),
61 ('b', mkS('b', 'sub_b')),
62 ]))
63 self.assertEqual(len(c.failed_checks), 0)
64
65 def test_built_extra_includes(self):
66 f = self.f('a', 'b', 'x')
67 c = checker.Checker('<filename>', 0, f, (), {})
68 self.assertEqual(f(c, self.d), mkD('a', 'b'))
69 self.assertEqual(len(c.failed_checks), 1)
70 self.assertEqual(c.failed_checks[0].frames[-1].code,
71 'check((len(unused_includes) == 0))')
72 self.assertEqual(c.failed_checks[0].frames[-1].varmap,
73 {'unused_includes': "{'x': ()}"})
74
75 def test_re(self):
76 f = self.f().include_re('b\.')
77 c = checker.Checker('<filename>', 0, f, (), {})
78 self.assertEqual(f(c, self.d), mkD('b.sub', 'b.sub2'))
79 self.assertEqual(len(c.failed_checks), 0)
80
81 def test_re_low_limit(self):
82 f = self.f().include_re('b\.', at_least=3)
83 c = checker.Checker('<filename>', 0, f, (), {})
84 self.assertEqual(f(c, self.d), mkD('b.sub', 'b.sub2'))
85 self.assertEqual(len(c.failed_checks), 1)
86 self.assertEqual(c.failed_checks[0].frames[-1].code,
87 'check((re_usage_count[regex] >= at_least))')
88 self.assertEqual(c.failed_checks[0].frames[-1].varmap,
89 {'at_least': '3',
90 're_usage_count[regex]': '2',
91 'regex': "re.compile('b\\\\.')"})
92
93 def test_re_high_limit(self):
94 f = self.f().include_re('b\.', at_most=1)
95 c = checker.Checker('<filename>', 0, f, (), {})
96 self.assertEqual(f(c, self.d), mkD('b.sub', 'b.sub2'))
97 self.assertEqual(len(c.failed_checks), 1)
98 self.assertEqual(c.failed_checks[0].frames[-1].code,
99 'check((re_usage_count[regex] <= at_most))')
100 self.assertEqual(c.failed_checks[0].frames[-1].varmap,
101 {'at_most': '1',
102 're_usage_count[regex]': '2',
103 'regex': "re.compile('b\\\\.')"})
104
105
106 class TestRun(unittest.TestCase):
107 def setUp(self):
108 self.d = mkD('a', 'b', 'b.sub', 'b.sub2')
109
110 def expect_fails(self, num_fails, func, *args, **kwargs):
111 c = checker.Checker('<filename>', 0, func, args, kwargs)
112 func(c, self.d, *args, **kwargs)
113 self.assertEqual(len(c.failed_checks), num_fails)
114
115 def test_mr_pass(self):
116 self.expect_fails(0, post_process.MustRun, 'a')
117
118 def test_mr_fail(self):
119 self.expect_fails(1, post_process.MustRun, 'x')
120
121 def test_mr_pass_re(self):
122 self.expect_fails(0, post_process.MustRunRE, 'a')
123 self.expect_fails(0, post_process.MustRunRE, 'a', at_most=1)
124 self.expect_fails(0, post_process.MustRunRE, 'a', at_least=1, at_most=1)
125
126 def test_mr_fail_re(self):
127 self.expect_fails(1, post_process.MustRunRE, 'x')
128 self.expect_fails(1, post_process.MustRunRE, 'b', at_most=1)
129 self.expect_fails(1, post_process.MustRunRE, 'b', at_least=4)
130
131 def test_dnr_pass(self):
132 self.expect_fails(0, post_process.DoesNotRun, 'x')
133
134 def test_dnr_fail(self):
135 self.expect_fails(1, post_process.DoesNotRun, 'a')
136
137 def test_dnr_pass_re(self):
138 self.expect_fails(0, post_process.DoesNotRunRE, 'x')
139
140 def test_dnr_fail_re(self):
141 self.expect_fails(3, post_process.DoesNotRunRE, 'b')
142
143
144 if __name__ == '__main__':
145 sys.exit(unittest.main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698