@@ -106,6 +106,10 @@ pub fn decrypt(file_path: &Path, key: &str) -> Result<PathBuf, Error> {
106
106
107
107
#[ cfg( test) ]
108
108
mod tests {
109
+ use rand:: Rng ;
110
+
111
+ use crate :: utils:: gen_random_dir_name;
112
+
109
113
use super :: * ;
110
114
use std:: env;
111
115
@@ -116,19 +120,34 @@ mod tests {
116
120
#[ test]
117
121
fn test_encrypt_file ( ) {
118
122
let temp_dir = env:: temp_dir ( ) ;
123
+
124
+ let dir_name = gen_random_dir_name ( "test" ) ;
125
+
126
+ let temp_dir = temp_dir. join ( dir_name) ;
127
+
128
+ fs:: create_dir ( & temp_dir) . expect ( "Error creating temp dir" ) ;
129
+
119
130
let file_path = temp_dir. join ( FILE_NAME ) ;
120
131
121
132
std:: fs:: write ( & file_path, FILE_CONTENT ) . expect ( "Failed to write file" ) ;
122
133
123
134
let encrypted_file_path = encrypt ( & file_path, KEY ) . expect ( "Failed to encrypt file" ) ;
124
135
125
- assert_eq ! ( encrypted_file_path. exists( ) , true )
136
+ assert_eq ! ( encrypted_file_path. exists( ) , true ) ;
137
+
138
+ fs:: remove_dir_all ( temp_dir) . unwrap ( ) ;
126
139
}
127
140
128
141
#[ test]
129
142
fn test_decrypt_file ( ) {
130
143
let temp_dir = env:: temp_dir ( ) ;
131
144
145
+ let dir_name = gen_random_dir_name ( "test" ) ;
146
+
147
+ let temp_dir = temp_dir. join ( dir_name) ;
148
+
149
+ fs:: create_dir ( & temp_dir) . expect ( "Error creating temp dir" ) ;
150
+
132
151
let file_path = temp_dir. join ( FILE_NAME ) ;
133
152
134
153
// Encrypt test file again
@@ -137,11 +156,17 @@ mod tests {
137
156
138
157
let file_encrypt_path = encrypt ( & file_path, KEY ) . expect ( "Failed to encrypt file" ) ;
139
158
159
+ // Remove file before decrypt
160
+
161
+ fs:: remove_file ( & file_path) . expect ( "Error removing file before decrypt" ) ;
162
+
140
163
let file_decrypt_path = decrypt ( & file_encrypt_path, KEY ) . expect ( "Failed to decrypt file" ) ;
141
164
142
165
let file_content =
143
166
std:: fs:: read_to_string ( & file_decrypt_path) . expect ( "Failed to read file" ) ;
144
167
145
- assert_eq ! ( file_content, FILE_CONTENT )
168
+ assert_eq ! ( file_content, FILE_CONTENT ) ;
169
+
170
+ fs:: remove_dir_all ( temp_dir) . unwrap ( ) ;
146
171
}
147
- }
172
+ }
0 commit comments