Skip to content

Commit 666f33a

Browse files
committed
feat: properly remove temporary files leftover after running tests
1 parent 152d802 commit 666f33a

File tree

10 files changed

+129
-19
lines changed

10 files changed

+129
-19
lines changed

fil-proofs-param/tests/paramfetch/mod.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ fn rand_bytes_with_blake2b() -> Result<(Vec<u8>, String), FailureError> {
2929
Ok((bytes.to_vec(), hasher.finalize().to_hex()[..32].into()))
3030
}
3131

32+
fn clean_up_manifest_and_parent(manifest_pbuf: &PathBuf) {
33+
let parent_dir = std::path::Path::new(manifest_pbuf)
34+
.parent()
35+
.expect("failed to get parent dir");
36+
std::fs::remove_file(manifest_pbuf).expect("failed to remove manifest file");
37+
std::fs::remove_dir_all(parent_dir).expect("failed to remove parent dir");
38+
}
39+
3240
#[test]
3341
fn nothing_to_fetch_if_cache_fully_hydrated() -> Result<(), FailureError> {
3442
let mut manifest: BTreeMap<String, ParameterData> = BTreeMap::new();
@@ -48,7 +56,7 @@ fn nothing_to_fetch_if_cache_fully_hydrated() -> Result<(), FailureError> {
4856

4957
let manifest_pbuf = tmp_manifest(Some(manifest))?;
5058

51-
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf))
59+
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf.clone()))
5260
.with_session_timeout_ms(1000)
5361
.with_file_and_bytes("aaa.vk", &mut aaa_bytes)
5462
.build();
@@ -57,6 +65,9 @@ fn nothing_to_fetch_if_cache_fully_hydrated() -> Result<(), FailureError> {
5765
session.exp_string("file is up to date")?;
5866
session.exp_string("no outdated files, exiting")?;
5967

68+
clean_up_manifest_and_parent(&manifest_pbuf);
69+
std::fs::remove_dir_all(session._cache_dir.path())?;
70+
6071
Ok(())
6172
}
6273

@@ -75,7 +86,7 @@ fn prompts_to_download_if_file_in_manifest_is_missing() -> Result<(), FailureErr
7586

7687
let manifest_pbuf = tmp_manifest(Some(manifest))?;
7788

78-
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf))
89+
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf.clone()))
7990
.with_session_timeout_ms(1000)
8091
.build();
8192

@@ -84,6 +95,9 @@ fn prompts_to_download_if_file_in_manifest_is_missing() -> Result<(), FailureErr
8495
session.exp_string("Select files to be downloaded")?;
8596
session.exp_string("aaa.vk (1 KiB)")?;
8697

98+
clean_up_manifest_and_parent(&manifest_pbuf);
99+
std::fs::remove_dir_all(session._cache_dir.path())?;
100+
87101
Ok(())
88102
}
89103

@@ -105,7 +119,7 @@ fn prompts_to_download_if_file_checksum_does_not_match_manifest() -> Result<(),
105119

106120
let manifest_pbuf = tmp_manifest(Some(manifest))?;
107121

108-
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf))
122+
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf.clone()))
109123
.with_session_timeout_ms(1000)
110124
.with_file_and_bytes("aaa.vk", &mut aaa_bytes)
111125
.build();
@@ -116,6 +130,9 @@ fn prompts_to_download_if_file_checksum_does_not_match_manifest() -> Result<(),
116130
session.exp_string("Select files to be downloaded")?;
117131
session.exp_string("aaa.vk (1 KiB)")?;
118132

133+
clean_up_manifest_and_parent(&manifest_pbuf);
134+
std::fs::remove_dir_all(session._cache_dir.path())?;
135+
119136
Ok(())
120137
}
121138

