| # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| """Tests for precq exonerating code.""" |
| |
| from __future__ import absolute_import |
| from __future__ import division |
| from __future__ import print_function |
| |
| import functools |
| import glob |
| import json |
| import os |
| |
| import pytest |
| |
| from exonerator import gerrit_precq |
| from exonerator import gerrit_cq_test |
| |
| |
| # DO NOT SUBMIT create some example test case files |
| REMOVED_BY_HUMAN = 'Trybot-Ready_removed_by_human_*.json' |
| REMOVED_BY_BOT = 'Trybot-Ready_removed_by_bot_*.json' |
| READY = 'precq_ready_for_exoneration_*.json' |
| _TEST_CASE_DIR = os.path.join(os.path.dirname(__file__), 'test_cases') |
| |
| _FakeChangeDetails = functools.partial( |
| gerrit_cq_test.FakeChangeDetails, gerrit_precq) |
| |
| def _TestCaseChangeDetails(test_case_pattern): |
| """Globs a test case pattern and reads the files as JSON.""" |
| for fname in glob.glob(os.path.join(_TEST_CASE_DIR, test_case_pattern)): |
| with open(fname) as fh: |
| yield json.load(fh) |
| |
| |
| # pylint: disable=protected-access |
| @pytest.mark.parametrize('change_details', _TestCaseChangeDetails( |
| READY)) |
| def TestReady(change_details): |
| """Test cases that are ready to exonerate.""" |
| # Pass 99 in as the "known_patch_number" - this will work as long as |
| # known_patch_number is greater-or-equal to the largest patch number in |
| # change_details. |
| assert gerrit_precq._ShouldBeMarkedPreCQReady(change_details, 99) |
| |
| |
| @pytest.mark.parametrize('change_details', _TestCaseChangeDetails( |
| REMOVED_BY_HUMAN)) |
| def TestRemovedByHuman(change_details): |
| """If the Trybot-Ready label was removed by a human, don't exonerate.""" |
| assert not gerrit_precq._PreCQApprovalWasRemovedByBot(change_details) |
| assert not gerrit_precq._ShouldBeMarkedPreCQReady(change_details, 99) |
| |
| |
| @pytest.mark.parametrize('change_details', _TestCaseChangeDetails( |
| REMOVED_BY_BOT)) |
| def TestRemovedByBot(change_details): |
| """If the Trybot-Ready label was removed by a bot, can exonerate.""" |
| assert gerrit_precq._PreCQApprovalWasRemovedByBot(change_details) |
| assert gerrit_precq._ShouldBeMarkedPreCQReady(change_details, 99) |