Skip to content

Commit fa1e329

Browse files
author
tkuestner
committed
For brief (--short) and verbose version output, reverse the default order. Also add integration tests for both.
1 parent 7ad0f33 commit fa1e329

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

src/output.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ pub fn show_brief_version_list(
2727
sort_recent_first: bool,
2828
w: &mut dyn Write
2929
) -> Result<()> {
30-
for info in get_band_infos(archive, sort_recent_first)? {
31-
writeln!(w, "{}", info.id)?
30+
let mut band_ids = archive.list_band_ids()?;
31+
if sort_recent_first {
32+
band_ids.reverse();
33+
}
34+
for band_id in band_ids {
35+
writeln!(w, "{}", band_id)?
3236
}
3337
Ok(())
3438
}
@@ -39,8 +43,25 @@ pub fn show_verbose_version_list(
3943
show_sizes: bool,
4044
w: &mut dyn Write,
4145
) -> Result<()> {
42-
for info in get_band_infos(archive, sort_recent_first)? {
43-
let band_id = &info.id;
46+
let mut band_ids = archive.list_band_ids()?;
47+
if sort_recent_first {
48+
band_ids.reverse();
49+
}
50+
for band_id in band_ids {
51+
let band = match Band::open(&archive, &band_id) {
52+
Ok(band) => band,
53+
Err(e) => {
54+
ui::problem(&format!("Failed to open band {:?}: {:?}", band_id, e));
55+
continue;
56+
}
57+
};
58+
let info = match band.get_info() {
59+
Ok(info) => info,
60+
Err(e) => {
61+
ui::problem(&format!("Failed to read band tail {:?}: {:?}", band_id, e));
62+
continue;
63+
}
64+
};
4465
let is_complete_str = if info.is_closed {
4566
"complete"
4667
} else {
@@ -93,28 +114,3 @@ pub fn show_entry_names<E: Entry, I: Iterator<Item = E>>(it: I, w: &mut dyn Writ
93114
}
94115
Ok(())
95116
}
96-
97-
fn get_band_infos(archive: &Archive, sort_recent_first: bool) -> Result<Vec<band::Info>> {
98-
let mut band_infos = vec![];
99-
for band_id in archive.list_band_ids()? {
100-
let band = match Band::open(&archive, &band_id) {
101-
Ok(band) => band,
102-
Err(e) => {
103-
ui::problem(&format!("Failed to open band {:?}: {:?}", band_id, e));
104-
continue;
105-
}
106-
};
107-
let info = match band.get_info() {
108-
Ok(info) => info,
109-
Err(e) => {
110-
ui::problem(&format!("Failed to read band tail {:?}: {:?}", band_id, e));
111-
continue;
112-
}
113-
};
114-
band_infos.push(info);
115-
}
116-
if sort_recent_first {
117-
band_infos.sort_unstable_by_key(|info| std::cmp::Reverse(info.start_time));
118-
}
119-
Ok(band_infos)
120-
}

tests/cli.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,31 @@ fn size_exclude() {
483483
.success()
484484
.stdout("10\n");
485485
}
486+
487+
#[test]
488+
fn brief_versions_sort_recent_first() {
489+
let af = ScratchArchive::new();
490+
af.store_two_versions();
491+
492+
run_conserve()
493+
.args(&["versions", "--short", "--newest"])
494+
.arg(af.path())
495+
.assert()
496+
.success()
497+
.stderr(predicate::str::is_empty())
498+
.stdout("b0001\nb0000\n");
499+
}
500+
501+
#[test]
502+
fn verbose_versions_sort_recent_first() {
503+
let af = ScratchArchive::new();
504+
af.store_two_versions();
505+
506+
run_conserve()
507+
.args(&["versions", "--newest"])
508+
.arg(af.path())
509+
.assert()
510+
.success()
511+
.stderr(predicate::str::is_empty())
512+
.stdout(predicate::str::is_match("b0001.*\nb0000.*").unwrap());
513+
}

0 commit comments

Comments
 (0)