# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

import pytest

from hypothesistooling.release import bump_version_info, parse_release_file_contents


def parse_release(contents):
    return parse_release_file_contents(contents, "<string>")


def test_bump_minor_version():
    assert bump_version_info((1, 1, 1), "minor")[0] == "1.2.0"


def test_parse_release_file():
    assert parse_release("RELEASE_TYPE: patch\nhi") == ("patch", "hi")
    assert parse_release("RELEASE_TYPE: minor\n\n\n\nhi") == ("minor", "hi")
    assert parse_release("RELEASE_TYPE: major\n \n\nhi") == ("major", "hi")


def test_invalid_release():
    with pytest.raises(ValueError):
        parse_release("RELEASE_TYPE: wrong\nstuff")

    with pytest.raises(ValueError):
        parse_release("")


def test_changelog_parsing_strips_trailing_whitespace():
    header = "RELEASE_TYPE: patch\n\n"
    contents = "Adds a feature\n    indented.\n"
    _level, out = parse_release(header + contents.replace("feature", "feature    "))
    assert contents.strip() == out
