blob: 0d1f6e7f33e55b5dd5566a083acd47e9e3d82908 [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
Paul Hobbse46a42b2017-03-21 14:04:13 -070015
Mike Frysinger6db648e2018-07-24 19:57:58 -040016import mock
17
Allen Li060e84c2017-07-12 18:42:48 -070018from chromite.lib import cros_test_lib
19from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070020from chromite.scripts.sysmon import git_metrics
21
Paul Hobbse46a42b2017-03-21 14:04:13 -070022
Allen Li060e84c2017-07-12 18:42:48 -070023class TestGitMetricCollector(cros_test_lib.TestCase):
24 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070025
26 def setUp(self):
Allen Li060e84c2017-07-12 18:42:48 -070027 patcher = mock.patch('infra_libs.ts_mon.common.interface.state.store',
28 autospec=True)
29 self.store = patcher.start()
30 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070031
Allen Li060e84c2017-07-12 18:42:48 -070032 def test_collect(self):
33 with mock.patch.object(git_metrics, '_GitRepo', autospec=True) as _GitRepo:
34 instance = _GitRepo('dummy')
35 instance.get_commit_hash.return_value = \
36 '2b1ce059425edc91e013c260e59019195f927a07'
37 instance.get_commit_time.return_value = 1483257600
38 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070039
Allen Li060e84c2017-07-12 18:42:48 -070040 collector = git_metrics._GitMetricCollector('~/solciel', 'dummy')
41 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070042
Allen Li060e84c2017-07-12 18:42:48 -070043 setter = self.store.set
44 calls = [
45 mock.call('git/hash', ('~/solciel',), None,
46 '2b1ce059425edc91e013c260e59019195f927a07',
47 enforce_ge=mock.ANY),
48 mock.call('git/timestamp', ('~/solciel',), None,
49 1483257600, enforce_ge=mock.ANY),
50 mock.call('git/unstaged_changes',
51 ('added', '~/solciel'), None,
52 0, enforce_ge=mock.ANY),
53 mock.call('git/unstaged_changes',
54 ('deleted', '~/solciel'), None,
55 3, enforce_ge=mock.ANY),
56 ]
57 setter.assert_has_calls(calls)
58 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070059
60
Allen Li8934e042017-06-21 15:54:42 -070061class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
62 """Tests for _GitRepo using a Git fixture."""
63
64 def setUp(self):
65 self.git_dir = os.path.join(self.tempdir, '.git')
Allen Li060e84c2017-07-12 18:42:48 -070066
67 devnull = open(os.devnull, 'w')
68 self.addCleanup(devnull.close)
69
70 def call(args, **kwargs):
71 subprocess.check_call(args, stdout=devnull, stderr=devnull, **kwargs)
72
Allen Li8934e042017-06-21 15:54:42 -070073 with osutils.ChdirContext(self.tempdir):
Allen Li060e84c2017-07-12 18:42:48 -070074 call(['git', 'init'])
75 call(['git', 'config', 'user.name', 'John Doe'])
76 call(['git', 'config', 'user.email', 'john@example.com'])
Allen Li8934e042017-06-21 15:54:42 -070077 with open('foo', 'w') as f:
78 f.write('a\nb\nc\n')
Allen Li060e84c2017-07-12 18:42:48 -070079 call(['git', 'add', 'foo'])
80 env = os.environ.copy()
81 env['GIT_AUTHOR_DATE'] = '2017-01-01T00:00:00Z'
82 env['GIT_COMMITTER_DATE'] = '2017-01-01T00:00:00Z'
83 call(['git', 'commit', '-m', 'Initial commit'], env=env)
84
85 def test_get_commit_hash(self):
86 """Test get_commit_hash()."""
87 repo = git_metrics._GitRepo(self.git_dir)
88 got = repo.get_commit_hash()
89 self.assertEqual(got, '7c88f131e520e8455e2403b88ff4f723758c5dd6')
90
91 def test_get_commit_time(self):
92 """Test get_commit_time()."""
93 repo = git_metrics._GitRepo(self.git_dir)
94 got = repo.get_commit_time()
95 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -070096
97 def test_get_unstaged_changes(self):
Allen Li060e84c2017-07-12 18:42:48 -070098 """Test get_unstaged_changes()."""
Allen Li8934e042017-06-21 15:54:42 -070099 with open(os.path.join(self.tempdir, 'spam'), 'w') as f:
100 f.write('a\n')
101 os.remove(os.path.join(self.tempdir, 'foo'))
102 repo = git_metrics._GitRepo(self.git_dir)
103 added, removed = repo.get_unstaged_changes()
104 self.assertEqual(added, 0) # Does not count untracked files
105 self.assertEqual(removed, 3)