Skip to content

Commit

Permalink
tbump: also make an error if new version is older
Browse files Browse the repository at this point in the history
  • Loading branch information
dmerejkowsky committed Mar 12, 2024
1 parent 3016339 commit 7e969e6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
10 changes: 5 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ docopt = "^0.6.2"
cli-ui = ">=0.10.3"
schema = "^0.7.1"
tomlkit = "^0.11"
packaging = "^24.0"

[tool.poetry.dev-dependencies]
# Task runner
Expand Down
36 changes: 34 additions & 2 deletions tbump/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import sys
import textwrap
import urllib.parse
from contextlib import suppress
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Dict, List, Optional, Union, cast

import cli_ui as ui
import docopt
from packaging.version import InvalidVersion
from packaging.version import parse as parse_version

from tbump.config import get_config_file
from tbump.error import Error
Expand Down Expand Up @@ -216,6 +219,25 @@ def print_error(self) -> None:
ui.error("New version is the same as the previous one")


class OlderNewVersion(Error):
def __init__(self, *, new_version: str, current_version: str) -> None:
self.new_version = new_version
self.current_version = current_version
super().__init__()

def print_error(self) -> None:
ui.error(
ui.reset,
"New version",
ui.bold,
self.new_version,
ui.reset,
"is older than current version",
ui.bold,
self.current_version,
)


def bump(options: BumpOptions, operations: List[str]) -> None:
working_path = options.working_path
new_version = options.new_version
Expand All @@ -228,8 +250,7 @@ def bump(options: BumpOptions, operations: List[str]) -> None:
)
config = config_file.get_config()

if config.current_version == new_version:
raise NotANewVersion()
check_versions(current=config.current_version, new=new_version)

# fmt: off
ui.info_1(
Expand Down Expand Up @@ -289,6 +310,17 @@ def bump(options: BumpOptions, operations: List[str]) -> None:
suggest_creating_github_release(config.github_url, tag_name)


def check_versions(*, current: str, new: str) -> None:
if current == new:
raise NotANewVersion()

with suppress(InvalidVersion):
parsed_current = parse_version(current)
parsed_new = parse_version(new)
if parsed_new < parsed_current:
raise OlderNewVersion(current_version=current, new_version=new)


def suggest_creating_github_release(github_url: str, tag_name: str) -> None:
query_string = urllib.parse.urlencode({"tag": tag_name})
if not github_url.endswith("/"):
Expand Down
7 changes: 6 additions & 1 deletion tbump/test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import tomlkit

from tbump.cli import NotANewVersion
from tbump.cli import NotANewVersion, OlderNewVersion
from tbump.cli import run as run_tbump
from tbump.config import ConfigNotFound, InvalidConfig
from tbump.error import Error
Expand Down Expand Up @@ -327,6 +327,11 @@ def test_not_a_new_versions(test_repo: Path) -> None:
run_tbump(["-C", str(test_repo), "1.2.41-alpha-1"])


def test_new_version_is_older(test_repo: Path) -> None:
with pytest.raises(OlderNewVersion):
run_tbump(["-C", str(test_repo), "1.2.40"])


def test_no_tracked_branch_non_interactive(test_repo: Path) -> None:
run_git(test_repo, "checkout", "-b", "devel")

Expand Down

0 comments on commit 7e969e6

Please sign in to comment.