@@ -143,7 +160,7 @@ fn fetches_vk_even_if_sector_size_does_not_match() -> Result<(), FailureError> {
143160

144161
let manifest_pbuf = tmp_manifest(Some(manifest))?;
145162

146-
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf))
163+
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf.clone()))
147164
.with_session_timeout_ms(1000)
148165
.whitelisted_sector_sizes(vec!["6666".to_string(), "4444".to_string()])
149166
.build();
@@ -153,6 +170,9 @@ fn fetches_vk_even_if_sector_size_does_not_match() -> Result<(), FailureError> {
153170
session.exp_string("determining if file is out of date: aaa.vk")?;
154171
session.exp_string("file not found, marking for download")?;
155172

173+
clean_up_manifest_and_parent(&manifest_pbuf);
174+
std::fs::remove_dir_all(session._cache_dir.path())?;
175+
156176
Ok(())
157177
}
158178

@@ -165,22 +185,29 @@ fn invalid_json_path_produces_error() -> Result<(), FailureError> {
165185
session.exp_string("using json file: /invalid/path")?;
166186
session.exp_string("failed to open json file, exiting")?;
167187

188+
std::fs::remove_dir_all(session._cache_dir.path())?;
189+
168190
Ok(())
169191
}
170192

171193
#[test]
172194
fn invalid_json_produces_error() -> Result<(), FailureError> {
173195
let manifest_pbuf = tmp_manifest(None)?;
174196

175-
let mut file = File::create(&manifest_pbuf)?;
176-
file.write_all(b"invalid json")?;
197+
{
198+
let mut file = File::create(&manifest_pbuf)?;
199+
file.write_all(b"invalid json")?;
200+
}
177201

178-
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf))
202+
let mut session = ParamFetchSessionBuilder::new(Some(manifest_pbuf.clone()))
179203
.with_session_timeout_ms(1000)
180204
.build();
181205

182206
session.exp_string("failed to parse json file, exiting")?;
183207

208+
clean_up_manifest_and_parent(&manifest_pbuf);
209+
std::fs::remove_dir_all(session._cache_dir.path())?;
210+
184211
Ok(())
185212
}
186213

@@ -203,5 +230,7 @@ fn no_json_path_uses_default_manifest() -> Result<(), FailureError> {
203230
))?;
204231
}
205232

233+
std::fs::remove_dir_all(session._cache_dir.path())?;
234+
206235
Ok(())
207236
}

