From 7b07e57b9c3cf86e347bd288cc6a8221cb12ff18 Mon Sep 17 00:00:00 2001 From: Lorenz Leutgeb Date: Thu, 23 Apr 2026 23:48:20 +0200 Subject: [PATCH] treewide/test: Use `impl Arbitrary for Oid` Many usages of `Oid::from_sha1` are actually in the context of generating arbitrary `Oid`s. In these cases, `impl Arbitrary for Oid` may be used directly. --- crates/radicle-core/src/repo.rs | 5 +---- crates/radicle-protocol/src/wire.rs | 4 ++-- crates/radicle/src/storage/refs/arbitrary.rs | 12 ++++-------- crates/radicle/src/test/arbitrary.rs | 3 +-- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/crates/radicle-core/src/repo.rs b/crates/radicle-core/src/repo.rs index 7ec85721..7e9bb1c7 100644 --- a/crates/radicle-core/src/repo.rs +++ b/crates/radicle-core/src/repo.rs @@ -313,10 +313,7 @@ pub mod arbitrary { #[cfg(feature = "qcheck")] impl qcheck::Arbitrary for RepoId { fn arbitrary(g: &mut qcheck::Gen) -> Self { - let bytes = <[u8; 20]>::arbitrary(g); - let oid = radicle_oid::Oid::from_sha1(bytes); - - RepoId::from(oid) + RepoId::from(radicle_oid::Oid::arbitrary(g)) } } diff --git a/crates/radicle-protocol/src/wire.rs b/crates/radicle-protocol/src/wire.rs index 729a023a..9cd4935d 100644 --- a/crates/radicle-protocol/src/wire.rs +++ b/crates/radicle-protocol/src/wire.rs @@ -624,8 +624,8 @@ mod tests { } #[quickcheck] - fn prop_oid(input: [u8; git::Oid::SHA1_LEN]) { - roundtrip(git::Oid::from_sha1(input)); + fn prop_oid(input: git::Oid) { + roundtrip(input); } #[test] diff --git a/crates/radicle/src/storage/refs/arbitrary.rs b/crates/radicle/src/storage/refs/arbitrary.rs index 6dddaab2..5cb40075 100644 --- a/crates/radicle/src/storage/refs/arbitrary.rs +++ b/crates/radicle/src/storage/refs/arbitrary.rs @@ -17,7 +17,7 @@ where if bool::arbitrary(g) { level = Some(FeatureLevel::Parent); - let parent = Oid::from_sha1(Arbitrary::arbitrary(g)); + let parent = Oid::arbitrary(g); refs.insert(SIGREFS_PARENT.to_ref_string(), parent); } @@ -28,13 +28,12 @@ where id: *signer.node_id(), level: level.unwrap_or_else(|| FeatureLevel::arbitrary(g)), parent: Arbitrary::arbitrary(g), - at: Oid::from_sha1(Arbitrary::arbitrary(g)), + at: Oid::arbitrary(g), } } impl Arbitrary for Refs { fn arbitrary(g: &mut qcheck::Gen) -> Self { - let mut bytes: [u8; 20] = [0; 20]; let names = &[ "heads/master", "heads/feature/1", @@ -49,10 +48,7 @@ impl Arbitrary for Refs { let mut refs = Self::new(); for _ in 0..g.size().min(names.len()) { if let Some(name) = g.choose(names) { - for byte in &mut bytes { - *byte = u8::arbitrary(g); - } - let oid = storage::Oid::from_sha1(bytes); + let oid = storage::Oid::arbitrary(g); let name = git::fmt::RefString::try_from(*name).unwrap(); refs.insert(name, oid); @@ -66,7 +62,7 @@ impl Arbitrary for RefsAt { fn arbitrary(g: &mut qcheck::Gen) -> Self { Self { remote: PublicKey::arbitrary(g), - at: Oid::from_sha1(Arbitrary::arbitrary(g)), + at: Oid::arbitrary(g), } } } diff --git a/crates/radicle/src/test/arbitrary.rs b/crates/radicle/src/test/arbitrary.rs index d6eba3f2..4c322c2d 100644 --- a/crates/radicle/src/test/arbitrary.rs +++ b/crates/radicle/src/test/arbitrary.rs @@ -25,8 +25,7 @@ use crate::test::storage::{MockRepository, MockStorage}; use crate::{cob, git}; pub fn oid() -> storage::Oid { - let oid_bytes: [u8; 20] = r#gen(1); - storage::Oid::from_sha1(oid_bytes) + r#gen(1) } pub fn entry_id() -> cob::EntryId {