Add tests for FromStr for GitRev and Version, rethink GitRev slightly in the process of making tests pass

This commit is contained in:
Nathan Fisher 2024-01-21 19:12:16 -05:00
parent 9255c3e1f2
commit ab572849c6
5 changed files with 105 additions and 40 deletions

40
Cargo.lock generated
View File

@ -90,18 +90,18 @@ dependencies = [
[[package]]
name = "js-sys"
version = "0.3.66"
version = "0.3.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.151"
version = "0.2.152"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7"
[[package]]
name = "log"
@ -126,9 +126,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "proc-macro2"
version = "1.0.75"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907a61bd0f64c2f29cd1cf1dc34d05176426a3f504a78010f08416ddb7b13708"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
@ -144,18 +144,18 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.194"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773"
checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.194"
version = "1.0.195"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0"
checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c"
dependencies = [
"proc-macro2",
"quote",
@ -189,9 +189,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@ -199,9 +199,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd"
dependencies = [
"bumpalo",
"log",
@ -214,9 +214,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -224,9 +224,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7"
dependencies = [
"proc-macro2",
"quote",
@ -237,9 +237,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
version = "0.2.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b"
[[package]]
name = "windows-core"

View File

@ -13,7 +13,7 @@ use {
/// Git revisions are not the preferred method of distribution for obvious reasons, as
/// they generally do not represent a stable release of the code. Another drawback is
/// that a Git revision can only be compared against another Git revision, making it
/// impossible to properly order updates between a proper SemVer type release and a\
/// impossible to properly order updates between a proper `SemVer` type release and a\
/// Git revision.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct GitRev {
@ -25,7 +25,7 @@ pub struct GitRev {
impl fmt::Display for GitRev {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "git_{}.{}", self.hash, self.datetime.format("%Y%m%d"))
write!(f, "git_{}.{}", self.hash, self.datetime.timestamp())
}
}
@ -46,12 +46,15 @@ impl FromStr for GitRev {
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(gitrev) = s.strip_prefix("git_") {
if let Some((hash, date)) = gitrev.split_once('_') {
if let Some((hash, datetime)) = gitrev.split_once('.') {
if hash.len() == 7 {
let datetime = DateTime::parse_from_str(date, "%Y%m%d")?;
let secs: i64 = datetime.parse()?;
let Some(datetime) = DateTime::<Utc>::from_timestamp(secs, 0) else {
return Err(Error::ParseInt);
};
return Ok(Self {
hash: hash.to_string(),
datetime: datetime.into(),
datetime,
});
}
}
@ -128,14 +131,28 @@ mod test {
#[test]
fn ord() {
let a = GitRev {
hash: "aaab".to_string(),
hash: "aaabxxx".to_string(),
datetime: Utc::now(),
};
thread::sleep(Duration::from_millis(10));
let b = GitRev {
hash: "aaaa".to_string(),
hash: "aaaaxxx".to_string(),
datetime: Utc::now(),
};
assert!(a < b);
}
#[test]
fn from_str() {
let now = DateTime::<Utc>::from_timestamp(Utc::now().timestamp(), 0).unwrap();
let rev: GitRev = format!("git_r2d2xxx.{}", now.timestamp()).parse().unwrap();
println!("Version = {rev}");
assert_eq!(
rev,
GitRev {
hash: "r2d2xxx".to_string(),
datetime: now,
}
);
}
}

View File

@ -1,3 +1,4 @@
#![warn(clippy::all, clippy::pedantic)]
mod arch;
mod error;
mod extended;

View File

@ -1,7 +1,7 @@
use {
crate::error::Error,
serde::{Deserialize, Serialize},
std::{cmp, fmt, num::NonZeroU16, str},
std::{cmp, convert::Into, fmt, num::NonZeroU16, str},
};
static MAX_U10: u16 = 1024;
@ -17,26 +17,26 @@ pub enum PreRelease {
}
impl PreRelease {
pub fn is_prerelease(&self) -> bool {
pub fn is_prerelease(self) -> bool {
!matches!(self, Self::None)
}
pub fn is_alpha(&self) -> bool {
pub fn is_alpha(self) -> bool {
matches!(self, Self::Alpha(_))
}
pub fn is_beta(&self) -> bool {
pub fn is_beta(self) -> bool {
matches!(self, Self::Beta(_))
}
pub fn is_rc(&self) -> bool {
pub fn is_rc(self) -> bool {
matches!(self, Self::RC(_))
}
pub fn number(&self) -> Option<u16> {
pub fn number(self) -> Option<u16> {
match self {
Self::Alpha(n) | Self::Beta(n) | Self::RC(n) => n.map(|x| x.into()),
_ => None,
Self::Alpha(n) | Self::Beta(n) | Self::RC(n) => n.map(Into::into),
Self::None => None,
}
}
@ -49,26 +49,25 @@ impl PreRelease {
Some(num) => {
if u16::from(*num) >= MAX_U10 {
return Err(Error::Range);
} else {
*num = num.saturating_add(1);
}
*num = num.saturating_add(1);
}
None => *n = Some(NonZeroU16::new(2).unwrap()),
},
_ => {}
Self::None => {}
}
Ok(())
}
/// Promotes the prerelease spec to a higher type eg. alpha->beta or beta->rc
/// If the spec is already Self::RC(_) then it becomes a regular release ie.
/// If the spec is already `Self::RC(_)` then it becomes a regular release ie.
/// `PreRelease::None`
pub fn promote(&mut self) {
match self {
Self::Alpha(_) => *self = Self::Beta(None),
Self::Beta(_) => *self = Self::RC(None),
Self::RC(_) => *self = Self::None,
_ => {}
Self::None => {}
}
}
}

View File

@ -152,3 +152,51 @@ impl PartialOrd for Version {
}
}
}
#[cfg(test)]
mod tests {
use chrono::{DateTime, Utc};
use std::num::NonZeroU16;
use super::*;
#[test]
fn from_str() {
let mut version: Version = "2.4.1_alpha2-aarch64".parse().unwrap();
assert_eq!(
version,
Version(
Kind::SemVer(SemVer {
major: 2,
minor: 4,
patch: 1,
pre: crate::prerelease::PreRelease::Alpha(Some(NonZeroU16::new(2).unwrap()))
}),
Arch::Arm64
)
);
version = "6.4-i486".parse().unwrap();
assert_eq!(
version,
Version(
Kind::Rapid(Rapid {
major: 6,
minor: 4,
pre: crate::prerelease::PreRelease::None,
}),
Arch::X86,
)
);
version = "git_r2d2xxx.1705881493-amd64".parse().unwrap();
assert_eq!(
version,
Version(
Kind::GitRev(GitRev {
hash: "r2d2xxx".to_string(),
datetime: DateTime::<Utc>::from_timestamp(1705881493, 0).unwrap(),
}),
Arch::X86_64,
)
);
}
}