OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright 2008-2009 Google Inc. All Rights Reserved. | 3 # Copyright 2008-2009 Google Inc. All Rights Reserved. |
4 # | 4 # |
5 # Licensed under the Apache License, Version 2.0 (the "License"); | 5 # Licensed under the Apache License, Version 2.0 (the "License"); |
6 # you may not use this file except in compliance with the License. | 6 # you may not use this file except in compliance with the License. |
7 # You may obtain a copy of the License at | 7 # You may obtain a copy of the License at |
8 # | 8 # |
9 # http://www.apache.org/licenses/LICENSE-2.0 | 9 # http://www.apache.org/licenses/LICENSE-2.0 |
10 # | 10 # |
11 # Unless required by applicable law or agreed to in writing, software | 11 # Unless required by applicable law or agreed to in writing, software |
12 # distributed under the License is distributed on an "AS IS" BASIS, | 12 # distributed under the License is distributed on an "AS IS" BASIS, |
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 # See the License for the specific language governing permissions and | 14 # See the License for the specific language governing permissions and |
15 # limitations under the License. | 15 # limitations under the License. |
16 | 16 |
17 """Unit tests for gclient.py.""" | 17 """Unit tests for gclient.py.""" |
18 | 18 |
19 __author__ = 'stephen5.ng@gmail.com (Stephen Ng)' | 19 __author__ = 'stephen5.ng@gmail.com (Stephen Ng)' |
20 | 20 |
21 import __builtin__ | 21 import __builtin__ |
22 import StringIO | 22 import StringIO |
23 | 23 |
24 import gclient | 24 import gclient |
| 25 # Temporary due to the "from scm import *" in gclient_scm. |
| 26 import scm |
25 from super_mox import mox, IsOneOf, SuperMoxTestBase | 27 from super_mox import mox, IsOneOf, SuperMoxTestBase |
26 | 28 |
27 | 29 |
28 class BaseTestCase(SuperMoxTestBase): | 30 class BaseTestCase(SuperMoxTestBase): |
29 # Like unittest's assertRaises, but checks for Gclient.Error. | 31 # Like unittest's assertRaises, but checks for Gclient.Error. |
30 def assertRaisesError(self, msg, fn, *args, **kwargs): | 32 def assertRaisesError(self, msg, fn, *args, **kwargs): |
31 try: | 33 try: |
32 fn(*args, **kwargs) | 34 fn(*args, **kwargs) |
33 except gclient.gclient_utils.Error, e: | 35 except gclient.gclient_utils.Error, e: |
34 self.assertEquals(e.args[0], msg) | 36 self.assertEquals(e.args[0], msg) |
(...skipping 11 matching lines...) Expand all Loading... |
46 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileRead') | 48 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileRead') |
47 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileWrite') | 49 self.mox.StubOutWithMock(gclient.gclient_utils, 'FileWrite') |
48 self.mox.StubOutWithMock(gclient.gclient_utils, 'SubprocessCall') | 50 self.mox.StubOutWithMock(gclient.gclient_utils, 'SubprocessCall') |
49 self.mox.StubOutWithMock(gclient.gclient_utils, 'RemoveDirectory') | 51 self.mox.StubOutWithMock(gclient.gclient_utils, 'RemoveDirectory') |
50 # Mock them to be sure nothing bad happens. | 52 # Mock them to be sure nothing bad happens. |
51 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') | 53 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') |
52 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNInfo') | 54 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNInfo') |
53 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNStatus') | 55 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVNStatus') |
54 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVN') | 56 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVN') |
55 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVNAndGetFileList') | 57 self.mox.StubOutWithMock(gclient.gclient_scm, 'RunSVNAndGetFileList') |
| 58 self.mox.StubOutWithMock(scm, 'CaptureSVN') |
| 59 self.mox.StubOutWithMock(scm, 'CaptureSVNInfo') |
| 60 self.mox.StubOutWithMock(scm, 'CaptureSVNStatus') |
| 61 self.mox.StubOutWithMock(scm, 'RunSVN') |
| 62 self.mox.StubOutWithMock(scm, 'RunSVNAndGetFileList') |
56 self._gclient_gclient = gclient.GClient | 63 self._gclient_gclient = gclient.GClient |
57 gclient.GClient = self.mox.CreateMockAnything() | 64 gclient.GClient = self.mox.CreateMockAnything() |
58 self._scm_wrapper = gclient.gclient_scm.CreateSCM | 65 self._scm_wrapper = gclient.gclient_scm.CreateSCM |
59 gclient.gclient_scm.CreateSCM = self.mox.CreateMockAnything() | 66 gclient.gclient_scm.CreateSCM = self.mox.CreateMockAnything() |
60 | 67 |
61 def tearDown(self): | 68 def tearDown(self): |
62 gclient.GClient = self._gclient_gclient | 69 gclient.GClient = self._gclient_gclient |
63 gclient.gclient_scm.CreateSCM = self._scm_wrapper | 70 gclient.gclient_scm.CreateSCM = self._scm_wrapper |
64 BaseTestCase.tearDown(self) | 71 BaseTestCase.tearDown(self) |
65 | 72 |
(...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1059 def test_LoadConfig(self): | 1066 def test_LoadConfig(self): |
1060 pass | 1067 pass |
1061 def test_ReadEntries(self): | 1068 def test_ReadEntries(self): |
1062 pass | 1069 pass |
1063 def test_SaveEntries(self): | 1070 def test_SaveEntries(self): |
1064 pass | 1071 pass |
1065 def test_VarImpl(self): | 1072 def test_VarImpl(self): |
1066 pass | 1073 pass |
1067 | 1074 |
1068 | 1075 |
1069 class SubprocessCallAndFilterTestCase(BaseTestCase): | |
1070 def setUp(self): | |
1071 BaseTestCase.setUp(self) | |
1072 self.mox.StubOutWithMock(gclient.gclient_scm, 'CaptureSVN') | |
1073 | |
1074 def testSubprocessCallAndFilter(self): | |
1075 command = ['boo', 'foo', 'bar'] | |
1076 in_directory = 'bleh' | |
1077 fail_status = None | |
1078 pattern = 'a(.*)b' | |
1079 test_string = 'ahah\naccb\nallo\naddb\n' | |
1080 class Mock(object): | |
1081 stdout = StringIO.StringIO(test_string) | |
1082 def wait(self): | |
1083 pass | |
1084 kid = Mock() | |
1085 print("\n________ running 'boo foo bar' in 'bleh'") | |
1086 for i in test_string: | |
1087 gclient.sys.stdout.write(i) | |
1088 gclient.gclient_utils.subprocess.Popen( | |
1089 command, bufsize=0, cwd=in_directory, | |
1090 shell=(gclient.sys.platform == 'win32'), | |
1091 stdout=gclient.gclient_utils.subprocess.PIPE, | |
1092 stderr=gclient.gclient_utils.subprocess.STDOUT).AndReturn(kid) | |
1093 self.mox.ReplayAll() | |
1094 compiled_pattern = gclient.re.compile(pattern) | |
1095 line_list = [] | |
1096 capture_list = [] | |
1097 def FilterLines(line): | |
1098 line_list.append(line) | |
1099 match = compiled_pattern.search(line) | |
1100 if match: | |
1101 capture_list.append(match.group(1)) | |
1102 gclient.gclient_utils.SubprocessCallAndFilter( | |
1103 command, in_directory, | |
1104 True, True, | |
1105 fail_status, FilterLines) | |
1106 self.assertEquals(line_list, ['ahah', 'accb', 'allo', 'addb']) | |
1107 self.assertEquals(capture_list, ['cc', 'dd']) | |
1108 | |
1109 def testCaptureSVNStatus(self): | |
1110 gclient.gclient_scm.CaptureSVN( | |
1111 ['status', '--xml', '.'] | |
1112 ).AndReturn(r"""<?xml version="1.0"?> | |
1113 <status> | |
1114 <target path="."> | |
1115 <entry path="unversionned_file.txt"> | |
1116 <wc-status props="none" item="unversioned"></wc-status> | |
1117 </entry> | |
1118 <entry path="build\internal\essential.vsprops"> | |
1119 <wc-status props="normal" item="modified" revision="14628"> | |
1120 <commit revision="13818"> | |
1121 <author>ajwong@chromium.org</author> | |
1122 <date>2009-04-16T00:42:06.872358Z</date> | |
1123 </commit> | |
1124 </wc-status> | |
1125 </entry> | |
1126 <entry path="chrome\app\d"> | |
1127 <wc-status props="none" copied="true" tree-conflicted="true" item="added"> | |
1128 </wc-status> | |
1129 </entry> | |
1130 <entry path="chrome\app\DEPS"> | |
1131 <wc-status props="modified" item="modified" revision="14628"> | |
1132 <commit revision="1279"> | |
1133 <author>brettw@google.com</author> | |
1134 <date>2008-08-23T17:16:42.090152Z</date> | |
1135 </commit> | |
1136 </wc-status> | |
1137 </entry> | |
1138 <entry path="scripts\master\factory\gclient_factory.py"> | |
1139 <wc-status props="normal" item="conflicted" revision="14725"> | |
1140 <commit revision="14633"> | |
1141 <author>nsylvain@chromium.org</author> | |
1142 <date>2009-04-27T19:37:17.977400Z</date> | |
1143 </commit> | |
1144 </wc-status> | |
1145 </entry> | |
1146 </target> | |
1147 </status> | |
1148 """) | |
1149 self.mox.ReplayAll() | |
1150 info = gclient.gclient_scm.CaptureSVNStatus('.') | |
1151 expected = [ | |
1152 ('? ', 'unversionned_file.txt'), | |
1153 ('M ', 'build\\internal\\essential.vsprops'), | |
1154 ('A + ', 'chrome\\app\\d'), | |
1155 ('MM ', 'chrome\\app\\DEPS'), | |
1156 ('C ', 'scripts\\master\\factory\\gclient_factory.py'), | |
1157 ] | |
1158 self.assertEquals(sorted(info), sorted(expected)) | |
1159 | |
1160 def testCaptureSVNStatusEmpty(self): | |
1161 gclient.gclient_scm.CaptureSVN( | |
1162 ['status', '--xml'] | |
1163 ).AndReturn(r"""<?xml version="1.0"?> | |
1164 <status> | |
1165 <target | |
1166 path="perf"> | |
1167 </target> | |
1168 </status> | |
1169 """) | |
1170 self.mox.ReplayAll() | |
1171 info = gclient.gclient_scm.CaptureSVNStatus(None) | |
1172 self.assertEquals(info, []) | |
1173 | |
1174 | |
1175 if __name__ == '__main__': | 1076 if __name__ == '__main__': |
1176 import unittest | 1077 import unittest |
1177 unittest.main() | 1078 unittest.main() |
1178 | 1079 |
1179 # vim: ts=2:sw=2:tw=80:et: | 1080 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |