blob: 72a01e6e09ba23a264c6e8fd1abcc3791e40f3b3 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Paul Hobbse46a42b2017-03-21 14:04:13 -07002# Copyright 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for git_metrics."""
7
Allen Li060e84c2017-07-12 18:42:48 -07008# pylint: disable=protected-access
9
Paul Hobbse46a42b2017-03-21 14:04:13 -070010from __future__ import absolute_import
11from __future__ import print_function
Paul Hobbse46a42b2017-03-21 14:04:13 -070012
13import mock
14import os
Allen Li060e84c2017-07-12 18:42:48 -070015import subprocess
Paul Hobbse46a42b2017-03-21 14:04:13 -070016
Allen Li060e84c2017-07-12 18:42:48 -070017from chromite.lib import cros_test_lib
18from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070019from chromite.scripts.sysmon import git_metrics
20
Paul Hobbse46a42b2017-03-21 14:04:13 -070021
Allen Li060e84c2017-07-12 18:42:48 -070022class TestGitMetricCollector(cros_test_lib.TestCase):
23 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070024
25 def setUp(self):
Allen Li060e84c2017-07-12 18:42:48 -070026 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
27 autospec=True)
28 self.store = patcher.start()
29 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070030
Allen Li060e84c2017-07-12 18:42:48 -070031 def test_collect(self):
32 with mock.patch.object(git_metrics, '_GitRepo', autospec=True) as _GitRepo:
33 instance = _GitRepo('dummy')
34 instance.get_commit_hash.return_value = \
35 '2b1ce059425edc91e013c260e59019195f927a07'
36 instance.get_commit_time.return_value = 1483257600
37 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070038
Allen Li060e84c2017-07-12 18:42:48 -070039 collector = git_metrics._GitMetricCollector('~/solciel', 'dummy')
40 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070041
Allen Li060e84c2017-07-12 18:42:48 -070042 setter = self.store.set
43 calls = [
44 mock.call('git/hash', ('~/solciel',), None,
45 '2b1ce059425edc91e013c260e59019195f927a07',
46 enforce_ge=mock.ANY),
47 mock.call('git/timestamp', ('~/solciel',), None,
48 1483257600, enforce_ge=mock.ANY),
49 mock.call('git/unstaged_changes',
50 ('added', '~/solciel'), None,
51 0, enforce_ge=mock.ANY),
52 mock.call('git/unstaged_changes',
53 ('deleted', '~/solciel'), None,
54 3, enforce_ge=mock.ANY),
55 ]
56 setter.assert_has_calls(calls)
57 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070058
59
Allen Li8934e042017-06-21 15:54:42 -070060class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
61 """Tests for _GitRepo using a Git fixture."""
62
63 def setUp(self):
64 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li060e84c2017-07-12 18:42:48 -070065
66 devnull = open(os.devnull, 'w')
67 self.addCleanup(devnull.close)
68
69 def call(args, **kwargs):
70 subprocess.check_call(args, stdout=devnull, stderr=devnull, **kwargs)
71
Allen Li8934e042017-06-21 15:54:42 -070072 with osutils.ChdirContext(self.tempdir):
Allen Li060e84c2017-07-12 18:42:48 -070073 call(['git', 'init'])
74 call(['git', 'config', 'user.name', 'John Doe'])
75 call(['git', 'config', 'user.email', 'john@example.com'])
Allen Li8934e042017-06-21 15:54:42 -070076 with open('foo', 'w') as f:
77 f.write('a\nb\nc\n')
Allen Li060e84c2017-07-12 18:42:48 -070078 call(['git', 'add', 'foo'])
79 env = os.environ.copy()
80 env['GIT_AUTHOR_DATE'] = '2017-01-01T00:00:00Z'
81 env['GIT_COMMITTER_DATE'] = '2017-01-01T00:00:00Z'
82 call(['git', 'commit', '-m', 'Initial commit'], env=env)
83
84 def test_get_commit_hash(self):
85 """Test get_commit_hash()."""
86 repo = git_metrics._GitRepo(self.git_dir)
87 got = repo.get_commit_hash()
88 self.assertEqual(got, '7c88f131e520e8455e2403b88ff4f723758c5dd6')
89
90 def test_get_commit_time(self):
91 """Test get_commit_time()."""
92 repo = git_metrics._GitRepo(self.git_dir)
93 got = repo.get_commit_time()
94 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -070095
96 def test_get_unstaged_changes(self):
Allen Li060e84c2017-07-12 18:42:48 -070097 """Test get_unstaged_changes()."""
Allen Li8934e042017-06-21 15:54:42 -070098 with open(os.path.join(self.tempdir, 'spam'), 'w') as f:
99 f.write('a\n')
100 os.remove(os.path.join(self.tempdir, 'foo'))
101 repo = git_metrics._GitRepo(self.git_dir)
102 added, removed = repo.get_unstaged_changes()
103 self.assertEqual(added, 0) # Does not count untracked files
104 self.assertEqual(removed, 3)