From 705e51807fec5f379bc32ece0448d60b1cc8a110 Mon Sep 17 00:00:00 2001 From: Matt Giuca Date: Wed, 7 Feb 2018 13:16:56 +1100 Subject: [PATCH] git_test_utils: Fixed custom git metadata in git commit schemas. Previously, if you used custom metadata keys in a test commit (like GitRepo.AUTHOR_NAME), they would also be treated as filenames, and fail because they aren't strings. (This wasn't a problem because it isn't currently used in any tests.) Bug: 808941 Change-Id: Id7c4fa5822741925beba591ea587bd8ebbf2e478 Reviewed-on: https://chromium-review.googlesource.com/901122 Reviewed-by: Robbie Iannucci Commit-Queue: Robbie Iannucci --- testing_support/git_test_utils.py | 13 ++++++++----- tests/git_common_test.py | 25 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/testing_support/git_test_utils.py b/testing_support/git_test_utils.py index ae48dbe22..619844339 100644 --- a/testing_support/git_test_utils.py +++ b/testing_support/git_test_utils.py @@ -322,6 +322,10 @@ class GitRepo(object): env = self.get_git_commit_env(commit_data) for fname, file_data in commit_data.iteritems(): + # If it isn't a string, it's one of the special keys. + if not isinstance(fname, basestring): + continue + deleted = False if 'data' in file_data: data = file_data.get('data') @@ -358,12 +362,11 @@ class GitRepo(object): key = getattr(self, singleton) if key in commit_data: val = commit_data[key] + elif suffix == 'DATE': + val = self._date + self._date += datetime.timedelta(days=1) else: - if suffix == 'DATE': - val = self._date - self._date += datetime.timedelta(days=1) - else: - val = getattr(self, 'DEFAULT_%s' % singleton) + val = getattr(self, 'DEFAULT_%s' % singleton) env['GIT_%s' % singleton] = str(val) return env diff --git a/tests/git_common_test.py b/tests/git_common_test.py index 210ff2346..ba570c8cc 100755 --- a/tests/git_common_test.py +++ b/tests/git_common_test.py @@ -7,6 +7,7 @@ import binascii import collections +import datetime import os import shutil import signal @@ -21,6 +22,8 @@ sys.path.insert(0, DEPOT_TOOLS_ROOT) from testing_support import coverage_utils from testing_support import git_test_utils +GitRepo = git_test_utils.GitRepo + class GitCommonTestBase(unittest.TestCase): @classmethod @@ -926,7 +929,7 @@ class GitMakeWorkdir(git_test_utils.GitRepoReadOnlyTestBase, GitCommonTestBase): class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): REPO_SCHEMA = """ - A B + A B C """ COMMIT_A = { @@ -937,6 +940,19 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): 'file1': {'data': 'file1 changed'}, } + # Test special keys (custom commit data). + COMMIT_C = { + GitRepo.AUTHOR_NAME: 'Custom Author', + GitRepo.AUTHOR_EMAIL: 'author@example.com', + GitRepo.AUTHOR_DATE: datetime.datetime(1980, 9, 8, 7, 6, 5, + tzinfo=git_test_utils.UTC), + GitRepo.COMMITTER_NAME: 'Custom Committer', + GitRepo.COMMITTER_EMAIL: 'committer@example.com', + GitRepo.COMMITTER_DATE: datetime.datetime(1990, 4, 5, 6, 7, 8, + tzinfo=git_test_utils.UTC), + 'file1': {'data': 'file1 changed again'}, + } + def testAutomaticCommitDates(self): # The dates should start from 1970-01-01 and automatically increment. They # must be in UTC (otherwise the tests are system-dependent, and if your @@ -951,6 +967,13 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000', self.repo.show_commit('B', format_string='%cn %ci')) + def testCustomCommitData(self): + self.assertEquals('Custom Author author@example.com ' + '1980-09-08 07:06:05 +0000', + self.repo.show_commit('C', format_string='%an %ae %ai')) + self.assertEquals('Custom Committer committer@example.com ' + '1990-04-05 06:07:08 +0000', + self.repo.show_commit('C', format_string='%cn %ce %ci')) if __name__ == '__main__': sys.exit(coverage_utils.covered_main(