| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2006-2009 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 """Unit tests for scm.py.""" | 6 """Unit tests for scm.py.""" |
| 7 | 7 |
| 8 import shutil | |
| 9 import tempfile | |
| 10 | |
| 11 from gclient_test import BaseTestCase | 8 from gclient_test import BaseTestCase |
| 12 import scm | 9 import scm |
| 13 from super_mox import mox, SuperMoxBaseTestBase | 10 from super_mox import mox |
| 14 | 11 |
| 15 | 12 |
| 16 class BaseSCMTestCase(BaseTestCase): | 13 class BaseSCMTestCase(BaseTestCase): |
| 17 def setUp(self): | 14 def setUp(self): |
| 18 BaseTestCase.setUp(self) | 15 BaseTestCase.setUp(self) |
| 19 self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCall') | 16 self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCall') |
| 20 self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCallAndFilter') | 17 self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCallAndFilter') |
| 21 | 18 |
| 22 | 19 |
| 23 class RootTestCase(BaseSCMTestCase): | 20 class RootTestCase(BaseSCMTestCase): |
| 24 def testMembersChanged(self): | 21 def testMembersChanged(self): |
| 25 self.mox.ReplayAll() | 22 self.mox.ReplayAll() |
| 26 members = [ | 23 members = [ |
| 27 'GIT', 'SVN', | 24 'CaptureGit', 'CaptureGitStatus', 'GIT_COMMAND', |
| 28 'gclient_utils', 'os', 're', 'subprocess', 'sys', 'tempfile', 'xml', | 25 'CaptureSVN', 'CaptureSVNHeadRevision', 'CaptureSVNInfo', |
| 26 'CaptureSVNStatus', 'RunSVN', 'RunSVNAndFilterOutput', |
| 27 'RunSVNAndGetFileList', 'SVN_COMMAND', |
| 28 'gclient_utils', 'os', 're', 'subprocess', 'sys', 'xml', |
| 29 ] | 29 ] |
| 30 # If this test fails, you should add the relevant test. | 30 # If this test fails, you should add the relevant test. |
| 31 self.compareMembers(scm, members) | 31 self.compareMembers(scm, members) |
| 32 | 32 |
| 33 | 33 |
| 34 class GitWrapperTestCase(SuperMoxBaseTestBase): | 34 class GitWrapperTestCase(BaseSCMTestCase): |
| 35 sample_git_import = """blob | 35 sample_git_import = """blob |
| 36 mark :1 | 36 mark :1 |
| 37 data 6 | 37 data 6 |
| 38 Hello | 38 Hello |
| 39 | 39 |
| 40 blob | 40 blob |
| 41 mark :2 | 41 mark :2 |
| 42 data 4 | 42 data 4 |
| 43 Bye | 43 Bye |
| 44 | 44 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 73 from :3 | 73 from :3 |
| 74 M 100644 :4 a | 74 M 100644 :4 a |
| 75 M 100644 :5 b | 75 M 100644 :5 b |
| 76 | 76 |
| 77 reset refs/heads/master | 77 reset refs/heads/master |
| 78 from :3 | 78 from :3 |
| 79 """ | 79 """ |
| 80 | 80 |
| 81 def CreateGitRepo(self, git_import, path): | 81 def CreateGitRepo(self, git_import, path): |
| 82 try: | 82 try: |
| 83 scm.subprocess.Popen(['git', 'init'], | 83 subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE, |
| 84 stdout=scm.subprocess.PIPE, | 84 stderr=subprocess.STDOUT, cwd=path).communicate() |
| 85 stderr=scm.subprocess.STDOUT, | 85 except WindowsError: |
| 86 cwd=path).communicate() | |
| 87 except OSError: | |
| 88 # git is not available, skip this test. | 86 # git is not available, skip this test. |
| 89 return False | 87 return False |
| 90 scm.subprocess.Popen(['git', 'fast-import'], | 88 subprocess.Popen(['git', 'fast-import'], stdin=subprocess.PIPE, |
| 91 stdin=scm.subprocess.PIPE, | 89 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 92 stdout=scm.subprocess.PIPE, | 90 cwd=path).communicate(input=git_import) |
| 93 stderr=scm.subprocess.STDOUT, | 91 subprocess.Popen(['git', 'checkout'], stdout=subprocess.PIPE, |
| 94 cwd=path).communicate(input=git_import) | 92 stderr=subprocess.STDOUT, cwd=path).communicate() |
| 95 scm.subprocess.Popen(['git', 'checkout'], | |
| 96 stdout=scm.subprocess.PIPE, | |
| 97 stderr=scm.subprocess.STDOUT, | |
| 98 cwd=path).communicate() | |
| 99 return True | 93 return True |
| 100 | 94 |
| 101 def setUp(self): | 95 def setUp(self): |
| 102 SuperMoxBaseTestBase.setUp(self) | 96 BaseSCMTestCase.setUp(self) |
| 103 self.args = self.Args() | 97 self.args = self.Args() |
| 104 self.url = 'git://foo' | 98 self.url = 'git://foo' |
| 105 self.root_dir = tempfile.mkdtemp() | 99 self.root_dir = tempfile.mkdtemp() |
| 106 self.relpath = '.' | 100 self.relpath = '.' |
| 107 self.base_path = scm.os.path.join(self.root_dir, self.relpath) | 101 self.base_path = os.path.join(self.root_dir, self.relpath) |
| 108 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) | 102 self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) |
| 109 | 103 |
| 110 def tearDown(self): | 104 def tearDown(self): |
| 111 shutil.rmtree(self.root_dir) | 105 shutil.rmtree(self.root_dir) |
| 112 SuperMoxBaseTestBase.tearDown(self) | 106 gclient_test.BaseTestCase.tearDown(self) |
| 113 | |
| 114 def testMembersChanged(self): | |
| 115 self.mox.ReplayAll() | |
| 116 members = [ | |
| 117 'COMMAND', 'Capture', 'CaptureStatus', | |
| 118 ] | |
| 119 # If this test fails, you should add the relevant test. | |
| 120 self.compareMembers(scm.GIT, members) | |
| 121 | 107 |
| 122 | 108 |
| 123 class SVNTestCase(BaseSCMTestCase): | 109 class SVNTestCase(BaseSCMTestCase): |
| 124 def setUp(self): | 110 def setUp(self): |
| 125 BaseSCMTestCase.setUp(self) | 111 BaseSCMTestCase.setUp(self) |
| 126 self.root_dir = self.Dir() | 112 self.root_dir = self.Dir() |
| 127 self.args = self.Args() | 113 self.args = self.Args() |
| 128 self.url = self.Url() | 114 self.url = self.Url() |
| 129 self.relpath = 'asf' | 115 self.relpath = 'asf' |
| 130 | 116 |
| 131 def testMembersChanged(self): | 117 def testGetSVNFileInfo(self): |
| 132 self.mox.ReplayAll() | |
| 133 members = [ | |
| 134 'COMMAND', 'Capture', 'CaptureHeadRevision', 'CaptureInfo', | |
| 135 'CaptureStatus', 'DiffItem', 'GetFileProperty', 'IsMoved', 'Run', | |
| 136 'RunAndFilterOutput', 'RunAndGetFileList', | |
| 137 ] | |
| 138 # If this test fails, you should add the relevant test. | |
| 139 self.compareMembers(scm.SVN, members) | |
| 140 | |
| 141 def testGetFileInfo(self): | |
| 142 xml_text = r"""<?xml version="1.0"?> | 118 xml_text = r"""<?xml version="1.0"?> |
| 143 <info> | 119 <info> |
| 144 <entry kind="file" path="%s" revision="14628"> | 120 <entry kind="file" path="%s" revision="14628"> |
| 145 <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url> | 121 <url>http://src.chromium.org/svn/trunk/src/chrome/app/d</url> |
| 146 <repository><root>http://src.chromium.org/svn</root></repository> | 122 <repository><root>http://src.chromium.org/svn</root></repository> |
| 147 <wc-info> | 123 <wc-info> |
| 148 <schedule>add</schedule> | 124 <schedule>add</schedule> |
| 149 <depth>infinity</depth> | 125 <depth>infinity</depth> |
| 150 <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-
url> | 126 <copy-from-url>http://src.chromium.org/svn/trunk/src/chrome/app/DEPS</copy-from-
url> |
| 151 <copy-from-rev>14628</copy-from-rev> | 127 <copy-from-rev>14628</copy-from-rev> |
| 152 <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum> | 128 <checksum>369f59057ba0e6d9017e28f8bdfb1f43</checksum> |
| 153 </wc-info> | 129 </wc-info> |
| 154 </entry> | 130 </entry> |
| 155 </info> | 131 </info> |
| 156 """ % self.url | 132 """ % self.url |
| 157 self.mox.StubOutWithMock(scm.SVN, 'Capture') | 133 self.mox.StubOutWithMock(scm, 'CaptureSVN') |
| 158 scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text) | 134 scm.CaptureSVN(['info', '--xml', self.url], '.', True).AndReturn(xml_text) |
| 159 expected = { | 135 expected = { |
| 160 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', | 136 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', |
| 161 'UUID': None, | 137 'UUID': None, |
| 162 'Repository Root': 'http://src.chromium.org/svn', | 138 'Repository Root': 'http://src.chromium.org/svn', |
| 163 'Schedule': 'add', | 139 'Schedule': 'add', |
| 164 'Copied From URL': | 140 'Copied From URL': |
| 165 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS', | 141 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS', |
| 166 'Copied From Rev': '14628', | 142 'Copied From Rev': '14628', |
| 167 'Path': self.url, | 143 'Path': self.url, |
| 168 'Revision': 14628, | 144 'Revision': 14628, |
| 169 'Node Kind': 'file', | 145 'Node Kind': 'file', |
| 170 } | 146 } |
| 171 self.mox.ReplayAll() | 147 self.mox.ReplayAll() |
| 172 file_info = scm.SVN.CaptureInfo(self.url, '.', True) | 148 file_info = scm.CaptureSVNInfo(self.url, '.', True) |
| 173 self.assertEquals(sorted(file_info.items()), sorted(expected.items())) | 149 self.assertEquals(sorted(file_info.items()), sorted(expected.items())) |
| 174 | 150 |
| 175 def testCaptureInfo(self): | 151 def testCaptureSvnInfo(self): |
| 176 xml_text = """<?xml version="1.0"?> | 152 xml_text = """<?xml version="1.0"?> |
| 177 <info> | 153 <info> |
| 178 <entry | 154 <entry |
| 179 kind="dir" | 155 kind="dir" |
| 180 path="." | 156 path="." |
| 181 revision="35"> | 157 revision="35"> |
| 182 <url>%s</url> | 158 <url>%s</url> |
| 183 <repository> | 159 <repository> |
| 184 <root>%s</root> | 160 <root>%s</root> |
| 185 <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> | 161 <uuid>7b9385f5-0452-0410-af26-ad4892b7a1fb</uuid> |
| 186 </repository> | 162 </repository> |
| 187 <wc-info> | 163 <wc-info> |
| 188 <schedule>normal</schedule> | 164 <schedule>normal</schedule> |
| 189 <depth>infinity</depth> | 165 <depth>infinity</depth> |
| 190 </wc-info> | 166 </wc-info> |
| 191 <commit | 167 <commit |
| 192 revision="35"> | 168 revision="35"> |
| 193 <author>maruel</author> | 169 <author>maruel</author> |
| 194 <date>2008-12-04T20:12:19.685120Z</date> | 170 <date>2008-12-04T20:12:19.685120Z</date> |
| 195 </commit> | 171 </commit> |
| 196 </entry> | 172 </entry> |
| 197 </info> | 173 </info> |
| 198 """ % (self.url, self.root_dir) | 174 """ % (self.url, self.root_dir) |
| 199 self.mox.StubOutWithMock(scm.SVN, 'Capture') | 175 self.mox.StubOutWithMock(scm, 'CaptureSVN') |
| 200 scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text) | 176 scm.CaptureSVN(['info', '--xml', self.url], '.', True).AndReturn(xml_text) |
| 201 self.mox.ReplayAll() | 177 self.mox.ReplayAll() |
| 202 file_info = scm.SVN.CaptureInfo(self.url, '.', True) | 178 file_info = scm.CaptureSVNInfo(self.url, '.', True) |
| 203 expected = { | 179 expected = { |
| 204 'URL': self.url, | 180 'URL': self.url, |
| 205 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', | 181 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', |
| 206 'Revision': 35, | 182 'Revision': 35, |
| 207 'Repository Root': self.root_dir, | 183 'Repository Root': self.root_dir, |
| 208 'Schedule': 'normal', | 184 'Schedule': 'normal', |
| 209 'Copied From URL': None, | 185 'Copied From URL': None, |
| 210 'Copied From Rev': None, | 186 'Copied From Rev': None, |
| 211 'Path': '.', | 187 'Path': '.', |
| 212 'Node Kind': 'directory', | 188 'Node Kind': 'dir', |
| 213 } | 189 } |
| 214 self.assertEqual(file_info, expected) | 190 self.assertEqual(file_info, expected) |
| 215 | 191 |
| 216 def testCaptureStatus(self): | 192 def testCaptureSVNStatus(self): |
| 217 text =r"""<?xml version="1.0"?> | 193 text =r"""<?xml version="1.0"?> |
| 218 <status> | 194 <status> |
| 219 <target path="."> | 195 <target path="."> |
| 220 <entry path="unversionned_file.txt"> | 196 <entry path="unversionned_file.txt"> |
| 221 <wc-status props="none" item="unversioned"></wc-status> | 197 <wc-status props="none" item="unversioned"></wc-status> |
| 222 </entry> | 198 </entry> |
| 223 <entry path="build\internal\essential.vsprops"> | 199 <entry path="build\internal\essential.vsprops"> |
| 224 <wc-status props="normal" item="modified" revision="14628"> | 200 <wc-status props="normal" item="modified" revision="14628"> |
| 225 <commit revision="13818"> | 201 <commit revision="13818"> |
| 226 <author>ajwong@chromium.org</author> | 202 <author>ajwong@chromium.org</author> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 253 """ | 229 """ |
| 254 proc = self.mox.CreateMockAnything() | 230 proc = self.mox.CreateMockAnything() |
| 255 scm.subprocess.Popen(['svn', 'status', '--xml', '.'], | 231 scm.subprocess.Popen(['svn', 'status', '--xml', '.'], |
| 256 cwd=None, | 232 cwd=None, |
| 257 shell=scm.sys.platform.startswith('win'), | 233 shell=scm.sys.platform.startswith('win'), |
| 258 stderr=None, | 234 stderr=None, |
| 259 stdout=scm.subprocess.PIPE).AndReturn(proc) | 235 stdout=scm.subprocess.PIPE).AndReturn(proc) |
| 260 proc.communicate().AndReturn((text, 0)) | 236 proc.communicate().AndReturn((text, 0)) |
| 261 | 237 |
| 262 self.mox.ReplayAll() | 238 self.mox.ReplayAll() |
| 263 info = scm.SVN.CaptureStatus('.') | 239 info = scm.CaptureSVNStatus('.') |
| 264 expected = [ | 240 expected = [ |
| 265 ('? ', 'unversionned_file.txt'), | 241 ('? ', 'unversionned_file.txt'), |
| 266 ('M ', 'build\\internal\\essential.vsprops'), | 242 ('M ', 'build\\internal\\essential.vsprops'), |
| 267 ('A + ', 'chrome\\app\\d'), | 243 ('A + ', 'chrome\\app\\d'), |
| 268 ('MM ', 'chrome\\app\\DEPS'), | 244 ('MM ', 'chrome\\app\\DEPS'), |
| 269 ('C ', 'scripts\\master\\factory\\gclient_factory.py'), | 245 ('C ', 'scripts\\master\\factory\\gclient_factory.py'), |
| 270 ] | 246 ] |
| 271 self.assertEquals(sorted(info), sorted(expected)) | 247 self.assertEquals(sorted(info), sorted(expected)) |
| 272 | 248 |
| 273 def testRun(self): | 249 def testRunSVN(self): |
| 274 param2 = 'bleh' | 250 param2 = 'bleh' |
| 275 scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], | 251 scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], |
| 276 param2).AndReturn(None) | 252 param2).AndReturn(None) |
| 277 self.mox.ReplayAll() | 253 self.mox.ReplayAll() |
| 278 scm.SVN.Run(['foo', 'bar'], param2) | 254 scm.RunSVN(['foo', 'bar'], param2) |
| 279 | 255 |
| 280 def testCaptureStatusEmpty(self): | 256 def testCaptureSVNStatusEmpty(self): |
| 281 text = r"""<?xml version="1.0"?> | 257 text = r"""<?xml version="1.0"?> |
| 282 <status> | 258 <status> |
| 283 <target | 259 <target |
| 284 path="perf"> | 260 path="perf"> |
| 285 </target> | 261 </target> |
| 286 </status>""" | 262 </status>""" |
| 287 proc = self.mox.CreateMockAnything() | 263 proc = self.mox.CreateMockAnything() |
| 288 scm.subprocess.Popen(['svn', 'status', '--xml'], | 264 scm.subprocess.Popen(['svn', 'status', '--xml'], |
| 289 cwd=None, | 265 cwd=None, |
| 290 shell=scm.sys.platform.startswith('win'), | 266 shell=scm.sys.platform.startswith('win'), |
| 291 stderr=None, | 267 stderr=None, |
| 292 stdout=scm.subprocess.PIPE).AndReturn(proc) | 268 stdout=scm.subprocess.PIPE).AndReturn(proc) |
| 293 proc.communicate().AndReturn((text, 0)) | 269 proc.communicate().AndReturn((text, 0)) |
| 294 self.mox.ReplayAll() | 270 self.mox.ReplayAll() |
| 295 info = scm.SVN.CaptureStatus(None) | 271 info = scm.CaptureSVNStatus(None) |
| 296 self.assertEquals(info, []) | 272 self.assertEquals(info, []) |
| 297 | 273 |
| 298 | 274 |
| 299 if __name__ == '__main__': | 275 if __name__ == '__main__': |
| 300 import unittest | 276 import unittest |
| 301 unittest.main() | 277 unittest.main() |
| 302 | 278 |
| 303 # vim: ts=2:sw=2:tw=80:et: | 279 # vim: ts=2:sw=2:tw=80:et: |
| OLD | NEW |