Skip to content

Commit

Permalink
Prevent deletion attempt if dir doesn't exist (#683)
Browse files Browse the repository at this point in the history
* prevent deletion attempt if dir doesn't exist

* spotless
  • Loading branch information
breedx-splk authored Nov 6, 2023
1 parent f3cdb38 commit 021db76
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ void safeDelete(File file) {
Log.w(LOG_TAG, "Error deleting file " + file);
}
}

public boolean exists(File file) {
return file.exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ private void cleanupUnsentBackgroundSpans() {
fileUtils.safeDelete(dir);
});
File backgroundDir = getCurrentSessionBackgroundDirectory();
if (!fileUtils.exists(backgroundDir)) {
return;
}
if (!fileUtils.listFilesRecursively(backgroundDir).findAny().isPresent()) {
fileUtils.safeDelete(backgroundDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -62,6 +63,7 @@ void create_onNewId_shouldCleanOldBackgroundFiles() {
fileUtils = mock(FileUtils.class);
when(fileUtils.listDirectories(any())).thenReturn(Stream.of(file));
when(fileUtils.listFiles(file)).thenReturn(Stream.of(file));
when(fileUtils.exists(any())).thenReturn(true);

fileProvider = StartTypeAwareSpanStorage.create(visibleScreenTracker, fileUtils, rootDir);

Expand All @@ -71,6 +73,19 @@ void create_onNewId_shouldCleanOldBackgroundFiles() {
fileProvider.provideSpansDirectory(), fileArgumentCaptor.getAllValues().get(1));
}

@Test
void doesNotAttemptIfBackgroundDirNotExist() {
fileProvider = StartTypeAwareSpanStorage.create(visibleScreenTracker, fileUtils, rootDir);
fileUtils = mock(FileUtils.class);
when(fileUtils.exists(any())).thenReturn(false);
when(fileUtils.listDirectories(any())).thenReturn(Stream.empty());

fileProvider = StartTypeAwareSpanStorage.create(visibleScreenTracker, fileUtils, rootDir);

verify(fileUtils, never()).listFiles(any());
verify(fileUtils, never()).safeDelete(any());
}

@Test
void create_cleanDoesntRemoveDirIfNotEmpty() {
String uniqueId = UUID.randomUUID().toString();
Expand Down

0 comments on commit 021db76

Please sign in to comment.