blob: 58341dffb12561071a4234fe142b469ffbfb40e9 [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
Paul Hobbse46a42b2017-03-21 14:04:13 -070013import os
Allen Li060e84c2017-07-12 18:42:48 -070014import subprocess
Mike Frysinger1ca14432020-02-16 00:18:56 -050015import sys
Paul Hobbse46a42b2017-03-21 14:04:13 -070016
Mike Frysinger6db648e2018-07-24 19:57:58 -040017import mock
18
Allen Li060e84c2017-07-12 18:42:48 -070019from chromite.lib import cros_test_lib
20from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070021from chromite.scripts.sysmon import git_metrics
22
Paul Hobbse46a42b2017-03-21 14:04:13 -070023
Mike Frysinger1ca14432020-02-16 00:18:56 -050024assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
25
26
Allen Li060e84c2017-07-12 18:42:48 -070027class TestGitMetricCollector(cros_test_lib.TestCase):
28 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070029
30 def setUp(self):
Allen Li060e84c2017-07-12 18:42:48 -070031 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
32 autospec=True)
33 self.store = patcher.start()
34 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070035
Allen Li060e84c2017-07-12 18:42:48 -070036 def test_collect(self):
37 with mock.patch.object(git_metrics, '_GitRepo', autospec=True) as _GitRepo:
38 instance = _GitRepo('dummy')
Mike Frysingerfcca49e2021-03-17 01:09:20 -040039 instance.get_commit_hash.return_value = (
40 '2b1ce059425edc91e013c260e59019195f927a07')
Allen Li060e84c2017-07-12 18:42:48 -070041 instance.get_commit_time.return_value = 1483257600
42 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070043
Allen Li060e84c2017-07-12 18:42:48 -070044 collector = git_metrics._GitMetricCollector('~/solciel', 'dummy')
45 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070046
Allen Li060e84c2017-07-12 18:42:48 -070047 setter = self.store.set
48 calls = [
49 mock.call('git/hash', ('~/solciel',), None,
50 '2b1ce059425edc91e013c260e59019195f927a07',
51 enforce_ge=mock.ANY),
52 mock.call('git/timestamp', ('~/solciel',), None,
53 1483257600, enforce_ge=mock.ANY),
54 mock.call('git/unstaged_changes',
55 ('added', '~/solciel'), None,
56 0, enforce_ge=mock.ANY),
57 mock.call('git/unstaged_changes',
58 ('deleted', '~/solciel'), None,
59 3, enforce_ge=mock.ANY),
60 ]
61 setter.assert_has_calls(calls)
62 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070063
64
Allen Li8934e042017-06-21 15:54:42 -070065class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
66 """Tests for _GitRepo using a Git fixture."""
67
68 def setUp(self):
69 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li060e84c2017-07-12 18:42:48 -070070
71 devnull = open(os.devnull, 'w')
72 self.addCleanup(devnull.close)
73
74 def call(args, **kwargs):
75 subprocess.check_call(args, stdout=devnull, stderr=devnull, **kwargs)
76
Allen Li8934e042017-06-21 15:54:42 -070077 with osutils.ChdirContext(self.tempdir):
Allen Li060e84c2017-07-12 18:42:48 -070078 call(['git', 'init'])
79 call(['git', 'config', 'user.name', 'John Doe'])
80 call(['git', 'config', 'user.email', 'john@example.com'])
Allen Li8934e042017-06-21 15:54:42 -070081 with open('foo', 'w') as f:
82 f.write('a\nb\nc\n')
Allen Li060e84c2017-07-12 18:42:48 -070083 call(['git', 'add', 'foo'])
84 env = os.environ.copy()
85 env['GIT_AUTHOR_DATE'] = '2017-01-01T00:00:00Z'
86 env['GIT_COMMITTER_DATE'] = '2017-01-01T00:00:00Z'
87 call(['git', 'commit', '-m', 'Initial commit'], env=env)
88
89 def test_get_commit_hash(self):
90 """Test get_commit_hash()."""
91 repo = git_metrics._GitRepo(self.git_dir)
92 got = repo.get_commit_hash()
93 self.assertEqual(got, '7c88f131e520e8455e2403b88ff4f723758c5dd6')
94
95 def test_get_commit_time(self):
96 """Test get_commit_time()."""
97 repo = git_metrics._GitRepo(self.git_dir)
98 got = repo.get_commit_time()
99 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -0700100
101 def test_get_unstaged_changes(self):
Allen Li060e84c2017-07-12 18:42:48 -0700102 """Test get_unstaged_changes()."""
Allen Li8934e042017-06-21 15:54:42 -0700103 with open(os.path.join(self.tempdir, 'spam'), 'w') as f:
104 f.write('a\n')
105 os.remove(os.path.join(self.tempdir, 'foo'))
106 repo = git_metrics._GitRepo(self.git_dir)
107 added, removed = repo.get_unstaged_changes()
108 self.assertEqual(added, 0) # Does not count untracked files
109 self.assertEqual(removed, 3)