Skip to content

Improve pysemver subcommands #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions docs/pysemver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,39 @@ to indicates which is the bigger version:
* ``0`` if both versions are the same,
* ``1`` if the first version is greater than the second version.

The *error code* returned by the script indicates if both versions
are valid (return code 0) or not (return code != 0)::

Return Code
-----------

The *return code* of the script (accessible by ``$?`` from the Bash)
indicates if the subcommand returned successfully nor not. It is *not*
meant as the result of the subcommand.

The result of the subcommand is printed on the standard out channel
("stdout" or ``0``), any error messages to standard error ("stderr" or
``2``).

For example, to compare two versions, the command expects two valid
semver versions::

$ pysemver compare 1.2.3 2.4.0
-1
$ pysemver compare 1.2.3 2.4.0 ; echo $?
$ echo $?
0
$ pysemver compare 1.2.3 2.4.0 ; echo $?
ERROR 1.2.x is not valid SemVer string

The return code is zero, but the result is ``-1``.

However, if you pass invalid versions, you get this situation::

$ pysemver compare 1.2.3 2.4
ERROR 2.4 is not valid SemVer string
$ echo $?
2

If you use the :command:`pysemver` in your own scripts, check the
return code first before you process the standard output.


See also
--------

Expand Down
90 changes: 61 additions & 29 deletions semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,61 @@ def finalize_version(version):
return format_version(verinfo["major"], verinfo["minor"], verinfo["patch"])


def cmd_bump(args):
"""
Subcommand: Bumps a version.

Synopsis: bump <PART> <VERSION>
<PART> can be major, minor, patch, prerelease, or build

:param args: The parsed arguments
:type args: :class:`argparse.Namespace`
:return: the new, bumped version
"""
maptable = {
"major": "bump_major",
"minor": "bump_minor",
"patch": "bump_patch",
"prerelease": "bump_prerelease",
"build": "bump_build",
}
if args.bump is None:
# When bump is called without arguments,
# print the help and exit
args.parser.parse_args(["bump", "-h"])

ver = parse_version_info(args.version)
# get the respective method and call it
func = getattr(ver, maptable[args.bump])
return str(func())


def cmd_check(args):
"""
Subcommand: Checks if a string is a valid semver version.

Synopsis: check <VERSION>

:param args: The parsed arguments
:type args: :class:`argparse.Namespace`
"""
if VersionInfo.isvalid(args.version):
return None
raise ValueError("Invalid version %r" % args.version)


def cmd_compare(args):
"""
Subcommand: Compare two versions

Synopsis: compare <VERSION1> <VERSION2>

:param args: The parsed arguments
:type args: :class:`argparse.Namespace`
"""
return str(compare(args.version1, args.version2))


def createparser():
"""Create an :class:`argparse.ArgumentParser` instance

Expand All @@ -678,13 +733,13 @@ def createparser():
s = parser.add_subparsers()
# create compare subcommand
parser_compare = s.add_parser("compare", help="Compare two versions")
parser_compare.set_defaults(which="compare")
parser_compare.set_defaults(func=cmd_compare)
parser_compare.add_argument("version1", help="First version")
parser_compare.add_argument("version2", help="Second version")

# create bump subcommand
parser_bump = s.add_parser("bump", help="Bumps a version")
parser_bump.set_defaults(which="bump")
parser_bump.set_defaults(func=cmd_bump)
sb = parser_bump.add_subparsers(title="Bump commands", dest="bump")

# Create subparsers for the bump subparser:
Expand All @@ -701,7 +756,7 @@ def createparser():
parser_check = s.add_parser(
"check", help="Checks if a string is a valid semver version"
)
parser_check.set_defaults(which="check")
parser_check.set_defaults(func=cmd_check)
parser_check.add_argument("version", help="Version to check")

return parser
Expand All @@ -717,35 +772,12 @@ def process(args):
:return: result of the selected action
:rtype: str
"""
if not hasattr(args, "which"):
if not hasattr(args, "func"):
args.parser.print_help()
raise SystemExit()

elif args.which == "bump":
maptable = {
"major": "bump_major",
"minor": "bump_minor",
"patch": "bump_patch",
"prerelease": "bump_prerelease",
"build": "bump_build",
}
if args.bump is None:
# When bump is called without arguments,
# print the help and exit
args.parser.parse_args([args.which, "-h"])

ver = parse_version_info(args.version)
# get the respective method and call it
func = getattr(ver, maptable[args.bump])
return str(func())

elif args.which == "compare":
return str(compare(args.version1, args.version2))

