Commit d2ef7086 authored by mgiuca@chromium.org's avatar mgiuca@chromium.org

git_test_utils: Automatic commit dates are now in UTC.

This fixes the test being dependent on the system time, and undefined
behaviour resulting from negative timestamps in positive-offset
timezones.

BUG=581895

Review URL: https://codereview.chromium.org/1640973002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@298438 0039d316-1c4b-4281-b951-d872f2087c98
parent c6824851
......@@ -93,6 +93,24 @@ class OrderedSet(collections.MutableSet):
return key
class UTC(datetime.tzinfo):
"""UTC time zone.
from https://docs.python.org/2/library/datetime.html#tzinfo-objects
"""
def utcoffset(self, dt):
return datetime.timedelta(0)
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return datetime.timedelta(0)
UTC = UTC()
class GitRepoSchema(object):
"""A declarative git testing repo.
......@@ -267,7 +285,7 @@ class GitRepo(object):
"""
self.repo_path = tempfile.mkdtemp(dir=self.BASE_TEMP_DIR)
self.commit_map = {}
self._date = datetime.datetime(1970, 1, 1)
self._date = datetime.datetime(1970, 1, 1, tzinfo=UTC)
self.to_schema_refs = ['--branches']
......@@ -360,6 +378,11 @@ class GitRepo(object):
except subprocess.CalledProcessError as e:
return self.COMMAND_OUTPUT(e.returncode, e.output)
def show_commit(self, commit_name, format_string):
"""Shows a commit (by its schema name) with a given format string."""
return self.git('show', '-q', '--pretty=format:%s' % format_string,
self[commit_name]).stdout
def git_commit(self, message):
return self.git('commit', '-am', message, env=self.get_git_commit_env())
......
......@@ -772,6 +772,33 @@ class GitMakeWorkdir(git_test_utils.GitRepoReadOnlyTestBase, GitCommonTestBase):
self.assertFalse(os.path.islink(os.path.join(workdir, '.git', 'HEAD')))
class GitTestUtilsTest(git_test_utils.GitRepoReadOnlyTestBase):
REPO_SCHEMA = """
A B
"""
COMMIT_A = {
'file1': {'data': 'file1'},
}
COMMIT_B = {
'file1': {'data': 'file1 changed'},
}
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
# local timezone is positive, timestamps will be <0 which causes bizarre
# behaviour in Git; http://crbug.com/581895).
self.assertEquals('Author McAuthorly 1970-01-01 00:00:00 +0000',
self.repo.show_commit('A', format_string='%an %ai'))
self.assertEquals('Charles Committish 1970-01-02 00:00:00 +0000',
self.repo.show_commit('A', format_string='%cn %ci'))
self.assertEquals('Author McAuthorly 1970-01-03 00:00:00 +0000',
self.repo.show_commit('B', format_string='%an %ai'))
self.assertEquals('Charles Committish 1970-01-04 00:00:00 +0000',
self.repo.show_commit('B', format_string='%cn %ci'))
if __name__ == '__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