|
2 | 2 | from django.test import TestCase, override_settings |
3 | 3 | from unittest.mock import Mock, patch |
4 | 4 |
|
5 | | -from codespeed.commits.git import getlogs |
| 5 | +from codespeed import commits |
| 6 | +from codespeed.commits.git import Git, getlogs |
| 7 | +from codespeed.commits.logs import FunctionBackend, get_scm |
6 | 8 | from codespeed.models import Project, Revision, Branch, Environment |
7 | 9 |
|
8 | 10 |
|
@@ -58,3 +60,77 @@ def side_effect(cmd, *args, **kwargs): |
58 | 60 | 'tag': 'tag', |
59 | 61 | } |
60 | 62 | self.assertEqual([expected], logs) |
| 63 | + |
| 64 | + @patch("codespeed.commits.git.Popen") |
| 65 | + def test_logs_through_dispatcher(self, popen): |
| 66 | + """commits.get_logs must reach the git backend for a Git project.""" |
| 67 | + ret = Mock() |
| 68 | + ret.returncode = 0 |
| 69 | + ret.communicate.return_value = ( |
| 70 | + b"id\x00long_id\x001583489681\x00author\x00email\x00msg\x00\x1e", b'') |
| 71 | + popen.return_value = ret |
| 72 | + |
| 73 | + logs = commits.get_logs(self.revision, self.revision) |
| 74 | + |
| 75 | + self.assertEqual(len(logs), 1) |
| 76 | + self.assertEqual(logs[0]['commitid'], 'long_id') |
| 77 | + |
| 78 | + @patch("codespeed.commits.git.Popen") |
| 79 | + def test_get_commit_date(self, popen): |
| 80 | + ret = Mock() |
| 81 | + ret.returncode = 0 |
| 82 | + ret.communicate.return_value = (b"1583489681\n", b'') |
| 83 | + popen.return_value = ret |
| 84 | + |
| 85 | + date = commits.get_commit_date(self.project, 'long_id') |
| 86 | + |
| 87 | + self.assertEqual(date, datetime(2020, 3, 6, 10, 14, 41)) |
| 88 | + cmd = popen.call_args_list[-1][0][0] |
| 89 | + self.assertEqual(cmd[:3], ['git', 'log', '-1']) |
| 90 | + self.assertEqual(cmd[-1], 'long_id') |
| 91 | + |
| 92 | + @patch("codespeed.commits.git.Popen") |
| 93 | + def test_get_commit_date_unknown_commit_falls_back(self, popen): |
| 94 | + ret = Mock() |
| 95 | + ret.returncode = 128 |
| 96 | + ret.communicate.return_value = (b'', b'fatal: bad object') |
| 97 | + popen.return_value = ret |
| 98 | + |
| 99 | + date = commits.get_commit_date(self.project, 'nope') |
| 100 | + |
| 101 | + # Unknown commit: today rather than an exception, as before |
| 102 | + self.assertEqual(date.date(), datetime.today().date()) |
| 103 | + |
| 104 | + |
| 105 | +class GetScmTest(TestCase): |
| 106 | + def make(self, repo_type, path='https://github.com/python/cpython'): |
| 107 | + return Project.objects.create(name='p-%s' % repo_type, repo_path=path, |
| 108 | + repo_type=repo_type) |
| 109 | + |
| 110 | + def test_git(self): |
| 111 | + self.assertIsInstance(get_scm(self.make(Project.GIT)), Git) |
| 112 | + |
| 113 | + def test_github_and_subversion_get_an_adapter(self): |
| 114 | + for repo_type in (Project.GITHUB, Project.SUBVERSION): |
| 115 | + self.assertIsInstance(get_scm(self.make(repo_type)), FunctionBackend) |
| 116 | + |
| 117 | + def test_subversion_has_no_commit_date(self): |
| 118 | + self.assertIsNone(get_scm(self.make(Project.SUBVERSION)).get_commit_date('1')) |
| 119 | + |
| 120 | + @patch("codespeed.commits.github.fetch_json") |
| 121 | + def test_github_commit_date_from_api(self, fetch_json): |
| 122 | + def fake(url): |
| 123 | + if url.endswith('/git/refs/tags'): |
| 124 | + return [] |
| 125 | + return {'sha': 'abc', 'parents': [], 'message': 'm', |
| 126 | + 'committer': {'date': '2020-03-06T10:14:41Z'}, |
| 127 | + 'author': {'name': 'a', 'email': 'a@example.com'}} |
| 128 | + fetch_json.side_effect = fake |
| 129 | + date = commits.get_commit_date(self.make(Project.GITHUB), 'abc') |
| 130 | + self.assertEqual(date.isoformat(), '2020-03-06T10:14:41+00:00') |
| 131 | + self.assertIn('/repos/python/cpython/git/commits/abc', |
| 132 | + fetch_json.call_args_list[0][0][0]) |
| 133 | + |
| 134 | + def test_no_logs(self): |
| 135 | + self.assertIsNone(get_scm(self.make(Project.NO_LOGS))) |
| 136 | + self.assertEqual(commits.get_logs.__name__, 'get_logs') |
0 commit comments