elif args.which == "check":
if VersionInfo.isvalid(args.version):
return None
raise ValueError("Invalid version %r" % args.version)
# Call the respective function object:
return args.func(args)


def main(cliargs=None):
Expand Down
74 changes: 30 additions & 44 deletions test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
bump_minor,
bump_patch,
bump_prerelease,
cmd_bump,
cmd_check,
cmd_compare,
compare,
createparser,
finalize_version,
Expand Down Expand Up @@ -684,87 +687,70 @@ def test_should_be_able_to_use_integers_as_prerelease_build():
@pytest.mark.parametrize(
"cli,expected",
[
(
["bump", "major", "1.2.3"],
Namespace(which="bump", bump="major", version="1.2.3"),
),
(
["bump", "minor", "1.2.3"],
Namespace(which="bump", bump="minor", version="1.2.3"),
),
(
["bump", "patch", "1.2.3"],
Namespace(which="bump", bump="patch", version="1.2.3"),
),
(["bump", "major", "1.2.3"], Namespace(bump="major", version="1.2.3")),
(["bump", "minor", "1.2.3"], Namespace(bump="minor", version="1.2.3")),
(["bump", "patch", "1.2.3"], Namespace(bump="patch", version="1.2.3")),
(
["bump", "prerelease", "1.2.3"],
Namespace(which="bump", bump="prerelease", version="1.2.3"),
),
(
["bump", "build", "1.2.3"],
Namespace(which="bump", bump="build", version="1.2.3"),
Namespace(bump="prerelease", version="1.2.3"),
),
(["bump", "build", "1.2.3"], Namespace(bump="build", version="1.2.3")),
# ---
(
["compare", "1.2.3", "2.1.3"],
Namespace(which="compare", version1="1.2.3", version2="2.1.3"),
),
(["compare", "1.2.3", "2.1.3"], Namespace(version1="1.2.3", version2="2.1.3")),
# ---
(["check", "1.2.3"], Namespace(which="check", version="1.2.3")),
(["check", "1.2.3"], Namespace(version="1.2.3")),
],
)
def test_should_parse_cli_arguments(cli, expected):
parser = createparser()
assert parser
result = parser.parse_args(cli)
del result.func
assert result == expected


@pytest.mark.parametrize(
"args,expectation",
"func,args,expectation",
[
# bump subcommand
(cmd_bump, Namespace(bump="major", version="1.2.3"), does_not_raise("2.0.0")),
(cmd_bump, Namespace(bump="minor", version="1.2.3"), does_not_raise("1.3.0")),
(cmd_bump, Namespace(bump="patch", version="1.2.3"), does_not_raise("1.2.4")),
(
Namespace(which="bump", bump="major", version="1.2.3"),
does_not_raise("2.0.0"),
),
(
Namespace(which="bump", bump="minor", version="1.2.3"),
does_not_raise("1.3.0"),
),
(
Namespace(which="bump", bump="patch", version="1.2.3"),
does_not_raise("1.2.4"),
),
(
Namespace(which="bump", bump="prerelease", version="1.2.3-rc1"),
cmd_bump,
Namespace(bump="prerelease", version="1.2.3-rc1"),
does_not_raise("1.2.3-rc2"),
),
(
Namespace(which="bump", bump="build", version="1.2.3+build.13"),
cmd_bump,
Namespace(bump="build", version="1.2.3+build.13"),
does_not_raise("1.2.3+build.14"),
),
# compare subcommand
(
Namespace(which="compare", version1="1.2.3", version2="2.1.3"),
cmd_compare,
Namespace(version1="1.2.3", version2="2.1.3"),
does_not_raise("-1"),
),
(
Namespace(which="compare", version1="1.2.3", version2="1.2.3"),
cmd_compare,
Namespace(version1="1.2.3", version2="1.2.3"),
does_not_raise("0"),
),
(
Namespace(which="compare", version1="2.4.0", version2="2.1.3"),
cmd_compare,
Namespace(version1="2.4.0", version2="2.1.3"),
does_not_raise("1"),
),
# check subcommand
(Namespace(which="check", version="1.2.3"), does_not_raise(None)),
(Namespace(which="check", version="1.2"), pytest.raises(ValueError)),
(cmd_check, Namespace(version="1.2.3"), does_not_raise(None)),
(cmd_check, Namespace(version="1.2"), pytest.raises(ValueError)),
],
)
def test_should_process_parsed_cli_arguments(args, expectation):
def test_should_process_parsed_cli_arguments(func, args, expectation):
with expectation as expected:
assert process(args) == expected
result = func(args)
assert result == expected


def test_should_process_print(capsys):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy