@@ -154,6 +154,62 @@ def next_id():
154
154
step_folder = os .path .join (directory .full_path , 'basename-1000' )
155
155
self .assertIn (subdirectory , tf .io .gfile .listdir (step_folder ))
156
156
157
+ def test_export_file_manager_with_suffix_second_cleanup_succeeds (self ):
158
+ directory = self .create_tempdir ()
159
+ base_name = os .path .join (directory .full_path , 'basename' )
160
+
161
+ id_num = 0
162
+
163
+ def next_id ():
164
+ return id_num
165
+
166
+ subdirectory = 'sub'
167
+
168
+ manager = actions .ExportFileManager (
169
+ base_name , max_to_keep = 2 , next_id_fn = next_id , subdirectory = subdirectory
170
+ )
171
+ id_num = 30
172
+ directory .create_file (manager .next_name ())
173
+ id_num = 200
174
+ directory .create_file (manager .next_name ())
175
+ id_num = 1000
176
+ directory .create_file (manager .next_name ())
177
+ manager .clean_up () # Should delete file with lowest ID.
178
+ # Note that the base folder is intact, only the suffix folder is deleted.
179
+ self .assertEqual (
180
+ _id_sorted_file_base_names (directory .full_path ),
181
+ ['basename-30' , 'basename-200' , 'basename-1000' ],
182
+ )
183
+ # Verify that the suffix folder has been deleted from the lowest ID
184
+ # but not from the others.
185
+ self .assertEmpty (
186
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-30' ))
187
+ )
188
+ self .assertNotEmpty (
189
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-200' ))
190
+ )
191
+ self .assertNotEmpty (
192
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-1000' ))
193
+ )
194
+ # Add another ID, run clean_up again and verify that it worked.
195
+ id_num = 2000
196
+ directory .create_file (manager .next_name ())
197
+ manager .clean_up () # Should delete file with lowest ID.
198
+ # Verify that the suffix folder has been deleted from the two lowest ID
199
+ # directories but not from the others.
200
+ self .assertEmpty (
201
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-30' ))
202
+ )
203
+ self .assertEmpty (
204
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-200' ))
205
+ )
206
+ self .assertNotEmpty (
207
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-1000' ))
208
+ )
209
+ self .assertNotEmpty (
210
+ tf .io .gfile .listdir (os .path .join (directory .full_path , 'basename-2000' ))
211
+ )
212
+
157
213
def test_export_file_manager_managed_files (self ):
158
214
directory = self .create_tempdir ()
159
215
directory .create_file ('basename-5' )
0 commit comments