OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests for checkdeps. | 6 """Tests for checkdeps. |
7 """ | 7 """ |
8 | 8 |
9 import os | 9 import os |
10 import unittest | 10 import unittest |
11 | 11 |
12 | 12 |
13 import checkdeps | 13 import checkdeps |
| 14 import results |
14 | 15 |
15 | 16 |
16 class CheckDepsTest(unittest.TestCase): | 17 class CheckDepsTest(unittest.TestCase): |
17 | 18 |
18 def setUp(self): | 19 def setUp(self): |
19 self.deps_checker = checkdeps.DepsChecker(being_tested=True) | 20 self.deps_checker = checkdeps.DepsChecker(being_tested=True) |
20 | 21 |
21 def testRegularCheckDepsRun(self): | 22 def testRegularCheckDepsRun(self): |
22 problems = self.deps_checker.CheckDirectory( | 23 self.deps_checker.CheckDirectory( |
23 os.path.join(self.deps_checker.base_directory, | 24 os.path.join(self.deps_checker.base_directory, |
24 'tools/checkdeps/testdata')) | 25 'tools/checkdeps/testdata')) |
| 26 problems = self.deps_checker.results_formatter.GetResults() |
25 self.failUnlessEqual(3, len(problems)) | 27 self.failUnlessEqual(3, len(problems)) |
26 | 28 |
27 def VerifySubstringsInProblems(key_path, substrings_in_sequence): | 29 def VerifySubstringsInProblems(key_path, substrings_in_sequence): |
28 found = False | 30 found = False |
29 key_path = os.path.normpath(key_path) | 31 key_path = os.path.normpath(key_path) |
30 for problem in problems: | 32 for problem in problems: |
31 index = problem.find(key_path) | 33 index = problem.find(key_path) |
32 if index != -1: | 34 if index != -1: |
33 for substring in substrings_in_sequence: | 35 for substring in substrings_in_sequence: |
34 index = problem.find(substring, index + 1) | 36 index = problem.find(substring, index + 1) |
35 self.failUnless(index != -1) | 37 self.failUnless(index != -1) |
36 found = True | 38 found = True |
37 break | 39 break |
38 if not found: | 40 if not found: |
39 self.fail('Found no problem for file %s' % key_path) | 41 self.fail('Found no problem for file %s' % key_path) |
40 | 42 |
41 VerifySubstringsInProblems('testdata/allowed/test.h', | 43 VerifySubstringsInProblems('testdata/allowed/test.h', |
42 ['-tools/checkdeps/testdata/disallowed', | 44 ['-tools/checkdeps/testdata/disallowed', |
43 '-third_party/explicitly_disallowed', | 45 '-third_party/explicitly_disallowed', |
44 'Because of no rule applying']) | 46 'Because of no rule applying']) |
45 VerifySubstringsInProblems('testdata/disallowed/test.h', | 47 VerifySubstringsInProblems('testdata/disallowed/test.h', |
46 ['-third_party/explicitly_disallowed', | 48 ['-third_party/explicitly_disallowed', |
47 'Because of no rule applying', | 49 'Because of no rule applying', |
48 'Because of no rule applying']) | 50 'Because of no rule applying']) |
49 VerifySubstringsInProblems('disallowed/allowed/test.h', | 51 VerifySubstringsInProblems('disallowed/allowed/test.h', |
50 ['-third_party/explicitly_disallowed', | 52 ['-third_party/explicitly_disallowed', |
51 'Because of no rule applying', | 53 'Because of no rule applying', |
52 'Because of no rule applying']) | 54 'Because of no rule applying']) |
53 | 55 |
| 56 def testTempRulesGenerator(self): |
| 57 self.deps_checker.results_formatter = results.TemporaryRulesFormatter() |
| 58 self.deps_checker.CheckDirectory( |
| 59 os.path.join(self.deps_checker.base_directory, |
| 60 'tools/checkdeps/testdata/allowed')) |
| 61 temp_rules = self.deps_checker.results_formatter.GetResults() |
| 62 expected = [u' "!third_party/explicitly_disallowed/bad.h",', |
| 63 u' "!third_party/no_rule/bad.h",', |
| 64 u' "!tools/checkdeps/testdata/disallowed/bad.h",'] |
| 65 self.failUnlessEqual(expected, temp_rules) |
| 66 |
54 def testCheckAddedIncludesAllGood(self): | 67 def testCheckAddedIncludesAllGood(self): |
55 problems = self.deps_checker.CheckAddedCppIncludes( | 68 problems = self.deps_checker.CheckAddedCppIncludes( |
56 [['tools/checkdeps/testdata/allowed/test.cc', | 69 [['tools/checkdeps/testdata/allowed/test.cc', |
57 ['#include "tools/checkdeps/testdata/allowed/good.h"', | 70 ['#include "tools/checkdeps/testdata/allowed/good.h"', |
58 '#include "tools/checkdeps/testdata/disallowed/allowed/good.h"'] | 71 '#include "tools/checkdeps/testdata/disallowed/allowed/good.h"'] |
59 ]]) | 72 ]]) |
60 self.failIf(problems) | 73 self.failIf(problems) |
61 | 74 |
62 def testCheckAddedIncludesManyGarbageLines(self): | 75 def testCheckAddedIncludesManyGarbageLines(self): |
63 garbage_lines = ["My name is Sam%d\n" % num for num in range(50)] | 76 garbage_lines = ["My name is Sam%d\n" % num for num in range(50)] |
(...skipping 18 matching lines...) Expand all Loading... |
82 def testCheckAddedIncludesTempAllowed(self): | 95 def testCheckAddedIncludesTempAllowed(self): |
83 problems = self.deps_checker.CheckAddedCppIncludes( | 96 problems = self.deps_checker.CheckAddedCppIncludes( |
84 [['tools/checkdeps/testdata/allowed/test.cc', | 97 [['tools/checkdeps/testdata/allowed/test.cc', |
85 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"'] | 98 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"'] |
86 ]]) | 99 ]]) |
87 self.failUnless(problems) | 100 self.failUnless(problems) |
88 | 101 |
89 | 102 |
90 if __name__ == '__main__': | 103 if __name__ == '__main__': |
91 unittest.main() | 104 unittest.main() |
OLD | NEW |