fil-proofs-param/tests/paramfetch/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl ParamFetchSessionBuilder {
112112
/// An active pseudoterminal (pty) used to interact with paramfetch.
113113
pub struct ParamFetchSession {
114114
pty_session: PtyReplSession,
115-
_cache_dir: TempDir,
115+
pub _cache_dir: TempDir,
116116
}
117117

118118
impl ParamFetchSession {

fil-proofs-param/tests/parampublish/prompts_to_publish.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fn ignores_files_unrecognized_extensions() -> Result<(), FailureError> {
2222
session.send_line("")?;
2323
session.exp_string("no params selected, exiting")?;
2424

25+
std::fs::remove_dir_all(session._cache_dir.path())?;
26+
2527
Ok(())
2628
}
2729

@@ -47,6 +49,8 @@ fn displays_sector_size_in_prompt() -> Result<(), FailureError> {
4749
session.send_line("")?;
4850
session.exp_string("no params selected, exiting")?;
4951

52+
std::fs::remove_dir_all(session._cache_dir.path())?;
53+
5054
Ok(())
5155
}
5256

@@ -59,5 +63,7 @@ fn no_assets_no_prompt() -> Result<(), FailureError> {
5963
session.exp_string("found 0 param files in cache dir")?;
6064
session.exp_string("no file triples found, exiting")?;
6165

66+
std::fs::remove_dir_all(session._cache_dir.path())?;
67+
6268
Ok(())
6369
}

fil-proofs-param/tests/parampublish/read_metadata_files.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fn fails_if_missing_metadata_file() -> Result<(), FailureError> {
1515
session.exp_string("found 2 param files in cache dir")?;
1616
session.exp_string("no file triples found, exiting")?;
1717

18+
std::fs::remove_dir_all(session._cache_dir.path())?;
19+
1820
Ok(())
1921
}
2022

@@ -34,5 +36,7 @@ fn fails_if_malformed_metadata_file() -> Result<(), FailureError> {
3436
session.exp_string("failed to parse .meta file")?;
3537
session.exp_string("exiting")?;
3638

39+
std::fs::remove_dir_all(session._cache_dir.path())?;
40+
3741
Ok(())
3842
}

fil-proofs-param/tests/parampublish/support/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl ParamPublishSessionBuilder {
159159
/// An active pseudoterminal (pty) used to interact with parampublish.
160160
pub struct ParamPublishSession {
161161
pty_session: PtyReplSession,
162-
_cache_dir: TempDir,
162+
pub _cache_dir: TempDir,
163163
}
164164

165165
impl ParamPublishSession {

fil-proofs-param/tests/parampublish/write_json_manifest.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ fn writes_json_manifest() -> Result<(), failure::Error> {
6262
}
6363
}
6464

65+
let parent_dir = std::path::Path::new(&manifest_path)
66+
.parent()
67+
.expect("failed to get parent dir");
68+
std::fs::remove_file(&manifest_path)?;
69+
std::fs::remove_dir(parent_dir)?;
70+
std::fs::remove_dir_all(session._cache_dir.path())?;
71+
6572
Ok(())
6673
}
6774

storage-proofs-porep/tests/common.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1-
use std::path::PathBuf;
1+
use std::fs::remove_file;
2+
use std::io::Result;
3+
use std::path::{Path, PathBuf};
24

35
use filecoin_hashers::Hasher;
4-
use storage_proofs_core::{data::Data, merkle::MerkleTreeTrait};
6+
use merkletree::store::StoreConfig;
7+
use storage_proofs_core::{
8+
cache_key::CacheKey,
9+
data::Data,
10+
merkle::{get_base_tree_count, split_config, MerkleTreeTrait},
11+
};
512
use storage_proofs_porep::stacked::{PersistentAux, PublicParams, StackedDrg, Tau, TemporaryAux};
613

14+
// This method should ONLY be used in purposed test code.
15+
#[allow(dead_code)]
16+
pub fn remove_replica_and_tree_r<Tree: MerkleTreeTrait + 'static>(cache_path: &Path) -> Result<()> {
17+
let replica_path = cache_path.join("replica-path");
18+
let tree_r_last_config = StoreConfig {
19+
path: cache_path.to_path_buf(),
20+
id: CacheKey::CommRLastTree.to_string(),
21+
size: Some(0),
22+
rows_to_discard: 0,
23+
};
24+
let tree_count = get_base_tree_count::<Tree>();
25+
if tree_count > 1 {
26+
let configs =
27+
split_config(tree_r_last_config, tree_count).expect("Failed to split configs");
28+
for config in configs {
29+
let cur_path = StoreConfig::data_path(&config.path, &config.id);
30+
remove_file(cur_path).expect("Failed to remove TreeR");
31+
}
32+
} else {
33+
let cur_path = StoreConfig::data_path(&tree_r_last_config.path, &tree_r_last_config.id);
34+
remove_file(cur_path).expect("Failed to remove TreeR");
35+
}
36+
remove_file(replica_path)
37+
}
38+
739
#[allow(clippy::type_complexity)]
840
pub fn transform_and_replicate_layers<Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>(
941
pp: &PublicParams<Tree>,

storage-proofs-porep/tests/stacked_circuit.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ fn test_stacked_porep_circuit<Tree: MerkleTreeTrait + 'static>(
9292
replica_path.clone(),
9393
);
9494

95-
let mut copied = vec![0; data.len()];
96-
copied.copy_from_slice(&mmapped_data);
97-
assert_ne!(data, copied, "replication did not change data");
95+
{
96+
let mut copied = vec![0; data.len()];
97+
copied.copy_from_slice(&mmapped_data);
98+
assert_ne!(data, copied, "replication did not change data");
99+
}
100+
drop(mmapped_data);
98101

99102
let seed = rng.gen();
100103
let pub_inputs =
@@ -125,6 +128,10 @@ fn test_stacked_porep_circuit<Tree: MerkleTreeTrait + 'static>(
125128
// Discard cached MTs that are no longer needed.
126129
stacked::clear_cache_dir(cache_dir.path()).expect("cached files delete failed");
127130

131+
// Discard normally permanent files no longer needed in testing.
132+
common::remove_replica_and_tree_r::<Tree>(cache_dir.path())
133+
.expect("failed to remove replica and tree_r");
134+
128135
{
129136
// Verify that MetricCS returns the same metrics as TestConstraintSystem.
130137
let mut cs = MetricCS::<Fr>::new();
@@ -177,5 +184,7 @@ fn test_stacked_porep_circuit<Tree: MerkleTreeTrait + 'static>(
177184
"inputs are not the same length"
178185
);
179186

180-
cache_dir.close().expect("Failed to remove cache dir");
187+
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
188+
let _ = cache_dir.close();
189+
}
181190
}

