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

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

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

Powered by Google App Engine
This is Rietveld 408576698