1# Copyright (C) 2009 Google Inc. All rights reserved. 2# 3# Redistribution and use in source and binary forms, with or without 4# modification, are permitted provided that the following conditions are 5# met: 6# 7# * Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# * Redistributions in binary form must reproduce the above 10# copyright notice, this list of conditions and the following disclaimer 11# in the documentation and/or other materials provided with the 12# distribution. 13# * Neither the name of Google Inc. nor the names of its 14# contributors may be used to endorse or promote products derived from 15# this software without specific prior written permission. 16# 17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29import os 30import subprocess 31import tempfile 32import unittest 33from modules.scm import detect_scm_system, SCM, ScriptError 34 35 36# Eventually we will want to write tests which work for both scms. (like update_webkit, changed_files, etc.) 37# Perhaps through some SCMTest base-class which both SVNTest and GitTest inherit from. 38 39def run(args): 40 SCM.run_command(args) 41 42# Exists to share svn repository creation code between the git and svn tests 43class SVNTestRepository: 44 @staticmethod 45 def _setup_test_commits(test_object): 46 # Add some test commits 47 os.chdir(test_object.svn_checkout_path) 48 test_file = open('test_file', 'w') 49 test_file.write("test1") 50 test_file.flush() 51 52 run(['svn', 'add', 'test_file']) 53 run(['svn', 'commit', '--quiet', '--message', 'initial commit']) 54 55 test_file.write("test2") 56 test_file.flush() 57 58 run(['svn', 'commit', '--quiet', '--message', 'second commit']) 59 60 test_file.write("test3") 61 test_file.close() 62 63 run(['svn', 'commit', '--quiet', '--message', 'third commit']) 64 65 @classmethod 66 def setup(cls, test_object): 67 # Create an test SVN repository 68 test_object.svn_repo_path = tempfile.mkdtemp(suffix="svn_test_repo") 69 test_object.svn_repo_url = "file://%s" % test_object.svn_repo_path # Not sure this will work on windows 70 # git svn complains if we don't pass --pre-1.5-compatible, not sure why: 71 # Expected FS format '2'; found format '3' at /usr/local/libexec/git-core//git-svn line 1477 72 run(['svnadmin', 'create', '--pre-1.5-compatible', test_object.svn_repo_path]) 73 74 # Create a test svn checkout 75 test_object.svn_checkout_path = tempfile.mkdtemp(suffix="svn_test_checkout") 76 run(['svn', 'checkout', '--quiet', test_object.svn_repo_url, test_object.svn_checkout_path]) 77 78 cls._setup_test_commits(test_object) 79 80 @classmethod 81 def tear_down(cls, test_object): 82 run(['rm', '-rf', test_object.svn_repo_path]) 83 run(['rm', '-rf', test_object.svn_checkout_path]) 84 85 86class SVNTest(unittest.TestCase): 87 88 def setUp(self): 89 SVNTestRepository.setup(self) 90 os.chdir(self.svn_checkout_path) 91 92 def tearDown(self): 93 SVNTestRepository.tear_down(self) 94 95 def test_detection(self): 96 scm = detect_scm_system(self.svn_checkout_path) 97 self.assertEqual(scm.display_name(), "svn") 98 self.assertEqual(scm.supports_local_commits(), False) 99 100class GitTest(unittest.TestCase): 101 102 def _setup_git_clone_of_svn_repository(self): 103 self.git_checkout_path = tempfile.mkdtemp(suffix="git_test_checkout") 104 # --quiet doesn't make git svn silent, so we redirect output 105 args = ['git', 'svn', '--quiet', 'clone', self.svn_repo_url, self.git_checkout_path] 106 git_svn_clone = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 107 git_svn_clone.communicate() # ignore output 108 git_svn_clone.wait() 109 110 def _tear_down_git_clone_of_svn_repository(self): 111 run(['rm', '-rf', self.git_checkout_path]) 112 113 def setUp(self): 114 SVNTestRepository.setup(self) 115 self._setup_git_clone_of_svn_repository() 116 os.chdir(self.git_checkout_path) 117 118 def tearDown(self): 119 SVNTestRepository.tear_down(self) 120 self._tear_down_git_clone_of_svn_repository() 121 122 def test_detection(self): 123 scm = detect_scm_system(self.git_checkout_path) 124 self.assertEqual(scm.display_name(), "git") 125 self.assertEqual(scm.supports_local_commits(), True) 126 127 def test_commitish_parsing(self): 128 scm = detect_scm_system(self.git_checkout_path) 129 130 # Multiple revisions are cherry-picked. 131 self.assertEqual(len(scm.commit_ids_from_commitish_arguments(['HEAD~2'])), 1) 132 self.assertEqual(len(scm.commit_ids_from_commitish_arguments(['HEAD', 'HEAD~2'])), 2) 133 134 # ... is an invalid range specifier 135 self.assertRaises(ScriptError, scm.commit_ids_from_commitish_arguments, ['trunk...HEAD']) 136 137 138if __name__ == '__main__': 139 unittest.main() 140