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 2, 2023
1 parent af24090 commit c97e704
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/src/commit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use uuid::Uuid;

use crate::backend;
use crate::backend::{BackendResult, ChangeId, CommitId, Signature, TreeId};
use crate::commit::Commit;
Expand All @@ -28,10 +26,21 @@ pub struct CommitBuilder<'repo, 'settings> {
rewrite_source: Option<Commit>,
}

#[cfg(test)]
/// TODO: This function is still used in some tests, but shouldn't be. Its
/// output is not reproducible. Its replacement is available in a follow-up commit.
/// output is not reproducible.
pub fn new_change_id_deprecated() -> ChangeId {
ChangeId::from_bytes(Uuid::new_v4().as_bytes())
ChangeId::from_bytes(uuid::Uuid::new_v4().as_bytes())
}

pub fn new_change_id(settings: &UserSettings) -> ChangeId {
let random_bytes = settings.random_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(),
)
}

impl CommitBuilder<'_, '_> {
Expand All @@ -47,7 +56,7 @@ impl CommitBuilder<'_, '_> {
parents,
predecessors: vec![],
root_tree: tree_id,
change_id: new_change_id_deprecated(),
change_id: new_change_id(settings),
description: String::new(),
author: signature.clone(),
committer: signature,
Expand Down Expand Up @@ -106,7 +115,7 @@ impl CommitBuilder<'_, '_> {
}

pub fn generate_new_change_id(mut self) -> Self {
self.commit.change_id = new_change_id_deprecated();
self.commit.change_id = new_change_id(self.settings);
self
}

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 c97e704

Please sign in to comment.