| 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 import results |
| 15 | 15 |
| 16 | 16 |
| 17 class CheckDepsTest(unittest.TestCase): | 17 class CheckDepsTest(unittest.TestCase): |
| 18 | 18 |
| 19 def setUp(self): | 19 def setUp(self): |
| 20 self.deps_checker = checkdeps.DepsChecker(being_tested=True) | 20 self.deps_checker = checkdeps.DepsChecker(being_tested=True) |
| 21 | 21 |
| 22 def testRegularCheckDepsRun(self): | 22 def testRegularCheckDepsRun(self): |
| 23 self.deps_checker.CheckDirectory( | 23 self.deps_checker.CheckDirectory( |
| 24 os.path.join(self.deps_checker.base_directory, | 24 os.path.join(self.deps_checker.base_directory, |
| 25 'tools/checkdeps/testdata')) | 25 'tools/checkdeps/testdata')) |
| 26 problems = self.deps_checker.results_formatter.GetResults() | 26 problems = self.deps_checker.results_formatter.GetResults() |
| 27 self.failUnlessEqual(3, len(problems)) | 27 self.failUnlessEqual(4, len(problems)) |
| 28 | 28 |
| 29 def VerifySubstringsInProblems(key_path, substrings_in_sequence): | 29 def VerifySubstringsInProblems(key_path, substrings_in_sequence): |
| 30 found = False | 30 found = False |
| 31 key_path = os.path.normpath(key_path) | 31 key_path = os.path.normpath(key_path) |
| 32 for problem in problems: | 32 for problem in problems: |
| 33 index = problem.find(key_path) | 33 index = problem.find(key_path) |
| 34 if index != -1: | 34 if index != -1: |
| 35 for substring in substrings_in_sequence: | 35 for substring in substrings_in_sequence: |
| 36 index = problem.find(substring, index + 1) | 36 index = problem.find(substring, index + 1) |
| 37 self.failUnless(index != -1) | 37 self.failUnless(index != -1, '%s in %s' % (substring, problem)) |
| 38 found = True | 38 found = True |
| 39 break | 39 break |
| 40 if not found: | 40 if not found: |
| 41 self.fail('Found no problem for file %s' % key_path) | 41 self.fail('Found no problem for file %s' % key_path) |
| 42 | 42 |
| 43 VerifySubstringsInProblems('testdata/allowed/test.h', | 43 VerifySubstringsInProblems('testdata/allowed/test.h', |
| 44 ['-tools/checkdeps/testdata/disallowed', | 44 ['-tools/checkdeps/testdata/disallowed', |
| 45 '-third_party/explicitly_disallowed', | 45 '-third_party/explicitly_disallowed', |
| 46 'Because of no rule applying']) | 46 'Because of no rule applying']) |
| 47 VerifySubstringsInProblems('testdata/disallowed/test.h', | 47 VerifySubstringsInProblems('testdata/disallowed/test.h', |
| 48 ['-third_party/explicitly_disallowed', | 48 ['-third_party/explicitly_disallowed', |
| 49 'Because of no rule applying', | 49 'Because of no rule applying', |
| 50 'Because of no rule applying']) | 50 'Because of no rule applying']) |
| 51 VerifySubstringsInProblems('disallowed/allowed/test.h', | 51 VerifySubstringsInProblems('disallowed/allowed/test.h', |
| 52 ['-third_party/explicitly_disallowed', | 52 ['-third_party/explicitly_disallowed', |
| 53 'Because of no rule applying', | 53 'Because of no rule applying', |
| 54 'Because of no rule applying']) | 54 'Because of no rule applying']) |
| 55 VerifySubstringsInProblems('allowed/not_a_test.cc', |
| 56 ['-tools/checkdeps/testdata/disallowed']) |
| 55 | 57 |
| 56 def testTempRulesGenerator(self): | 58 def testTempRulesGenerator(self): |
| 57 self.deps_checker.results_formatter = results.TemporaryRulesFormatter() | 59 self.deps_checker.results_formatter = results.TemporaryRulesFormatter() |
| 58 self.deps_checker.CheckDirectory( | 60 self.deps_checker.CheckDirectory( |
| 59 os.path.join(self.deps_checker.base_directory, | 61 os.path.join(self.deps_checker.base_directory, |
| 60 'tools/checkdeps/testdata/allowed')) | 62 'tools/checkdeps/testdata/allowed')) |
| 61 temp_rules = self.deps_checker.results_formatter.GetResults() | 63 temp_rules = self.deps_checker.results_formatter.GetResults() |
| 62 expected = [u' "!third_party/explicitly_disallowed/bad.h",', | 64 expected = [u' "!third_party/explicitly_disallowed/bad.h",', |
| 63 u' "!third_party/no_rule/bad.h",', | 65 u' "!third_party/no_rule/bad.h",', |
| 64 u' "!tools/checkdeps/testdata/disallowed/bad.h",'] | 66 u' "!tools/checkdeps/testdata/disallowed/bad.h",', |
| 67 u' "!tools/checkdeps/testdata/disallowed/teststuff/bad.h",'] |
| 65 self.failUnlessEqual(expected, temp_rules) | 68 self.failUnlessEqual(expected, temp_rules) |
| 66 | 69 |
| 67 def testCheckAddedIncludesAllGood(self): | 70 def testCheckAddedIncludesAllGood(self): |
| 68 problems = self.deps_checker.CheckAddedCppIncludes( | 71 problems = self.deps_checker.CheckAddedCppIncludes( |
| 69 [['tools/checkdeps/testdata/allowed/test.cc', | 72 [['tools/checkdeps/testdata/allowed/test.cc', |
| 70 ['#include "tools/checkdeps/testdata/allowed/good.h"', | 73 ['#include "tools/checkdeps/testdata/allowed/good.h"', |
| 71 '#include "tools/checkdeps/testdata/disallowed/allowed/good.h"'] | 74 '#include "tools/checkdeps/testdata/disallowed/allowed/good.h"'] |
| 72 ]]) | 75 ]]) |
| 73 self.failIf(problems) | 76 self.failIf(problems) |
| 74 | 77 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 95 def testCheckAddedIncludesTempAllowed(self): | 98 def testCheckAddedIncludesTempAllowed(self): |
| 96 problems = self.deps_checker.CheckAddedCppIncludes( | 99 problems = self.deps_checker.CheckAddedCppIncludes( |
| 97 [['tools/checkdeps/testdata/allowed/test.cc', | 100 [['tools/checkdeps/testdata/allowed/test.cc', |
| 98 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"'] | 101 ['#include "tools/checkdeps/testdata/disallowed/temporarily_allowed.h"'] |
| 99 ]]) | 102 ]]) |
| 100 self.failUnless(problems) | 103 self.failUnless(problems) |
| 101 | 104 |
| 102 | 105 |
| 103 if __name__ == '__main__': | 106 if __name__ == '__main__': |
| 104 unittest.main() | 107 unittest.main() |
| OLD | NEW |