blob: 67be8a6c0fcb3e9d81b6785b8865d9ddaf8581bf [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
Paul Hobbse46a42b2017-03-21 14:04:13 -07002# 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(
Trent Aptedc4f366a2023-05-16 15:32:48 +100025 "chromite.third_party.infra_libs.ts_mon.common.interface.state."
26 "store",
Alex Klein1699fab2022-09-08 08:46:06 -060027 autospec=True,
28 )
29 self.store = patcher.start()
30 self.addCleanup(patcher.stop)
Paul Hobbse46a42b2017-03-21 14:04:13 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 def test_collect(self):
33 with mock.patch.object(
34 git_metrics, "_GitRepo", autospec=True
35 ) as _GitRepo:
36 instance = _GitRepo("stub")
37 instance.get_commit_hash.return_value = (
38 "2b1ce059425edc91e013c260e59019195f927a07"
39 )
40 instance.get_commit_time.return_value = 1483257600
41 instance.get_unstaged_changes.return_value = (0, 3)
Paul Hobbse46a42b2017-03-21 14:04:13 -070042
Alex Klein1699fab2022-09-08 08:46:06 -060043 collector = git_metrics._GitMetricCollector("~/solciel", "stub")
44 collector.collect()
Paul Hobbse46a42b2017-03-21 14:04:13 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 setter = self.store.set
47 calls = [
48 mock.call(
49 "git/hash",
50 ("~/solciel",),
51 None,
52 "2b1ce059425edc91e013c260e59019195f927a07",
53 enforce_ge=mock.ANY,
54 ),
55 mock.call(
56 "git/timestamp",
57 ("~/solciel",),
58 None,
59 1483257600,
60 enforce_ge=mock.ANY,
61 ),
62 mock.call(
63 "git/unstaged_changes",
64 ("added", "~/solciel"),
65 None,
66 0,
67 enforce_ge=mock.ANY,
68 ),
69 mock.call(
70 "git/unstaged_changes",
71 ("deleted", "~/solciel"),
72 None,
73 3,
74 enforce_ge=mock.ANY,
75 ),
76 ]
77 setter.assert_has_calls(calls)
78 self.assertEqual(len(setter.mock_calls), len(calls))
Allen Libdb9f042017-04-10 13:25:47 -070079
80
Allen Li8934e042017-06-21 15:54:42 -070081class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060082 """Tests for _GitRepo using a Git fixture."""
Allen Li8934e042017-06-21 15:54:42 -070083
Alex Klein1699fab2022-09-08 08:46:06 -060084 def setUp(self):
85 self.git_dir = os.path.join(self.tempdir, ".git")
Allen Li060e84c2017-07-12 18:42:48 -070086
Alex Klein1699fab2022-09-08 08:46:06 -060087 def call(args, **kwargs):
88 subprocess.check_call(
89 args,
90 stdout=subprocess.DEVNULL,
91 stderr=subprocess.DEVNULL,
92 **kwargs,
93 )
Allen Li060e84c2017-07-12 18:42:48 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 with osutils.ChdirContext(self.tempdir):
96 call(["git", "init"])
97 call(["git", "config", "user.name", "John Doe"])
98 call(["git", "config", "user.email", "john@example.com"])
Mike Frysinger31fdddd2023-02-24 15:50:55 -050099 osutils.WriteFile("foo", "a\nb\nc\n")
Alex Klein1699fab2022-09-08 08:46:06 -0600100 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()."""
Mike Frysinger31fdddd2023-02-24 15:50:55 -0500120 (self.tempdir / "spam").write_text("a\n", encoding="utf-8")
Alex Klein1699fab2022-09-08 08:46:06 -0600121 os.remove(os.path.join(self.tempdir, "foo"))
122 repo = git_metrics._GitRepo(self.git_dir)
123 added, removed = repo.get_unstaged_changes()
124 self.assertEqual(added, 0) # Does not count untracked files
125 self.assertEqual(removed, 3)