Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: hide previous metrics by default and add option #43

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cmd/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::service::Service;
/// Show metrics changes
#[derive(clap::Parser, Debug, Default)]
pub(crate) struct CommandDiff {
/// When enabled, the metrics prior the provided range will be displayed
#[clap(long)]
keep_previous: bool,
/// Commit range, default to HEAD
///
/// Can use ranges like HEAD~2..HEAD
Expand All @@ -21,6 +24,7 @@ impl super::Executor for CommandDiff {
stdout: &mut Out,
) -> Result<(), crate::service::Error> {
let opts = crate::service::diff::Options {
keep_previous: self.keep_previous,
remote: String::from("origin"),
target: self.target,
};
Expand Down
58 changes: 55 additions & 3 deletions src/service/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use crate::entity::MetricStack;

#[derive(Debug)]
pub(crate) struct Options {
pub keep_previous: bool,
pub remote: String,
pub target: String,
}

fn show_diff<Out: Write>(
keep_previous: bool,
output: &mut Out,
before: MetricStack,
mut after: MetricStack,
Expand All @@ -29,9 +31,10 @@ fn show_diff<Out: Write>(
writeln!(output, "+ {next}")?;
}
}
_ => {
_ if keep_previous => {
writeln!(output, " {previous}")?;
}
_ => {}
}
}
for metric in after.into_metric_iter() {
Expand Down Expand Up @@ -71,7 +74,7 @@ impl<B: Backend> super::Service<B> {
}
};

show_diff(stdout, before, after)
show_diff(opts.keep_previous, stdout, before, after)
}
}

Expand All @@ -82,7 +85,7 @@ mod tests {
use crate::service::Service;

#[test]
fn should_render_diff_with_single_target() {
fn should_render_diff_with_single_target_keeping_previous() {
let mut stdout = Vec::new();
let backend = MockBackend::default();
backend.set_rev_parse("HEAD", RevParse::Single("aaaaaaa".into()));
Expand Down Expand Up @@ -114,6 +117,7 @@ value = 1.0
.diff(
&mut stdout,
&super::Options {
keep_previous: true,
remote: "origin".into(),
target: "HEAD".into(),
},
Expand All @@ -128,6 +132,53 @@ value = 1.0
);
}

#[test]
fn should_render_diff_with_single_target_without_previous() {
let mut stdout = Vec::new();
let backend = MockBackend::default();
backend.set_rev_parse("HEAD", RevParse::Single("aaaaaaa".into()));
backend.set_rev_list("aaaaaaa~1", ["aaaaaab", "aaaaaac", "aaaaaad", "aaaaaae"]);
backend.set_note(
"aaaaaaa",
NoteRef::remote_metrics("origin"),
r#"[[metrics]]
name = "first"
tags = {}
value = 2.0
"#,
);
backend.set_note(
"aaaaaac",
NoteRef::remote_metrics("origin"),
r#"[[metrics]]
name = "first"
tags = {}
value = 1.0

[[metrics]]
name = "second"
tags = {}
value = 1.0
"#,
);
Service::new(backend)
.diff(
&mut stdout,
&super::Options {
keep_previous: false,
remote: "origin".into(),
target: "HEAD".into(),
},
)
.unwrap();
assert_eq!(
String::from_utf8_lossy(&stdout),
r#"- first 1.0
+ first 2.0 (+100.00 %)
"#
);
}

#[test]
fn should_render_diff_with_range_target() {
let mut stdout = Vec::new();
Expand Down Expand Up @@ -184,6 +235,7 @@ value = 0.1
.diff(
&mut stdout,
&super::Options {
keep_previous: true,
remote: "origin".into(),
target: "HEAD~3..HEAD".into(),
},
Expand Down
Loading