Skip to content

Commit aac5169

Browse files
committed
refactor
Better separate the setup block to leave only what it takes to clean it up.
1 parent dbc5caa commit aac5169

File tree

1 file changed

+29
-27
lines changed
  • git-discover/tests/upwards

1 file changed

+29
-27
lines changed

git-discover/tests/upwards/mod.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -204,35 +204,40 @@ fn cross_fs() -> crate::Result {
204204

205205
let top_level_repo = git_testtools::scripted_fixture_repo_writable("make_basic_repo.sh")?;
206206

207-
// Create an empty dmg file
208-
let dmg_location = tempfile::tempdir()?;
209-
let dmg_file = dmg_location.path().join("temp.dmg");
210-
Command::new("hdiutil")
211-
.args(&["create", "-size", "1m"])
212-
.arg(&dmg_file)
213-
.status()?;
214-
215-
// Mount dmg file into temporary location
216-
let mount_point = tempfile::tempdir()?;
217-
Command::new("hdiutil")
218-
.args(&["attach", "-nobrowse", "-mountpoint"])
219-
.arg(mount_point.path())
220-
.arg(&dmg_file)
221-
.status()?;
207+
let _cleanup = {
208+
// Create an empty dmg file
209+
let dmg_location = tempfile::tempdir()?;
210+
let dmg_file = dmg_location.path().join("temp.dmg");
211+
Command::new("hdiutil")
212+
.args(&["create", "-size", "1m"])
213+
.arg(&dmg_file)
214+
.status()?;
222215

223-
// Ensure that the mount point is always cleaned up
224-
let defer_detach = defer::defer(|| {
216+
// Mount dmg file into temporary location
217+
let mount_point = tempfile::tempdir()?;
225218
Command::new("hdiutil")
226-
.arg("detach")
219+
.args(&["attach", "-nobrowse", "-mountpoint"])
227220
.arg(mount_point.path())
228-
.status()
229-
.expect("detach temporary test dmg filesystem successfully");
230-
});
221+
.arg(&dmg_file)
222+
.status()?;
231223

232-
// Symlink the mount point into the repo
233-
symlink(mount_point.path(), top_level_repo.path().join("remote"))?;
224+
// Ensure that the mount point is always cleaned up
225+
let cleanup = defer::defer({
226+
let arg = mount_point.path().to_owned();
227+
move || {
228+
Command::new("hdiutil")
229+
.arg("detach")
230+
.arg(arg)
231+
.status()
232+
.expect("detach temporary test dmg filesystem successfully");
233+
}
234+
});
235+
236+
// Symlink the mount point into the repo
237+
symlink(mount_point.path(), top_level_repo.path().join("remote"))?;
238+
cleanup
239+
};
234240

235-
// Disovery tests
236241
let res = git_discover::upwards(top_level_repo.path().join("remote"))
237242
.expect_err("the cross-fs option should prevent us from discovering the repo");
238243
assert!(matches!(
@@ -258,9 +263,6 @@ fn cross_fs() -> crate::Result {
258263
top_level_repo.path().file_name()
259264
);
260265

261-
// Cleanup
262-
drop(defer_detach);
263-
264266
Ok(())
265267
}
266268

0 commit comments

Comments
 (0)