blob: 44ff6371b4b0a685f76a0fac03f7c58772306734 [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
Mike Frysinger166fea02021-02-12 05:30:33 -050013from unittest import mock
Paul Hobbse46a42b2017-03-21 14:04:13 -070014
Allen Li060e84c2017-07-12 18:42:48 -070015from chromite.lib import cros_test_lib
16from chromite.lib import osutils
Paul Hobbse46a42b2017-03-21 14:04:13 -070017from chromite.scripts.sysmon import git_metrics
18
Paul Hobbse46a42b2017-03-21 14:04:13 -070019
Allen Li060e84c2017-07-12 18:42:48 -070020class TestGitMetricCollector(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060021 """Tests for _GitMetricCollector."""
Paul Hobbse46a42b2017-03-21 14:04:13 -070022
Alex Klein1699fab2022-09-08 08:46:06 -060023 def setUp(self):
24 patcher = mock.patch(
25 "chromite.third_party.infra_libs.ts_mon.common.interface.state.store",
26 autospec=True,
27 )
28 self.store = patcher.start()
29 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070030
Alex Klein1699fab2022-09-08 08:46:06 -060031 def test_collect(self):
32 with mock.patch.object(
33 git_metrics, "_GitRepo", autospec=True
34 ) as _GitRepo:
35 instance = _GitRepo("stub")
36 instance.get_commit_hash.return_value = (
37 "2b1ce059425edc91e013c260e59019195f927a07"
38 )
39 instance.get_commit_time.return_value = 1483257600
40 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070041
Alex Klein1699fab2022-09-08 08:46:06 -060042 collector = git_metrics._GitMetricCollector("~/solciel", "stub")
43 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 setter = self.store.set
46 calls = [
47 mock.call(
48 "git/hash",
49 ("~/solciel",),
50 None,
51 "2b1ce059425edc91e013c260e59019195f927a07",
52 enforce_ge=mock.ANY,
53 ),
54 mock.call(
55 "git/timestamp",
56 ("~/solciel",),
57 None,
58 1483257600,
59 enforce_ge=mock.ANY,
60 ),
61 mock.call(
62 "git/unstaged_changes",
63 ("added", "~/solciel"),
64 None,
65 0,
66 enforce_ge=mock.ANY,
67 ),
68 mock.call(
69 "git/unstaged_changes",
70 ("deleted", "~/solciel"),
71 None,
72 3,
73 enforce_ge=mock.ANY,
74 ),
75 ]
76 setter.assert_has_calls(calls)
77 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070078
79
Allen Li8934e042017-06-21 15:54:42 -070080class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060081 """Tests for _GitRepo using a Git fixture."""
Allen Li8934e042017-06-21 15:54:42 -070082
Alex Klein1699fab2022-09-08 08:46:06 -060083 def setUp(self):
84 self.git_dir = os.path.join(self.tempdir, ".git")
Allen Li060e84c2017-07-12 18:42:48 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 def call(args, **kwargs):
87 subprocess.check_call(
88 args,
89 stdout=subprocess.DEVNULL,
90 stderr=subprocess.DEVNULL,
91 **kwargs,
92 )
Allen Li060e84c2017-07-12 18:42:48 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 with osutils.ChdirContext(self.tempdir):
95 call(["git", "init"])
96 call(["git", "config", "user.name", "John Doe"])
97 call(["git", "config", "user.email", "john@example.com"])
98 with open("foo", "w") as f:
99 f.write("a\nb\nc\n")
100 call(["git", "add", "foo"])
101 env = os.environ.copy()
102 env["GIT_AUTHOR_DATE"] = "2017-01-01T00:00:00Z"
103 env["GIT_COMMITTER_DATE"] = "2017-01-01T00:00:00Z"
104 call(["git", "commit", "-m", "Initial commit"], env=env)
Allen Li060e84c2017-07-12 18:42:48 -0700105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 def test_get_commit_hash(self):
107 """Test get_commit_hash()."""
108 repo = git_metrics._GitRepo(self.git_dir)
109 got = repo.get_commit_hash()
110 self.assertEqual(got, "7c88f131e520e8455e2403b88ff4f723758c5dd6")
Allen Li060e84c2017-07-12 18:42:48 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 def test_get_commit_time(self):
113 """Test get_commit_time()."""
114 repo = git_metrics._GitRepo(self.git_dir)
115 got = repo.get_commit_time()
116 self.assertEqual(got, 1483228800)
Allen Li8934e042017-06-21 15:54:42 -0700117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 def test_get_unstaged_changes(self):
119 """Test get_unstaged_changes()."""
120 with open(os.path.join(self.tempdir, "spam"), "w") as f:
121 f.write("a\n")
122 os.remove(os.path.join(self.tempdir, "foo"))
123 repo = git_metrics._GitRepo(self.git_dir)
124 added, removed = repo.get_unstaged_changes()
125 self.assertEqual(added, 0) # Does not count untracked files
126 self.assertEqual(removed, 3)