def test_rebase_in_progress(self): svn_test_file = os.path.join(self.svn_checkout_path, 'test_file') write_into_file_at_path(svn_test_file, "svn_checkout") run_command([ 'svn', 'commit', '--message', 'commit to conflict with git commit' ], cwd=self.svn_checkout_path) git_test_file = os.path.join(self.git_checkout_path, 'test_file') write_into_file_at_path(git_test_file, "git_checkout") run_command([ 'git', 'commit', '-a', '-m', 'commit to be thrown away by rebase abort' ]) # --quiet doesn't make git svn silent, so use run_silent to redirect output self.assertRaises( ScriptError, run_silent, ['git', 'svn', '--quiet', 'rebase' ]) # Will fail due to a conflict leaving us mid-rebase. scm = detect_scm_system(self.git_checkout_path) self.assertTrue(scm.rebase_in_progress()) # Make sure our cleanup works. scm.clean_working_directory() self.assertFalse(scm.rebase_in_progress()) # Make sure cleanup doesn't throw when no rebase is in progress. scm.clean_working_directory()
def test_create_patch_is_full_patch(self): test_dir_path = os.path.join(self.svn_checkout_path, 'test_dir') os.mkdir(test_dir_path) test_file_path = os.path.join(test_dir_path, 'test_file2') write_into_file_at_path(test_file_path, 'test content') run_command(['svn', 'add', 'test_dir']) # create_patch depends on 'svn-create-patch', so make a dummy version. scripts_path = os.path.join(self.svn_checkout_path, 'WebKitTools', 'Scripts') os.makedirs(scripts_path) create_patch_path = os.path.join(scripts_path, 'svn-create-patch') write_into_file_at_path( create_patch_path, '#!/bin/sh\necho $PWD' ) # We could pass -n to prevent the \n, but not all echo accept -n. os.chmod(create_patch_path, stat.S_IXUSR | stat.S_IRUSR) # Change into our test directory and run the create_patch command. os.chdir(test_dir_path) scm = detect_scm_system(test_dir_path) self.assertEqual(scm.checkout_root, self.svn_checkout_path ) # Sanity check that detection worked right. patch_contents = scm.create_patch() # Our fake 'svn-create-patch' returns $PWD instead of a patch, check that it was executed from the root of the repo. self.assertEqual("%s\n" % os.path.realpath(scm.checkout_root), patch_contents) # Add a \n because echo adds a \n.
def ensure_clean_working_directory(self, force_clean): if not force_clean and not self.working_directory_is_clean(): print run_command(self.status_command(), error_handler=Executive.ignore_error) raise ScriptError(message="Working directory has modifications, pass --force-clean or --no-clean to continue.") log("Cleaning working directory") self.clean_working_directory()
def test_run_command_with_pipe(self): input_process = subprocess.Popen(['echo', 'foo\nbar'], stdout=subprocess.PIPE, stderr=self.dev_null) self.assertEqual( run_command(['grep', 'bar'], input=input_process.stdout), "bar\n") # Test the non-pipe case too: self.assertEqual(run_command(['grep', 'bar'], input="foo\nbar"), "bar\n") command_returns_non_zero = ['/bin/sh', '--invalid-option'] # Test when the input pipe process fails. input_process = subprocess.Popen(command_returns_non_zero, stdout=subprocess.PIPE, stderr=self.dev_null) self.assertTrue(input_process.poll() != 0) self.assertRaises(ScriptError, run_command, ['grep', 'bar'], input=input_process.stdout) # Test when the run_command process fails. input_process = subprocess.Popen( ['echo', 'foo\nbar'], stdout=subprocess.PIPE, stderr=self.dev_null ) # grep shows usage and calls exit(2) when called w/o arguments. self.assertRaises(ScriptError, run_command, command_returns_non_zero, input=input_process.stdout)
def tear_down(cls, test_object): run_command(['rm', '-rf', test_object.svn_repo_path]) run_command(['rm', '-rf', test_object.svn_checkout_path]) # Now that we've deleted the checkout paths, cwddir may be invalid # Change back to a valid directory so that later calls to os.getcwd() do not fail. os.chdir(detect_scm_system(os.path.dirname(__file__)).checkout_root)
def ensure_clean_working_directory(self, force_clean): if not force_clean and not self.working_directory_is_clean(): print run_command(self.status_command(), error_handler=Executive.ignore_error) raise ScriptError( message= "Working directory has modifications, pass --force-clean or --no-clean to continue." ) log("Cleaning working directory") self.clean_working_directory()
def apply_reverse_diff(self, revision): # '-c -revision' applies the inverse diff of 'revision' svn_merge_args = [ 'svn', 'merge', '--non-interactive', '-c', '-%s' % revision, self._repository_url() ] log("WARNING: svn merge has been known to take more than 10 minutes to complete. It is recommended you use git for rollouts." ) log("Running '%s'" % " ".join(svn_merge_args)) run_command(svn_merge_args)
def apply_patch(self, patch, force=False): # It's possible that the patch was not made from the root directory. # We should detect and handle that case. # FIXME: scm.py should not deal with fetching Attachment data. Attachment should just have a .data() accessor. curl_process = subprocess.Popen(['curl', '--location', '--silent', '--show-error', patch.url()], stdout=subprocess.PIPE) args = [self.script_path('svn-apply')] if patch.reviewer(): args += ['--reviewer', patch.reviewer().full_name] if force: args.append('--force') run_command(args, input=curl_process.stdout)
def setup(cls, test_object): # Create an test SVN repository test_object.svn_repo_path = tempfile.mkdtemp(suffix="svn_test_repo") test_object.svn_repo_url = "file://%s" % test_object.svn_repo_path # Not sure this will work on windows # git svn complains if we don't pass --pre-1.5-compatible, not sure why: # Expected FS format '2'; found format '3' at /usr/local/libexec/git-core//git-svn line 1477 run_command(['svnadmin', 'create', '--pre-1.5-compatible', test_object.svn_repo_path]) # Create a test svn checkout test_object.svn_checkout_path = tempfile.mkdtemp(suffix="svn_test_checkout") run_command(['svn', 'checkout', '--quiet', test_object.svn_repo_url, test_object.svn_checkout_path]) cls._setup_test_commits(test_object)
def apply_reverse_diff(self, revision): # Assume the revision is an svn revision. git_commit = self.git_commit_from_svn_revision(revision) if not git_commit: raise ScriptError(message='Failed to find git commit for revision %s, git svn log output: "%s"' % (revision, git_commit)) # I think this will always fail due to ChangeLogs. # FIXME: We need to detec specific failure conditions and handle them. run_command(['git', 'revert', '--no-commit', git_commit], error_handler=Executive.ignore_error) # Fix any ChangeLogs if necessary. changelog_paths = self.modified_changelogs() if len(changelog_paths): run_command([self.script_path('resolve-ChangeLogs')] + changelog_paths)
def test_create_binary_patch(self): # Create a git binary patch and check the contents. scm = detect_scm_system(self.git_checkout_path) test_file_name = 'binary_file' test_file_path = os.path.join(self.git_checkout_path, test_file_name) file_contents = ''.join(map(chr, range(256))) write_into_file_at_path(test_file_path, file_contents) run_command(['git', 'add', test_file_name]) patch = scm.create_patch() self.assertTrue(re.search(r'\nliteral 0\n', patch)) self.assertTrue(re.search(r'\nliteral 256\n', patch)) # Check if we can apply the created patch. run_command(['git', 'rm', '-f', test_file_name]) self._setup_webkittools_scripts_symlink(scm) self.scm.apply_patch(self._create_patch(patch)) self.assertEqual(file_contents, read_from_path(test_file_path)) # Check if we can create a patch from a local commit. write_into_file_at_path(test_file_path, file_contents) run_command(['git', 'add', test_file_name]) run_command(['git', 'commit', '-m', 'binary diff']) patch_from_local_commit = scm.create_patch_from_local_commit('HEAD') self.assertTrue(re.search(r'\nliteral 0\n', patch_from_local_commit)) self.assertTrue(re.search(r'\nliteral 256\n', patch_from_local_commit)) patch_since_local_commit = scm.create_patch_since_local_commit('HEAD^1') self.assertTrue(re.search(r'\nliteral 0\n', patch_since_local_commit)) self.assertTrue(re.search(r'\nliteral 256\n', patch_since_local_commit)) self.assertEqual(patch_from_local_commit, patch_since_local_commit)
def commit_ids_from_commitish_arguments(self, args): if not len(args): # FIXME: trunk is not always the remote branch name, need a way to detect the name. args.append('trunk..HEAD') commit_ids = [] for commitish in args: if '...' in commitish: raise ScriptError(message="'...' is not supported (found in '%s'). Did you mean '..'?" % commitish) elif '..' in commitish: commit_ids += reversed(run_command(['git', 'rev-list', commitish]).splitlines()) else: # Turn single commits or branch or tag names into commit ids. commit_ids += run_command(['git', 'rev-parse', '--revs-only', commitish]).splitlines() return commit_ids
def apply_patch(self, patch, force=False): # It's possible that the patch was not made from the root directory. # We should detect and handle that case. # FIXME: scm.py should not deal with fetching Attachment data. Attachment should just have a .data() accessor. curl_process = subprocess.Popen( ['curl', '--location', '--silent', '--show-error', patch.url()], stdout=subprocess.PIPE) args = [self.script_path('svn-apply')] if patch.reviewer(): args += ['--reviewer', patch.reviewer().full_name] if force: args.append('--force') run_command(args, input=curl_process.stdout)
def test_run_command_with_pipe(self): input_process = subprocess.Popen(['echo', 'foo\nbar'], stdout=subprocess.PIPE, stderr=self.dev_null) self.assertEqual(run_command(['grep', 'bar'], input=input_process.stdout), "bar\n") # Test the non-pipe case too: self.assertEqual(run_command(['grep', 'bar'], input="foo\nbar"), "bar\n") command_returns_non_zero = ['/bin/sh', '--invalid-option'] # Test when the input pipe process fails. input_process = subprocess.Popen(command_returns_non_zero, stdout=subprocess.PIPE, stderr=self.dev_null) self.assertTrue(input_process.poll() != 0) self.assertRaises(ScriptError, run_command, ['grep', 'bar'], input=input_process.stdout) # Test when the run_command process fails. input_process = subprocess.Popen(['echo', 'foo\nbar'], stdout=subprocess.PIPE, stderr=self.dev_null) # grep shows usage and calls exit(2) when called w/o arguments. self.assertRaises(ScriptError, run_command, command_returns_non_zero, input=input_process.stdout)
def value_from_svn_info(cls, path, field_name): svn_info_args = ['svn', 'info', path] info_output = run_command(svn_info_args).rstrip() match = re.search("^%s: (?P<value>.+)$" % field_name, info_output, re.MULTILINE) if not match: raise ScriptError(script_args=svn_info_args, message='svn info did not contain a %s.' % field_name) return match.group('value')
def find_checkout_root(cls, path): # "git rev-parse --show-cdup" would be another way to get to the root (checkout_root, dot_git) = os.path.split(run_command(['git', 'rev-parse', '--git-dir'], cwd=path)) # If we were using 2.6 # checkout_root = os.path.relpath(checkout_root, path) if not os.path.isabs(checkout_root): # Sometimes git returns relative paths checkout_root = os.path.join(path, checkout_root) return checkout_root
def test_error_handlers(self): git_failure_message = "Merge conflict during commit: Your file or directory 'WebCore/ChangeLog' is probably out-of-date: resource out of date; try updating at /usr/local/libexec/git-core//git-svn line 469" svn_failure_message = """svn: Commit failed (details follow): svn: File or directory 'ChangeLog' is out of date; try updating svn: resource out of date; try updating """ command_does_not_exist = ['does_not_exist', 'invalid_option'] self.assertRaises(OSError, run_command, command_does_not_exist) self.assertRaises(OSError, run_command, command_does_not_exist, error_handler=Executive.ignore_error) command_returns_non_zero = ['/bin/sh', '--invalid-option'] self.assertRaises(ScriptError, run_command, command_returns_non_zero) # Check if returns error text: self.assertTrue( run_command(command_returns_non_zero, error_handler=Executive.ignore_error)) self.assertRaises(CheckoutNeedsUpdate, commit_error_handler, ScriptError(output=git_failure_message)) self.assertRaises(CheckoutNeedsUpdate, commit_error_handler, ScriptError(output=svn_failure_message)) self.assertRaises(ScriptError, commit_error_handler, ScriptError(output='blah blah blah'))
def find_checkout_root(cls, path): # "git rev-parse --show-cdup" would be another way to get to the root (checkout_root, dot_git) = os.path.split( run_command(['git', 'rev-parse', '--git-dir'], cwd=path)) # If we were using 2.6 # checkout_root = os.path.relpath(checkout_root, path) if not os.path.isabs( checkout_root): # Sometimes git returns relative paths checkout_root = os.path.join(path, checkout_root) return checkout_root
def run_status_and_extract_filenames(self, status_command, status_regexp): filenames = [] for line in run_command(status_command).splitlines(): match = re.search(status_regexp, line) if not match: continue # status = match.group('status') filename = match.group('filename') filenames.append(filename) return filenames
def commit_message_for_local_commit(self, commit_id): commit_lines = run_command(['git', 'cat-file', 'commit', commit_id]).splitlines() # Skip the git headers. first_line_after_headers = 0 for line in commit_lines: first_line_after_headers += 1 if line == "": break return CommitMessage(commit_lines[first_line_after_headers:])
def test_commitish_order(self): scm = detect_scm_system(self.git_checkout_path) commit_range = 'HEAD~3..HEAD' actual_commits = scm.commit_ids_from_commitish_arguments([commit_range]) expected_commits = [] expected_commits += reversed(run_command(['git', 'rev-list', commit_range]).splitlines()) self.assertEqual(actual_commits, expected_commits)
def apply_reverse_diff(self, revision): # Assume the revision is an svn revision. git_commit = self.git_commit_from_svn_revision(revision) if not git_commit: raise ScriptError( message= 'Failed to find git commit for revision %s, git svn log output: "%s"' % (revision, git_commit)) # I think this will always fail due to ChangeLogs. # FIXME: We need to detec specific failure conditions and handle them. run_command(['git', 'revert', '--no-commit', git_commit], error_handler=Executive.ignore_error) # Fix any ChangeLogs if necessary. changelog_paths = self.modified_changelogs() if len(changelog_paths): run_command([self.script_path('resolve-ChangeLogs')] + changelog_paths)
def commit_ids_from_commitish_arguments(self, args): if not len(args): # FIXME: trunk is not always the remote branch name, need a way to detect the name. args.append('trunk..HEAD') commit_ids = [] for commitish in args: if '...' in commitish: raise ScriptError( message= "'...' is not supported (found in '%s'). Did you mean '..'?" % commitish) elif '..' in commitish: commit_ids += reversed( run_command(['git', 'rev-list', commitish]).splitlines()) else: # Turn single commits or branch or tag names into commit ids. commit_ids += run_command( ['git', 'rev-parse', '--revs-only', commitish]).splitlines() return commit_ids
def test_create_patch_is_full_patch(self): test_dir_path = os.path.join(self.svn_checkout_path, 'test_dir') os.mkdir(test_dir_path) test_file_path = os.path.join(test_dir_path, 'test_file2') write_into_file_at_path(test_file_path, 'test content') run_command(['svn', 'add', 'test_dir']) # create_patch depends on 'svn-create-patch', so make a dummy version. scripts_path = os.path.join(self.svn_checkout_path, 'WebKitTools', 'Scripts') os.makedirs(scripts_path) create_patch_path = os.path.join(scripts_path, 'svn-create-patch') write_into_file_at_path(create_patch_path, '#!/bin/sh\necho $PWD') # We could pass -n to prevent the \n, but not all echo accept -n. os.chmod(create_patch_path, stat.S_IXUSR | stat.S_IRUSR) # Change into our test directory and run the create_patch command. os.chdir(test_dir_path) scm = detect_scm_system(test_dir_path) self.assertEqual(scm.checkout_root, self.svn_checkout_path) # Sanity check that detection worked right. patch_contents = scm.create_patch() # Our fake 'svn-create-patch' returns $PWD instead of a patch, check that it was executed from the root of the repo. self.assertEqual("%s\n" % os.path.realpath(scm.checkout_root), patch_contents) # Add a \n because echo adds a \n.
def test_rebase_in_progress(self): svn_test_file = os.path.join(self.svn_checkout_path, 'test_file') write_into_file_at_path(svn_test_file, "svn_checkout") run_command(['svn', 'commit', '--message', 'commit to conflict with git commit'], cwd=self.svn_checkout_path) git_test_file = os.path.join(self.git_checkout_path, 'test_file') write_into_file_at_path(git_test_file, "git_checkout") run_command(['git', 'commit', '-a', '-m', 'commit to be thrown away by rebase abort']) # --quiet doesn't make git svn silent, so use run_silent to redirect output self.assertRaises(ScriptError, run_silent, ['git', 'svn', '--quiet', 'rebase']) # Will fail due to a conflict leaving us mid-rebase. scm = detect_scm_system(self.git_checkout_path) self.assertTrue(scm.rebase_in_progress()) # Make sure our cleanup works. scm.clean_working_directory() self.assertFalse(scm.rebase_in_progress()) # Make sure cleanup doesn't throw when no rebase is in progress. scm.clean_working_directory()
def test_error_handlers(self): git_failure_message="Merge conflict during commit: Your file or directory 'WebCore/ChangeLog' is probably out-of-date: resource out of date; try updating at /usr/local/libexec/git-core//git-svn line 469" svn_failure_message="""svn: Commit failed (details follow): svn: File or directory 'ChangeLog' is out of date; try updating svn: resource out of date; try updating """ command_does_not_exist = ['does_not_exist', 'invalid_option'] self.assertRaises(OSError, run_command, command_does_not_exist) self.assertRaises(OSError, run_command, command_does_not_exist, error_handler=Executive.ignore_error) command_returns_non_zero = ['/bin/sh', '--invalid-option'] self.assertRaises(ScriptError, run_command, command_returns_non_zero) # Check if returns error text: self.assertTrue(run_command(command_returns_non_zero, error_handler=Executive.ignore_error)) self.assertRaises(CheckoutNeedsUpdate, commit_error_handler, ScriptError(output=git_failure_message)) self.assertRaises(CheckoutNeedsUpdate, commit_error_handler, ScriptError(output=svn_failure_message)) self.assertRaises(ScriptError, commit_error_handler, ScriptError(output='blah blah blah'))
def _build_path(self, *comps): if not self._cached_build_root: self._cached_build_root = executive.run_command( ["webkit-build-directory", "--base"]).rstrip() return os.path.join(self._cached_build_root, self._options.target, *comps)
def _tear_down_git_clone_of_svn_repository(self): run_command(['rm', '-rf', self.git_checkout_path])
def test_apply_svn_patch(self): scm = detect_scm_system(self.svn_checkout_path) patch = self._create_patch(run_command(['svn', 'diff', '-r4:3'])) self._setup_webkittools_scripts_symlink(scm) scm.apply_patch(patch)
def _setup_test_commits(test_object): # Add some test commits os.chdir(test_object.svn_checkout_path) test_file = open('test_file', 'w') test_file.write("test1") test_file.flush() run_command(['svn', 'add', 'test_file']) run_command(['svn', 'commit', '--quiet', '--message', 'initial commit']) test_file.write("test2") test_file.flush() run_command(['svn', 'commit', '--quiet', '--message', 'second commit']) test_file.write("test3\n") test_file.flush() run_command(['svn', 'commit', '--quiet', '--message', 'third commit']) test_file.write("test4\n") test_file.close() run_command(['svn', 'commit', '--quiet', '--message', 'fourth commit']) # svn does not seem to update after commit as I would expect. run_command(['svn', 'update'])
def commit_locally_with_message(self, message): run_command(['git', 'commit', '--all', '-F', '-'], input=message)
def create_patch_since_local_commit(self, commit_id): return run_command(['git', 'diff', '--binary', commit_id])
def push_local_commits_to_server(self): if self.dryrun: # Return a string which looks like a commit so that things which parse this output will succeed. return "Dry run, no remote commit.\nCommitted r0" return run_command(['git', 'svn', 'dcommit'], error_handler=commit_error_handler)
def create_patch_from_local_commit(self, commit_id): return run_command( ['git', 'diff', '--binary', commit_id + "^.." + commit_id])
def files_changed_summary_for_commit(self, commit_id): return run_command( ['git', 'diff-tree', '--shortstat', '--no-commit-id', commit_id])
def tear_down(cls, test_object): run_command(['rm', '-rf', test_object.svn_repo_path]) run_command(['rm', '-rf', test_object.svn_checkout_path])
def last_svn_commit_log(self): return run_command(['git', 'svn', 'log', '--limit=1'])
def test_apply_git_patch(self): scm = detect_scm_system(self.git_checkout_path) patch = self._create_patch(run_command(['git', 'diff', 'HEAD..HEAD^'])) self._setup_webkittools_scripts_symlink(scm) scm.apply_patch(patch)
def test_apply_git_patch_force(self): scm = detect_scm_system(self.git_checkout_path) patch = self._create_patch(run_command(['git', 'diff', 'HEAD~2..HEAD'])) self._setup_webkittools_scripts_symlink(scm) self.assertRaises(ScriptError, scm.apply_patch, patch, force=True)
def test_svn_apply(self): first_entry = """2009-10-26 Eric Seidel <*****@*****.**> Reviewed by Foo Bar. Most awesome change ever. * scm_unittest.py: """ intermediate_entry = """2009-10-27 Eric Seidel <*****@*****.**> Reviewed by Baz Bar. A more awesomer change yet! * scm_unittest.py: """ one_line_overlap_patch = """Index: ChangeLog =================================================================== --- ChangeLog (revision 5) +++ ChangeLog (working copy) @@ -1,5 +1,13 @@ 2009-10-26 Eric Seidel <*****@*****.**> + Reviewed by NOBODY (OOPS!). + + Second most awsome change ever. + + * scm_unittest.py: + +2009-10-26 Eric Seidel <*****@*****.**> + Reviewed by Foo Bar. Most awesome change ever. """ one_line_overlap_entry = """DATE_HERE Eric Seidel <*****@*****.**> Reviewed by REVIEWER_HERE. Second most awsome change ever. * scm_unittest.py: """ two_line_overlap_patch = """Index: ChangeLog =================================================================== --- ChangeLog (revision 5) +++ ChangeLog (working copy) @@ -2,6 +2,14 @@ Reviewed by Foo Bar. + Second most awsome change ever. + + * scm_unittest.py: + +2009-10-26 Eric Seidel <*****@*****.**> + + Reviewed by Foo Bar. + Most awesome change ever. * scm_unittest.py: """ two_line_overlap_entry = """DATE_HERE Eric Seidel <*****@*****.**> Reviewed by Foo Bar. Second most awsome change ever. * scm_unittest.py: """ write_into_file_at_path('ChangeLog', first_entry) run_command(['svn', 'add', 'ChangeLog']) run_command(['svn', 'commit', '--quiet', '--message', 'ChangeLog commit']) # Patch files were created against just 'first_entry'. # Add a second commit to make svn-apply have to apply the patches with fuzz. changelog_contents = "%s\n%s" % (intermediate_entry, first_entry) write_into_file_at_path('ChangeLog', changelog_contents) run_command(['svn', 'commit', '--quiet', '--message', 'Intermediate commit']) self._setup_webkittools_scripts_symlink(self.scm) self.scm.apply_patch(self._create_patch(one_line_overlap_patch)) expected_changelog_contents = "%s\n%s" % (self._set_date_and_reviewer(one_line_overlap_entry), changelog_contents) self.assertEquals(read_from_path('ChangeLog'), expected_changelog_contents) self.scm.revert_files(['ChangeLog']) self.scm.apply_patch(self._create_patch(two_line_overlap_patch)) expected_changelog_contents = "%s\n%s" % (self._set_date_and_reviewer(two_line_overlap_entry), changelog_contents) self.assertEquals(read_from_path('ChangeLog'), expected_changelog_contents)
def files_changed_summary_for_commit(self, commit_id): return run_command(['git', 'diff-tree', '--shortstat', '--no-commit-id', commit_id])
def _build_path(self, *comps): if not self._cached_build_root: self._cached_build_root = executive.run_command(["webkit-build-directory", "--base"]).rstrip() return os.path.join(self._cached_build_root, self._options.target, *comps)
def create_patch_from_local_commit(self, commit_id): return run_command(['git', 'diff', '--binary', commit_id + "^.." + commit_id])