Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ools'

Merges #48
Closes #48
  • Loading branch information
sduenas committed Jun 20, 2022
2 parents 63d55a5 + cc5b091 commit e1dd4db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
27 changes: 24 additions & 3 deletions release_tools/semverup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
@click.command()
@click.option('--dry-run', is_flag=True,
help="Do not write a new version number. Print to the standard output instead.")
def semverup(dry_run):
@click.option('--bump-version',
type=click.Choice(['MAJOR', 'MINOR', 'PATCH'], case_sensitive=False),
help="Increase only the defined version.")
def semverup(dry_run, bump_version):
"""Increment version number following semver specification.
This script will bump up the version number of a package in a
Expand All @@ -66,11 +69,15 @@ def semverup(dry_run):
Additionally, 'pyproject' file will also be updated. Take into
account this file must be tracked by the repository.
WARNING: this script does not increases MAJOR version yet.
WARNING: this script does not increase MAJOR version yet
unless it is forced using --bump=major.
If you don't want to create a new version and see only the final
result, please active '--dry-run' flag.
If you want to update the version number regardless the release changes,
use '--bump-version=[MAJOR,MINOR,PATCH]'.
More info about semver specification can be found in the next
link: https://semver.org/.
"""
Expand All @@ -87,7 +94,10 @@ def semverup(dry_run):
pyproject_file = find_pyproject_file(project)

# Determine the new version and produce the output
new_version = determine_new_version_number(project, current_version)
if bump_version:
new_version = force_new_version_number(current_version, bump_version)
else:
new_version = determine_new_version_number(project, current_version)

if not dry_run:
write_version_number(version_file, new_version)
Expand Down Expand Up @@ -149,6 +159,17 @@ def read_version_number(filepath):
return version


def force_new_version_number(current_version, bump_version):
"""Increment version number based on bump_version choice"""

if bump_version == 'MAJOR':
return current_version.bump_major()
elif bump_version == 'MINOR':
return current_version.bump_minor()
elif bump_version == 'PATCH':
return current_version.bump_patch()


def determine_new_version_number(project, current_version):
"""Guess the next version number."""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Option `--bump-version` to increase version manually
category: added
author: Jose Javier Merchante <jjmerchante@bitergia.com>
issue: null
notes: >
Include `--bump-version=[MAJOR, MINOR, PATCH]` argument to
`sermverup` command to increase the version number regardless
the release notes changes.
37 changes: 37 additions & 0 deletions tests/test_semverup.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,43 @@ def test_pyproject_file_not_found(self, mock_project):
lines = result.stderr.split('\n')
self.assertEqual(lines[-2], PYPROJECT_FILE_NOT_FOUND)

@unittest.mock.patch('release_tools.semverup.Project')
def test_force_version_number_argument(self, mock_project):
"""Check if it increases the version number passed by argument"""

runner = click.testing.CliRunner()

with runner.isolated_filesystem() as fs:
version_file = os.path.join(fs, '_version.py')
mock_project.return_value.version_file = version_file

project_file = os.path.join(fs, 'pyproject.toml')
mock_project.return_value.pyproject_file = project_file

self.setup_files(version_file, project_file, "0.8.10")

# Run the script command for major
result = runner.invoke(semverup.semverup, ['--bump-version', 'major'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "1.0.0\n")

# Version changed in files
version = self.read_version_number(version_file)
self.assertEqual(version, "1.0.0")

version = self.read_version_number_from_pyproject(project_file)
self.assertEqual(version, "1.0.0")

# Run the script command for minor
result = runner.invoke(semverup.semverup, ['--bump-version', 'minor'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "1.1.0\n")

# Run the script command for patch
result = runner.invoke(semverup.semverup, ['--bump-version', 'patch'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.stdout, "1.1.1\n")


if __name__ == '__main__':
unittest.main()

0 comments on commit e1dd4db

Please sign in to comment.