#!/usr/bin/python # Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Unit tests for scm.py.""" import shutil import tempfile from gclient_test import BaseTestCase import scm from super_mox import mox, SuperMoxBaseTestBase class BaseSCMTestCase(BaseTestCase): def setUp(self): BaseTestCase.setUp(self) self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCall') self.mox.StubOutWithMock(scm.gclient_utils, 'SubprocessCallAndFilter') class RootTestCase(BaseSCMTestCase): def testMembersChanged(self): self.mox.ReplayAll() members = [ 'GetCasedPath', 'GIT', 'SVN', 'ValidateEmail', 'gclient_utils', 'glob', 'os', 're', 'shutil', 'subprocess', 'sys', 'tempfile', 'xml', ] # If this test fails, you should add the relevant test. self.compareMembers(scm, members) class GitWrapperTestCase(SuperMoxBaseTestBase): sample_git_import = """blob mark :1 data 6 Hello blob mark :2 data 4 Bye reset refs/heads/master commit refs/heads/master mark :3 author Bob 1253744361 -0700 committer Bob 1253744361 -0700 data 8 A and B M 100644 :1 a M 100644 :2 b blob mark :4 data 10 Hello You blob mark :5 data 8 Bye You commit refs/heads/origin mark :6 author Alice 1253744424 -0700 committer Alice 1253744424 -0700 data 13 Personalized from :3 M 100644 :4 a M 100644 :5 b reset refs/heads/master from :3 """ def CreateGitRepo(self, git_import, path): try: scm.subprocess.Popen(['git', 'init'], stdout=scm.subprocess.PIPE, stderr=scm.subprocess.STDOUT, cwd=path).communicate() except OSError: # git is not available, skip this test. return False scm.subprocess.Popen(['git', 'fast-import'], stdin=scm.subprocess.PIPE, stdout=scm.subprocess.PIPE, stderr=scm.subprocess.STDOUT, cwd=path).communicate(input=git_import) scm.subprocess.Popen(['git', 'checkout'], stdout=scm.subprocess.PIPE, stderr=scm.subprocess.STDOUT, cwd=path).communicate() return True def setUp(self): SuperMoxBaseTestBase.setUp(self) self.args = self.Args() self.url = 'git://foo' self.root_dir = tempfile.mkdtemp() self.relpath = '.' self.base_path = scm.os.path.join(self.root_dir, self.relpath) self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path) self.fake_root = self.Dir() def tearDown(self): shutil.rmtree(self.root_dir) SuperMoxBaseTestBase.tearDown(self) def testMembersChanged(self): self.mox.ReplayAll() members = [ 'COMMAND', 'Capture', 'CaptureStatus', 'FetchUpstreamTuple', 'GenerateDiff', 'GetBranch', 'GetBranchRef', 'GetCheckoutRoot', 'GetDifferentFiles', 'GetEmail', 'GetPatchName', 'GetSVNBranch', 'GetUpstream', 'IsGitSvn', 'RunAndFilterOutput', 'ShortBranchName', ] # If this test fails, you should add the relevant test. self.compareMembers(scm.GIT, members) def testGetEmail(self): self.mox.StubOutWithMock(scm.GIT, 'Capture') scm.GIT.Capture(['config', 'user.email'], self.fake_root, error_ok=True ).AndReturn('mini@me.com') self.mox.ReplayAll() self.assertEqual(scm.GIT.GetEmail(self.fake_root), 'mini@me.com') class SVNTestCase(BaseSCMTestCase): def setUp(self): BaseSCMTestCase.setUp(self) self.root_dir = self.Dir() self.args = self.Args() self.url = self.Url() self.relpath = 'asf' def testMembersChanged(self): self.mox.ReplayAll() members = [ 'COMMAND', 'Capture', 'CaptureHeadRevision', 'CaptureInfo', 'CaptureStatus', 'DiffItem', 'GenerateDiff', 'GetCheckoutRoot', 'GetEmail', 'GetFileProperty', 'IsMoved', 'ReadSimpleAuth', 'Run', 'RunAndFilterOutput', 'RunAndGetFileList', ] # If this test fails, you should add the relevant test. self.compareMembers(scm.SVN, members) def testGetCheckoutRoot(self): self.mox.StubOutWithMock(scm.SVN, 'CaptureInfo') self.mox.StubOutWithMock(scm, 'GetCasedPath') scm.os.path.abspath(self.root_dir + 'x').AndReturn(self.root_dir) scm.GetCasedPath(self.root_dir).AndReturn(self.root_dir) result1 = { "Repository Root": "Some root" } scm.SVN.CaptureInfo(self.root_dir, print_error=False).AndReturn(result1) results2 = { "Repository Root": "A different root" } scm.SVN.CaptureInfo(scm.os.path.dirname(self.root_dir), print_error=False).AndReturn(results2) self.mox.ReplayAll() self.assertEquals(scm.SVN.GetCheckoutRoot(self.root_dir + 'x'), self.root_dir) def testGetFileInfo(self): xml_text = r""" http://src.chromium.org/svn/trunk/src/chrome/app/d http://src.chromium.org/svn add infinity http://src.chromium.org/svn/trunk/src/chrome/app/DEPS 14628 369f59057ba0e6d9017e28f8bdfb1f43 """ % self.url self.mox.StubOutWithMock(scm.SVN, 'Capture') scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text) expected = { 'URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/d', 'UUID': None, 'Repository Root': 'http://src.chromium.org/svn', 'Schedule': 'add', 'Copied From URL': 'http://src.chromium.org/svn/trunk/src/chrome/app/DEPS', 'Copied From Rev': '14628', 'Path': self.url, 'Revision': 14628, 'Node Kind': 'file', } self.mox.ReplayAll() file_info = scm.SVN.CaptureInfo(self.url, '.', True) self.assertEquals(sorted(file_info.items()), sorted(expected.items())) def testCaptureInfo(self): xml_text = """ %s %s 7b9385f5-0452-0410-af26-ad4892b7a1fb normal infinity maruel 2008-12-04T20:12:19.685120Z """ % (self.url, self.root_dir) self.mox.StubOutWithMock(scm.SVN, 'Capture') scm.SVN.Capture(['info', '--xml', self.url], '.', True).AndReturn(xml_text) self.mox.ReplayAll() file_info = scm.SVN.CaptureInfo(self.url, '.', True) expected = { 'URL': self.url, 'UUID': '7b9385f5-0452-0410-af26-ad4892b7a1fb', 'Revision': 35, 'Repository Root': self.root_dir, 'Schedule': 'normal', 'Copied From URL': None, 'Copied From Rev': None, 'Path': '.', 'Node Kind': 'directory', } self.assertEqual(file_info, expected) def testCaptureStatus(self): text =r""" ajwong@chromium.org 2009-04-16T00:42:06.872358Z brettw@google.com 2008-08-23T17:16:42.090152Z nsylvain@chromium.org 2009-04-27T19:37:17.977400Z """ proc = self.mox.CreateMockAnything() scm.subprocess.Popen(['svn', 'status', '--xml', '.'], cwd=None, shell=scm.sys.platform.startswith('win'), stderr=None, stdout=scm.subprocess.PIPE).AndReturn(proc) proc.communicate().AndReturn((text, 0)) self.mox.ReplayAll() info = scm.SVN.CaptureStatus('.') expected = [ ('? ', 'unversionned_file.txt'), ('M ', 'build\\internal\\essential.vsprops'), ('A + ', 'chrome\\app\\d'), ('MM ', 'chrome\\app\\DEPS'), ('C ', 'scripts\\master\\factory\\gclient_factory.py'), ] self.assertEquals(sorted(info), sorted(expected)) def testRun(self): param2 = 'bleh' scm.gclient_utils.SubprocessCall(['svn', 'foo', 'bar'], param2).AndReturn(None) self.mox.ReplayAll() scm.SVN.Run(['foo', 'bar'], param2) def testCaptureStatusEmpty(self): text = r""" """ proc = self.mox.CreateMockAnything() scm.subprocess.Popen(['svn', 'status', '--xml'], cwd=None, shell=scm.sys.platform.startswith('win'), stderr=None, stdout=scm.subprocess.PIPE).AndReturn(proc) proc.communicate().AndReturn((text, 0)) self.mox.ReplayAll() info = scm.SVN.CaptureStatus(None) self.assertEquals(info, []) if __name__ == '__main__': import unittest unittest.main() # vim: ts=2:sw=2:tw=80:et: