blob: 9464beac1e279e29df6da837e28de9f1aa33aaa2 [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
Paul Hobbse46a42b2017-03-21 14:04:13 -070010
Paul Hobbse46a42b2017-03-21 14:04:13 -070011import os
Allen Li060e84c2017-07-12 18:42:48 -070012import subprocess
Paul Hobbse46a42b2017-03-21 14:04:13 -070013
Allen Li060e84c2017-07-12 18:42:48 -070014from chromite.lib import cros_test_lib
15from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070016from chromite.scripts.sysmon import git_metrics
Mike Frysinger40ffb532021-02-12 07:36:08 -050017from chromite.third_party import mock
Paul Hobbse46a42b2017-03-21 14:04:13 -070018
Paul Hobbse46a42b2017-03-21 14:04:13 -070019
Allen Li060e84c2017-07-12 18:42:48 -070020class TestGitMetricCollector(cros_test_lib.TestCase):
21 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070022
23 def setUp(self):
Allen Li060e84c2017-07-12 18:42:48 -070024 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
25 autospec=True)
26 self.store = patcher.start()
27 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070028
Allen Li060e84c2017-07-12 18:42:48 -070029 def test_collect(self):
30 with mock.patch.object(git_metrics, '_GitRepo', autospec=True) as _GitRepo:
31 instance = _GitRepo('dummy')
Mike Frysingerfcca49e2021-03-17 01:09:20 -040032 instance.get_commit_hash.return_value = (
33 '2b1ce059425edc91e013c260e59019195f927a07')
Allen Li060e84c2017-07-12 18:42:48 -070034 instance.get_commit_time.return_value = 1483257600
35 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070036
Allen Li060e84c2017-07-12 18:42:48 -070037 collector = git_metrics._GitMetricCollector('~/solciel', 'dummy')
38 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070039
Allen Li060e84c2017-07-12 18:42:48 -070040 setter = self.store.set
41 calls = [
42 mock.call('git/hash', ('~/solciel',), None,
43 '2b1ce059425edc91e013c260e59019195f927a07',
44 enforce_ge=mock.ANY),
45 mock.call('git/timestamp', ('~/solciel',), None,
46 1483257600, enforce_ge=mock.ANY),
47 mock.call('git/unstaged_changes',
48 ('added', '~/solciel'), None,
49 0, enforce_ge=mock.ANY),
50 mock.call('git/unstaged_changes',
51 ('deleted', '~/solciel'), None,
52 3, enforce_ge=mock.ANY),
53 ]
54 setter.assert_has_calls(calls)
55 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070056
57
Allen Li8934e042017-06-21 15:54:42 -070058class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
59 """Tests for _GitRepo using a Git fixture."""
60
61 def setUp(self):
62 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li060e84c2017-07-12 18:42:48 -070063
64 devnull = open(os.devnull, 'w')
65 self.addCleanup(devnull.close)
66
67 def call(args, **kwargs):
68 subprocess.check_call(args, stdout=devnull, stderr=devnull, **kwargs)
69
Allen Li8934e042017-06-21 15:54:42 -070070 with osutils.ChdirContext(self.tempdir):
Allen Li060e84c2017-07-12 18:42:48 -070071 call(['git', 'init'])
72 call(['git', 'config', 'user.name', 'John Doe'])
73 call(['git', 'config', 'user.email', 'john@example.com'])
Allen Li8934e042017-06-21 15:54:42 -070074 with open('foo', 'w') as f:
75 f.write('a\nb\nc\n')
Allen Li060e84c2017-07-12 18:42:48 -070076 call(['git', 'add', 'foo'])
77 env = os.environ.copy()
78 env['GIT_AUTHOR_DATE'] = '2017-01-01T00:00:00Z'
79 env['GIT_COMMITTER_DATE'] = '2017-01-01T00:00:00Z'
80 call(['git', 'commit', '-m', 'Initial commit'], env=env)
81
82 def test_get_commit_hash(self):
83 """Test get_commit_hash()."""
84 repo = git_metrics._GitRepo(self.git_dir)
85 got = repo.get_commit_hash()
86 self.assertEqual(got, '7c88f131e520e8455e2403b88ff4f723758c5dd6')
87
88 def test_get_commit_time(self):
89 """Test get_commit_time()."""
90 repo = git_metrics._GitRepo(self.git_dir)
91 got = repo.get_commit_time()
92 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -070093
94 def test_get_unstaged_changes(self):
Allen Li060e84c2017-07-12 18:42:48 -070095 """Test get_unstaged_changes()."""
Allen Li8934e042017-06-21 15:54:42 -070096 with open(os.path.join(self.tempdir, 'spam'), 'w') as f:
97 f.write('a\n')
98 os.remove(os.path.join(self.tempdir, 'foo'))
99 repo = git_metrics._GitRepo(self.git_dir)
100 added, removed = repo.get_unstaged_changes()
101 self.assertEqual(added, 0) # Does not count untracked files
102 self.assertEqual(removed, 3)