Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bad behavior in IOUtil.deletePaths #1416

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/htsjdk/samtools/util/DiskBackedQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private E readFileRecord (final Path file) {
private void closeIOResources() {
CloserUtil.close(this.outputStream);
CloserUtil.close(this.inputStream);
if (this.diskRecords != null) IOUtil.deletePaths(this.diskRecords);
if (this.diskRecords != null) IOUtil.deletePath(this.diskRecords);
}

/**
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/htsjdk/samtools/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,33 @@ public static void deleteFiles(final Iterable<File> files) {
}

public static void deletePaths(final Path... paths) {
deletePaths(Arrays.asList(paths));
for(Path path: paths){
deletePath(path);
}
}

/**
* Iterate through Paths and delete each one.
* Note: Path is itself an Iterable<Path>. This method special cases that and deletes the single Path rather than
* Iterating the Path for targets to delete.
* @param paths an iterable of Paths to delete
*/
public static void deletePaths(final Iterable<Path> paths) {
for (final Path p : paths) {
try {
Files.delete(p);
} catch (IOException e) {
System.err.println("Could not delete file " + p);
}
//Path is itself an Iterable<Path> which causes very confusing behavior if we don't explicitly check here.
if( paths instanceof Path){
deletePath((Path)paths);
}
paths.forEach(IOUtil::deletePath);
}

/**
* Attempt to delete a single path and log an error if it is not deleted.
*/
public static void deletePath(Path path){
try {
Files.delete(path);
} catch (IOException e) {
System.err.println("Could not delete file " + path);
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/test/java/htsjdk/samtools/util/IOUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;

import htsjdk.samtools.BamFileIoUtils;
import htsjdk.samtools.SAMException;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
Expand All @@ -44,15 +43,12 @@
import org.testng.annotations.Test;

import java.lang.IllegalArgumentException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Random;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;


Expand Down Expand Up @@ -318,13 +314,29 @@ public void testGetDefaultTmpDirPath() throws Exception {
public void testDeletePathLocal(final List<String> fileNames) throws Exception {
final File tmpDir = IOUtil.createTempDir("testDeletePath", "");
final List<Path> paths = createLocalFiles(tmpDir, fileNames);
testDeletePath(paths);
testDeletePaths(paths);
}

@Test
public void testDeleteSinglePath() throws Exception {
final Path toDelete = Files.createTempFile("file",".bad");
Assert.assertTrue(Files.exists(toDelete));
IOUtil.deletePath(toDelete);
Assert.assertFalse(Files.exists(toDelete));
}

@Test
public void testDeleteSingleWithDeletePaths() throws Exception {
final Path toDelete = Files.createTempFile("file",".bad");
Assert.assertTrue(Files.exists(toDelete));
IOUtil.deletePaths(toDelete);
Assert.assertFalse(Files.exists(toDelete));
}

@Test(dataProvider = "fileNamesForDelete")
public void testDeletePathJims(final List<String> fileNames) throws Exception {
final List<Path> paths = createJimfsFiles("testDeletePath", fileNames);
testDeletePath(paths);
testDeletePaths(paths);
}

@Test(dataProvider = "fileNamesForDelete")
Expand All @@ -340,7 +352,8 @@ public void testDeleteArrayPathJims(final List<String> fileNames) throws Excepti
testDeletePathArray(paths);
}

private static void testDeletePath(final List<Path> paths) {

private static void testDeletePaths(final List<Path> paths) {
paths.forEach(p -> Assert.assertTrue(Files.exists(p)));
IOUtil.deletePaths(paths);
paths.forEach(p -> Assert.assertFalse(Files.exists(p)));
Expand Down