|
| 1 | +# Copyright 2022 Simone Rubino - TAKOBI |
| 2 | +# Distributed under the MIT License (http://opensource.org/licenses/MIT). |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from oca_github_bot.tasks.migration_pr_check import is_migration_pr |
| 7 | + |
| 8 | +MIGRATION_PR_PATH = "oca_github_bot.tasks.migration_pr_check" |
| 9 | + |
| 10 | + |
| 11 | +def _get_addons_gen_mock(pr_new_modules=None): |
| 12 | + """ |
| 13 | + Return a callable that returns a list of modules. |
| 14 | + The list contains `pr_new_modules` only after first call. |
| 15 | + """ |
| 16 | + if pr_new_modules is None: |
| 17 | + pr_new_modules = list() |
| 18 | + |
| 19 | + class AddonsGenMock: |
| 20 | + def __init__(self): |
| 21 | + self.addons_gen_calls = 0 |
| 22 | + |
| 23 | + def __call__(self, *args, **kwargs): |
| 24 | + # First time, only the existing addons are returned |
| 25 | + existing_addons = ["existing_addon"] |
| 26 | + if self.addons_gen_calls > 0: |
| 27 | + # After that, return also `pr_new_modules` |
| 28 | + if pr_new_modules: |
| 29 | + existing_addons.extend(pr_new_modules) |
| 30 | + self.addons_gen_calls += 1 |
| 31 | + return existing_addons |
| 32 | + |
| 33 | + return AddonsGenMock() |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.vcr() |
| 37 | +def test_no_new_module(mocker): |
| 38 | + """ |
| 39 | + If a PR does not add a new module, then it is not a migration. |
| 40 | + """ |
| 41 | + mocker.patch("%s.github" % MIGRATION_PR_PATH) |
| 42 | + mocker.patch("%s.check_call" % MIGRATION_PR_PATH) |
| 43 | + |
| 44 | + migration_issue = mocker.patch("%s._find_issue" % MIGRATION_PR_PATH) |
| 45 | + migration_issue.return_value.body = "Migration Issue Body" |
| 46 | + |
| 47 | + addons_gen = mocker.patch("%s.addon_dirs_in" % MIGRATION_PR_PATH) |
| 48 | + addons_gen.side_effect = _get_addons_gen_mock() |
| 49 | + |
| 50 | + is_migration = is_migration_pr("org", "repo", "pr") |
| 51 | + assert not is_migration |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.vcr() |
| 55 | +def test_new_module_no_migration(mocker): |
| 56 | + """ |
| 57 | + If a PR adds a new module but the module is not in the migration issue, |
| 58 | + then it is not a migration. |
| 59 | + """ |
| 60 | + mocker.patch("%s.github" % MIGRATION_PR_PATH) |
| 61 | + mocker.patch("%s.check_call" % MIGRATION_PR_PATH) |
| 62 | + |
| 63 | + migration_issue_body = """ |
| 64 | +Modules to migrate: |
| 65 | +- [ ] a_module |
| 66 | + """ |
| 67 | + migration_issue = mocker.patch("%s._find_issue" % MIGRATION_PR_PATH) |
| 68 | + migration_issue.return_value.body = migration_issue_body |
| 69 | + |
| 70 | + addons_gen = mocker.patch("%s.addon_dirs_in" % MIGRATION_PR_PATH) |
| 71 | + addons_gen.side_effect = _get_addons_gen_mock() |
| 72 | + |
| 73 | + is_migration = is_migration_pr("org", "repo", "pr") |
| 74 | + assert not is_migration |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.vcr() |
| 78 | +def test_new_module_migration(mocker): |
| 79 | + """ |
| 80 | + If a PR adds a new module and the module is in the migration issue, |
| 81 | + then it is a migration. |
| 82 | + """ |
| 83 | + mocker.patch("%s.github" % MIGRATION_PR_PATH) |
| 84 | + mocker.patch("%s.check_call" % MIGRATION_PR_PATH) |
| 85 | + |
| 86 | + addon_name = "migrated_module" |
| 87 | + migration_issue_body = f""" |
| 88 | +Modules to migrate: |
| 89 | +- [ ] {addon_name} |
| 90 | + """ |
| 91 | + migration_issue = mocker.patch("%s._find_issue" % MIGRATION_PR_PATH) |
| 92 | + migration_issue.return_value.body = migration_issue_body |
| 93 | + |
| 94 | + addons_gen = mocker.patch("%s.addon_dirs_in" % MIGRATION_PR_PATH) |
| 95 | + addons_gen.side_effect = _get_addons_gen_mock([addon_name]) |
| 96 | + |
| 97 | + is_migration = is_migration_pr("org", "repo", "pr") |
| 98 | + assert is_migration |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.vcr() |
| 102 | +def test_migration_comment(mocker): |
| 103 | + """ |
| 104 | + If a PR adds a new module and it is in the migration issue, |
| 105 | + then it is a migration. |
| 106 | + """ |
| 107 | + github_mock = mocker.patch("%s.github" % MIGRATION_PR_PATH) |
| 108 | + mocker.patch("%s.check_call" % MIGRATION_PR_PATH) |
| 109 | + |
| 110 | + addon_name = "migrated_module" |
| 111 | + migration_issue_body = f""" |
| 112 | +Modules to migrate: |
| 113 | +- [ ] {addon_name} |
| 114 | + """ |
| 115 | + migration_issue = mocker.patch("%s._find_issue" % MIGRATION_PR_PATH) |
| 116 | + migration_issue.return_value.body = migration_issue_body |
| 117 | + |
| 118 | + addons_gen = mocker.patch("%s.addon_dirs_in" % MIGRATION_PR_PATH) |
| 119 | + addons_gen.side_effect = _get_addons_gen_mock([addon_name]) |
| 120 | + |
| 121 | + gh_context = mocker.MagicMock() |
| 122 | + github_mock_login_cm = github_mock.login.return_value |
| 123 | + github_mock_login_cm.__enter__.return_value = gh_context |
| 124 | + |
| 125 | + is_migration = is_migration_pr("org", "repo", "pr") |
| 126 | + assert is_migration |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.vcr() |
| 130 | +def test_pr_title(mocker): |
| 131 | + """ |
| 132 | + If a PR has [MIG] in its Title, |
| 133 | + then it is a migration. |
| 134 | + """ |
| 135 | + github_mock = mocker.patch("%s.github" % MIGRATION_PR_PATH) |
| 136 | + mocker.patch("%s.check_call" % MIGRATION_PR_PATH) |
| 137 | + |
| 138 | + migration_issue_body = """ |
| 139 | + Modules to migrate |
| 140 | + """ |
| 141 | + migration_issue = mocker.patch("%s._find_issue" % MIGRATION_PR_PATH) |
| 142 | + migration_issue.return_value.body = migration_issue_body |
| 143 | + |
| 144 | + addons_gen = mocker.patch("%s.addon_dirs_in" % MIGRATION_PR_PATH) |
| 145 | + addons_gen.side_effect = _get_addons_gen_mock(["another_module"]) |
| 146 | + |
| 147 | + pr_title = "[MIG] migrated_module" |
| 148 | + gh_pr = mocker.MagicMock() |
| 149 | + gh_pr.title = pr_title |
| 150 | + gh_repo = mocker.MagicMock() |
| 151 | + gh_repo.pull_request.return_value = gh_pr |
| 152 | + gh_context = mocker.MagicMock() |
| 153 | + gh_context.repository.return_value = gh_repo |
| 154 | + github_mock_login_cm = github_mock.login.return_value |
| 155 | + github_mock_login_cm.__enter__.return_value = gh_context |
| 156 | + |
| 157 | + is_migration = is_migration_pr("org", "repo", "pr") |
| 158 | + assert is_migration |
0 commit comments