Skip to content

Commit

Permalink
python binding for bump_segment
Browse files Browse the repository at this point in the history
  • Loading branch information
hadim committed Dec 20, 2023
1 parent 9283770 commit 4ac1858
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions py-rattler/rattler/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ def bump_last(self) -> Version:
"""
return Version._from_py_version(self._version.bump_last())

def bump_segment(self, index: int) -> Version:
"""
Returns a new version where the last segment of this version has
been bumped.
Examples
--------
```python
>>> v = Version('1.0')
>>> v.bump_segment(index=1)
Version("1.1")
>>>
```
"""
return Version._from_py_version(self._version.bump_segment(index))

@property
def has_local(self) -> bool:
"""
Expand Down
8 changes: 8 additions & 0 deletions py-rattler/src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ impl PyVersion {
}
}

/// Returns a new version where the given segment of this version has been bumped.
pub fn bump_segment(&self, index: i32) -> PyResult<Self> {
match self.inner.bump(VersionBumpType::Segment(index)) {
Ok(v) => Ok(Self { inner: v }),
Err(e) => Err(PyRattlerError::from(e).into()),
}
}

/// Compute the hash of the version.
fn __hash__(&self) -> u64 {
let mut hasher = DefaultHasher::new();
Expand Down
8 changes: 8 additions & 0 deletions py-rattler/tests/unit/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_bump() -> None:
assert Version("0.5.5").bump_minor() == Version("0.6.5")
assert Version("0.5.5").bump_patch() == Version("0.5.6")
assert Version("0.5.5").bump_last() == Version("0.5.6")
assert Version("0.5.5").bump_segment(1) == Version("0.6.5")
assert Version("0.5.5").bump_segment(-1) == Version("0.5.6")


def test_bump_fail() -> None:
Expand All @@ -33,3 +35,9 @@ def test_bump_fail() -> None:

with pytest.raises(VersionBumpError):
Version("1.5").bump_patch()

with pytest.raises(VersionBumpError):
Version("1.5").bump_segment(-5)

with pytest.raises(VersionBumpError):
Version("1.5").bump_segment(5)

0 comments on commit 4ac1858

Please sign in to comment.