@@ -8,6 +8,7 @@ use std::{
8
8
fmt:: Debug ,
9
9
fs,
10
10
io:: { self , Write } ,
11
+ ops:: { Deref , DerefMut } ,
11
12
path:: { Path , PathBuf } ,
12
13
process:: Command ,
13
14
sync:: { Arc , RwLock , RwLockWriteGuard } ,
@@ -358,6 +359,15 @@ pub struct CliTestContext {
358
359
_test_dir : TempDir ,
359
360
}
360
361
362
+ impl CliTestContext {
363
+ /// Move the dist server to the specified scenario and restore it
364
+ /// afterwards.
365
+ pub fn with_dist_dir ( & mut self , scenario : Scenario ) -> DistDirGuard < ' _ > {
366
+ self . config . distdir = Some ( CONST_TEST_STATE . dist_server_for ( scenario) . unwrap ( ) ) ;
367
+ DistDirGuard { inner : self }
368
+ }
369
+ }
370
+
361
371
impl From < Scenario > for CliTestContext {
362
372
fn from ( scenario : Scenario ) -> Self {
363
373
// Things we might cache or what not
@@ -374,7 +384,35 @@ impl From<Scenario> for CliTestContext {
374
384
config. distdir = Some ( config. test_dist_dir . path ( ) . to_path_buf ( ) ) ;
375
385
}
376
386
377
- Self { config, _test_dir }
387
+ Self {
388
+ config,
389
+ _test_dir,
390
+ }
391
+ }
392
+ }
393
+
394
+ #[ must_use]
395
+ pub struct DistDirGuard < ' a > {
396
+ inner : & ' a mut CliTestContext ,
397
+ }
398
+
399
+ impl Deref for DistDirGuard < ' _ > {
400
+ type Target = CliTestContext ;
401
+
402
+ fn deref ( & self ) -> & Self :: Target {
403
+ & * self . inner
404
+ }
405
+ }
406
+
407
+ impl DerefMut for DistDirGuard < ' _ > {
408
+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
409
+ self . inner
410
+ }
411
+ }
412
+
413
+ impl Drop for DistDirGuard < ' _ > {
414
+ fn drop ( & mut self ) {
415
+ self . inner . config . distdir = None ;
378
416
}
379
417
}
380
418
@@ -454,15 +492,6 @@ impl Config {
454
492
raw:: write_file ( & version_file, "" ) . unwrap ( ) ;
455
493
}
456
494
457
- /// Move the dist server to the specified scenario and restore it
458
- /// afterwards.
459
- pub fn with_scenario ( & mut self , s : Scenario , f : & dyn Fn ( & mut Config ) ) {
460
- let dist_path = CONST_TEST_STATE . dist_server_for ( s) . unwrap ( ) ;
461
- self . const_dist_dir = Some ( dist_path) ;
462
- f ( self ) ;
463
- self . const_dist_dir = None ;
464
- }
465
-
466
495
pub fn cmd < I , A > ( & self , name : & str , args : I ) -> Command
467
496
where
468
497
I : IntoIterator < Item = A > ,
0 commit comments