Skip to content

Commit

Permalink
adding back deleted method(#1178)
Browse files Browse the repository at this point in the history
* adding back in an unintentionally deleted method found by compatibility testing tools
* refactoring the relevant test
  • Loading branch information
lbergelson authored Sep 7, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e37d8de commit fb36a36
Showing 2 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -495,6 +495,19 @@ else if (STREAM_TYPES.contains(this.outType))
return writer;
}

/**
* Attempts to determine the type of file/data to write based on the File path being
* written to. Will attempt to determine using the logical filename; if that fails it will
* attempt to resolve any symlinks and try again. If that fails, and the output file exists
* but is neither a file or directory then VCF_STREAM is returned.
*
* @param file A file whose {@link OutputType} we want to infer
* @return The file's {@link OutputType}. Never {@code null}.
*/
public static OutputType determineOutputTypeFromFile(final File file) {
return determineOutputTypeFromFile(file.toPath());
}

/**
* Attempts to determine the type of file/data to write based on the File path being
* written to. Will attempt to determine using the logical filename; if that fails it will
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@
import htsjdk.samtools.SAMSequenceDictionary;
import htsjdk.samtools.util.BlockCompressedOutputStream;
import htsjdk.samtools.util.IOUtil;
import htsjdk.samtools.util.RuntimeIOException;
import htsjdk.tribble.Tribble;
import htsjdk.tribble.util.TabixUtils;
import htsjdk.variant.VariantBaseTest;
@@ -40,6 +39,7 @@
import java.nio.file.FileSystem;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -48,6 +48,7 @@
import java.util.function.Consumer;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.*;
@@ -133,27 +134,47 @@ public void testSetOutputFile() throws IOException {
Assert.assertTrue(writer instanceof BCF2Writer, "testSetOutputFile BCF File");
}

@Test
public void testDetermineOutputType() {
Assert.assertEquals(OutputType.VCF, VariantContextWriterBuilder.determineOutputTypeFromFile(this.vcf.toPath()));
Assert.assertEquals(OutputType.BCF, VariantContextWriterBuilder.determineOutputTypeFromFile(this.bcf.toPath()));
Assert.assertEquals(OutputType.VCF_STREAM, VariantContextWriterBuilder.determineOutputTypeFromFile(new File("/dev/stdout").toPath()));
for (final File f: this.blockCompressedVCFs) {
Assert.assertEquals(OutputType.BLOCK_COMPRESSED_VCF, VariantContextWriterBuilder.determineOutputTypeFromFile(f.toPath()));
}

// Test symlinking
try {
final Path link = Files.createTempFile("foo.", ".tmp");
Files.deleteIfExists(link);
Files.createSymbolicLink(link, this.vcf.toPath());
link.toFile().deleteOnExit();
Assert.assertEquals(OutputType.VCF, VariantContextWriterBuilder.determineOutputTypeFromFile(link));
link.toFile().delete();
}
catch (final IOException ioe) {
throw new RuntimeIOException(ioe);
@DataProvider
public Iterator<Object[]> getFilesAndOutputTypes(){
List<Object[]> cases = new ArrayList<>();
cases.add(new Object[]{OutputType.VCF, this.vcf});
cases.add(new Object[]{OutputType.BCF, this.bcf});
cases.add(new Object[]{OutputType.VCF_STREAM, new File("/dev/stdout")});
for (final File file: this.blockCompressedVCFs) {
cases.add( new Object[]{OutputType.BLOCK_COMPRESSED_VCF, file});
}
return cases.iterator();
}

@Test(dataProvider = "getFilesAndOutputTypes")
public void testDetermineOutputType(OutputType expected, File file) {
Assert.assertEquals(VariantContextWriterBuilder.determineOutputTypeFromFile(file), expected);
}

@Test(dataProvider = "getFilesAndOutputTypes")
public void testDetermineOutputTypeWithSymlink(OutputType expected, File file) throws IOException {
final File link = setUpSymlink(file);
Assert.assertEquals(expected, VariantContextWriterBuilder.determineOutputTypeFromFile(link));
}

@Test(dataProvider = "getFilesAndOutputTypes")
public void testDetermineOutputTypeOnPath(OutputType expected, File file) {
Assert.assertEquals(VariantContextWriterBuilder.determineOutputTypeFromFile(file.toPath()), expected);
}

@Test(dataProvider = "getFilesAndOutputTypes")
public void testDetermineOutputTypeWithSymlinkOnPath(OutputType expected, File file) throws IOException {
final File link = setUpSymlink(file);
Assert.assertEquals(expected, VariantContextWriterBuilder.determineOutputTypeFromFile(link.toPath()));
}

private static File setUpSymlink(File target) throws IOException {
final Path link = Files.createTempFile("foo.", ".tmp");
Files.deleteIfExists(link);
Files.createSymbolicLink(link, target.toPath());
link.toFile().deleteOnExit();
return link.toFile();
}

@Test

0 comments on commit fb36a36

Please sign in to comment.