Commit 705e5180 authored by Matt Giuca's avatar Matt Giuca Committed by Commit Bot

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/901122Reviewed-by: 's avatarRobbie Iannucci <iannucci@chromium.org>
Commit-Queue: Robbie Iannucci <iannucci@chromium.org>
parent 13101a6e
...@@ -322,6 +322,10 @@ class GitRepo(object): ...@@ -322,6 +322,10 @@ class GitRepo(object):
env = self.get_git_commit_env(commit_data) env = self.get_git_commit_env(commit_data)
for fname, file_data in commit_data.iteritems(): 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 deleted = False
if 'data' in file_data: if 'data' in file_data:
data = file_data.get('data') data = file_data.get('data')
...@@ -358,12 +362,11 @@ class GitRepo(object): ...@@ -358,12 +362,11 @@ class GitRepo(object):
key = getattr(self, singleton) key = getattr(self, singleton)
if key in commit_data: if key in commit_data:
val = commit_data[key] val = commit_data[key]
elif suffix == 'DATE':
val = self._date
self._date += datetime.timedelta(days=1)
else: else:
if suffix == 'DATE': val = getattr(self, 'DEFAULT_%s' % singleton)
val = self._date
self._date += datetime.timedelta(days=1)
else:
val = getattr(self, 'DEFAULT_%s' % singleton)
env['GIT_%s' % singleton] = str(val) env['GIT_%s' % singleton] = str(val)
return env return env
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
import binascii import binascii
import collections import collections
import datetime
import os import os
import shutil import shutil
import signal import signal
...@@ -21,6 +22,8 @@ sys.path.insert(0, DEPOT_TOOLS_ROOT) ...@@ -21,6 +22,8 @@ sys.path.insert(0, DEPOT_TOOLS_ROOT)
from testing_support import coverage_utils from testing_support import coverage_utils
from testing_support import git_test_utils from testing_support import git_test_utils
GitRepo = git_test_utils.GitRepo
class GitCommonTestBase(unittest.TestCase): class GitCommonTestBase(unittest.TestCase):
@classmethod @classmethod
...@@ -926,7 +929,7 @@ class GitMakeWorkdir(git_test_utils.GitRepoReadOnlyTestBase, GitCommonTestBase): ...@@ -926,7 +929,7 @@ class GitMakeWorkdir(git_test_utils.GitRepoReadOnlyTestBase, GitCommonTestBase):
class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase):
REPO_SCHEMA = """ REPO_SCHEMA = """
A B A B C
""" """
COMMIT_A = { COMMIT_A = {
...@@ -937,6 +940,19 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): ...@@ -937,6 +940,19 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase):
'file1': {'data': 'file1 changed'}, '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): def testAutomaticCommitDates(self):
# The dates should start from 1970-01-01 and automatically increment. They # 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 # must be in UTC (otherwise the tests are system-dependent, and if your
...@@ -951,6 +967,13 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase): ...@@ -951,6 +967,13 @@ class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase):
self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000', self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000',
self.repo.show_commit('B', format_string='%cn %ci')) 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__': if __name__ == '__main__':
sys.exit(coverage_utils.covered_main( sys.exit(coverage_utils.covered_main(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment