mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-28 15:26:25 +00:00
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:
parent
44d443a63b
commit
30d98974bb
3 changed files with 33 additions and 13 deletions
|
@ -12,25 +12,21 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// 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::commit::Commit;
|
||||||
use crate::repo::MutableRepo;
|
use crate::repo::MutableRepo;
|
||||||
use crate::settings::UserSettings;
|
use crate::settings::{JJRng, UserSettings};
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub struct CommitBuilder<'repo> {
|
pub struct CommitBuilder<'repo> {
|
||||||
mut_repo: &'repo mut MutableRepo,
|
mut_repo: &'repo mut MutableRepo,
|
||||||
|
rng: Arc<JJRng>,
|
||||||
commit: backend::Commit,
|
commit: backend::Commit,
|
||||||
rewrite_source: Option<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<'_> {
|
impl CommitBuilder<'_> {
|
||||||
pub fn for_new_commit<'repo>(
|
pub fn for_new_commit<'repo>(
|
||||||
mut_repo: &'repo mut MutableRepo,
|
mut_repo: &'repo mut MutableRepo,
|
||||||
|
@ -40,17 +36,19 @@ impl CommitBuilder<'_> {
|
||||||
) -> CommitBuilder<'repo> {
|
) -> CommitBuilder<'repo> {
|
||||||
let signature = settings.signature();
|
let signature = settings.signature();
|
||||||
assert!(!parents.is_empty());
|
assert!(!parents.is_empty());
|
||||||
|
let rng = settings.get_rng();
|
||||||
let commit = backend::Commit {
|
let commit = backend::Commit {
|
||||||
parents,
|
parents,
|
||||||
predecessors: vec![],
|
predecessors: vec![],
|
||||||
root_tree: tree_id,
|
root_tree: tree_id,
|
||||||
change_id: new_change_id(),
|
change_id: rng.new_change_id(),
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
author: signature.clone(),
|
author: signature.clone(),
|
||||||
committer: signature,
|
committer: signature,
|
||||||
};
|
};
|
||||||
CommitBuilder {
|
CommitBuilder {
|
||||||
mut_repo,
|
mut_repo,
|
||||||
|
rng,
|
||||||
commit,
|
commit,
|
||||||
rewrite_source: None,
|
rewrite_source: None,
|
||||||
}
|
}
|
||||||
|
@ -75,6 +73,7 @@ impl CommitBuilder<'_> {
|
||||||
CommitBuilder {
|
CommitBuilder {
|
||||||
mut_repo,
|
mut_repo,
|
||||||
commit,
|
commit,
|
||||||
|
rng: settings.get_rng(),
|
||||||
rewrite_source: Some(predecessor.clone()),
|
rewrite_source: Some(predecessor.clone()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,7 +100,7 @@ impl CommitBuilder<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_new_change_id(mut self) -> Self {
|
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
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ use chrono::DateTime;
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use rand_chacha::ChaCha20Rng;
|
use rand_chacha::ChaCha20Rng;
|
||||||
|
|
||||||
use crate::backend::{Signature, Timestamp};
|
use crate::backend::{ChangeId, ObjectId, Signature, Timestamp};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct UserSettings {
|
pub struct UserSettings {
|
||||||
|
@ -178,8 +178,19 @@ impl UserSettings {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct JJRng(Mutex<ChaCha20Rng>);
|
pub struct JJRng(Mutex<ChaCha20Rng>);
|
||||||
impl JJRng {
|
impl JJRng {
|
||||||
/// Wraps Rng::gen but only requires an immutable reference.
|
pub fn new_change_id(&self) -> ChangeId {
|
||||||
pub fn gen<T>(&self) -> T
|
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
|
where
|
||||||
rand::distributions::Standard: rand::distributions::Distribution<T>,
|
rand::distributions::Standard: rand::distributions::Distribution<T>,
|
||||||
{
|
{
|
||||||
|
|
|
@ -61,6 +61,16 @@ fn test_log_with_or_without_diff() {
|
||||||
o (no description set)
|
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"]);
|
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "--no-graph"]);
|
||||||
insta::assert_snapshot!(stdout, @r###"
|
insta::assert_snapshot!(stdout, @r###"
|
||||||
a new commit
|
a new commit
|
||||||
|
|
Loading…
Reference in a new issue