mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-27 14:47:05 +00:00
operations: allow overriding timestamps for tests
It would be nice to be able to use snapshot testing and not have to parse the output of `jj op log`. This patch lets us do that by providing a new environment variable and config for overriding the timestamps. Unlike `operation.hostname` and `operation.username`, these are only meant for tests.
This commit is contained in:
parent
7c99fa0750
commit
802e21bf25
4 changed files with 26 additions and 9 deletions
|
@ -29,15 +29,19 @@ pub struct RepoSettings {
|
|||
_config: config::Config,
|
||||
}
|
||||
|
||||
impl UserSettings {
|
||||
pub fn from_config(config: config::Config) -> Self {
|
||||
let timestamp = match config.get_string("user.timestamp") {
|
||||
fn get_timestamp_config(config: &config::Config, key: &str) -> Option<Timestamp> {
|
||||
match config.get_string(key) {
|
||||
Ok(timestamp_str) => match DateTime::parse_from_rfc3339(×tamp_str) {
|
||||
Ok(datetime) => Some(Timestamp::from_datetime(datetime)),
|
||||
Err(_) => None,
|
||||
},
|
||||
Err(_) => None,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl UserSettings {
|
||||
pub fn from_config(config: config::Config) -> Self {
|
||||
let timestamp = get_timestamp_config(&config, "user.timestamp");
|
||||
UserSettings { config, timestamp }
|
||||
}
|
||||
|
||||
|
@ -88,6 +92,10 @@ impl UserSettings {
|
|||
"(no email configured)"
|
||||
}
|
||||
|
||||
pub fn operation_timestamp(&self) -> Option<Timestamp> {
|
||||
get_timestamp_config(&self.config, "operation.timestamp")
|
||||
}
|
||||
|
||||
pub fn operation_hostname(&self) -> String {
|
||||
self.config
|
||||
.get_string("operation.hostname")
|
||||
|
|
|
@ -28,6 +28,7 @@ pub struct Transaction {
|
|||
repo: Option<MutableRepo>,
|
||||
parent_ops: Vec<Operation>,
|
||||
op_metadata: OperationMetadata,
|
||||
end_time: Option<Timestamp>,
|
||||
}
|
||||
|
||||
impl Transaction {
|
||||
|
@ -38,10 +39,12 @@ impl Transaction {
|
|||
) -> Transaction {
|
||||
let parent_ops = vec![mut_repo.base_repo().operation().clone()];
|
||||
let op_metadata = create_op_metadata(user_settings, description.to_string());
|
||||
let end_time = user_settings.operation_timestamp();
|
||||
Transaction {
|
||||
repo: Some(mut_repo),
|
||||
parent_ops,
|
||||
op_metadata,
|
||||
end_time,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +100,7 @@ impl Transaction {
|
|||
let index = base_repo.index_store().write_index(mut_index).unwrap();
|
||||
|
||||
let view_id = base_repo.op_store().write_view(view.store_view()).unwrap();
|
||||
self.op_metadata.end_time = Timestamp::now();
|
||||
self.op_metadata.end_time = self.end_time.unwrap_or_else(Timestamp::now);
|
||||
let parents = self.parent_ops.iter().map(|op| op.id().clone()).collect();
|
||||
let store_operation = op_store::Operation {
|
||||
view_id,
|
||||
|
@ -119,7 +122,9 @@ impl Transaction {
|
|||
}
|
||||
|
||||
pub fn create_op_metadata(user_settings: &UserSettings, description: String) -> OperationMetadata {
|
||||
let start_time = Timestamp::now();
|
||||
let start_time = user_settings
|
||||
.operation_timestamp()
|
||||
.unwrap_or_else(Timestamp::now);
|
||||
let end_time = start_time.clone();
|
||||
let hostname = user_settings.operation_hostname();
|
||||
let username = user_settings.operation_username();
|
||||
|
|
|
@ -78,6 +78,9 @@ fn env_overrides() -> config::Config {
|
|||
if let Ok(value) = env::var("JJ_TIMESTAMP") {
|
||||
builder = builder.set_override("user.timestamp", value).unwrap();
|
||||
}
|
||||
if let Ok(value) = env::var("JJ_OP_TIMESTAMP") {
|
||||
builder = builder.set_override("operation.timestamp", value).unwrap();
|
||||
}
|
||||
if let Ok(value) = env::var("JJ_OP_HOSTNAME") {
|
||||
builder = builder.set_override("operation.hostname", value).unwrap();
|
||||
}
|
||||
|
|
|
@ -72,6 +72,7 @@ impl TestEnvironment {
|
|||
cmd.env("JJ_TIMESTAMP", timestamp.to_rfc3339());
|
||||
cmd.env("JJ_USER", "Test User");
|
||||
cmd.env("JJ_EMAIL", "test.user@example.com");
|
||||
cmd.env("JJ_OP_TIMESTAMP", timestamp.to_rfc3339());
|
||||
cmd.env("JJ_OP_HOSTNAME", "host.example.com");
|
||||
cmd.env("JJ_OP_USERNAME", "test-username");
|
||||
cmd
|
||||
|
|
Loading…
Reference in a new issue