storage-proofs-porep/tests/stacked_compound.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ fn test_stacked_compound<Tree: 'static + MerkleTreeTrait>() {
170170
// Discard cached MTs that are no longer needed.
171171
stacked::clear_cache_dir(cache_dir.path()).expect("cached files delete failed");
172172

173+
// Discard normally permanent files no longer needed in testing.
174+
common::remove_replica_and_tree_r::<Tree>(cache_dir.path())
175+
.expect("failed to remove replica and tree_r");
176+
173177
let proofs = StackedCompound::prove(
174178
&public_params,
175179
&public_inputs,
@@ -200,5 +204,7 @@ fn test_stacked_compound<Tree: 'static + MerkleTreeTrait>() {
200204

201205
assert!(verified);
202206

203-
cache_dir.close().expect("Failed to remove cache dir");
207+
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
208+
let _ = cache_dir.close();
209+
}
204210
}

storage-proofs-porep/tests/stacked_vanilla.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,16 @@ fn test_extract_all<Tree: 'static + MerkleTreeTrait>() {
164164

165165
assert_eq!(data, mmapped_data.as_ref());
166166

167-
cache_dir.close().expect("Failed to remove cache dir");
167+
// Discard cached MTs that are no longer needed.
168+
stacked::clear_cache_dir(cache_dir.path()).expect("cached files delete failed");
169+
170+
// Discard normally permanent files no longer needed in testing.
171+
common::remove_replica_and_tree_r::<Tree>(cache_dir.path())
172+
.expect("failed to remove replica and tree_r");
173+
174+
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
175+
let _ = cache_dir.close();
176+
}
168177
}
169178

170179
#[test]
@@ -281,7 +290,9 @@ fn test_stacked_porep_resume_seal() {
281290

282291
assert_eq!(data, mmapped_data1.as_ref());
283292

284-
cache_dir.close().expect("Failed to remove cache dir");
293+
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
294+
let _ = cache_dir.close();
295+
}
285296
}
286297

287298
table_tests! {
@@ -405,9 +416,15 @@ fn test_prove_verify<Tree: 'static + MerkleTreeTrait>(n: usize, challenges: Chal
405416
// Discard cached MTs that are no longer needed.
406417
stacked::clear_cache_dir(cache_dir.path()).expect("cached files delete failed");
407418

419+
// Discard normally permanent files no longer needed in testing.
420+
common::remove_replica_and_tree_r::<Tree>(cache_dir.path())
421+
.expect("failed to remove replica and tree_r");
422+
408423
assert!(proofs_are_valid);
409424

410-
cache_dir.close().expect("Failed to remove cache dir");
425+
if std::fs::remove_dir(cache_dir.path()).is_ok() && cache_dir.path().exists() {
426+
let _ = cache_dir.close();
427+
}
411428
}
412429

413430
// We are seeing a bug, in which setup never terminates for some sector sizes. This test is to

0 commit comments

Comments
 (0)