diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/pkgver/mod.rs | 216 |
1 files changed, 209 insertions, 7 deletions
diff --git a/src/pkgver/mod.rs b/src/pkgver/mod.rs index ef00b55..cc00877 100644 --- a/src/pkgver/mod.rs +++ b/src/pkgver/mod.rs @@ -10,7 +10,7 @@ pub fn full_version(epoch: &str, pkgver: &str, pkgrel: &str) -> String { } } -pub fn split_full_version(v: &str) -> (String, String, String) { +fn split_full_version(v: &str) -> (String, String, String) { let (epoch, rest) = match v.split_once(':') { Some((e, r)) => (e.to_string(), r.to_string()), None => ("0".to_string(), v.to_string()), @@ -110,7 +110,7 @@ fn preceded_by_separator(s: &[u8], k: usize) -> bool { k > 0 && !is_alnum(s[k - 1]) } -pub fn vercmp_string(a: &str, b: &str) -> Ordering { +fn vercmp_string(a: &str, b: &str) -> Ordering { let (a, b) = (a.as_bytes(), b.as_bytes()); let (mut i, mut j) = (0usize, 0usize); loop { @@ -190,7 +190,7 @@ pub fn is_downgrade(current: &str, new: &str) -> bool { // ------------------------------------------------------------- dep strings -- #[derive(Debug, Clone, PartialEq, Eq)] -pub enum DepOp { +enum DepOp { Any, Eq, Ge, @@ -213,14 +213,14 @@ impl fmt::Display for DepOp { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Dep { +struct Dep { pub name: String, pub op: DepOp, pub version: Option<String>, } impl Dep { - pub fn parse(s: &str) -> Option<Dep> { + fn parse(s: &str) -> Option<Dep> { let s = s.trim(); if s.is_empty() { return None; @@ -251,11 +251,17 @@ impl Dep { Some(Dep { name: s.trim().to_string(), op: DepOp::Any, version: None }) } - pub fn name(&self) -> &str { + /// Only the tests inside this module ask for a `Dep`'s name; production + /// reads the field. Gated so the release build does not compile it. + #[cfg(test)] + fn name(&self) -> &str { &self.name } - pub fn satisfied_by(&self, version: &str) -> bool { + /// Constraint checking is exercised only by this module's tests today; no + /// production path compares a dependency against an installed version yet. + #[cfg(test)] + fn satisfied_by(&self, version: &str) -> bool { match self.op { DepOp::Any => true, DepOp::Eq => vercmp(version, self.version.as_deref().unwrap_or("0")) == Ordering::Equal, @@ -339,4 +345,200 @@ mod tests { assert!(!d.satisfied_by("3.10-1")); assert!(Dep::parse("foo").unwrap().satisfied_by("anything")); } + + // --------------------------------------------------------------------- + // Moved verbatim from tests/versions.rs: every test here is a function of + // its arguments alone — no database, no fixture, no temporary directory — + // which is what makes it a unit test of this module rather than a consumer + // of it. + // --------------------------------------------------------------------- + + const ORDERED: &[(&str, &str)] = &[ + ("v1.2.3", "v1.2.4"), ("1.0", "1.1"), ("1.1", "1.2"), ("1.2", "2.0"), + ("2.0", "2.1.0"), ("2.1.0", "2.1.1"), ("1.0rc1", "1.0"), ("1.0", "1.0.0"), + ("1.0.0", "1.0.1"), ("1.0.1", "1.1.0"), ("1.1", "1.1.0"), ("1", "1.0"), + ("1.0", "1.0.1"), ("1.0.1", "1.1"), ("1.1", "1.1.1"), ("1.1.1", "1.2"), + ("1.2", "1.2.2"), ("1.2.2", "1.3"), ("1.3", "1.3.1"), ("1.3.1", "1.4"), + ("1.4", "1.4.1"), ("1.4.1", "2.0"), ("2.0", "2.0.1"), ("2.0.1", "2.1.0"), + ("2.1.0", "2.1.1"), ("2.1.1", "3.0"), ("3.0", "3.0.1"), ("3.0.1", "4.0"), + ("1.0.a", "1.0.1"), ("1.0.b", "1.0.2"), ("1.0.2", "1.0.2.a"), + ("1.0.2.a", "1.0.2.aa"), ("1.0.2.aa", "1.0.2.b"), ("1.0.2.b", "1.0.2.bb"), + ("1.0.2.bb", "1.0.3"), ("1.0.3a", "1.0.3b"), ("1.0.3b", "1.1"), + ("1.1a", "1.2"), ("1:0.8", "2:0.1"), ("2:0.1", "3:0.1"), ("1.0-1", "1.0-2"), + ("1.0-2", "1.0-3"), ("1.0-3", "1.1-1"), ("1:1.0-1", "1:1.0-2"), + ("1:1.0-2", "2:0.1-1"), ("2.5", "2.24"), ("2.0", "2.0.01"), + ]; + + #[test] + fn vercmp_orders_the_canonical_vectors() { + for (a, b) in ORDERED { + assert_eq!(vercmp(a, b), Ordering::Less, "vercmp({a:?}, {b:?}) should be Less"); + assert_eq!( + vercmp(b, a), + Ordering::Greater, + "vercmp({b:?}, {a:?}) should be Greater (comparison must be antisymmetric)" + ); + } + } + + fn sign(o: Ordering) -> i32 { + match o { + Ordering::Less => -1, + Ordering::Equal => 0, + Ordering::Greater => 1, + } + } + + #[test] + fn vercmp_matches_the_recorded_oracle() { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("testcorpus/vercmp-oracle.tsv"); + let table = std::fs::read_to_string(&path) + .expect("testcorpus/vercmp-oracle.tsv; regenerate with tools/gen-vercmp-oracle.py"); + let mut recorded = std::collections::HashMap::new(); + let mut checked = 0; + for line in table.lines().filter(|l| !l.starts_with('#') && !l.is_empty()) { + let mut cols = line.split('\t'); + let (a, b) = (cols.next().unwrap(), cols.next().unwrap()); + let theirs: i32 = cols.next().unwrap().parse().expect("verdict column"); + assert_eq!( + sign(vercmp_string(a, b)), + theirs, + "vercmp({a:?}, {b:?}) disagrees with the recorded oracle" + ); + checked += 1; + recorded.insert((a, b), theirs); + } + assert!(checked >= 1089, "the cross-product should be substantial, checked {checked}"); + for ((a, b), verdict) in &recorded { + assert_eq!( + recorded.get(&(*b, *a)), + Some(&-*verdict), + "the recorded table is not antisymmetric at {a:?} / {b:?}" + ); + } + } + + #[test] + fn the_live_vercmp_tier_is_recorded_not_silently_absent() { + if std::env::var("AUR_LIVE_VERCMP").is_err() { + println!("note: AUR_LIVE_VERCMP unset; the live vercmp differential is skipped, not passed"); + return; + } + if std::process::Command::new("vercmp").arg("1").arg("2").output().is_err() { + println!("note: AUR_LIVE_VERCMP set but pacman's vercmp is not on PATH; tier skipped"); + return; + } + let fragments = [ + "1", "1.0", "1.0.0", "1.0.1", "1.1", "2", "2.0", "1.0a", "1.0rc1", "1.0.2a", "1.0.2.1", + "1.0.2.a", "1.0.2.aa", "1.0.2.b", "1.0.2z", "1.0z", "1.0.z", "0.9", "10", "2.24", "2.5", + "1.02", "1.0_2", "1.0.p", "1.0.3a", "1.0.3b", "1.a", "1a", "1.0+git", "1.0~beta", "2.0_1", + "1.0.x", "2024.01", + ]; + let mut checked = 0; + for a in fragments { + for b in fragments { + let theirs = std::process::Command::new("vercmp").arg(a).arg(b).output().expect("vercmp"); + let theirs: i32 = String::from_utf8_lossy(&theirs.stdout).trim().parse().expect("vercmp output"); + assert_eq!(sign(vercmp_string(a, b)), theirs, "vercmp({a:?}, {b:?}) disagrees with pacman"); + checked += 1; + } + } + assert!(checked > 1000, "the cross-product should be substantial, checked {checked}"); + } + + #[test] + fn equal_versions_compare_equal_and_leading_zeros_do_not_matter() { + for (a, b) in [ + ("1.0-1", "1.0-1"), + ("1:1.0-1", "1:1.0-1"), + ("1.02-1", "1.2-1"), + ("01.0-1", "1.0-1"), + ("1.0-01", "1.0-1"), + ] { + assert_eq!(vercmp(a, b), Ordering::Equal, "{a} vs {b}"); + } + } + + #[test] + fn an_epoch_always_beats_a_larger_version_without_one() { + assert_eq!(vercmp("1:0.1-1", "9.9.9-1"), Ordering::Greater); + assert_eq!(vercmp("9.9.9-1", "1:0.1-1"), Ordering::Less); + assert_eq!(vercmp("0:1.0-1", "1.0-1"), Ordering::Equal); + assert_eq!(vercmp("1:1.0-1", "1.0-1"), Ordering::Greater); + } + + #[test] + fn a_pkgrel_reset_under_a_newer_pkgver_is_still_an_upgrade() { + assert_eq!(vercmp("1.2.3-4", "1.2.4-1"), Ordering::Less); + assert!(!is_downgrade("1.2.3-4", "1.2.4-1")); + assert!(is_downgrade("1.2.4-1", "1.2.3-9")); + } + + #[test] + fn alpha_numeric_boundaries_follow_the_chunk_rule() { + assert_eq!(vercmp("1.0.1", "1.0.a"), Ordering::Greater); + assert_eq!(vercmp("1.0.a", "1.0.1"), Ordering::Less); + assert_eq!(vercmp("1.0", "1.0.1"), Ordering::Less); + assert_eq!(vercmp("1.0", "1.0a"), Ordering::Greater); + assert_eq!(vercmp("1.0rc1", "1.0"), Ordering::Less); + assert_eq!(vercmp("1.0", "1.0rc1"), Ordering::Greater); + } + + #[test] + fn dependency_strings_parse_with_their_constraints() { + let cases = [ + ("gcc", "gcc", DepOp::Any, None), + ("python>=3.11", "python", DepOp::Ge, Some("3.11")), + ("openssl<3", "openssl", DepOp::Lt, Some("3")), + ("glibc>=2.39-1", "glibc", DepOp::Ge, Some("2.39-1")), + ("linux-api-headers=6.0", "linux-api-headers", DepOp::Eq, Some("6.0")), + ("gcc-libs<=13.2.1-2", "gcc-libs", DepOp::Le, Some("13.2.1-2")), + ("foo>=1:2.3-4", "foo", DepOp::Ge, Some("1:2.3-4")), + ("python-pytest: for tests", "python-pytest", DepOp::Any, None), + ]; + for (src, name, op, ver) in cases { + let d = Dep::parse(src).unwrap_or_else(|| panic!("failed to parse {src:?}")); + assert_eq!(d.name(), name, "{src:?} name"); + assert_eq!(d.op, op, "{src:?} operator"); + assert_eq!(d.version.as_deref(), ver, "{src:?} version"); + } + assert!(Dep::parse("").is_none()); + assert!(Dep::parse(">=1.0").is_none()); + assert_eq!(dep_names(&["python>=3.11".into(), "gcc".into()]), vec!["python", "gcc"]); + } + + #[test] + fn version_constraints_are_satisfied_by_the_right_versions() { + let d = Dep::parse("glibc>=2.39-1").unwrap(); + assert!(d.satisfied_by("2.39-1")); + assert!(d.satisfied_by("2.40-1")); + assert!(!d.satisfied_by("2.38-1")); + assert!(Dep::parse("foo>=1.0").unwrap().satisfied_by("1:0.1-1")); + assert!(Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-1")); + assert!(!Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-2")); + assert!(Dep::parse("foo").unwrap().satisfied_by("0")); + } + + #[test] + fn provides_and_conflicts_use_the_same_grammar() { + for src in ["aur-build", "aur-build=0.1-1", "virtual-thing>=1.0"] { + let d = Dep::parse(src).expect(src); + assert!(!d.name().is_empty()); + assert!(!d.name().contains(['>', '<', '='])); + } + assert_eq!(dep_names(&["aur-build=0.1-1".into(), "other".into()]), vec!["aur-build", "other"]); + } + + #[test] + fn full_version_round_trips_through_split() { + for (epoch, pkgver, pkgrel, full) in [ + ("0", "1.2.3", "4", "1.2.3-4".to_string()), + ("1", "1.2.3", "4", "1:1.2.3-4".to_string()), + ("2", "1.0", "1", "2:1.0-1".to_string()), + ] { + assert_eq!(full_version(epoch, pkgver, pkgrel), full); + let (e, v, r) = split_full_version(&full); + assert_eq!((e.as_str(), v.as_str(), r.as_str()), (epoch, pkgver, pkgrel), "round trip of {full}"); + } + } } |
