From d2ef7086bbebaebec1d8983d701d96ce2eb5459b Mon Sep 17 00:00:00 2001 From: "mgiuca@chromium.org" Date: Thu, 28 Jan 2016 07:57:34 +0000 Subject: [PATCH] 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 --- testing_support/git_test_utils.py | 25 ++++++++++++++++++++++++- tests/git_common_test.py | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/testing_support/git_test_utils.py b/testing_support/git_test_utils.py index fbe7c73c30..a08e8189f8 100644 --- a/testing_support/git_test_utils.py +++ b/testing_support/git_test_utils.py @@ -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()) diff --git a/tests/git_common_test.py b/tests/git_common_test.py index ef411ce9da..76a91520dc 100755 --- a/tests/git_common_test.py +++ b/tests/git_common_test.py @@ -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(