diff options
| author | aur-build <aur-build@localhost> | 2026-11-20 20:24:47 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-20 20:24:47 +0000 |
| commit | 9d4078919717559c90e40736917603401135437c (patch) | |
| tree | 7479b0782fa03365f3e7c440c30bbdec90acb0e7 | |
| parent | 8426843087178cac55c3dcea52aea3baccc29b69 (diff) | |
| download | aur-build-9d4078919717559c90e40736917603401135437c.tar.gz aur-build-9d4078919717559c90e40736917603401135437c.zip | |
hash, time, db::model: move the pure clock and digest tests home
tests/hash.rs held three tests and none of them belonged there. Two were
about `canonical_json` and moved into src/hash.rs. The third asserted
that the identity digest is a pure function of an identity's own fields
— a property of `db::model::Identity`, not of the hashing helper it
happens to call — so it moved to db::model, where the thing it tests
lives. The file is now empty and is deleted; the tests are moves, not
losses, and each is named in its new module.
tests/time.rs held three tests, two of which were clock arithmetic with
no dependencies at all. They moved into src/time.rs. The third pins a
build lease against a pinned clock and needs a database, so it stayed
where a database is available.
No visibility changed in this commit: `canonical_json` is used in
production and stays public, and `time::pin`/`unpin` are ungated test
escapes that the disposition table will decide on rather than being
quietly narrowed here as a side effect.
Suite: 281 passed, 0 failed, 0 ignored. Release build: zero warnings.
| -rw-r--r-- | src/db/model/mod.rs | 28 | ||||
| -rw-r--r-- | src/hash.rs | 52 | ||||
| -rw-r--r-- | src/time.rs | 25 | ||||
| -rw-r--r-- | tests/architecture-budget.json | 12 | ||||
| -rw-r--r-- | tests/hash.rs | 65 | ||||
| -rw-r--r-- | tests/time.rs | 18 |
6 files changed, 114 insertions, 86 deletions
diff --git a/src/db/model/mod.rs b/src/db/model/mod.rs index acfcda5..7c1fbf5 100644 --- a/src/db/model/mod.rs +++ b/src/db/model/mod.rs @@ -206,3 +206,31 @@ pub(crate) use state::StateEnum; pub use build_state::{BUILD_STEPS, BuildState, StepState}; pub use state::{CandidateState, ReleaseState, SubscriptionState, candidate_kind, notice_kind}; + +#[cfg(test)] +mod tests { + use super::*; + + // Moved verbatim from tests/hash.rs, where it sat next to the canonical + // JSON tests but asserted nothing about hashing: it pins the contract that + // the identity digest is a pure function of the identity's own fields. + // That is a property of `Identity`, so it lives with `Identity`. + + #[test] + fn the_identity_digest_is_a_function_of_the_identity_alone() { + let base = Identity { + aur_commit: "cafe123".into(), + epoch: "0".into(), + pkgver: "1.2.3".into(), + pkgrel: "1".into(), + upstream_pins: vec![], + source_digests: vec![], + }; + let digest = |i: &Identity| i.hash().unwrap(); + assert!(digest(&base).starts_with("sha256:")); + assert_eq!(digest(&base), digest(&base.clone()), "the same identity hashed twice, twice differently"); + assert_ne!(digest(&base), digest(&Identity { pkgrel: "2".into(), ..base.clone() })); + assert_ne!(digest(&base), digest(&Identity { epoch: "1".into(), ..base.clone() })); + assert_ne!(digest(&base), digest(&Identity { aur_commit: "cafe124".into(), ..base.clone() })); + } +} diff --git a/src/hash.rs b/src/hash.rs index 5de6f3f..8a2d884 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -56,3 +56,55 @@ pub fn recipe_hash(entries: &[(String, String)]) -> String { pub fn canonical_json<T: serde::Serialize>(v: &T) -> Result<String, serde_json::Error> { serde_json::to_string(&serde_json::to_value(v)?) } + +#[cfg(test)] +mod tests { + use super::*; + + struct Unserializable; + + impl serde::Serialize for Unserializable { + fn serialize<S: serde::Serializer>(&self, _s: S) -> Result<S::Ok, S::Error> { + Err(serde::ser::Error::custom("this value cannot be serialized")) + } + } + + // Moved verbatim from tests/hash.rs. Both tests are a function of their + // argument alone; the third test that file held was about `Identity`, not + // about hashing, and moved to db::model where it belongs. + + #[test] + fn a_value_that_cannot_be_serialized_is_an_error_not_an_empty_object() { + let err = canonical_json(&Unserializable).unwrap_err(); + assert!(err.to_string().contains("cannot be serialized"), "the failure was swallowed: {err}"); + assert!(canonical_json(&Unserializable).is_err()); + } + + #[test] + fn the_rendering_is_canonical_in_fact_not_in_name() { + #[derive(serde::Serialize)] + struct Ab { + alpha: String, + beta: String, + } + #[derive(serde::Serialize)] + struct Ba { + beta: String, + alpha: String, + } + let ab = canonical_json(&Ab { alpha: "1".into(), beta: "2".into() }).unwrap(); + let ba = canonical_json(&Ba { beta: "2".into(), alpha: "1".into() }).unwrap(); + assert_eq!(ab, ba, "field declaration order leaked into the rendering: {ab} vs {ba}"); + assert_eq!(ab, r#"{"alpha":"1","beta":"2"}"#, "not the compact sorted rendering"); + + let mut one = std::collections::HashMap::new(); + one.insert("z", 1); + one.insert("m", 2); + one.insert("a", 3); + let mut two = std::collections::HashMap::new(); + two.insert("a", 3); + two.insert("m", 2); + two.insert("z", 1); + assert_eq!(canonical_json(&one).unwrap(), canonical_json(&two).unwrap()); + } +} diff --git a/src/time.rs b/src/time.rs index 29206d6..5e82f0c 100644 --- a/src/time.rs +++ b/src/time.rs @@ -203,3 +203,28 @@ mod clock { f } } + +#[cfg(test)] +mod tests { + use super::*; + + // Moved verbatim from tests/time.rs. These two are arithmetic on the + // clock itself and need nothing else; the third test in that file pins a + // lease against a pinned clock, which needs a database, so it stayed. + + #[test] + fn a_pinned_clock_moves_relative_arithmetic_with_it() { + pin("2026-01-01T00:00:00Z"); + assert_eq!(now(), "2026-01-01T00:00:00Z"); + assert_eq!(secs_ahead(90), "2026-01-01T00:01:30Z"); + assert_eq!(secs_ago(90), "2025-12-31T23:58:30Z"); + unpin(); + } + + #[test] + fn an_unpinned_clock_is_the_real_one() { + let n = now(); + assert_eq!(delta_secs(&n, &secs_ahead(60)), Some(60)); + assert_eq!(delta_secs(&n, &secs_ago(60)), Some(-60)); + } +} diff --git a/tests/architecture-budget.json b/tests/architecture-budget.json index d361c54..c71c764 100644 --- a/tests/architecture-budget.json +++ b/tests/architecture-budget.json @@ -94,7 +94,7 @@ "glob": 0, "largest": 364, "largest_file": "src/db/candidates.rs", - "lines": 3742, + "lines": 3770, "priv": 10, "pub": 202, "test": 13 @@ -136,9 +136,9 @@ "dead": 0, "deadonly": 0, "glob": 0, - "largest": 59, + "largest": 111, "largest_file": "src/hash.rs", - "lines": 59, + "lines": 111, "priv": 0, "pub": 7, "test": 0 @@ -268,9 +268,9 @@ "dead": 5, "deadonly": 2, "glob": 0, - "largest": 206, + "largest": 231, "largest_file": "src/time.rs", - "lines": 206, + "lines": 231, "priv": 0, "pub": 14, "test": 3 @@ -316,7 +316,7 @@ "glob": 0, "largest": 667, "largest_file": "src/config.rs", - "lines": 14733, + "lines": 14838, "priv": 109, "pub": 497, "test": 65 diff --git a/tests/hash.rs b/tests/hash.rs deleted file mode 100644 index 32995ff..0000000 --- a/tests/hash.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::collections::HashMap; - -use aur_build::db::model::Identity; -use aur_build::hash; - -struct Unserializable; - -impl serde::Serialize for Unserializable { - fn serialize<S: serde::Serializer>(&self, _s: S) -> Result<S::Ok, S::Error> { - Err(serde::ser::Error::custom("this value cannot be serialized")) - } -} - -#[test] -fn a_value_that_cannot_be_serialized_is_an_error_not_an_empty_object() { - let err = hash::canonical_json(&Unserializable).unwrap_err(); - assert!(err.to_string().contains("cannot be serialized"), "the failure was swallowed: {err}"); - assert!(hash::canonical_json(&Unserializable).is_err()); -} - -#[test] -fn the_rendering_is_canonical_in_fact_not_in_name() { - #[derive(serde::Serialize)] - struct Ab { - alpha: String, - beta: String, - } - #[derive(serde::Serialize)] - struct Ba { - beta: String, - alpha: String, - } - let ab = hash::canonical_json(&Ab { alpha: "1".into(), beta: "2".into() }).unwrap(); - let ba = hash::canonical_json(&Ba { beta: "2".into(), alpha: "1".into() }).unwrap(); - assert_eq!(ab, ba, "field declaration order leaked into the rendering: {ab} vs {ba}"); - assert_eq!(ab, r#"{"alpha":"1","beta":"2"}"#, "not the compact sorted rendering"); - - let mut one = HashMap::new(); - one.insert("z", 1); - one.insert("m", 2); - one.insert("a", 3); - let mut two = HashMap::new(); - two.insert("a", 3); - two.insert("m", 2); - two.insert("z", 1); - assert_eq!(hash::canonical_json(&one).unwrap(), hash::canonical_json(&two).unwrap()); -} - -#[test] -fn the_identity_digest_is_a_function_of_the_identity_alone() { - let base = Identity { - aur_commit: "cafe123".into(), - epoch: "0".into(), - pkgver: "1.2.3".into(), - pkgrel: "1".into(), - upstream_pins: vec![], - source_digests: vec![], - }; - let hash_of = |i: &Identity| i.hash().unwrap(); - assert!(hash_of(&base).starts_with("sha256:")); - assert_eq!(hash_of(&base), hash_of(&base.clone()), "the same identity hashed twice, twice differently"); - assert_ne!(hash_of(&base), hash_of(&Identity { pkgrel: "2".into(), ..base.clone() })); - assert_ne!(hash_of(&base), hash_of(&Identity { epoch: "1".into(), ..base.clone() })); - assert_ne!(hash_of(&base), hash_of(&Identity { aur_commit: "cafe124".into(), ..base.clone() })); -} diff --git a/tests/time.rs b/tests/time.rs index 3d4a203..a34202f 100644 --- a/tests/time.rs +++ b/tests/time.rs @@ -4,21 +4,9 @@ use aur_build::db::{builds, BuildState}; use aur_build::time; use common::Env; -#[test] -fn a_pinned_clock_moves_relative_arithmetic_with_it() { - time::pin("2026-01-01T00:00:00Z"); - assert_eq!(time::now(), "2026-01-01T00:00:00Z"); - assert_eq!(time::secs_ahead(90), "2026-01-01T00:01:30Z"); - assert_eq!(time::secs_ago(90), "2025-12-31T23:58:30Z"); - time::unpin(); -} - -#[test] -fn an_unpinned_clock_is_the_real_one() { - let now = time::now(); - assert_eq!(time::delta_secs(&now, &time::secs_ahead(60)), Some(60)); - assert_eq!(time::delta_secs(&now, &time::secs_ago(60)), Some(-60)); -} +// What remains is the test that needs a database: it pins a clock, takes a +// lease, and checks that expiry is measured against the pinned clock rather +// than the real one. The pure clock arithmetic moved into src/time.rs. #[test] fn a_lease_taken_under_a_pinned_clock_expires_against_that_clock() { |
