Skip to content

Commit

Permalink
Ensure patch or minor is present when required
Browse files Browse the repository at this point in the history
Minor is required to be present if patch is provided.
Patch is required if either pre_release or build are provided.
  • Loading branch information
CasperWA committed May 23, 2023
1 parent 6ef6d4d commit 0cb5694
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ci_cd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,24 @@ def _build_version(
if minor is not None:
version += f".{minor}"
if patch is not None:
if minor is None:
raise ValueError("Minor must be given if patch is given")
version += f".{patch}"
if pre_release is not None:
# semver spec #9: A pre-release version MAY be denoted by appending a
# hyphen and a series of dot separated identifiers immediately following
# the patch version.
# https://semver.org/#spec-item-9
if patch is None:
raise ValueError("Patch must be given if pre_release is given")
version += f"-{pre_release}"
if build is not None:
# semver spec #10: Build metadata MAY be denoted by appending a plus sign
# and a series of dot separated identifiers immediately following the patch
# or pre-release version.
# https://semver.org/#spec-item-10
if patch is None:
raise ValueError("Patch must be given if build is given")
version += f"+{build}"
return version

Expand Down
20 changes: 20 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ def test_semanticversion_invalid() -> None:
{"version": "1.0.0", "major": 1, "minor": 0, "patch": 0},
"version cannot be specified along with other parameters",
),
(
{"major": 1, "patch": 0},
"Minor must be given if patch is given",
),
(
{
"major": 1,
"minor": 0,
"pre_release": "alpha",
},
"Patch must be given if pre_release is given",
),
(
{
"major": 1,
"minor": 0,
"build": "001",
},
"Patch must be given if build is given",
),
("", "At least major must be given"),
({}, "At least major must be given"),
]
Expand Down

0 comments on commit 0cb5694

Please sign in to comment.