cob: Improve stability of test setup

We employ various tricks to ensure that tests are reproducible.
In this case, having stable commit ids allows us to test specific
traversal orders of a COB history in cases of concurrent updates.
This commit is contained in:
cloudhead 2023-10-08 00:52:28 +02:00
parent 69fe1d5989
commit 5709f5d886
No known key found for this signature in database
5 changed files with 28 additions and 8 deletions

View File

@ -12,6 +12,11 @@ edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["radicle", "cob", "cobs"]
[features]
default = []
# Only used for testing. Ensures that commit ids are stable.
stable-commit-ids = []
[dependencies]
fastrand = { version = "2.0.0" }
log = { version = "0.4.17" }

View File

@ -261,6 +261,7 @@ fn write_commit(
) -> Result<(Oid, Timestamp), error::Create> {
let trailers: Vec<OwnedTrailer> = vec![trailers::ResourceCommitTrailer::from(resource).into()];
let author = repo.signature()?;
#[allow(unused_variables)]
let timestamp = author.when().seconds();
let mut headers = Headers::new();
@ -273,6 +274,15 @@ fn write_commit(
);
let author = Author::try_from(&author)?;
#[cfg(feature = "stable-commit-ids")]
// Ensures the commit id doesn't change on every run.
let (author, timestamp) = (
Author {
time: git_ext::author::Time::new(1514817556, 0),
..author
},
1514817556,
);
#[cfg(debug_assertions)]
let (author, timestamp) = if let Ok(s) = std::env::var(crate::git::RAD_COMMIT_TIME) {
// SAFETY: It's ok to panic here, since this is only enabled in debug mode.

View File

@ -64,3 +64,8 @@ qcheck = { version = "1", default-features = false }
path = "../radicle-crypto"
version = "0"
features = ["test"]
[dev-dependencies.radicle-cob]
path = "../radicle-cob"
version = "0"
features = ["stable-commit-ids"]

View File

@ -92,15 +92,13 @@ pub mod setup {
fn default() -> Self {
let root = tempfile::tempdir().unwrap();
Self::new(root)
Self::new(root, MockSigner::default())
}
}
impl Node {
pub fn new(root: impl AsRef<Path>) -> Self {
pub fn new(root: impl AsRef<Path>, signer: MockSigner) -> Self {
let root = root.as_ref().to_path_buf();
let mut rng = fastrand::Rng::new();
let signer = MockSigner::new(&mut rng);
let home = root.join("home");
let paths = Home::new(home.as_path()).unwrap();
let storage = Storage::open(paths.storage()).unwrap();
@ -138,10 +136,12 @@ pub mod setup {
}
impl NodeRepo {
#[track_caller]
pub fn fetch(&self, from: &Node) -> Vec<RefUpdate> {
super::fetch(&self.repo, from.signer.public_key(), Namespaces::All).unwrap()
}
#[track_caller]
pub fn checkout(&self) -> &NodeRepoCheckout {
self.checkout.as_ref().unwrap()
}
@ -240,9 +240,9 @@ pub mod setup {
impl Default for Network {
fn default() -> Self {
let tmp = tempfile::tempdir().unwrap();
let alice = Node::new(tmp.path().join("alice"));
let mut bob = Node::new(tmp.path().join("bob"));
let mut eve = Node::new(tmp.path().join("eve"));
let alice = Node::new(tmp.path().join("alice"), MockSigner::from_seed([!0; 32]));
let mut bob = Node::new(tmp.path().join("bob"), MockSigner::from_seed([!1; 32]));
let mut eve = Node::new(tmp.path().join("eve"), MockSigner::from_seed([!2; 32]));
let repo = alice.project();
let rid = repo.id;

View File

@ -10,7 +10,7 @@ use crate::storage::git::Storage;
use crate::storage::refs::SignedRefs;
/// The birth of the radicle project, January 1st, 2018.
const RADICLE_EPOCH: i64 = 1514817556;
pub const RADICLE_EPOCH: i64 = 1514817556;
/// Create a new storage with a project.
pub fn storage<P: AsRef<Path>, G: Signer>(path: P, signer: &G) -> Result<Storage, rad::InitError> {