@@ -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]
3341fn 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]
172194fn 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}
0 commit comments