Skip to content

Commit

Permalink
Make change ids in tests repeatable
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilyagr committed Jan 4, 2023
1 parent 44d443a commit 30d9897
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
19 changes: 9 additions & 10 deletions lib/src/commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand All @@ -75,6 +73,7 @@ impl CommitBuilder<'_> {
CommitBuilder {
mut_repo,
commit,
rng: settings.get_rng(),
rewrite_source: Some(predecessor.clone()),
}
}
Expand All @@ -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
}

Expand Down
17 changes: 14 additions & 3 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>,
{
Expand Down
10 changes: 10 additions & 0 deletions tests/test_log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected] 2001-02-03 04:05:10.000 +07:00 789e536fd2e0
| a new commit
o 9a45c67d3e96 [email protected] 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
Expand Down

0 comments on commit 30d9897

Please sign in to comment.