mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
tests: pass timestamps via env vars for reproducible hashes
This patch introduces a `JJ_TIMESTAMP` environment variable that lets us specify the timestamp to use in tests. It also updates the tests to use it, which means we get to simplify the tests a lot now that that the hashes are predictable.
This commit is contained in:
parent
b7942ad55a
commit
0c6d89581e
5 changed files with 82 additions and 52 deletions
|
@ -233,10 +233,15 @@ pub struct Timestamp {
|
||||||
|
|
||||||
impl Timestamp {
|
impl Timestamp {
|
||||||
pub fn now() -> Self {
|
pub fn now() -> Self {
|
||||||
let now = chrono::offset::Local::now();
|
Self::from_datetime(chrono::offset::Local::now())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_datetime<Tz: chrono::TimeZone<Offset = chrono::offset::FixedOffset>>(
|
||||||
|
datetime: chrono::DateTime<Tz>,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
timestamp: MillisSinceEpoch(now.timestamp_millis() as u64),
|
timestamp: MillisSinceEpoch(datetime.timestamp_millis() as u64),
|
||||||
tz_offset: now.offset().local_minus_utc() / 60,
|
tz_offset: datetime.offset().local_minus_utc() / 60,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,8 +12,10 @@
|
||||||
// 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 std::env;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use chrono::DateTime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::backend;
|
use crate::backend;
|
||||||
|
@ -35,8 +37,13 @@ pub fn new_change_id() -> ChangeId {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn signature(settings: &UserSettings) -> Signature {
|
pub fn signature(settings: &UserSettings) -> Signature {
|
||||||
// TODO: check if it's slow to get the timezone etc for every signature
|
let timestamp = match env::var("JJ_TIMESTAMP") {
|
||||||
let timestamp = Timestamp::now();
|
Ok(timestamp_str) => match DateTime::parse_from_rfc3339(×tamp_str) {
|
||||||
|
Ok(datetime) => Timestamp::from_datetime(datetime),
|
||||||
|
Err(_) => Timestamp::now(),
|
||||||
|
},
|
||||||
|
Err(_) => Timestamp::now(),
|
||||||
|
};
|
||||||
Signature {
|
Signature {
|
||||||
name: settings.user_name(),
|
name: settings.user_name(),
|
||||||
email: settings.user_email(),
|
email: settings.user_email(),
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
// 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 std::cell::RefCell;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
@ -22,6 +23,7 @@ pub struct TestEnvironment {
|
||||||
_temp_dir: TempDir,
|
_temp_dir: TempDir,
|
||||||
env_root: PathBuf,
|
env_root: PathBuf,
|
||||||
home_dir: PathBuf,
|
home_dir: PathBuf,
|
||||||
|
command_number: RefCell<i64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TestEnvironment {
|
impl Default for TestEnvironment {
|
||||||
|
@ -33,6 +35,7 @@ impl Default for TestEnvironment {
|
||||||
_temp_dir: tmp_dir,
|
_temp_dir: tmp_dir,
|
||||||
env_root,
|
env_root,
|
||||||
home_dir,
|
home_dir,
|
||||||
|
command_number: RefCell::new(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +46,11 @@ impl TestEnvironment {
|
||||||
cmd.current_dir(current_dir);
|
cmd.current_dir(current_dir);
|
||||||
cmd.args(args);
|
cmd.args(args);
|
||||||
cmd.env("HOME", self.home_dir.to_str().unwrap());
|
cmd.env("HOME", self.home_dir.to_str().unwrap());
|
||||||
|
let timestamp = chrono::DateTime::parse_from_rfc3339("2001-02-03T04:05:06+07:00").unwrap();
|
||||||
|
let mut command_number = self.command_number.borrow_mut();
|
||||||
|
*command_number += 1;
|
||||||
|
let timestamp = timestamp + chrono::Duration::seconds(*command_number);
|
||||||
|
cmd.env("JJ_TIMESTAMP", timestamp.to_rfc3339());
|
||||||
cmd
|
cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,59 +12,64 @@
|
||||||
// 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 jujutsu::testutils::{capture_matches, TestEnvironment};
|
use jujutsu::testutils::TestEnvironment;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn smoke_test() {
|
fn smoke_test() {
|
||||||
let test_env = TestEnvironment::default();
|
let test_env = TestEnvironment::default();
|
||||||
test_env
|
test_env
|
||||||
.jj_cmd(test_env.env_root(), &["init", "repo"])
|
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
let repo_path = test_env.env_root().join("repo");
|
let repo_path = test_env.env_root().join("repo");
|
||||||
// Check the output of `jj status` right after initializing repo
|
// Check the output of `jj status` right after initializing repo
|
||||||
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success();
|
let expected_output =
|
||||||
let output_regex = "^Parent commit: 000000000000 \nWorking copy : ([[:xdigit:]]+) \nThe \
|
"Parent commit: 000000000000 \nWorking copy : 1d1984a23811 \nThe working copy is clean\n";
|
||||||
working copy is clean\n$";
|
test_env
|
||||||
let (_, matches) = capture_matches(assert, output_regex);
|
.jj_cmd(&repo_path, &["status"])
|
||||||
let wc_hex_id_empty = matches[0].clone();
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(expected_output);
|
||||||
|
|
||||||
// Write some files and check the output of `jj status`
|
// Write some files and check the output of `jj status`
|
||||||
std::fs::write(repo_path.join("file1"), "file1").unwrap();
|
std::fs::write(repo_path.join("file1"), "file1").unwrap();
|
||||||
std::fs::write(repo_path.join("file2"), "file2").unwrap();
|
std::fs::write(repo_path.join("file2"), "file2").unwrap();
|
||||||
std::fs::write(repo_path.join("file3"), "file3").unwrap();
|
std::fs::write(repo_path.join("file3"), "file3").unwrap();
|
||||||
|
|
||||||
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success();
|
// The working copy's ID should have changed
|
||||||
let output_regex = "^Parent commit: 000000000000 \nWorking copy : ([[:xdigit:]]+) \nWorking \
|
let expected_output = "Parent commit: 000000000000 \nWorking copy : 5e60c5091e43 \nWorking \
|
||||||
copy changes:
|
copy changes:
|
||||||
A file1
|
A file1
|
||||||
A file2
|
A file2
|
||||||
A file3
|
A file3
|
||||||
$";
|
";
|
||||||
let (_, matches) = capture_matches(assert, output_regex);
|
test_env
|
||||||
let wc_hex_id_non_empty = matches[0].clone();
|
.jj_cmd(&repo_path, &["status"])
|
||||||
|
.assert()
|
||||||
// The working copy's id should have changed
|
.success()
|
||||||
assert_ne!(wc_hex_id_non_empty, wc_hex_id_empty);
|
.stdout(expected_output);
|
||||||
|
|
||||||
// Running `jj status` again gives the same output
|
// Running `jj status` again gives the same output
|
||||||
let assert = test_env.jj_cmd(&repo_path, &["status"]).assert().success();
|
test_env
|
||||||
let (_, matches) = capture_matches(assert, output_regex);
|
.jj_cmd(&repo_path, &["status"])
|
||||||
let wc_hex_id_again = matches[0].clone();
|
.assert()
|
||||||
assert_eq!(wc_hex_id_again, wc_hex_id_non_empty);
|
.success()
|
||||||
|
.stdout(expected_output);
|
||||||
|
|
||||||
// Add a commit description
|
// Add a commit description
|
||||||
let assert = test_env
|
let expected_output = "Working copy now at: 6f13b3e41065 add some files\n";
|
||||||
|
test_env
|
||||||
.jj_cmd(&repo_path, &["describe", "-m", "add some files"])
|
.jj_cmd(&repo_path, &["describe", "-m", "add some files"])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success()
|
||||||
let output_regex = "^Working copy now at: [[:xdigit:]]+ add some files
|
.stdout(expected_output);
|
||||||
$";
|
|
||||||
assert.stdout(predicates::str::is_match(output_regex).unwrap());
|
|
||||||
|
|
||||||
// Close the commit
|
// Close the commit
|
||||||
let assert = test_env.jj_cmd(&repo_path, &["close"]).assert().success();
|
let expected_output = "Working copy now at: 6ff8a22d8ce1 \n";
|
||||||
let output_regex = "^Working copy now at: [[:xdigit:]]+ \n$";
|
test_env
|
||||||
assert.stdout(predicates::str::is_match(output_regex).unwrap());
|
.jj_cmd(&repo_path, &["close"])
|
||||||
|
.assert()
|
||||||
|
.success()
|
||||||
|
.stdout(expected_output);
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,62 +12,67 @@
|
||||||
// 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 jujutsu::testutils::{get_stdout_string, TestEnvironment};
|
use jujutsu::testutils::TestEnvironment;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_commit_working_copy() {
|
fn test_no_commit_working_copy() {
|
||||||
let test_env = TestEnvironment::default();
|
let test_env = TestEnvironment::default();
|
||||||
test_env
|
test_env
|
||||||
.jj_cmd(test_env.env_root(), &["init", "repo"])
|
.jj_cmd(test_env.env_root(), &["init", "repo", "--git"])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success();
|
||||||
|
|
||||||
let repo_path = test_env.env_root().join("repo");
|
let repo_path = test_env.env_root().join("repo");
|
||||||
|
|
||||||
std::fs::write(repo_path.join("file"), "initial").unwrap();
|
std::fs::write(repo_path.join("file"), "initial").unwrap();
|
||||||
let assert = test_env
|
let expected_output = "@ 1e9ff0ea7220c37a1d2c4aab153e238c12ff3cd0
|
||||||
|
o 0000000000000000000000000000000000000000
|
||||||
|
";
|
||||||
|
test_env
|
||||||
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
|
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success()
|
||||||
let initial_commit_id_hex = get_stdout_string(&assert);
|
.stdout(expected_output);
|
||||||
|
|
||||||
// Modify the file. With --no-commit-working-copy, we still get the same commit
|
// Modify the file. With --no-commit-working-copy, we still get the same commit
|
||||||
// ID.
|
// ID.
|
||||||
std::fs::write(repo_path.join("file"), "modified").unwrap();
|
std::fs::write(repo_path.join("file"), "modified").unwrap();
|
||||||
let assert = test_env
|
test_env
|
||||||
.jj_cmd(
|
.jj_cmd(
|
||||||
&repo_path,
|
&repo_path,
|
||||||
&["log", "-T", "commit_id", "--no-commit-working-copy"],
|
&["log", "-T", "commit_id", "--no-commit-working-copy"],
|
||||||
)
|
)
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success()
|
||||||
let still_initial_commit_id_hex = get_stdout_string(&assert);
|
.stdout(expected_output);
|
||||||
assert_eq!(still_initial_commit_id_hex, initial_commit_id_hex);
|
|
||||||
// But without --no-commit-working-copy, we get a new commit ID.
|
// But without --no-commit-working-copy, we get a new commit ID.
|
||||||
let assert = test_env
|
let expected_output = "@ cc12440b719c67fcd8c55848eb345f67b6e2d9f1
|
||||||
|
o 0000000000000000000000000000000000000000
|
||||||
|
";
|
||||||
|
test_env
|
||||||
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
|
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
|
||||||
.assert()
|
.assert()
|
||||||
.success();
|
.success()
|
||||||
let modified_commit_id_hex = get_stdout_string(&assert);
|
.stdout(expected_output);
|
||||||
assert_ne!(modified_commit_id_hex, initial_commit_id_hex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_repo_arg_with_init() {
|
fn test_repo_arg_with_init() {
|
||||||
let test_env = TestEnvironment::default();
|
let test_env = TestEnvironment::default();
|
||||||
let assert = test_env
|
test_env
|
||||||
.jj_cmd(test_env.env_root(), &["init", "-R=.", "repo"])
|
.jj_cmd(test_env.env_root(), &["init", "-R=.", "repo"])
|
||||||
.assert()
|
.assert()
|
||||||
.failure();
|
.failure()
|
||||||
assert.stdout("Error: '--repository' cannot be used with 'init'\n");
|
.stdout("Error: '--repository' cannot be used with 'init'\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_repo_arg_with_git_clone() {
|
fn test_repo_arg_with_git_clone() {
|
||||||
let test_env = TestEnvironment::default();
|
let test_env = TestEnvironment::default();
|
||||||
let assert = test_env
|
test_env
|
||||||
.jj_cmd(test_env.env_root(), &["git", "clone", "-R=.", "remote"])
|
.jj_cmd(test_env.env_root(), &["git", "clone", "-R=.", "remote"])
|
||||||
.assert()
|
.assert()
|
||||||
.failure();
|
.failure()
|
||||||
assert.stdout("Error: '--repository' cannot be used with 'git clone'\n");
|
.stdout("Error: '--repository' cannot be used with 'git clone'\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue