2 # Copyright (c) 2012 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.
6 """Tests for checkdeps.
17 class CheckDepsTest(unittest
.TestCase
):
20 self
.deps_checker
= checkdeps
.DepsChecker(being_tested
=True)
22 def ImplTestRegularCheckDepsRun(self
, ignore_temp_rules
, skip_tests
):
23 self
.deps_checker
._ignore
_temp
_rules
= ignore_temp_rules
24 self
.deps_checker
._skip
_tests
= skip_tests
25 self
.deps_checker
.CheckDirectory(
26 os
.path
.join(self
.deps_checker
.base_directory
,
27 'tools/checkdeps/testdata'))
29 problems
= self
.deps_checker
.results_formatter
.GetResults()
31 self
.failUnlessEqual(3, len(problems
))
33 self
.failUnlessEqual(4, len(problems
))
35 def VerifySubstringsInProblems(key_path
, substrings_in_sequence
):
36 """Finds the problem in |problems| that contains |key_path|,
37 then verifies that each of |substrings_in_sequence| occurs in
38 that problem, in the order they appear in
39 |substrings_in_sequence|.
42 key_path
= os
.path
.normpath(key_path
)
43 for problem
in problems
:
44 index
= problem
.find(key_path
)
46 for substring
in substrings_in_sequence
:
47 index
= problem
.find(substring
, index
+ 1)
48 self
.failUnless(index
!= -1, '%s in %s' % (substring
, problem
))
52 self
.fail('Found no problem for file %s' % key_path
)
55 VerifySubstringsInProblems('testdata/allowed/test.h',
56 ['-tools/checkdeps/testdata/disallowed',
57 'temporarily_allowed.h',
58 '-third_party/explicitly_disallowed',
59 'Because of no rule applying'])
61 VerifySubstringsInProblems('testdata/allowed/test.h',
62 ['-tools/checkdeps/testdata/disallowed',
63 '-third_party/explicitly_disallowed',
64 'Because of no rule applying'])
66 VerifySubstringsInProblems('testdata/disallowed/test.h',
67 ['-third_party/explicitly_disallowed',
68 'Because of no rule applying',
69 'Because of no rule applying'])
70 VerifySubstringsInProblems('disallowed/allowed/test.h',
71 ['-third_party/explicitly_disallowed',
72 'Because of no rule applying',
73 'Because of no rule applying'])
76 VerifySubstringsInProblems('allowed/not_a_test.cc',
77 ['-tools/checkdeps/testdata/disallowed'])
79 def testRegularCheckDepsRun(self
):
80 self
.ImplTestRegularCheckDepsRun(False, False)
82 def testRegularCheckDepsRunIgnoringTempRules(self
):
83 self
.ImplTestRegularCheckDepsRun(True, False)
85 def testRegularCheckDepsRunSkipTests(self
):
86 self
.ImplTestRegularCheckDepsRun(False, True)
88 def testRegularCheckDepsRunIgnoringTempRulesSkipTests(self
):
89 self
.ImplTestRegularCheckDepsRun(True, True)
91 def CountViolations(self
, ignore_temp_rules
):
92 self
.deps_checker
._ignore
_temp
_rules
= ignore_temp_rules
93 self
.deps_checker
.results_formatter
= results
.CountViolationsFormatter()
94 self
.deps_checker
.CheckDirectory(
95 os
.path
.join(self
.deps_checker
.base_directory
,
96 'tools/checkdeps/testdata'))
97 return self
.deps_checker
.results_formatter
.GetResults()
99 def testCountViolations(self
):
100 self
.failUnlessEqual('10', self
.CountViolations(False))
102 def testCountViolationsIgnoringTempRules(self
):
103 self
.failUnlessEqual('11', self
.CountViolations(True))
105 def testTempRulesGenerator(self
):
106 self
.deps_checker
.results_formatter
= results
.TemporaryRulesFormatter()
107 self
.deps_checker
.CheckDirectory(
108 os
.path
.join(self
.deps_checker
.base_directory
,
109 'tools/checkdeps/testdata/allowed'))
110 temp_rules
= self
.deps_checker
.results_formatter
.GetResults()
111 expected
= [u
' "!third_party/explicitly_disallowed/bad.h",',
112 u
' "!third_party/no_rule/bad.h",',
113 u
' "!tools/checkdeps/testdata/disallowed/bad.h",',
114 u
' "!tools/checkdeps/testdata/disallowed/teststuff/bad.h",']
115 self
.failUnlessEqual(expected
, temp_rules
)
117 def testCheckAddedIncludesAllGood(self
):
118 problems
= self
.deps_checker
.CheckAddedCppIncludes(
119 [['tools/checkdeps/testdata/allowed/test.cc',
120 ['#include "tools/checkdeps/testdata/allowed/good.h"',
121 '#include "tools/checkdeps/testdata/disallowed/allowed/good.h"']
123 self
.failIf(problems
)
125 def testCheckAddedIncludesManyGarbageLines(self
):
126 garbage_lines
= ["My name is Sam%d\n" % num
for num
in range(50)]
127 problems
= self
.deps_checker
.CheckAddedCppIncludes(
128 [['tools/checkdeps/testdata/allowed/test.cc', garbage_lines
]])
129 self
.failIf(problems
)
131 def testCheckAddedIncludesNoRule(self
):
132 problems
= self
.deps_checker
.CheckAddedCppIncludes(
133 [['tools/checkdeps/testdata/allowed/test.cc',
134 ['#include "no_rule_for_this/nogood.h"']
136 self
.failUnless(problems
)
138 def testCheckAddedIncludesSkippedDirectory(self
):
139 problems
= self
.deps_checker
.CheckAddedCppIncludes(
140 [['tools/checkdeps/testdata/disallowed/allowed/skipped/test.cc',
141 ['#include "whatever/whocares.h"']
143 self
.failIf(problems
)
145 def testCheckAddedIncludesTempAllowed(self
):
146 problems
= self
.deps_checker
.CheckAddedCppIncludes(
147 [['tools/checkdeps/testdata/allowed/test.cc',
148 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"']
150 self
.failUnless(problems
)
152 def testCopyIsDeep(self
):
153 # Regression test for a bug where we were making shallow copies of
154 # Rules objects and therefore all Rules objects shared the same
155 # dictionary for specific rules.
157 # The first pair should bring in a rule from testdata/allowed/DEPS
158 # into that global dictionary that allows the
159 # temp_allowed_for_tests.h file to be included in files ending
160 # with _unittest.cc, and the second pair should completely fail
161 # once the bug is fixed, but succeed (with a temporary allowance)
162 # if the bug is in place.
163 problems
= self
.deps_checker
.CheckAddedCppIncludes(
164 [['tools/checkdeps/testdata/allowed/test.cc',
165 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"']
167 ['tools/checkdeps/testdata/disallowed/foo_unittest.cc',
168 ['#include "tools/checkdeps/testdata/bongo/temp_allowed_for_tests.h"']
170 # With the bug in place, there would be two problems reported, and
171 # the second would be for foo_unittest.cc.
172 self
.failUnless(len(problems
) == 1)
173 self
.failUnless(problems
[0][0].endswith('/test.cc'))
176 if __name__
== '__main__':