Skip to content

Commit 39a4c5f

Browse files
committed
Remove Config::with_scenario()
1 parent 3cc9095 commit 39a4c5f

File tree

5 files changed

+1835
-1876
lines changed

5 files changed

+1835
-1876
lines changed

src/test/mock/clitools.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{
88
fmt::Debug,
99
fs,
1010
io::{self, Write},
11+
ops::{Deref, DerefMut},
1112
path::{Path, PathBuf},
1213
process::Command,
1314
sync::{Arc, RwLock, RwLockWriteGuard},
@@ -358,6 +359,15 @@ pub struct CliTestContext {
358359
_test_dir: TempDir,
359360
}
360361

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+
361371
impl From<Scenario> for CliTestContext {
362372
fn from(scenario: Scenario) -> Self {
363373
// Things we might cache or what not
@@ -374,7 +384,35 @@ impl From<Scenario> for CliTestContext {
374384
config.distdir = Some(config.test_dist_dir.path().to_path_buf());
375385
}
376386

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;
378416
}
379417
}
380418

@@ -454,15 +492,6 @@ impl Config {
454492
raw::write_file(&version_file, "").unwrap();
455493
}
456494

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-
466495
pub fn cmd<I, A>(&self, name: &str, args: I) -> Command
467496
where
468497
I: IntoIterator<Item = A>,

0 commit comments

Comments
 (0)