blob: 849a5bc15a8a31433ec6c940ca8caf4d9727e518 [file] [log] [blame]
Paul Hobbse46a42b2017-03-21 14:04:13 -07001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for git_metrics."""
6
Allen Li060e84c2017-07-12 18:42:48 -07007# pylint: disable=protected-access
8
Paul Hobbse46a42b2017-03-21 14:04:13 -07009from __future__ import absolute_import
10from __future__ import print_function
Paul Hobbse46a42b2017-03-21 14:04:13 -070011
12import mock
13import os
Allen Li060e84c2017-07-12 18:42:48 -070014import subprocess
Paul Hobbse46a42b2017-03-21 14:04:13 -070015
Allen Li060e84c2017-07-12 18:42:48 -070016from chromite.lib import cros_test_lib
17from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070018from chromite.scripts.sysmon import git_metrics
19
Paul Hobbse46a42b2017-03-21 14:04:13 -070020
Allen Li060e84c2017-07-12 18:42:48 -070021class TestGitMetricCollector(cros_test_lib.TestCase):
22 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070023
24 def setUp(self):
Allen Li060e84c2017-07-12 18:42:48 -070025 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
26 autospec=True)
27 self.store = patcher.start()
28 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070029
Allen Li060e84c2017-07-12 18:42:48 -070030 def test_collect(self):
31 with mock.patch.object(git_metrics, '_GitRepo', autospec=True) as _GitRepo:
32 instance = _GitRepo('dummy')
33 instance.get_commit_hash.return_value = \
34 '2b1ce059425edc91e013c260e59019195f927a07'
35 instance.get_commit_time.return_value = 1483257600
36 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070037
Allen Li060e84c2017-07-12 18:42:48 -070038 collector = git_metrics._GitMetricCollector('~/solciel', 'dummy')
39 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070040
Allen Li060e84c2017-07-12 18:42:48 -070041 setter = self.store.set
42 calls = [
43 mock.call('git/hash', ('~/solciel',), None,
44 '2b1ce059425edc91e013c260e59019195f927a07',
45 enforce_ge=mock.ANY),
46 mock.call('git/timestamp', ('~/solciel',), None,
47 1483257600, enforce_ge=mock.ANY),
48 mock.call('git/unstaged_changes',
49 ('added', '~/solciel'), None,
50 0, enforce_ge=mock.ANY),
51 mock.call('git/unstaged_changes',
52 ('deleted', '~/solciel'), None,
53 3, enforce_ge=mock.ANY),
54 ]
55 setter.assert_has_calls(calls)
56 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070057
58
Allen Li8934e042017-06-21 15:54:42 -070059class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
60 """Tests for _GitRepo using a Git fixture."""
61
62 def setUp(self):
63 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li060e84c2017-07-12 18:42:48 -070064
65 devnull = open(os.devnull, 'w')
66 self.addCleanup(devnull.close)
67
68 def call(args, **kwargs):
69 subprocess.check_call(args, stdout=devnull, stderr=devnull, **kwargs)
70
Allen Li8934e042017-06-21 15:54:42 -070071 with osutils.ChdirContext(self.tempdir):
Allen Li060e84c2017-07-12 18:42:48 -070072 call(['git', 'init'])
73 call(['git', 'config', 'user.name', 'John Doe'])
74 call(['git', 'config', 'user.email', 'john@example.com'])
Allen Li8934e042017-06-21 15:54:42 -070075 with open('foo', 'w') as f:
76 f.write('a\nb\nc\n')
Allen Li060e84c2017-07-12 18:42:48 -070077 call(['git', 'add', 'foo'])
78 env = os.environ.copy()
79 env['GIT_AUTHOR_DATE'] = '2017-01-01T00:00:00Z'
80 env['GIT_COMMITTER_DATE'] = '2017-01-01T00:00:00Z'
81 call(['git', 'commit', '-m', 'Initial commit'], env=env)
82
83 def test_get_commit_hash(self):
84 """Test get_commit_hash()."""
85 repo = git_metrics._GitRepo(self.git_dir)
86 got = repo.get_commit_hash()
87 self.assertEqual(got, '7c88f131e520e8455e2403b88ff4f723758c5dd6')
88
89 def test_get_commit_time(self):
90 """Test get_commit_time()."""
91 repo = git_metrics._GitRepo(self.git_dir)
92 got = repo.get_commit_time()
93 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -070094
95 def test_get_unstaged_changes(self):
Allen Li060e84c2017-07-12 18:42:48 -070096 """Test get_unstaged_changes()."""
Allen Li8934e042017-06-21 15:54:42 -070097 with open(os.path.join(self.tempdir, 'spam'), 'w') as f:
98 f.write('a\n')
99 os.remove(os.path.join(self.tempdir, 'foo'))
100 repo = git_metrics._GitRepo(self.git_dir)
101 added, removed = repo.get_unstaged_changes()
102 self.assertEqual(added, 0) # Does not count untracked files
103 self.assertEqual(removed, 3)