| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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. | |
| 5 | |
| 6 """Tests for checkdeps. | |
| 7 """ | |
| 8 | |
| 9 import os | |
| 10 import unittest | |
| 11 | |
| 12 | |
| 13 import checkdeps | |
| 14 import results | |
| 15 | |
| 16 | |
| 17 class CheckDepsTest(unittest.TestCase): | |
| 18 | |
| 19 def setUp(self): | |
| 20 self.deps_checker = checkdeps.DepsChecker(being_tested=True) | |
| 21 | |
| 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')) | |
| 28 | |
| 29 problems = self.deps_checker.results_formatter.GetResults() | |
| 30 if skip_tests: | |
| 31 self.failUnlessEqual(3, len(problems)) | |
| 32 else: | |
| 33 self.failUnlessEqual(4, len(problems)) | |
| 34 | |
| 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|. | |
| 40 """ | |
| 41 found = False | |
| 42 key_path = os.path.normpath(key_path) | |
| 43 for problem in problems: | |
| 44 index = problem.find(key_path) | |
| 45 if index != -1: | |
| 46 for substring in substrings_in_sequence: | |
| 47 index = problem.find(substring, index + 1) | |
| 48 self.failUnless(index != -1, '%s in %s' % (substring, problem)) | |
| 49 found = True | |
| 50 break | |
| 51 if not found: | |
| 52 self.fail('Found no problem for file %s' % key_path) | |
| 53 | |
| 54 if ignore_temp_rules: | |
| 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']) | |
| 60 else: | |
| 61 VerifySubstringsInProblems('testdata/allowed/test.h', | |
| 62 ['-tools/checkdeps/testdata/disallowed', | |
| 63 '-third_party/explicitly_disallowed', | |
| 64 'Because of no rule applying']) | |
| 65 | |
| 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']) | |
| 74 | |
| 75 if not skip_tests: | |
| 76 VerifySubstringsInProblems('allowed/not_a_test.cc', | |
| 77 ['-tools/checkdeps/testdata/disallowed']) | |
| 78 | |
| 79 def testRegularCheckDepsRun(self): | |
| 80 self.ImplTestRegularCheckDepsRun(False, False) | |
| 81 | |
| 82 def testRegularCheckDepsRunIgnoringTempRules(self): | |
| 83 self.ImplTestRegularCheckDepsRun(True, False) | |
| 84 | |
| 85 def testRegularCheckDepsRunSkipTests(self): | |
| 86 self.ImplTestRegularCheckDepsRun(False, True) | |
| 87 | |
| 88 def testRegularCheckDepsRunIgnoringTempRulesSkipTests(self): | |
| 89 self.ImplTestRegularCheckDepsRun(True, True) | |
| 90 | |
| 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() | |
| 98 | |
| 99 def testCountViolations(self): | |
| 100 self.failUnlessEqual('10', self.CountViolations(False)) | |
| 101 | |
| 102 def testCountViolationsIgnoringTempRules(self): | |
| 103 self.failUnlessEqual('11', self.CountViolations(True)) | |
| 104 | |
| 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) | |
| 116 | |
| 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"'] | |
| 122 ]]) | |
| 123 self.failIf(problems) | |
| 124 | |
| 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) | |
| 130 | |
| 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"'] | |
| 135 ]]) | |
| 136 self.failUnless(problems) | |
| 137 | |
| 138 def testCheckAddedIncludesSkippedDirectory(self): | |
| 139 problems = self.deps_checker.CheckAddedCppIncludes( | |
| 140 [['tools/checkdeps/testdata/disallowed/allowed/skipped/test.cc', | |
| 141 ['#include "whatever/whocares.h"'] | |
| 142 ]]) | |
| 143 self.failIf(problems) | |
| 144 | |
| 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"'] | |
| 149 ]]) | |
| 150 self.failUnless(problems) | |
| 151 | |
| 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. | |
| 156 # | |
| 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"'] | |
| 166 ], | |
| 167 ['tools/checkdeps/testdata/disallowed/foo_unittest.cc', | |
| 168 ['#include "tools/checkdeps/testdata/bongo/temp_allowed_for_tests.h"'] | |
| 169 ]]) | |
| 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')) | |
| 174 | |
| 175 | |
| 176 if __name__ == '__main__': | |
| 177 unittest.main() | |
| OLD | NEW |