Skip to content

Commit 2d52c09

Browse files
aawsomesimonsan
andauthored
fix: properly finish progress bar in Repository::get_snapshot_group (#263)
Also adds a few more integration tests for snapshot gathering methods. --------- Co-authored-by: simonsan <[email protected]>
1 parent 41033bd commit 2d52c09

16 files changed

+574
-50
lines changed

crates/core/src/commands/snapshots.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
SnapshotFile,
99
},
1010
repository::{Open, Repository},
11+
Progress,
1112
};
1213

1314
/// Get the snapshots from the repository.
@@ -56,6 +57,7 @@ pub(crate) fn get_snapshot_group<P: ProgressBars, S: Open>(
5657
vec![item]
5758
}
5859
};
60+
p.finish();
5961

6062
Ok(groups)
6163
}

crates/core/tests/integration.rs

Lines changed: 70 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ use rstest::{fixture, rstest};
3636
use rustic_core::{
3737
repofile::SnapshotFile, BackupOptions, CheckOptions, ConfigOptions, FindMatches, FindNode,
3838
FullIndex, IndexedFull, IndexedStatus, KeyOptions, LimitOption, LsOptions, NoProgressBars,
39-
OpenStatus, PathList, Repository, RepositoryBackends, RepositoryOptions, RusticResult,
39+
OpenStatus, ParentOptions, PathList, Repository, RepositoryBackends, RepositoryOptions,
40+
RusticResult, SnapshotGroupCriterion, SnapshotOptions, StringList,
4041
};
4142
use rustic_core::{
4243
repofile::{Metadata, Node},
@@ -98,36 +99,38 @@ fn handle_option(val: Content, _: ContentPath<'_>) -> String {
9899
}
99100

100101
#[fixture]
101-
fn insta_summary_redaction() -> Settings {
102+
fn insta_snapshotfile_redaction() -> Settings {
102103
let mut settings = Settings::clone_current();
103104

104-
settings.add_redaction(".tree", "[tree_id]");
105-
settings.add_dynamic_redaction(".program_version", |val, _| {
105+
settings.add_redaction(".**.tree", "[tree_id]");
106+
settings.add_dynamic_redaction(".**.program_version", |val, _| {
106107
val.resolve_inner()
107108
.as_str()
108109
.map_or("[program_version]".to_string(), |v| {
109110
v.replace(env!("CARGO_PKG_VERSION"), "[rustic_core_version]")
110111
})
111112
});
112-
settings.add_redaction(".time", "[time]");
113-
settings.add_dynamic_redaction(".parent", handle_option);
114-
settings.add_redaction(".tags", "[tags]");
115-
settings.add_redaction(".id", "[id]");
116-
settings.add_redaction(".summary.backup_start", "[backup_start]");
117-
settings.add_redaction(".summary.backup_end", "[backup_end]");
118-
settings.add_redaction(".summary.backup_duration", "[backup_duration]");
119-
settings.add_redaction(".summary.total_duration", "[total_duration]");
120-
settings.add_redaction(".summary.data_added", "[data_added]");
121-
settings.add_redaction(".summary.data_added_packed", "[data_added_packed]");
113+
settings.add_redaction(".**.time", "[time]");
114+
settings.add_dynamic_redaction(".**.parent", handle_option);
115+
settings.add_redaction(".**.id", "[id]");
116+
settings.add_redaction(".**.original", "[original]");
117+
settings.add_redaction(".**.hostname", "[hostname]");
118+
settings.add_redaction(".**.command", "[command]");
119+
settings.add_redaction(".**.summary.backup_start", "[backup_start]");
120+
settings.add_redaction(".**.summary.backup_end", "[backup_end]");
121+
settings.add_redaction(".**.summary.backup_duration", "[backup_duration]");
122+
settings.add_redaction(".**.summary.total_duration", "[total_duration]");
123+
settings.add_redaction(".**.summary.data_added", "[data_added]");
124+
settings.add_redaction(".**.summary.data_added_packed", "[data_added_packed]");
122125
settings.add_redaction(
123-
".summary.total_dirsize_processed",
126+
".**.summary.total_dirsize_processed",
124127
"[total_dirsize_processed]",
125128
);
126129
settings.add_redaction(
127-
".summary.data_added_trees_packed",
130+
".**.summary.data_added_trees_packed",
128131
"[data_added_trees_packed]",
129132
);
130-
settings.add_redaction(".summary.data_added_trees", "[data_added_trees]");
133+
settings.add_redaction(".**.summary.data_added_trees", "[data_added_trees]");
131134

132135
settings
133136
}
@@ -176,7 +179,7 @@ fn assert_with_win<T: Serialize>(test: &str, snap: T) {
176179
fn test_backup_with_tar_gz_passes(
177180
tar_gz_testdata: Result<TestSource>,
178181
set_up_repo: Result<RepoOpen>,
179-
insta_summary_redaction: Settings,
182+
insta_snapshotfile_redaction: Settings,
180183
insta_node_redaction: Settings,
181184
) -> Result<()> {
182185
// uncomment for logging output
@@ -196,7 +199,7 @@ fn test_backup_with_tar_gz_passes(
196199
// We can also bind to scope ( https://docs.rs/insta/latest/insta/struct.Settings.html#method.bind_to_scope )
197200
// But I think that can get messy with a lot of tests, also checking which settings are currently applied
198201
// will be probably harder
199-
insta_summary_redaction.bind(|| {
202+
insta_snapshotfile_redaction.bind(|| {
200203
assert_with_win("backup-tar-summary-first", &first_snapshot);
201204
});
202205

@@ -223,21 +226,43 @@ fn test_backup_with_tar_gz_passes(
223226
// second backup
224227
let second_snapshot = repo.backup(&opts, paths, SnapshotFile::default())?;
225228

226-
insta_summary_redaction.bind(|| {
229+
insta_snapshotfile_redaction.bind(|| {
227230
assert_with_win("backup-tar-summary-second", &second_snapshot);
228231
});
229232

230233
assert_eq!(second_snapshot.parent, Some(first_snapshot.id));
231234
assert_eq!(first_snapshot.tree, second_snapshot.tree);
232235

236+
// pack files should be unchanged
237+
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
238+
assert_eq!(packs1, packs2);
239+
240+
// re-read index
241+
let repo = repo.to_indexed_ids()?;
242+
// third backup with tags and explicitely given parent
243+
let snap = SnapshotOptions::default()
244+
.tag([StringList::from_str("a,b")?])
245+
.to_snapshot()?;
246+
let opts = opts.parent_opts(ParentOptions::default().parent(second_snapshot.id.to_string()));
247+
let third_snapshot = repo.backup(&opts, paths, snap)?;
248+
249+
insta_snapshotfile_redaction.bind(|| {
250+
assert_with_win("backup-tar-summary-third", &third_snapshot);
251+
});
252+
assert_eq!(third_snapshot.parent, Some(second_snapshot.id));
253+
assert_eq!(third_snapshot.tree, second_snapshot.tree);
254+
233255
// get all snapshots and check them
234256
let mut all_snapshots = repo.get_all_snapshots()?;
235257
all_snapshots.sort_unstable();
236-
assert_eq!(vec![first_snapshot, second_snapshot], all_snapshots);
258+
assert_eq!(
259+
vec![first_snapshot, second_snapshot, third_snapshot],
260+
all_snapshots
261+
);
237262

238263
// pack files should be unchanged
239-
let packs2: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
240-
assert_eq!(packs1, packs2);
264+
let packs3: Vec<_> = repo.list(rustic_core::FileType::Pack)?.collect();
265+
assert_eq!(packs1, packs3);
241266

242267
// Check if snapshots can be retrieved
243268
let mut ids: Vec<_> = all_snapshots.iter().map(|sn| sn.id.to_string()).collect();
@@ -250,14 +275,33 @@ fn test_backup_with_tar_gz_passes(
250275
let snaps = repo.get_snapshots(&ids)?;
251276
assert_eq!(snaps, all_snapshots);
252277

278+
// get snapshot group
279+
let group_by = SnapshotGroupCriterion::new().tags(true);
280+
let mut groups = repo.get_snapshot_group(&[], group_by, |_| true)?;
281+
282+
// sort groups to get unique result
283+
groups.iter_mut().for_each(|(_, snaps)| snaps.sort());
284+
groups.sort_by_key(|(group, _)| group.tags.clone());
285+
286+
insta_snapshotfile_redaction.bind(|| {
287+
assert_with_win("backup-tar-groups", &groups);
288+
});
289+
290+
// filter snapshots by tag
291+
let filter = |snap: &SnapshotFile| snap.tags.contains("a");
292+
let snaps = repo.get_matching_snapshots(filter)?;
293+
insta_snapshotfile_redaction.bind(|| {
294+
assert_with_win("backup-tar-matching-snaps", &snaps);
295+
});
296+
253297
Ok(())
254298
}
255299

256300
#[rstest]
257301
fn test_backup_dry_run_with_tar_gz_passes(
258302
tar_gz_testdata: Result<TestSource>,
259303
set_up_repo: Result<RepoOpen>,
260-
insta_summary_redaction: Settings,
304+
insta_snapshotfile_redaction: Settings,
261305
insta_node_redaction: Settings,
262306
) -> Result<()> {
263307
// Fixtures
@@ -273,7 +317,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
273317
// dry-run backup
274318
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;
275319

276-
insta_summary_redaction.bind(|| {
320+
insta_snapshotfile_redaction.bind(|| {
277321
assert_with_win("dryrun-tar-summary-first", &snap_dry_run);
278322
});
279323

@@ -305,7 +349,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
305349
let opts = opts.dry_run(true);
306350
let snap_dry_run = repo.backup(&opts, paths, SnapshotFile::default())?;
307351

308-
insta_summary_redaction.bind(|| {
352+
insta_snapshotfile_redaction.bind(|| {
309353
assert_with_win("dryrun-tar-summary-second", &snap_dry_run);
310354
});
311355

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
source: crates/core/tests/integration.rs
3+
expression: snap
4+
---
5+
[
6+
(SnapshotGroup(
7+
tags: Some(StringList([])),
8+
), [
9+
SnapshotFile(
10+
time: "[time]",
11+
program_version: "rustic [rustic_core_version]",
12+
tree: "[tree_id]",
13+
paths: StringList([
14+
"test",
15+
]),
16+
hostname: "[hostname]",
17+
username: "",
18+
uid: 0,
19+
gid: 0,
20+
tags: StringList([]),
21+
original: "[original]",
22+
summary: Some(SnapshotSummary(
23+
files_new: 73,
24+
files_changed: 0,
25+
files_unmodified: 0,
26+
total_files_processed: 73,
27+
total_bytes_processed: 1125674,
28+
dirs_new: 6,
29+
dirs_changed: 0,
30+
dirs_unmodified: 0,
31+
total_dirs_processed: 6,
32+
total_dirsize_processed: "[total_dirsize_processed]",
33+
data_blobs: 70,
34+
tree_blobs: 6,
35+
data_added: "[data_added]",
36+
data_added_packed: "[data_added_packed]",
37+
data_added_files: 1125653,
38+
data_added_files_packed: 78740,
39+
data_added_trees: "[data_added_trees]",
40+
data_added_trees_packed: "[data_added_trees_packed]",
41+
command: "[command]",
42+
backup_start: "[backup_start]",
43+
backup_end: "[backup_end]",
44+
backup_duration: "[backup_duration]",
45+
total_duration: "[total_duration]",
46+
)),
47+
id: "[id]",
48+
),
49+
SnapshotFile(
50+
time: "[time]",
51+
program_version: "rustic [rustic_core_version]",
52+
parent: "[some]",
53+
tree: "[tree_id]",
54+
paths: StringList([
55+
"test",
56+
]),
57+
hostname: "[hostname]",
58+
username: "",
59+
uid: 0,
60+
gid: 0,
61+
tags: StringList([]),
62+
original: "[original]",
63+
summary: Some(SnapshotSummary(
64+
files_new: 0,
65+
files_changed: 0,
66+
files_unmodified: 73,
67+
total_files_processed: 73,
68+
total_bytes_processed: 1125682,
69+
dirs_new: 0,
70+
dirs_changed: 0,
71+
dirs_unmodified: 6,
72+
total_dirs_processed: 6,
73+
total_dirsize_processed: "[total_dirsize_processed]",
74+
data_blobs: 0,
75+
tree_blobs: 0,
76+
data_added: "[data_added]",
77+
data_added_packed: "[data_added_packed]",
78+
data_added_files: 0,
79+
data_added_files_packed: 0,
80+
data_added_trees: "[data_added_trees]",
81+
data_added_trees_packed: "[data_added_trees_packed]",
82+
command: "[command]",
83+
backup_start: "[backup_start]",
84+
backup_end: "[backup_end]",
85+
backup_duration: "[backup_duration]",
86+
total_duration: "[total_duration]",
87+
)),
88+
id: "[id]",
89+
),
90+
]),
91+
(SnapshotGroup(
92+
tags: Some(StringList([
93+
"a",
94+
"b",
95+
])),
96+
), [
97+
SnapshotFile(
98+
time: "[time]",
99+
program_version: "rustic [rustic_core_version]",
100+
parent: "[some]",
101+
tree: "[tree_id]",
102+
paths: StringList([
103+
"test",
104+
]),
105+
hostname: "[hostname]",
106+
username: "",
107+
uid: 0,
108+
gid: 0,
109+
tags: StringList([
110+
"a",
111+
"b",
112+
]),
113+
original: "[original]",
114+
summary: Some(SnapshotSummary(
115+
files_new: 0,
116+
files_changed: 0,
117+
files_unmodified: 73,
118+
total_files_processed: 73,
119+
total_bytes_processed: 1125682,
120+
dirs_new: 0,
121+
dirs_changed: 0,
122+
dirs_unmodified: 6,
123+
total_dirs_processed: 6,
124+
total_dirsize_processed: "[total_dirsize_processed]",
125+
data_blobs: 0,
126+
tree_blobs: 0,
127+
data_added: "[data_added]",
128+
data_added_packed: "[data_added_packed]",
129+
data_added_files: 0,
130+
data_added_files_packed: 0,
131+
data_added_trees: "[data_added_trees]",
132+
data_added_trees_packed: "[data_added_trees_packed]",
133+
command: "[command]",
134+
backup_start: "[backup_start]",
135+
backup_end: "[backup_end]",
136+
backup_duration: "[backup_duration]",
137+
total_duration: "[total_duration]",
138+
)),
139+
id: "[id]",
140+
),
141+
]),
142+
]

0 commit comments

Comments
 (0)