|
Pierre-Yves Chibon |
67d1cc |
|
|
Pierre-Yves Chibon |
67d1cc |
from __future__ import unicode_literals, absolute_import
|
|
Pierre-Yves Chibon |
67d1cc |
|
|
Slavek Kabrda |
5ab53d |
from mock import patch, MagicMock, Mock
|
|
Lubomír Sedlář |
d6299f |
from collections import namedtuple
|
|
Lubomír Sedlář |
d6299f |
import os
|
|
Lubomír Sedlář |
d6299f |
import unittest
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
from pagure.lib import tasks
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
MockUser = namedtuple('MockUser', ['fullname', 'default_email'])
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
class MockCommit(object):
|
|
Lubomír Sedlář |
d6299f |
def __init__(self, name, email, time='1970-01-01 00:00'):
|
|
Lubomír Sedlář |
d6299f |
self.author = Mock(email=email)
|
|
Lubomír Sedlář |
d6299f |
self.author.name = name
|
|
Lubomír Sedlář |
d6299f |
self.commit_time = time
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
|
|
Pierre-Yves Chibon |
930073 |
@patch('pagure.lib.query.create_session', new=Mock())
|
|
Lubomír Sedlář |
d6299f |
class TestCommitsAuthorStats(unittest.TestCase):
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def setUp(self):
|
|
Pierre-Yves Chibon |
930073 |
self.search_user_patcher = patch('pagure.lib.query.search_user')
|
|
Lubomír Sedlář |
d6299f |
mock_search_user = self.search_user_patcher.start()
|
|
Lubomír Sedlář |
d6299f |
mock_search_user.side_effect = lambda _, email: self.authors.get(email)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.pygit_patcher = patch('pygit2.Repository')
|
|
Lubomír Sedlář |
d6299f |
mock_repo = self.pygit_patcher.start().return_value
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def mock_walk_impl(*args, **kwargs):
|
|
Lubomír Sedlář |
d6299f |
for commit in self.commits:
|
|
Lubomír Sedlář |
d6299f |
yield commit
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
mock_repo.walk.side_effect = mock_walk_impl
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.repopath = Mock()
|
|
Lubomír Sedlář |
d6299f |
exists = os.path.exists
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def mock_exists_impl(path):
|
|
Lubomír Sedlář |
d6299f |
if path == self.repopath:
|
|
Lubomír Sedlář |
d6299f |
return True
|
|
Lubomír Sedlář |
d6299f |
return exists(path)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.exists_patcher = patch('os.path.exists')
|
|
Lubomír Sedlář |
d6299f |
mock_exists = self.exists_patcher.start()
|
|
Lubomír Sedlář |
d6299f |
mock_exists.side_effect = mock_exists_impl
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def tearDown(self):
|
|
Lubomír Sedlář |
d6299f |
self.search_user_patcher.stop()
|
|
Lubomír Sedlář |
d6299f |
self.pygit_patcher.stop()
|
|
Lubomír Sedlář |
d6299f |
self.exists_patcher.stop()
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def test_no_change(self):
|
|
Lubomír Sedlář |
d6299f |
self.commits = [
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
|
|
Lubomír Sedlář |
d6299f |
]
|
|
Lubomír Sedlář |
d6299f |
self.authors = {
|
|
Lubomír Sedlář |
d6299f |
'alice@example.com': MockUser('Alice', 'alice@example.com'),
|
|
Lubomír Sedlář |
d6299f |
}
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
num_commits, authors, num_authors, last_time = \
|
|
Lubomír Sedlář |
d6299f |
tasks.commits_author_stats(self.repopath)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_commits, 1)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_authors, 1)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(last_time, '2018-01-01 00:00')
|
|
Pierre-Yves Chibon |
409376 |
self.assertIn(
|
|
Pierre-Yves Chibon |
409376 |
authors,
|
|
Pierre-Yves Chibon |
409376 |
[
|
|
Pierre-Yves Chibon |
409376 |
[(1, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?s=32&d=retro'
|
|
Pierre-Yves Chibon |
409376 |
)])],
|
|
Pierre-Yves Chibon |
409376 |
[(1, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?d=retro&s=32'
|
|
Pierre-Yves Chibon |
409376 |
)])]
|
|
Pierre-Yves Chibon |
409376 |
]
|
|
Pierre-Yves Chibon |
409376 |
)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def test_rename_user_and_merge(self):
|
|
Lubomír Sedlář |
d6299f |
self.commits = [
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Alice', 'alice@example.com'),
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Bad name', 'alice@example.com', '2018-01-01 00:00'),
|
|
Lubomír Sedlář |
d6299f |
]
|
|
Lubomír Sedlář |
d6299f |
self.authors = {
|
|
Lubomír Sedlář |
d6299f |
'alice@example.com': MockUser('Alice', 'alice@example.com'),
|
|
Lubomír Sedlář |
d6299f |
}
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
num_commits, authors, num_authors, last_time = \
|
|
Lubomír Sedlář |
d6299f |
tasks.commits_author_stats(self.repopath)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_commits, 2)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_authors, 1)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(last_time, '2018-01-01 00:00')
|
|
Pierre-Yves Chibon |
409376 |
self.assertIn(
|
|
Pierre-Yves Chibon |
409376 |
authors,
|
|
Pierre-Yves Chibon |
409376 |
[
|
|
Pierre-Yves Chibon |
409376 |
[(2, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?s=32&d=retro'
|
|
Pierre-Yves Chibon |
409376 |
)])],
|
|
Pierre-Yves Chibon |
409376 |
[(2, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?d=retro&s=32'
|
|
Pierre-Yves Chibon |
409376 |
)])]
|
|
Pierre-Yves Chibon |
409376 |
]
|
|
Pierre-Yves Chibon |
409376 |
)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def test_preserve_unknown_author(self):
|
|
Lubomír Sedlář |
d6299f |
self.commits = [
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Alice', 'alice@example.com', '2018-01-01 00:00'),
|
|
Lubomír Sedlář |
d6299f |
]
|
|
Lubomír Sedlář |
d6299f |
self.authors = {}
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
num_commits, authors, num_authors, last_time = \
|
|
Lubomír Sedlář |
d6299f |
tasks.commits_author_stats(self.repopath)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_commits, 1)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_authors, 1)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(last_time, '2018-01-01 00:00')
|
|
Pierre-Yves Chibon |
409376 |
self.assertIn(
|
|
Pierre-Yves Chibon |
409376 |
authors,
|
|
Pierre-Yves Chibon |
409376 |
[
|
|
Pierre-Yves Chibon |
409376 |
[(1, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?s=32&d=retro'
|
|
Pierre-Yves Chibon |
409376 |
)])],
|
|
Pierre-Yves Chibon |
409376 |
[(1, [(
|
|
Pierre-Yves Chibon |
409376 |
'Alice', 'alice@example.com',
|
|
Pierre-Yves Chibon |
409376 |
'https://seccdn.libravatar.org/avatar/'
|
|
Pierre-Yves Chibon |
409376 |
'ff8d9819fc0e12bf0d24892e45987e249a28dce836a85cad60e28eaaa8c6d976'
|
|
Pierre-Yves Chibon |
409376 |
'?d=retro&s=32'
|
|
Pierre-Yves Chibon |
409376 |
)])]
|
|
Pierre-Yves Chibon |
409376 |
]
|
|
Pierre-Yves Chibon |
409376 |
)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
def test_handle_empty_email(self):
|
|
Lubomír Sedlář |
d6299f |
self.commits = [
|
|
Lubomír Sedlář |
d6299f |
# Two commits for Alice to ensure order of the result.
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Alice', None),
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Alice', None),
|
|
Lubomír Sedlář |
d6299f |
MockCommit('Bob', '', '2018-01-01 00:00'),
|
|
Lubomír Sedlář |
d6299f |
]
|
|
Lubomír Sedlář |
d6299f |
self.authors = {}
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
num_commits, authors, num_authors, last_time = \
|
|
Lubomír Sedlář |
d6299f |
tasks.commits_author_stats(self.repopath)
|
|
Lubomír Sedlář |
d6299f |
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_commits, 3)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(num_authors, 2)
|
|
Lubomír Sedlář |
d6299f |
self.assertEqual(last_time, '2018-01-01 00:00')
|
|
Pierre-Yves Chibon |
409376 |
self.assertEqual(authors, [(2, [('Alice', None, None)]),
|
|
Pierre-Yves Chibon |
409376 |
(1, [('Bob', '', None)])])
|
|
Slavek Kabrda |
5ab53d |
|
|
Slavek Kabrda |
5ab53d |
|
|
Slavek Kabrda |
5ab53d |
class TestGitolitePostCompileOnly(object):
|
|
Slavek Kabrda |
5ab53d |
@patch('pagure.lib.git_auth.get_git_auth_helper')
|
|
Slavek Kabrda |
5ab53d |
def test_backend_has_post_compile_only(self, get_helper):
|
|
Slavek Kabrda |
5ab53d |
helper = MagicMock()
|
|
Slavek Kabrda |
5ab53d |
get_helper.return_value = helper
|
|
Slavek Kabrda |
5ab53d |
helper.post_compile_only = MagicMock()
|
|
Slavek Kabrda |
5ab53d |
tasks.gitolite_post_compile_only()
|
|
Slavek Kabrda |
5ab53d |
helper.post_compile_only.assert_called_once()
|
|
Slavek Kabrda |
5ab53d |
|
|
Slavek Kabrda |
5ab53d |
@patch('pagure.lib.git_auth.get_git_auth_helper')
|
|
Slavek Kabrda |
5ab53d |
def test_backend_doesnt_have_post_compile_only(self, get_helper):
|
|
Slavek Kabrda |
5ab53d |
helper = MagicMock()
|
|
Slavek Kabrda |
5ab53d |
get_helper.return_value = helper
|
|
Slavek Kabrda |
5ab53d |
helper.generate_acls = MagicMock()
|
|
Slavek Kabrda |
5ab53d |
del helper.post_compile_only
|
|
Slavek Kabrda |
5ab53d |
tasks.gitolite_post_compile_only()
|
|
Slavek Kabrda |
5ab53d |
helper.generate_acls.assert_called_once_with(project=None)
|