From 43fe7b9a2aa606467fba8b18d632381885b2d6d5 Mon Sep 17 00:00:00 2001 From: Nitanshu Date: Fri, 13 Jul 2018 02:34:44 +0530 Subject: [PATCH] tests: Implement CoroboTestCase Shift from `tests.helper.plugin_testbot` to `CoroboTestCase` for loading plugins in the TestBot. Closes https://github.com/coala/corobo/issues/584 --- tests/ban_test.py | 35 ++-- tests/corobo_test_case.py | 44 +++++ tests/git_stats_test.py | 57 +++--- tests/helper.py | 21 --- tests/labhub_test.py | 361 ++++++++++++++++++-------------------- 5 files changed, 258 insertions(+), 260 deletions(-) create mode 100644 tests/corobo_test_case.py delete mode 100644 tests/helper.py diff --git a/tests/ban_test.py b/tests/ban_test.py index 678bde64..a59ea103 100644 --- a/tests/ban_test.py +++ b/tests/ban_test.py @@ -1,22 +1,22 @@ -import logging -import unittest from unittest.mock import MagicMock, patch, PropertyMock import plugins.ban -from tests.helper import plugin_testbot +from tests.corobo_test_case import CoroboTestCase -class TestBan(unittest.TestCase): +class TestBan(CoroboTestCase): + + def setUp(self): + super().setUp((plugins.ban.Ban,)) + self.ban = self.load_plugin('Ban') + self.ban.bot_config.ROOMS_TO_JOIN = ( + 'coala/coala', 'coala/coala-bears') + self.ban.bot_config.BOT_IDENTITY['token'] = 'mocked?' @patch('plugins.ban.requests') @patch('plugins.ban.json') def test_ban_cmd(self, mockjson, mockreq): - ban, testbot = plugin_testbot(plugins.ban.Ban, logging.ERROR) - ban.activate() - - ban.bot_config.ROOMS_TO_JOIN = ('coala/coala', 'coala/coala-bears') - ban.bot_config.BOT_IDENTITY['token'] = 'mocked?' status_mock = MagicMock() type(status_mock).status_code = PropertyMock(return_value=200) mockreq.post.return_value = status_mock @@ -27,18 +27,13 @@ def test_ban_cmd(self, mockjson, mockreq): {'id': '897', 'uri': 'coala/coala-bears'} ] mockjson.loads.return_value = fake_room_data - testbot.assertCommand('!ban @nvzard', - 'nvzard has been banned from: coala/coala, ' - 'coala/coala-bears') + self.assertCommand('!ban @nvzard', + 'nvzard has been banned from: coala/coala, ' + 'coala/coala-bears') @patch('plugins.ban.requests') @patch('plugins.ban.json') def test_unban_cmd(self, mockjson, mockreq): - ban, testbot = plugin_testbot(plugins.ban.Ban, logging.ERROR) - ban.activate() - - ban.bot_config.ROOMS_TO_JOIN = ('coala/coala', 'coala/coala-bears') - ban.bot_config.BOT_IDENTITY['token'] = 'mocked?' status_mock = MagicMock() type(status_mock).status_code = PropertyMock(return_value=200) mockreq.delete.return_value = status_mock @@ -49,6 +44,6 @@ def test_unban_cmd(self, mockjson, mockreq): {'id': '897', 'uri': 'coala/coala-bears'} ] mockjson.loads.return_value = fake_room_data - testbot.assertCommand('!unban @nvzard', - 'nvzard has been unbanned from: coala/coala, ' - 'coala/coala-bears') + self.assertCommand('!unban @nvzard', + 'nvzard has been unbanned from: coala/coala, ' + 'coala/coala-bears') diff --git a/tests/corobo_test_case.py b/tests/corobo_test_case.py new file mode 100644 index 00000000..e741d55e --- /dev/null +++ b/tests/corobo_test_case.py @@ -0,0 +1,44 @@ +from errbot.backends.test import FullStackTest +from errbot.plugin_info import PluginInfo +from errbot.templating import add_plugin_templates_path +from pathlib import Path + +import logging + + +class CoroboTestCase(FullStackTest): + + def setUp(self, klasses: tuple): + super().setUp(loglevel=logging.ERROR, + extra_config={'BACKEND': 'text'}) + self.klasses = {} + self.plug_files = {} + self.plugins = {} + + for klass in klasses: + self.klasses[klass.__name__] = klass + self.load_plugin_templates(klass) + + def load_plugin_templates(self, klass): + plug_filename = klass.__module__.split('.')[-1] + '.plug' + plug_file_path = (Path(__file__) + .parent / '..' / 'plugins' / plug_filename) + with plug_file_path.open() as plugfile: + plug_info = PluginInfo.load_file(plugfile, plug_file_path) + self.plug_files[klass.__name__] = plug_info + add_plugin_templates_path(plug_info) + + def load_plugin(self, plugin_name: str, mock_dict=False): + """Load plugin manually""" + klass = self.klasses[plugin_name] + plugin = klass(self.bot, plugin_name) + plugin.activate() + self.plugins[plugin_name] = plugin + self.bot.plugin_manager.plugins[plugin_name] = plugin + plug_file = self.plug_files[plugin_name] + self.bot.plugin_manager.plugin_infos[plug_file.name] = plug_file + + if mock_dict: + self.inject_mocks(plugin_name=plugin_name, mock_dict=mock_dict) + + return plugin diff --git a/tests/git_stats_test.py b/tests/git_stats_test.py index 4f16d0e7..8378c8ed 100644 --- a/tests/git_stats_test.py +++ b/tests/git_stats_test.py @@ -1,7 +1,5 @@ -import logging from tempfile import mkdtemp from errbot import BotPlugin -import unittest from unittest.mock import create_autospec from IGitt.GitHub.GitHubMergeRequest import GitHubMergeRequest @@ -14,12 +12,13 @@ import IGitt import plugins.git_stats import plugins.labhub -from tests.helper import plugin_testbot +from tests.corobo_test_case import CoroboTestCase -class TestGitStats(unittest.TestCase): +class TestGitStats(CoroboTestCase): def setUp(self): + super().setUp((plugins.git_stats.GitStats,)) plugins.git_stats.github3 = create_autospec(github3) self.mock_org = create_autospec(github3.orgs.Organization) self.mock_gh = create_autospec(github3.GitHub) @@ -30,12 +29,10 @@ def setUp(self): self.plugin = plugins.git_stats.GitStats self.plugin.__bases__ = (BotPlugin, ) + self.git_stats = self.load_plugin('GitStats') + self.git_stats.REPOS = {'test': self.mock_repo} def test_pr_list(self): - git_stats, testbot = plugin_testbot(self.plugin, logging.ERROR) - git_stats.activate() - - git_stats.REPOS = {'test': self.mock_repo} mock_github_mr = create_autospec(GitHubMergeRequest) mock_gitlab_mr = create_autospec(GitLabMergeRequest) mock_github_issue = create_autospec(GitHubIssue) @@ -53,10 +50,10 @@ def test_pr_list(self): self.mock_repo.merge_requests = [mock_github_mr] # Non-existing repo - testbot.assertCommand(cmd_github.format('b'), - 'Repository doesn\'t exist.') - testbot.assertCommand(cmd_gitlab.format('b'), - 'Repository doesn\'t exist.') + self.assertCommand(cmd_github.format('b'), + 'Repository doesn\'t exist.') + self.assertCommand(cmd_gitlab.format('b'), + 'Repository doesn\'t exist.') # PR is suitable mock_github_mr.labels = ['process/approved', 'difficulty/newcomer'] @@ -68,26 +65,26 @@ def test_pr_list(self): mock_repo_obj.head.commit.hexsha = '1' mock_github_mr.base.sha = '1' mock_gitlab_mr.base.sha = '1' - testbot.assertCommand(cmd_github.format('test'), - 'PRs ready to be merged:\n ' - 'http://www.example.com/') + self.assertCommand(cmd_github.format('test'), + 'PRs ready to be merged:\n ' + 'http://www.example.com/') self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_gitlab.format('test'), - 'PRs ready to be merged:\n ' - 'http://www.example.com/') + self.assertCommand(cmd_gitlab.format('test'), + 'PRs ready to be merged:\n ' + 'http://www.example.com/') # PR is not suitable (wrong labels) mock_github_mr.labels = ['process/wip', 'difficulty/newcomer'] mock_gitlab_mr.labels = ['process/wip', 'difficulty/newcomer'] self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_github.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_github.format('test'), + 'No merge-ready PRs!') self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_gitlab.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_gitlab.format('test'), + 'No merge-ready PRs!') mock_github_mr.labels = ['process/approved', 'difficulty/newcomer'] mock_gitlab_mr.labels = ['process/approved', 'difficulty/newcomer'] @@ -95,12 +92,12 @@ def test_pr_list(self): mock_repo_obj.head.commit.hexsha = '2' self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_github.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_github.format('test'), + 'No merge-ready PRs!') self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_gitlab.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_gitlab.format('test'), + 'No merge-ready PRs!') mock_repo_obj.head.commit.hexsha = '1' # PR is not suitable (already closed) @@ -108,9 +105,9 @@ def test_pr_list(self): mock_gitlab_mr.state = 'closed' self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_github.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_github.format('test'), + 'No merge-ready PRs!') self.mock_repo.get_clone.return_value = ( mock_repo_obj, mkdtemp('mock_repo/')) - testbot.assertCommand(cmd_gitlab.format('test'), - 'No merge-ready PRs!') + self.assertCommand(cmd_gitlab.format('test'), + 'No merge-ready PRs!') diff --git a/tests/helper.py b/tests/helper.py deleted file mode 100644 index b00db3b0..00000000 --- a/tests/helper.py +++ /dev/null @@ -1,21 +0,0 @@ -from errbot.backends.test import TestBot -from errbot.plugin_info import PluginInfo -from errbot.templating import add_plugin_templates_path -from pathlib import Path - - -def plugin_testbot(klass, loglevel, config=None): - config = config if config else dict() - testbot = TestBot(loglevel=loglevel, extra_config=config) - testbot.start() - plugin_name = klass.__name__ - plug = klass(testbot.bot, plugin_name) - return plug, testbot - - -def load_templates(plug_file_name): - plugin_info_path = (Path(__file__) - .parent / '..' / 'plugins' / plug_file_name) - with plugin_info_path.open() as plugfile: - plugin_info = PluginInfo.load_file(plugfile, plugin_info_path) - add_plugin_templates_path(plugin_info) diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 446f5ffd..568f60c1 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -1,7 +1,5 @@ -import logging import queue import textwrap -import unittest from unittest.mock import Mock, MagicMock, create_autospec, PropertyMock, patch import github3 @@ -16,13 +14,13 @@ import plugins.labhub from plugins.labhub import LabHub -from tests.helper import load_templates, plugin_testbot +from tests.corobo_test_case import CoroboTestCase -class TestLabHub(unittest.TestCase): +class TestLabHub(CoroboTestCase): def setUp(self): - load_templates('labhub.plug') + super().setUp((plugins.labhub.LabHub,)) plugins.labhub.github3 = create_autospec(github3) self.mock_org = create_autospec(github3.orgs.Organization) @@ -42,6 +40,22 @@ def setUp(self): self.mock_org.teams.return_value = [self.mock_team] plugins.labhub.github3.organization.return_value = self.mock_org + # patching + plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub) + plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab) + plugins.labhub.GitHubToken = create_autospec(IGitt.GitHub.GitHubToken) + plugins.labhub.GitLabPrivateToken = create_autospec( + IGitt.GitLab.GitLabPrivateToken) + + self.global_mocks = { + 'REPOS': { + 'repository': self.mock_repo, + 'repository.github.io': self.mock_repo, + }, + '_teams': self.teams, + } + self.labhub = self.load_plugin('LabHub', self.global_mocks) + def test_invite_cmd(self): mock_team_newcomers = create_autospec(github3.orgs.Team) mock_team_developers = create_autospec(github3.orgs.Team) @@ -51,69 +65,71 @@ def test_invite_cmd(self): self.teams['coala developers'] = mock_team_developers self.teams['coala maintainers'] = mock_team_maintainers - labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR) - labhub.activate() - labhub._teams = self.teams + mock_dict = { + 'TEAMS': self.teams, + 'is_room_member': MagicMock(), + } + self.inject_mocks('LabHub', mock_dict) plugins.labhub.os.environ['GH_TOKEN'] = 'patched?' - self.assertEqual(labhub.TEAMS, self.teams) + self.assertEqual(self.labhub.TEAMS, self.teams) - labhub.is_room_member = MagicMock(return_value=False) - testbot.assertCommand('!invite meet to newcomers', - '@meet is not a member of this room.') + mock_dict['is_room_member'].return_value = False + self.assertCommand('!invite meet to newcomers', + '@meet is not a member of this room.') - labhub.is_room_member = MagicMock(return_value=True) + mock_dict['is_room_member'].return_value = True # invite by maintainer mock_team_newcomers.is_member.return_value = True mock_team_developers.is_member.return_value = True mock_team_maintainers.is_member.return_value = True - testbot.assertCommand( + self.assertCommand( '!invite meet to newcomers', 'To get started, please follow our [newcomers guide]') - testbot.assertCommand('!invite meet to developers', - '@meet, you are a part of developers') - testbot.assertCommand('!invite meet to maintainers', - '@meet you seem to be awesome!') + self.assertCommand('!invite meet to developers', + '@meet, you are a part of developers') + self.assertCommand('!invite meet to maintainers', + '@meet you seem to be awesome!') # invite by developer mock_team_maintainers.is_member.return_value = False - labhub.is_room_member = MagicMock(return_value=True) + mock_dict['is_room_member'].return_value = True - testbot.assertCommand( + self.assertCommand( '!invite meet to newcomers', 'To get started, please follow our [newcomers guide]') - testbot.assertCommand('!invite meet to developers', - ':poop:') - testbot.assertCommand('!invite meet to maintainers', - ':poop:') + self.assertCommand('!invite meet to developers', + ':poop:') + self.assertCommand('!invite meet to maintainers', + ':poop:') # invite by newcomer mock_team_developers.is_member.return_value = False - testbot.assertCommand('!invite meet to newcomers', - ':poop') - testbot.assertCommand('!invite meet to developers', - ':poop:') - testbot.assertCommand('!invite meet to maintainers', - ':poop:') + self.assertCommand('!invite meet to newcomers', + ':poop') + self.assertCommand('!invite meet to developers', + ':poop:') + self.assertCommand('!invite meet to maintainers', + ':poop:') # invalid team - testbot.assertCommand('!invite meet to something', - 'select from one of the valid') + self.assertCommand('!invite meet to something', + 'select from one of the valid') # invalid command - testbot.assertCommand('!invite meetto newcomers', - 'Command "invite" / "invite meetto" not found.') + self.assertCommand('!invite meetto newcomers', + 'Command "invite" / "invite meetto" not found.') # not a member of org mock_team_newcomers.is_member.return_value = False mock_team_developers.is_member.return_value = False mock_team_maintainers.is_member.return_value = False - testbot.assertCommand( + self.assertCommand( '!invite meet to newcomers', 'You need to be a member of this organization to use this command') @@ -124,35 +140,14 @@ def test_is_room_member(self): self.assertTrue(LabHub.is_room_member('batman', msg)) def test_hello_world_callback(self): - testbot = TestBot(extra_plugin_dir='plugins', loglevel=logging.ERROR) - testbot.start() - labhub = testbot.bot.plugin_manager.get_plugin_obj_by_name('LabHub') - labhub.TEAMS = self.teams self.mock_team.is_member.return_value = False - testbot.assertCommand('hello, world', 'newcomer') + self.assertCommand('hello, world', 'newcomer') # Since the user won't be invited again, it'll timeout waiting for a # response. with self.assertRaises(queue.Empty): - testbot.assertCommand('helloworld', 'newcomer') + self.assertCommand('helloworld', 'newcomer') def test_create_issue_cmd(self): - plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub) - plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab) - plugins.labhub.GitHubToken = create_autospec(IGitt.GitHub.GitHubToken) - plugins.labhub.GitLabPrivateToken = create_autospec( - IGitt.GitLab.GitLabPrivateToken) - - labhub, testbot_private = plugin_testbot( - plugins.labhub.LabHub, logging.ERROR, - {'BACKEND': 'text', - 'ACCESS_CONTROLS': {'create_issue_cmd': {'allowprivate': False}}, - } - ) - labhub.activate() - labhub.REPOS = {'repository': self.mock_repo} - plugins.labhub.GitHubToken.assert_called_with(None) - plugins.labhub.GitLabPrivateToken.assert_called_with(None) - # Start ignoring PycodestyleBear, LineLengthBear # TODO # Ignoring assertion to prevent build failure for time being @@ -162,16 +157,12 @@ def test_create_issue_cmd(self): # Stop ignoring # Creating issue in public chat - labhub, testbot_public = plugin_testbot( - plugins.labhub.LabHub, logging.ERROR, {'BACKEND': 'text'} - ) - labhub.activate() - labhub._teams = self.teams self.mock_team.is_member.return_value = True - labhub.REPOS = {'repository': self.mock_repo, - 'repository.github.io': self.mock_repo} - testbot_public.assertCommand( + plugins.labhub.GitHubToken.assert_called_with(None) + plugins.labhub.GitLabPrivateToken.assert_called_with(None) + + self.assertCommand( textwrap.dedent('''\ !new issue repository this is the title first line of body @@ -179,39 +170,37 @@ def test_create_issue_cmd(self): '''), 'Here you go') - labhub.REPOS['repository'].create_issue.assert_called_once_with( - 'this is the title', - textwrap.dedent('''\ - first line of body - second line of body - Opened by @None at [text]()''') + self.global_mocks['REPOS']['repository'].create_issue \ + .assert_called_once_with( + 'this is the title', + textwrap.dedent('''\ + first line of body + second line of body + Opened by @None at [text]()''') ) - testbot_public.assertCommand( + self.assertCommand( textwrap.dedent('''\ !new issue repository.github.io another title body '''), 'Here you go') - labhub.REPOS['repository.github.io'].create_issue.assert_called_with( - 'another title', - textwrap.dedent('''\ - body - Opened by @None at [text]()''') + self.global_mocks['REPOS']['repository.github.io'].create_issue \ + .assert_called_with( + 'another title', + textwrap.dedent('''\ + body + Opened by @None at [text]()''') ) - testbot_public.assertCommand( + self.assertCommand( '!new issue coala title', 'repository that does not exist') # not a member of org self.mock_team.is_member.return_value = False - labhub.REPOS = { - 'repository': self.mock_repo, - 'repository.github.io': self.mock_repo, - } - testbot_public.assertCommand( + self.assertCommand( textwrap.dedent('''\ !new issue repository this is the title body @@ -228,14 +217,7 @@ def test_is_newcomer_issue(self): self.assertFalse(LabHub.is_newcomer_issue(mock_iss)) def test_unassign_cmd(self): - plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub) - plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab) - labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR) - - labhub.activate() - labhub.REPOS = {'example': self.mock_repo} - labhub._teams = self.teams - + self.inject_mocks('LabHub', {'REPOS': {'example': self.mock_repo}}) mock_iss = create_autospec(IGitt.GitHub.GitHubIssue) self.mock_repo.get_issue.return_value = mock_iss mock_iss.assignees = PropertyMock() @@ -243,7 +225,7 @@ def test_unassign_cmd(self): mock_iss.unassign = MagicMock() self.mock_team.is_member.return_value = True - testbot.assertCommand( + self.assertCommand( '!unassign https://github.com/coala/example/issues/999', 'you are unassigned now', timeout=10000) @@ -251,36 +233,29 @@ def test_unassign_cmd(self): mock_iss.unassign.assert_called_once_with(None) mock_iss.assignees = ('meetmangukiya', ) - testbot.assertCommand( + self.assertCommand( '!unassign https://github.com/coala/example/issues/999', 'not an assignee on the issue') - testbot.assertCommand( + self.assertCommand( '!unassign https://github.com/coala/example2/issues/999', 'Repository doesn\'t exist.') - testbot.assertCommand( + self.assertCommand( '!unassign https://gitlab.com/example/test/issues/999', 'Repository not owned by our org.') # not a member of org self.mock_team.is_member.return_value = False - testbot.assertCommand( + self.assertCommand( '!unassign https://github.com/coala/test/issues/23', 'You need to be a member of this organization ' 'to use this command.') def test_assign_cmd(self): - plugins.labhub.GitHub = create_autospec(IGitt.GitHub.GitHub.GitHub) - plugins.labhub.GitLab = create_autospec(IGitt.GitLab.GitLab.GitLab) - labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR) - labhub.activate() - mock_issue = create_autospec(GitHubIssue) self.mock_repo.get_issue.return_value = mock_issue - labhub.REPOS = {'a': self.mock_repo} - mock_dev_team = create_autospec(github3.orgs.Team) mock_maint_team = create_autospec(github3.orgs.Team) mock_dev_team.is_member.return_value = False @@ -288,16 +263,21 @@ def test_assign_cmd(self): self.teams['coala developers'] = mock_dev_team self.teams['coala maintainers'] = mock_maint_team - labhub._teams = self.teams + + mock_dict = { + 'REPOS': {'a': self.mock_repo}, + 'TEAMS': self.teams, + } + self.inject_mocks('LabHub', mock_dict) cmd = '!assign https://github.com/{}/{}/issues/{}' # no assignee, not newcomer mock_issue.assignees = tuple() self.mock_team.is_member.return_value = False - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'You need to be a member of this organization ' - 'to use this command.') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'You need to be a member of this organization ' + 'to use this command.') # no assignee, newcomer, initiatives/gci self.mock_team.is_member.return_value = True @@ -305,20 +285,20 @@ def test_assign_cmd(self): mock_dev_team.is_member.return_value = False mock_issue.labels = 'initiatives/gci', mock_issue.assignees = tuple() - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'You are not eligible to be assigned' - ' to this issue') - testbot.pop_message() + self.assertCommand(cmd.format('coala', 'a', '23'), + 'You are not eligible to be assigned' + ' to this issue') + self.pop_message() # no assignee, developer, initiatives/gci mock_maint_team.is_member.return_value = False mock_dev_team.is_member.return_value = True mock_issue.labels = 'initiatives/gci', mock_issue.assignees = tuple() - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'You are not eligible to be assigned' - ' to this issue') - testbot.pop_message() + self.assertCommand(cmd.format('coala', 'a', '23'), + 'You are not eligible to be assigned' + ' to this issue') + self.pop_message() mock_dev_team.is_member.return_value = False # no assignee, newcomer, difficulty/low @@ -327,59 +307,63 @@ def test_assign_cmd(self): mock_issue.assignees = tuple() self.mock_team.is_member.return_value = True - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'You\'ve been assigned to the issue') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'You\'ve been assigned to the issue') # no assignee, newcomer, no labels self.mock_team.is_member.return_value = True mock_issue.labels = tuple() mock_issue.assignees = tuple() - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'not eligible to be assigned to this issue') - testbot.pop_message() + self.assertCommand(cmd.format('coala', 'a', '23'), + 'not eligible to be assigned to this issue') + self.pop_message() # no assignee, newcomer, difficulty medium mock_issue.labels = ('difficulty/medium', ) - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'not eligible to be assigned to this issue') - testbot.pop_message() + self.assertCommand(cmd.format('coala', 'a', '23'), + 'not eligible to be assigned to this issue') + self.pop_message() # no assignee, newcomer, difficulty medium - labhub.GH_ORG_NAME = 'not-coala' - labhub.TEAMS = { - 'not-coala newcomers': self.mock_team, - 'not-coala developers': mock_dev_team, - 'not-coala maintainers': mock_maint_team + mock_dict = { + 'GH_ORG_NAME': 'not-coala', + 'TEAMS': { + 'not-coala newcomers': self.mock_team, + 'not-coala developers': mock_dev_team, + 'not-coala maintainers': mock_maint_team, + } } + self.inject_mocks('LabHub', mock_dict) - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'assigned') - labhub.GH_ORG_NAME = 'coala' - labhub.TEAMS = self.teams + self.assertCommand(cmd.format('coala', 'a', '23'), + 'assigned') + mock_dict['GH_ORG_NAME'] = 'coala' + mock_dict['TEAMS'] = self.teams + self.inject_mocks('LabHub', mock_dict) # newcomer, developer, difficulty/medium mock_dev_team.is_member.return_value = True mock_maint_team.is_member.return_value = False - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'assigned') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'assigned') # has assignee mock_issue.assignees = ('somebody', ) - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'already assigned to someone') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'already assigned to someone') # has assignee same as user mock_issue.assignees = (None, ) - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'already assigned to you') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'already assigned to you') # non-existent repository - testbot.assertCommand(cmd.format('coala', 'c', '23'), - 'Repository doesn\'t exist.') + self.assertCommand(cmd.format('coala', 'c', '23'), + 'Repository doesn\'t exist.') # unknown org - testbot.assertCommand(cmd.format('coa', 'a', '23'), - 'Repository not owned by our org.') + self.assertCommand(cmd.format('coa', 'a', '23'), + 'Repository not owned by our org.') # no assignee, newcomer, difficulty/newcomer, second newcomer issue mock_issue.assignees = tuple() @@ -389,15 +373,12 @@ def test_assign_cmd(self): with patch('plugins.labhub.GitHub') as mock_gh: mock_gh.raw_search = Mock() mock_gh.raw_search.return_value = ['mocked?'] - testbot.assertCommand(cmd.format('coala', 'a', '23'), - 'not eligible to be assigned to this issue') + self.assertCommand(cmd.format('coala', 'a', '23'), + 'not eligible to be assigned to this issue') def test_mark_cmd(self): - labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR) - labhub.activate() + self.inject_mocks('LabHub', {'REPOS': {'test': self.mock_repo}}) - labhub.REPOS = {'test': self.mock_repo} - labhub._teams = self.teams mock_github_mr = create_autospec(GitHubMergeRequest) mock_gitlab_mr = create_autospec(GitLabMergeRequest) mock_github_mr.labels = PropertyMock() @@ -411,10 +392,10 @@ def test_mark_cmd(self): self.mock_team.is_member.return_value = True # Non-eistent repo - testbot.assertCommand(cmd_github.format('wip', 'a', 'b', '23'), - 'Repository doesn\'t exist.') - testbot.assertCommand( - '!mark wip https://gitlab.com/a/b/merge_requests/2', + self.assertCommand(cmd_github.format('wip', 'example', 'b', '23'), + 'Repository doesn\'t exist.') + self.assertCommand( + '!mark wip https://gitlab.com/example/b/merge_requests/2', 'Repository doesn\'t exist.') mock_github_mr.web_url = 'https://github.com/coala/test/pull/23' @@ -424,18 +405,18 @@ def test_mark_cmd(self): # mark wip mock_github_mr.labels = ['process/pending review'] mock_gitlab_mr.labels = ['process/pending review'] - testbot.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), - 'marked work in progress') - testbot.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), - '@johndoe, please check your pull request') - testbot.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), - 'https://github.com/coala/test/pull/23') + self.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), + 'marked work in progress') + self.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), + '@johndoe, please check your pull request') + self.assertCommand(cmd_github.format('wip', 'coala', 'test', '23'), + 'https://github.com/coala/test/pull/23') self.mock_repo.get_mr.return_value = mock_gitlab_mr - testbot.assertCommand(cmd_gitlab.format('wip', 'coala', 'test', '23'), - '@johndoe, please check your pull request') - testbot.assertCommand( + self.assertCommand(cmd_gitlab.format('wip', 'coala', 'test', '23'), + '@johndoe, please check your pull request') + self.assertCommand( cmd_gitlab.format('wip', 'coala', 'test', '23'), 'https://gitlab.com/coala/test/merge_requests/23') @@ -444,26 +425,25 @@ def test_mark_cmd(self): # mark pending mock_github_mr.labels = ['process/wip'] mock_gitlab_mr.labels = ['process/wip'] - testbot.assertCommand( + self.assertCommand( cmd_github.format('pending', 'coala', 'test', '23'), 'marked pending review') - testbot.assertCommand( + self.assertCommand( cmd_github.format('pending-review', 'coala', 'test', '23'), 'marked pending review') - testbot.assertCommand( + self.assertCommand( cmd_github.format('pending review', 'coala', 'test', '23'), 'marked pending review') # not a member of org self.mock_team.is_member.return_value = False - testbot.assertCommand( + self.assertCommand( cmd_github.format('pending review', 'coala', 'a', '23'), 'You need to be a member of this organization to use this command') def test_alive(self): - labhub, testbot = plugin_testbot(plugins.labhub.LabHub, logging.ERROR) with patch('plugins.labhub.time.sleep') as mock_sleep: - labhub.gh_repos = { + gh_repos_mock = { 'coala': create_autospec(IGitt.GitHub.GitHub.GitHubRepository), 'coala-bears': @@ -472,40 +452,43 @@ def test_alive(self): create_autospec(IGitt.GitHub.GitHub.GitHubRepository), } # for the branch where program sleeps - labhub.gh_repos.update({str(i): - create_autospec( + gh_repos_mock.update({str(i): + create_autospec( IGitt.GitHub.GitHub.GitHubRepository) - for i in range(30)}) - labhub.gl_repos = { + for i in range(30)}) + gl_repos_mock = { 'test': create_autospec(IGitt.GitLab.GitLab.GitLabRepository), } - labhub.activate() - labhub._teams = self.teams self.mock_team.is_member.return_value = True + mock_dict = { + 'gh_repos': gh_repos_mock, + 'gl_repos': gl_repos_mock, + } + self.inject_mocks('LabHub', mock_dict) - labhub.gh_repos['coala'].search_mrs.return_value = [1, 2] - labhub.gh_repos['coala-bears'].search_mrs.return_value = [] - labhub.gh_repos['coala-utils'].search_mrs.return_value = [] - testbot.assertCommand('!pr stats 10hours', - '2 PRs opened in last 10 hours\n' - 'The community is alive', timeout=100) + mock_dict['gh_repos']['coala'].search_mrs.return_value = [1, 2] + mock_dict['gh_repos']['coala-bears'].search_mrs.return_value = [] + mock_dict['gh_repos']['coala-utils'].search_mrs.return_value = [] + self.assertCommand('!pr stats 10hours', + '2 PRs opened in last 10 hours\n' + 'The community is alive', timeout=100) - labhub.gh_repos['coala'].search_mrs.return_value = [] - testbot.assertCommand('!pr stats 5hours', - '0 PRs opened in last 5 hours\n' - 'The community is dead', timeout=100) + mock_dict['gh_repos']['coala'].search_mrs.return_value = [] + self.assertCommand('!pr stats 5hours', + '0 PRs opened in last 5 hours\n' + 'The community is dead', timeout=100) - labhub.gh_repos['coala'].search_mrs.return_value = [ + mock_dict['gh_repos']['coala'].search_mrs.return_value = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] - testbot.assertCommand('!pr stats 3hours', - '10 PRs opened in last 3 hours\n' - 'The community is on fire', timeout=100) + self.assertCommand('!pr stats 3hours', + '10 PRs opened in last 3 hours\n' + 'The community is on fire', timeout=100) # not a member of org self.mock_team.is_member.return_value = False - testbot.assertCommand( + self.assertCommand( '!pr stats 3hours', 'You need to be a member of this organization ' 'to use this command.', timeout=100)