Index.writeBasedOnFeaturePath should throw instead of silently Failing #841

Merged
merged 5 commits into from May 27, 2017
@@ -386,8 +386,7 @@ public void write(final Path idxPath) throws IOException {
@Override
public void writeBasedOnFeaturePath(final Path featurePath) throws IOException {
if (!Files.isRegularFile(featurePath)) {
- logger.warn("Index not written into ", featurePath);
- return;
+ throw new IOException("Cannot write based on a non-regular file: " + featurePath.toUri());
}
write(Tribble.indexPath(featurePath));
}
@@ -92,11 +92,11 @@ public default void write(final File idxFile) throws IOException {
/**
* Write an appropriately named and located Index file based on the name and location of the featureFile.
- * If featureFile is not a normal file, the index will silently not be written.
*
* Default implementation delegates to {@link #writeBasedOnFeaturePath(Path)}
*
* @param featureFile
+ * @throws IOException if featureFile is not a normal file.
*/
public default void writeBasedOnFeatureFile(File featureFile) throws IOException {
writeBasedOnFeaturePath(featureFile.toPath());
@@ -107,6 +107,7 @@ public default void writeBasedOnFeatureFile(File featureFile) throws IOException
* If featureFile is not a normal file, the index will silently not be written.
*
* @param featurePath
+ * @throws IOException if featureFile is not a normal file.
*/
public void writeBasedOnFeaturePath(Path featurePath) throws IOException;
@@ -66,8 +66,6 @@
MAGIC_NUMBER = bb.order(ByteOrder.LITTLE_ENDIAN).getInt();
}
- private static final Log LOGGER = Log.getInstance(TabixIndex.class);
-
private final TabixFormat formatSpec;
private final List<String> sequenceNames;
private final BinningIndexContent[] indices;
@@ -226,12 +224,12 @@ public void write(final Path tabixPath) throws IOException {
* Writes to a path with appropriate name and directory based on feature path.
*
* @param featurePath Path being indexed.
+ * @throws IOException if featureFile is not a normal file.
*/
@Override
public void writeBasedOnFeaturePath(final Path featurePath) throws IOException {
@droazen

droazen Apr 18, 2017 edited

Contributor

Can you update the javadoc for this method (and overloads) in the Index interface to mention that this method now throws an IOException in the case where featurePath is not a regular file?

@lbergelson

lbergelson Apr 18, 2017

Contributor

also update the javadoc for the associated file methods

@magicDGS

magicDGS Apr 23, 2017

Contributor

Done.

if (!Files.isRegularFile(featurePath)) {
- LOGGER.warn("Index not written into ", featurePath);
- return;
+ throw new IOException("Cannot write based on a non-regular file: " + featurePath.toUri());
@droazen

droazen Apr 18, 2017 edited

Contributor

Can you add a test case showing that the new exception is thrown in the non-regular file case?

@magicDGS

magicDGS Apr 23, 2017

Contributor

Done.

}
write(Tribble.tabixIndexPath(featurePath));
}
@@ -3,6 +3,7 @@
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import htsjdk.HtsjdkTest;
+import htsjdk.samtools.util.IOUtil;
import htsjdk.tribble.FeatureCodec;
import htsjdk.tribble.TestUtils;
import htsjdk.tribble.Tribble;
@@ -121,4 +122,13 @@ public void testWritePathIndex(final File inputFile, final IndexFactory.IndexTyp
}
}
}
+
+ @Test(dataProvider = "writeIndexData")
+ public void testWriteBasedOnNonRegularFeatureFile(final File inputFile, final IndexFactory.IndexType type, final FeatureCodec codec) throws Exception {
+ final File tmpFolder = IOUtil.createTempDir("NonRegultarFeatureFile", null);
+ // create the index
+ final Index index = IndexFactory.createIndex(inputFile, codec, type);
+ // try to write based on the tmpFolder
+ Assert.assertThrows(IOException.class, () -> index.writeBasedOnFeatureFile(tmpFolder));
+ }
}