radicle: fix tests to not leave temporary files behind

In radicle/src/test.rs, we have a helper type for a Node, which
creates a temporary directory using tempfile::tempdir, which creates a
TempDir. When the TempDir is dropped, it deletes its associated
directory. Before that happens, the Node has already stored the path.
Right after that the TempDir is dropped, and the directory deleted.
When the test code later opens the repository, using Storage::open,
it recreates the directory. As a result, there is nothing that would
delete the directory when the test finishes.

This means tests that use the helper Node type leave temporary files
behind.

Fix this by storing the TempDir in the helper Node, not just its path.
This means the TempDir is only dropped at the end of the test its used
in, and no temporary files are left behind.

Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius 2024-04-12 17:45:26 +03:00 committed by cloudhead
parent c07522365a
commit 1a0221f726
No known key found for this signature in database
1 changed files with 10 additions and 23 deletions

View File

@ -70,6 +70,8 @@ pub fn fetch<W: WriteRepository>(
pub mod setup { pub mod setup {
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};
use super::storage::{Namespaces, RefUpdate}; use super::storage::{Namespaces, RefUpdate};
use crate::crypto::test::signer::MockSigner; use crate::crypto::test::signer::MockSigner;
use crate::storage::git::transport::remote; use crate::storage::git::transport::remote;
@ -86,6 +88,7 @@ pub mod setup {
/// ///
/// Note that this isn't a real node; only a profile with storage and a signing key. /// Note that this isn't a real node; only a profile with storage and a signing key.
pub struct Node { pub struct Node {
pub tmp: TempDir,
pub root: PathBuf, pub root: PathBuf,
pub storage: Storage, pub storage: Storage,
pub signer: MockSigner, pub signer: MockSigner,
@ -93,15 +96,15 @@ pub mod setup {
impl Default for Node { impl Default for Node {
fn default() -> Self { fn default() -> Self {
let root = tempfile::tempdir().unwrap(); let root = tempdir().unwrap();
Self::new(root, MockSigner::default(), "Radcliff") Self::new(root, MockSigner::default(), "Radcliff")
} }
} }
impl Node { impl Node {
pub fn new(root: impl AsRef<Path>, signer: MockSigner, alias: &str) -> Self { pub fn new(tmp: TempDir, signer: MockSigner, alias: &str) -> Self {
let root = root.as_ref().to_path_buf(); let root = tmp.path().to_path_buf();
let home = root.join("home"); let home = root.join("home");
let paths = Home::new(home.as_path()).unwrap(); let paths = Home::new(home.as_path()).unwrap();
let storage = Storage::open( let storage = Storage::open(
@ -116,6 +119,7 @@ pub mod setup {
remote::mock::register(signer.public_key(), storage.path()); remote::mock::register(signer.public_key(), storage.path());
Self { Self {
tmp,
root, root,
storage, storage,
signer, signer,
@ -242,29 +246,13 @@ pub mod setup {
pub bob: NodeWithRepo, pub bob: NodeWithRepo,
pub eve: NodeWithRepo, pub eve: NodeWithRepo,
pub rid: RepoId, pub rid: RepoId,
#[allow(dead_code)]
tmp: tempfile::TempDir,
} }
impl Default for Network { impl Default for Network {
fn default() -> Self { fn default() -> Self {
let tmp = tempfile::tempdir().unwrap(); let alice = Node::new(tempdir().unwrap(), MockSigner::from_seed([!0; 32]), "alice");
let alice = Node::new( let mut bob = Node::new(tempdir().unwrap(), MockSigner::from_seed([!1; 32]), "bob");
tmp.path().join("alice"), let mut eve = Node::new(tempdir().unwrap(), MockSigner::from_seed([!2; 32]), "eve");
MockSigner::from_seed([!0; 32]),
"alice",
);
let mut bob = Node::new(
tmp.path().join("bob"),
MockSigner::from_seed([!1; 32]),
"bob",
);
let mut eve = Node::new(
tmp.path().join("eve"),
MockSigner::from_seed([!2; 32]),
"eve",
);
let repo = alice.project(); let repo = alice.project();
let rid = repo.id; let rid = repo.id;
@ -294,7 +282,6 @@ pub mod setup {
bob, bob,
eve, eve,
rid, rid,
tmp,
} }
} }
} }