25
25
26
26
use anyhow:: Result ;
27
27
use flate2:: read:: GzDecoder ;
28
+ use globset:: Glob ;
28
29
use insta:: internals:: { Content , ContentPath } ;
29
30
use insta:: { assert_ron_snapshot, Settings } ;
30
31
use pretty_assertions:: assert_eq;
31
32
use rstest:: fixture;
32
33
use rstest:: rstest;
34
+ use rustic_core:: repofile:: { Metadata , Node } ;
33
35
use rustic_core:: {
34
- repofile:: SnapshotFile , BackupOptions , ConfigOptions , KeyOptions , NoProgressBars , OpenStatus ,
35
- PathList , Repository , RepositoryBackends , RepositoryOptions ,
36
+ repofile:: SnapshotFile , BackupOptions , ConfigOptions , KeyOptions , LsOptions , NoProgressBars ,
37
+ OpenStatus , PathList , Repository , RepositoryBackends , RepositoryOptions ,
36
38
} ;
37
- use serde_derive:: Serialize ;
39
+ use rustic_core:: { FindMatches , FindNode , RusticResult } ;
40
+ use serde:: Serialize ;
38
41
39
42
use rustic_testing:: backend:: in_memory_backend:: InMemoryBackend ;
40
43
44
+ use std:: ffi:: OsStr ;
41
45
use std:: {
42
46
env,
43
47
fs:: File ,
@@ -153,6 +157,13 @@ fn tar_gz_testdata() -> Result<TestSource> {
153
157
Ok ( TestSource :: new ( dir) )
154
158
}
155
159
160
+ // helper function to do windows-specific snapshots (needed e.g. if paths are contained in the snapshot)
161
+ fn assert_with_win < T : Serialize > ( test : & str , snap : T ) {
162
+ #[ cfg( windows) ]
163
+ assert_ron_snapshot ! ( format!( "{test}-windows" ) , snap) ;
164
+ #[ cfg( not( windows) ) ]
165
+ assert_ron_snapshot ! ( format!( "{test}-nix" ) , snap) ;
166
+ }
156
167
// Parts of the snapshot summary we want to test against references
157
168
//
158
169
// # Note
@@ -189,13 +200,7 @@ fn test_backup_with_tar_gz_passes(
189
200
// But I think that can get messy with a lot of tests, also checking which settings are currently applied
190
201
// will be probably harder
191
202
insta_summary_redaction. bind ( || {
192
- #[ cfg( windows) ]
193
- assert_ron_snapshot ! (
194
- "backup-tar-summary-first-windows" ,
195
- TestSummary ( & first_snapshot)
196
- ) ;
197
- #[ cfg( not( windows) ) ]
198
- assert_ron_snapshot ! ( "backup-tar-summary-first-nix" , TestSummary ( & first_snapshot) ) ;
203
+ assert_with_win ( "backup-tar-summary-first" , TestSummary ( & first_snapshot) ) ;
199
204
} ) ;
200
205
201
206
assert_eq ! ( first_snapshot. parent, None ) ;
@@ -207,10 +212,7 @@ fn test_backup_with_tar_gz_passes(
207
212
let tree: rustic_core:: repofile:: Tree = repo. get_tree ( & tree. subtree . expect ( "Sub tree" ) ) ?;
208
213
209
214
insta_tree_redaction. bind ( || {
210
- #[ cfg( windows) ]
211
- assert_ron_snapshot ! ( "backup-tar-tree-windows" , tree) ;
212
- #[ cfg( not( windows) ) ]
213
- assert_ron_snapshot ! ( "backup-tar-tree-nix" , tree) ;
215
+ assert_with_win ( "backup-tar-tree" , tree) ;
214
216
} ) ;
215
217
216
218
// get all snapshots and check them
@@ -225,16 +227,7 @@ fn test_backup_with_tar_gz_passes(
225
227
let second_snapshot = repo. backup ( & opts, paths, SnapshotFile :: default ( ) ) ?;
226
228
227
229
insta_summary_redaction. bind ( || {
228
- #[ cfg( windows) ]
229
- assert_ron_snapshot ! (
230
- "backup-tar-summary-second-windows" ,
231
- TestSummary ( & second_snapshot)
232
- ) ;
233
- #[ cfg( not( windows) ) ]
234
- assert_ron_snapshot ! (
235
- "backup-tar-summary-second-nix" ,
236
- TestSummary ( & second_snapshot)
237
- ) ;
230
+ assert_with_win ( "backup-tar-summary-second" , TestSummary ( & second_snapshot) ) ;
238
231
} ) ;
239
232
240
233
assert_eq ! ( second_snapshot. parent, Some ( first_snapshot. id) ) ;
@@ -284,13 +277,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
284
277
let snap_dry_run = repo. backup ( & opts, paths, SnapshotFile :: default ( ) ) ?;
285
278
286
279
insta_summary_redaction. bind ( || {
287
- #[ cfg( windows) ]
288
- assert_ron_snapshot ! (
289
- "dryrun-tar-summary-first-windows" ,
290
- TestSummary ( & snap_dry_run)
291
- ) ;
292
- #[ cfg( not( windows) ) ]
293
- assert_ron_snapshot ! ( "dryrun-tar-summary-first-nix" , TestSummary ( & snap_dry_run) ) ;
280
+ assert_with_win ( "dryrun-tar-summary-first" , TestSummary ( & snap_dry_run) ) ;
294
281
} ) ;
295
282
296
283
// check that repo is still empty
@@ -312,10 +299,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
312
299
let tree = repo. get_tree ( & tree. subtree . expect ( "Sub tree" ) ) ?;
313
300
314
301
insta_tree_redaction. bind ( || {
315
- #[ cfg( windows) ]
316
- assert_ron_snapshot ! ( "dryrun-tar-tree-windows" , tree) ;
317
- #[ cfg( not( windows) ) ]
318
- assert_ron_snapshot ! ( "dryrun-tar-tree-nix" , tree) ;
302
+ assert_with_win ( "dryrun-tar-tree" , tree) ;
319
303
} ) ;
320
304
321
305
// re-read index
@@ -325,13 +309,7 @@ fn test_backup_dry_run_with_tar_gz_passes(
325
309
let snap_dry_run = repo. backup ( & opts, paths, SnapshotFile :: default ( ) ) ?;
326
310
327
311
insta_summary_redaction. bind ( || {
328
- #[ cfg( windows) ]
329
- assert_ron_snapshot ! (
330
- "dryrun-tar-summary-second-windows" ,
331
- TestSummary ( & snap_dry_run)
332
- ) ;
333
- #[ cfg( not( windows) ) ]
334
- assert_ron_snapshot ! ( "dryrun-tar-summary-second-nix" , TestSummary ( & snap_dry_run) ) ;
312
+ assert_with_win ( "dryrun-tar-summary-second" , TestSummary ( & snap_dry_run) ) ;
335
313
} ) ;
336
314
337
315
// check that no data has been added
@@ -348,3 +326,79 @@ fn test_backup_dry_run_with_tar_gz_passes(
348
326
assert_eq ! ( snap_dry_run. tree, second_snapshot. tree) ;
349
327
Ok ( ( ) )
350
328
}
329
+
330
+ #[ rstest]
331
+ fn test_ls ( tar_gz_testdata : Result < TestSource > , set_up_repo : Result < RepoOpen > ) -> Result < ( ) > {
332
+ // Fixtures
333
+ let ( source, repo) = ( tar_gz_testdata?, set_up_repo?. to_indexed_ids ( ) ?) ;
334
+ let paths = & source. path_list ( ) ;
335
+
336
+ // we use as_path to not depend on the actual tempdir
337
+ let opts = BackupOptions :: default ( ) . as_path ( PathBuf :: from_str ( "test" ) ?) ;
338
+ // backup test-data
339
+ let snapshot = repo. backup ( & opts, paths, SnapshotFile :: default ( ) ) ?;
340
+
341
+ // test non-existing entries
342
+ let mut node = Node :: new_node (
343
+ OsStr :: new ( "" ) ,
344
+ rustic_core:: repofile:: NodeType :: Dir ,
345
+ Metadata :: default ( ) ,
346
+ ) ;
347
+ node. subtree = Some ( snapshot. tree ) ;
348
+
349
+ // re-read index
350
+ let repo = repo. to_indexed_ids ( ) ?;
351
+
352
+ let _entries: Vec < _ > = repo
353
+ . ls ( & node, & LsOptions :: default ( ) ) ?
354
+ . collect :: < RusticResult < _ > > ( ) ?;
355
+ // TODO: Snapshot-test entries
356
+ // assert_ron_snapshot!("ls", entries);
357
+ Ok ( ( ) )
358
+ }
359
+
360
+ #[ rstest]
361
+ fn test_find ( tar_gz_testdata : Result < TestSource > , set_up_repo : Result < RepoOpen > ) -> Result < ( ) > {
362
+ // Fixtures
363
+ let ( source, repo) = ( tar_gz_testdata?, set_up_repo?. to_indexed_ids ( ) ?) ;
364
+ let paths = & source. path_list ( ) ;
365
+
366
+ // we use as_path to not depend on the actual tempdir
367
+ let opts = BackupOptions :: default ( ) . as_path ( PathBuf :: from_str ( "test" ) ?) ;
368
+ // backup test-data
369
+ let snapshot = repo. backup ( & opts, paths, SnapshotFile :: default ( ) ) ?;
370
+
371
+ // re-read index
372
+ let repo = repo. to_indexed_ids ( ) ?;
373
+
374
+ // test non-existing path
375
+ let not_found = repo. find_nodes_from_path ( vec ! [ snapshot. tree] , Path :: new ( "not_existing" ) ) ?;
376
+ assert_with_win ( "find-nodes-not-found" , not_found) ;
377
+ // test non-existing match
378
+ let glob = Glob :: new ( "not_existing" ) ?. compile_matcher ( ) ;
379
+ let not_found =
380
+ repo. find_matching_nodes ( vec ! [ snapshot. tree] , & |path, _| glob. is_match ( path) ) ?;
381
+ assert_with_win ( "find-matching-nodes-not-found" , not_found) ;
382
+
383
+ // test existing path
384
+ let FindNode { matches, .. } =
385
+ repo. find_nodes_from_path ( vec ! [ snapshot. tree] , Path :: new ( "test/0/tests/testfile" ) ) ?;
386
+ assert_with_win ( "find-nodes-existing" , matches) ;
387
+ // test existing match
388
+ let glob = Glob :: new ( "testfile" ) ?. compile_matcher ( ) ;
389
+ let match_func = |path : & Path , _: & Node | {
390
+ glob. is_match ( path) || path. file_name ( ) . is_some_and ( |f| glob. is_match ( f) )
391
+ } ;
392
+ let FindMatches { paths, matches, .. } =
393
+ repo. find_matching_nodes ( vec ! [ snapshot. tree] , & match_func) ?;
394
+ assert_with_win ( "find-matching-existing" , ( paths, matches) ) ;
395
+ // test existing match
396
+ let glob = Glob :: new ( "testfile*" ) ?. compile_matcher ( ) ;
397
+ let match_func = |path : & Path , _: & Node | {
398
+ glob. is_match ( path) || path. file_name ( ) . is_some_and ( |f| glob. is_match ( f) )
399
+ } ;
400
+ let FindMatches { paths, matches, .. } =
401
+ repo. find_matching_nodes ( vec ! [ snapshot. tree] , & match_func) ?;
402
+ assert_with_win ( "find-matching-wildcard-existing" , ( paths, matches) ) ;
403
+ Ok ( ( ) )
404
+ }
0 commit comments