Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | from __future__ import absolute_import |
| 8 | from __future__ import print_function |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 9 | |
| 10 | import mock |
| 11 | import os |
| 12 | |
| 13 | from chromite.lib import cros_test_lib, osutils |
| 14 | from chromite.scripts.sysmon import git_metrics |
| 15 | |
| 16 | # pylint: disable=protected-access |
| 17 | # pylint: disable=attribute-defined-outside-init |
| 18 | |
Allen Li | 73b14b0 | 2017-04-10 13:18:06 -0700 | [diff] [blame] | 19 | class TestGitMetricsWithTempdir(cros_test_lib.TempDirTestCase): |
| 20 | """Tests for git metrics using a Git fixture.""" |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 21 | |
| 22 | def setUp(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 23 | self.git_dir = os.path.join(self.tempdir, '.git') |
Allen Li | 5fc64b3 | 2017-04-10 12:53:47 -0700 | [diff] [blame] | 24 | with osutils.ChdirContext(self.tempdir): |
| 25 | self.git_repo = git_metrics._GitRepo(self.git_dir) |
| 26 | self.git_repo._check_output(['init']) |
| 27 | self.git_repo._check_output(['commit', '--allow-empty', '-m', 'hi']) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 28 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 29 | def test_collect_git_hash_types_are_correct(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 30 | """Tests that collecting the git hash doesn't have type conflicts.""" |
| 31 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 32 | |
| 33 | # This has the side-effect of checking the types are correct. |
| 34 | collector._collect_commit_hash_metric() |
| 35 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 36 | def test_collect_git_time_types_are_correct(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 37 | """Tests that collecting the git commit time works.""" |
| 38 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 39 | |
| 40 | # This has the side-effect of checking the types are correct. |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 41 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 42 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 43 | def test_collect_git_hash_calls_set(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 44 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 45 | |
| 46 | with mock.patch.object(git_metrics._GitMetricCollector, |
| 47 | '_commit_hash_metric', |
| 48 | autospec=True) as hash_metric: |
| 49 | collector._collect_commit_hash_metric() |
| 50 | |
| 51 | commit_hash = self.git_repo.get_commit_hash() |
| 52 | self.assertEqual(hash_metric.set.call_args_list, [ |
| 53 | mock.call(commit_hash, {'repo': self.git_dir}) |
| 54 | ]) |
| 55 | |
Allen Li | 512d704 | 2017-04-10 13:14:22 -0700 | [diff] [blame] | 56 | def test_collect_git_time_calls_set(self): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 57 | collector = git_metrics._GitMetricCollector(self.git_dir, '/foo/bar') |
| 58 | with mock.patch.object(git_metrics._GitMetricCollector, |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 59 | '_timestamp_metric', |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 60 | autospec=True) as time_metric: |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 61 | collector._collect_timestamp_metric() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 62 | |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 63 | timestamp = self.git_repo.get_commit_time() |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 64 | self.assertEqual(time_metric.set.call_args_list, [ |
Allen Li | 3a2e180 | 2017-04-10 12:48:36 -0700 | [diff] [blame] | 65 | mock.call(timestamp, {'repo': self.git_dir}) |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 66 | ]) |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame] | 67 | |
| 68 | |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame^] | 69 | class TestGitRepoWithTempdir(cros_test_lib.TempDirTestCase): |
| 70 | """Tests for _GitRepo using a Git fixture.""" |
| 71 | |
| 72 | def setUp(self): |
| 73 | self.git_dir = os.path.join(self.tempdir, '.git') |
| 74 | with osutils.ChdirContext(self.tempdir): |
| 75 | self.git_repo = git_metrics._GitRepo(self.git_dir) |
| 76 | self.git_repo._check_output(['init']) |
| 77 | with open('foo', 'w') as f: |
| 78 | f.write('a\nb\nc\n') |
| 79 | self.git_repo._check_output(['add', 'foo']) |
| 80 | self.git_repo._check_output(['commit', '-m', 'hi']) |
| 81 | |
| 82 | def test_get_unstaged_changes(self): |
| 83 | """Test get_unstaged_changes.""" |
| 84 | with open(os.path.join(self.tempdir, 'spam'), 'w') as f: |
| 85 | f.write('a\n') |
| 86 | os.remove(os.path.join(self.tempdir, 'foo')) |
| 87 | repo = git_metrics._GitRepo(self.git_dir) |
| 88 | added, removed = repo.get_unstaged_changes() |
| 89 | self.assertEqual(added, 0) # Does not count untracked files |
| 90 | self.assertEqual(removed, 3) |
| 91 | |
| 92 | |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame] | 93 | class TestMetricCollector(cros_test_lib.TestCase): |
| 94 | """Tests for _GitMetricCollector.""" |
| 95 | |
| 96 | def test__gitdir_expand_user(self): |
| 97 | """Test that _gitdir is expanded for user.""" |
| 98 | with mock.patch.dict(os.environ, HOME='/home/arciel'): |
| 99 | collector = git_metrics._GitMetricCollector('~/solciel', 'dummy') |
| 100 | self.assertEqual(collector._gitdir, '~/solciel') |
| 101 | self.assertEqual(collector._gitrepo._gitdir, '/home/arciel/solciel') |