Make change ids in tests repeatable

This will be needed to test functionality for showing shortest
unique prefix for commit and change ids. As a bonus, this also
allows us to test log output with change ids.

As another bonus, this will prevent occasional CI failures like
https://github.com/martinvonz/jj/actions/runs/3817554687/jobs/6493881468.
This commit is contained in:
Ilya Grigoriev 2022-12-29 19:59:01 -08:00
parent 44d443a63b
commit 30d98974bb
3 changed files with 33 additions and 13 deletions

View file

@ -12,25 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use uuid::Uuid;
use std::sync::Arc;
use crate::backend::{self, BackendResult, ChangeId, CommitId, ObjectId, Signature, TreeId};
use crate::backend::{self, BackendResult, ChangeId, CommitId, Signature, TreeId};
use crate::commit::Commit;
use crate::repo::MutableRepo;
use crate::settings::UserSettings;
use crate::settings::{JJRng, UserSettings};
#[must_use]
pub struct CommitBuilder<'repo> {
mut_repo: &'repo mut MutableRepo,
rng: Arc<JJRng>,
commit: backend::Commit,
rewrite_source: Option<Commit>,
}
// TODO: This function will be replaced in the next commit.
pub fn new_change_id() -> ChangeId {
ChangeId::from_bytes(Uuid::new_v4().as_bytes())
}
impl CommitBuilder<'_> {
pub fn for_new_commit<'repo>(
mut_repo: &'repo mut MutableRepo,
@ -40,17 +36,19 @@ impl CommitBuilder<'_> {
) -> CommitBuilder<'repo> {
let signature = settings.signature();
assert!(!parents.is_empty());
let rng = settings.get_rng();
let commit = backend::Commit {
parents,
predecessors: vec![],
root_tree: tree_id,
change_id: new_change_id(),
change_id: rng.new_change_id(),
description: String::new(),
author: signature.clone(),
committer: signature,
};
CommitBuilder {
mut_repo,
rng,
commit,
rewrite_source: None,
}
@ -75,6 +73,7 @@ impl CommitBuilder<'_> {
CommitBuilder {
mut_repo,
commit,
rng: settings.get_rng(),
rewrite_source: Some(predecessor.clone()),
}
}
@ -101,7 +100,7 @@ impl CommitBuilder<'_> {
}
pub fn generate_new_change_id(mut self) -> Self {
self.commit.change_id = new_change_id();
self.commit.change_id = self.rng.new_change_id();
self
}

View file

@ -19,7 +19,7 @@ use chrono::DateTime;
use rand::prelude::*;
use rand_chacha::ChaCha20Rng;
use crate::backend::{Signature, Timestamp};
use crate::backend::{ChangeId, ObjectId, Signature, Timestamp};
#[derive(Debug, Clone)]
pub struct UserSettings {
@ -178,8 +178,19 @@ impl UserSettings {
#[derive(Debug)]
pub struct JJRng(Mutex<ChaCha20Rng>);
impl JJRng {
/// Wraps Rng::gen but only requires an immutable reference.
pub fn gen<T>(&self) -> T
pub fn new_change_id(&self) -> ChangeId {
let random_bytes = self.gen();
// The `uuid` crate is used merely to specify the number of random bytes (16)
ChangeId::from_bytes(
uuid::Builder::from_random_bytes(random_bytes)
.into_uuid()
.as_bytes(),
)
}
/// Wraps Rng::gen but only requires an immutable reference. Can be made
/// public if there's a use for it.
fn gen<T>(&self) -> T
where
rand::distributions::Standard: rand::distributions::Distribution<T>,
{

View file

@ -61,6 +61,16 @@ fn test_log_with_or_without_diff() {
o (no description set)
"###);
// Test default log output format
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["log"]), @r###"
@ ffdaa62087a2 test.user@example.com 2001-02-03 04:05:10.000 +07:00 789e536fd2e0
| a new commit
o 9a45c67d3e96 test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97
| add a file
o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000
(no description set)
"###);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "--no-graph"]);
insta::assert_snapshot!(stdout, @r###"
a new commit