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 import glob | |
7 import json | |
8 import os | |
9 import re | 6 import re |
10 import subprocess | 7 import subprocess |
11 import sys | |
12 import unittest | 8 import unittest |
13 | 9 |
14 import PRESUBMIT | 10 import PRESUBMIT |
15 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile | 11 from PRESUBMIT_test_mocks import MockChange, MockFile, MockAffectedFile |
16 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi | 12 from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi |
17 | 13 |
18 _TEST_DATA_DIR = 'base/test/data/presubmit' | 14 _TEST_DATA_DIR = 'base/test/data/presubmit' |
19 | 15 |
20 class IncludeOrderTest(unittest.TestCase): | 16 class IncludeOrderTest(unittest.TestCase): |
21 def testSystemHeaderOrder(self): | 17 def testSystemHeaderOrder(self): |
(...skipping 807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 | 825 |
830 output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()) | 826 output = PRESUBMIT._CheckUserActionUpdate(input_api, MockOutputApi()) |
831 self.assertEqual( | 827 self.assertEqual( |
832 ('File %s line %d: %s is missing in ' | 828 ('File %s line %d: %s is missing in ' |
833 'tools/metrics/actions/actions.xml. Please run ' | 829 'tools/metrics/actions/actions.xml. Please run ' |
834 'tools/metrics/actions/extract_actions.py to update.' | 830 'tools/metrics/actions/extract_actions.py to update.' |
835 % (file_with_user_action, 1, 'NotInActionsXml')), | 831 % (file_with_user_action, 1, 'NotInActionsXml')), |
836 output[0].message) | 832 output[0].message) |
837 | 833 |
838 | 834 |
| 835 class PydepsNeedsUpdatingTest(unittest.TestCase): |
| 836 |
| 837 class MockSubprocess(object): |
| 838 CalledProcessError = subprocess.CalledProcessError |
| 839 |
| 840 def setUp(self): |
| 841 self.old_PYDEPS_FILES = PRESUBMIT._PYDEPS_FILES |
| 842 PRESUBMIT._PYDEPS_FILES = ['A.pydeps', 'B.pydeps'] |
| 843 self.mock_input_api = MockInputApi() |
| 844 self.mock_output_api = MockOutputApi() |
| 845 self.mock_input_api.subprocess = PydepsNeedsUpdatingTest.MockSubprocess() |
| 846 self.checker = PRESUBMIT.PydepsChecker(self.mock_input_api) |
| 847 self.checker._file_cache = { |
| 848 'A.pydeps': '# target: //A.py\n# root: //\nA.py\nC.py\n', |
| 849 'B.pydeps': '# target: //B.py\n# root: //\nB.py\nC.py\n', |
| 850 } |
| 851 |
| 852 def tearDown(self): |
| 853 PRESUBMIT._PYDEPS_FILES = self.old_PYDEPS_FILES |
| 854 |
| 855 def _RunCheck(self): |
| 856 return PRESUBMIT._CheckPydepsNeedsUpdating(self.mock_input_api, |
| 857 self.mock_output_api, |
| 858 checker_for_tests=self.checker) |
| 859 |
| 860 def testAddedPydep(self): |
| 861 self.mock_input_api.files = [ |
| 862 MockAffectedFile('new.pydeps', [], action='A'), |
| 863 ] |
| 864 |
| 865 results = self._RunCheck() |
| 866 self.assertEqual(1, len(results)) |
| 867 self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 868 |
| 869 def testRemovedPydep(self): |
| 870 self.mock_input_api.files = [ |
| 871 MockAffectedFile(PRESUBMIT._PYDEPS_FILES[0], [], action='D'), |
| 872 ] |
| 873 |
| 874 results = self._RunCheck() |
| 875 self.assertEqual(1, len(results)) |
| 876 self.assertTrue('PYDEPS_FILES' in str(results[0])) |
| 877 |
| 878 def testRandomPyIgnored(self): |
| 879 self.mock_input_api.files = [ |
| 880 MockAffectedFile('random.py', []), |
| 881 ] |
| 882 |
| 883 results = self._RunCheck() |
| 884 self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 885 |
| 886 def testRelevantPyNoChange(self): |
| 887 self.mock_input_api.files = [ |
| 888 MockAffectedFile('A.py', []), |
| 889 ] |
| 890 |
| 891 def mock_check_output(cmd): |
| 892 self.assertEqual('A.py', cmd[3]) |
| 893 return self.checker._file_cache['A.pydeps'] |
| 894 |
| 895 self.mock_input_api.subprocess.check_output = mock_check_output |
| 896 |
| 897 results = self._RunCheck() |
| 898 self.assertEqual(0, len(results), 'Unexpected results: %r' % results) |
| 899 |
| 900 def testRelevantPyOneChange(self): |
| 901 self.mock_input_api.files = [ |
| 902 MockAffectedFile('A.py', []), |
| 903 ] |
| 904 |
| 905 def mock_check_output(cmd): |
| 906 self.assertEqual('A.py', cmd[3]) |
| 907 return 'changed data' |
| 908 |
| 909 self.mock_input_api.subprocess.check_output = mock_check_output |
| 910 |
| 911 results = self._RunCheck() |
| 912 self.assertEqual(1, len(results)) |
| 913 self.assertTrue('File is stale' in str(results[0])) |
| 914 |
| 915 def testRelevantPyTwoChanges(self): |
| 916 self.mock_input_api.files = [ |
| 917 MockAffectedFile('C.py', []), |
| 918 ] |
| 919 |
| 920 def mock_check_output(cmd): |
| 921 return 'changed data' |
| 922 |
| 923 self.mock_input_api.subprocess.check_output = mock_check_output |
| 924 |
| 925 results = self._RunCheck() |
| 926 self.assertEqual(2, len(results)) |
| 927 self.assertTrue('File is stale' in str(results[0])) |
| 928 self.assertTrue('File is stale' in str(results[1])) |
| 929 |
| 930 |
839 class LogUsageTest(unittest.TestCase): | 931 class LogUsageTest(unittest.TestCase): |
840 | 932 |
841 def testCheckAndroidCrLogUsage(self): | 933 def testCheckAndroidCrLogUsage(self): |
842 mock_input_api = MockInputApi() | 934 mock_input_api = MockInputApi() |
843 mock_output_api = MockOutputApi() | 935 mock_output_api = MockOutputApi() |
844 | 936 |
845 mock_input_api.files = [ | 937 mock_input_api.files = [ |
846 MockAffectedFile('RandomStuff.java', [ | 938 MockAffectedFile('RandomStuff.java', [ |
847 'random stuff' | 939 'random stuff' |
848 ]), | 940 ]), |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 ['char* host = "https://www.aol.com"; // google.com']) | 1078 ['char* host = "https://www.aol.com"; // google.com']) |
987 ] | 1079 ] |
988 | 1080 |
989 warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( | 1081 warnings = PRESUBMIT._CheckHardcodedGoogleHostsInLowerLayers( |
990 input_api, MockOutputApi()) | 1082 input_api, MockOutputApi()) |
991 self.assertEqual(0, len(warnings)) | 1083 self.assertEqual(0, len(warnings)) |
992 | 1084 |
993 | 1085 |
994 if __name__ == '__main__': | 1086 if __name__ == '__main__': |
995 unittest.main() | 1087 unittest.main() |
OLD | NEW |