Skip to content

Commit

Permalink
Merge pull request #127 from bpow/testing-fixes
Browse files Browse the repository at this point in the history
Prevent testing from leaving extra files around
  • Loading branch information
nh13 committed Oct 9, 2014
2 parents 878e36d + 514138a commit e36afc2
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/tests/java/htsjdk/samtools/BAMFileIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public void testUnmappedMateWithCoordinate() throws Exception {
}
writer.close();
final SAMFileReader bamReader = new SAMFileReader(bamFile);
SamFiles.findIndex(bamFile).deleteOnExit();
Assert.assertEquals(countElements(bamReader.queryContained("chr7", 100, 100)), 1);
Assert.assertEquals(countElements(bamReader.queryOverlapping("chr7", 100, 100)), 2);
}
Expand Down
6 changes: 2 additions & 4 deletions src/tests/java/htsjdk/samtools/SAMFileWriterFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ public void ordinaryFileWriterTest() throws Exception {
final File outputFile = File.createTempFile("tmp.", BamFileIoUtils.BAM_FILE_EXTENSION);
outputFile.delete();
outputFile.deleteOnExit();
String basename = outputFile.getName();
basename = basename.substring(0, basename.lastIndexOf("."));
final File indexFile = new File(outputFile.getParent(), basename + BAMIndex.BAMIndexSuffix);
createSmallBam(outputFile);
final File indexFile = SamFiles.findIndex(outputFile);
indexFile.deleteOnExit();
final File md5File = new File(outputFile.getParent(), outputFile.getName() + ".md5");
md5File.deleteOnExit();
createSmallBam(outputFile);
Assert.assertTrue(outputFile.length() > 0);
Assert.assertTrue(indexFile.length() > 0);
Assert.assertTrue(md5File.length() > 0);
Expand Down
4 changes: 2 additions & 2 deletions src/tests/java/htsjdk/samtools/SAMIntegerTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ private SAMRecord writeAndReadSamRecord(final String format) throws IOException
* @return The same record, after having being written and read back.
*/
private SAMRecord writeAndReadSamRecord(final String format, SAMRecord rec) throws IOException {
final File bamFile = File.createTempFile("test.", "." + format);
bamFile.deleteOnExit();
final File bamFile = File.createTempFile("htsjdk-writeAndReadSamRecord.", "." + format);
final SAMFileWriter bamWriter = new SAMFileWriterFactory().makeSAMOrBAMWriter(rec.getHeader(), false, bamFile);
bamWriter.addAlignment(rec);
bamWriter.close();
final SAMFileReader reader = new SAMFileReader(bamFile);
rec = reader.iterator().next();
reader.close();
bamFile.delete();
return rec;
}

Expand Down
32 changes: 22 additions & 10 deletions src/tests/java/htsjdk/samtools/util/BinaryCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@
*/

public class BinaryCodecTest {
public final static String TEST_BASENAME = "htsjdk-BinaryCodecTest";

@Test
public void testReadAndWrite() throws IOException {
final byte[] value = new byte[2];
value[0] = 1;
value[1] = 2;
//Writing to file
final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeBytes(value);
Expand All @@ -69,14 +71,16 @@ public void testReadAndWrite() throws IOException {
readCodec.readBytes(bytesFromBinaryFile);
Assert.assertEquals(valuesTwo, bytesFromBinaryFile);
readCodec.close();
outputFile.delete();
}

@Test
public void testReadAndWriteString() throws IOException {
final String value = "Test String to Write";

//Writing to file
final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeString(value, true, false);
Expand All @@ -97,7 +101,8 @@ public void testReadAndWriteInt() throws IOException {
final int value = 42;

//Writing to file
final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeInt(value);
Expand All @@ -114,7 +119,8 @@ public void testReadAndWriteInt() throws IOException {
public void testReadAndWriteDouble() throws IOException {
final double value = 54.4;

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeDouble(value);
Expand All @@ -131,7 +137,8 @@ public void testReadAndWriteDouble() throws IOException {
public void testReadAndWriteLong() throws IOException {
final long value = 42;

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeLong(value);
Expand All @@ -149,7 +156,8 @@ public void testReadAndWriteLong() throws IOException {
public void testReadAndWriteFloat() throws IOException{
final float value = 42.5F;

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeFloat(value);
Expand All @@ -168,7 +176,8 @@ public void testReadAndWriteBoolean() throws IOException{
boolean values[] = {true, false};

for (boolean value : values) {
final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeBoolean(value);
Expand All @@ -187,7 +196,8 @@ public void testReadAndWriteMutlitpleData() throws IOException{
final float fValue = 42.5F;
final String sValue = "TestString";

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
codec.writeFloat(fValue);
Expand All @@ -208,7 +218,8 @@ public void readPastEndOfFile() throws IOException{
final long startTime = System.currentTimeMillis();
int i = 0;

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
while (i<100){
Expand Down Expand Up @@ -242,7 +253,8 @@ public void timeTest() throws IOException{
final long startTime = System.currentTimeMillis();
int i = 0;

final File outputFile = File.createTempFile("temp", ".bin");
final File outputFile = File.createTempFile(TEST_BASENAME, ".bin");
outputFile.deleteOnExit();
final DataOutputStream stream = new DataOutputStream(new FileOutputStream(outputFile));
final BinaryCodec codec = new BinaryCodec(stream);
while (i<100){
Expand Down
5 changes: 3 additions & 2 deletions src/tests/java/htsjdk/samtools/util/IoUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
public class IoUtilTest {

private static final File SLURP_TEST_FILE = new File("testdata/htsjdk/samtools/io/slurptest.txt");
private static final File EMPTY_FILE = new File("testdata/htsjdk/samtools/io/empty.txt");;
private static final File EMPTY_FILE = new File("testdata/htsjdk/samtools/io/empty.txt");
private static final File FIVE_SPACES_THEN_A_NEWLINE_THEN_FIVE_SPACES_FILE = new File("testdata/htsjdk/samtools/io/5newline5.txt");
private static final List<String> SLURP_TEST_LINES = Arrays.asList("bacon and rice ","for breakfast ","wont you join me");
private static final String SLURP_TEST_LINE_SEPARATOR = "\n";
private static final String TEST_FILE_PREFIX = "foo";
private static final String TEST_FILE_PREFIX = "htsjdk-IOUtilTest";
private static final String TEST_FILE_EXTENSIONS[] = { ".txt", ".txt.gz" };
private static final String TEST_STRING = "bar!";
private File existingTempFile;
Expand All @@ -72,6 +72,7 @@ public void testFileReadingAndWriting() throws IOException
for (String ext : TEST_FILE_EXTENSIONS)
{
File f = File.createTempFile(TEST_FILE_PREFIX, ext);
f.deleteOnExit();

OutputStream os = IOUtil.openFileForWriting(f);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
Expand Down
5 changes: 3 additions & 2 deletions src/tests/java/htsjdk/tribble/FeatureReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@

public class FeatureReaderTest {
private final static File asciiBedFile = new File(TestUtils.DATA_DIR + "test.bed");
private final static File binaryBedFile = new File(TestUtils.OUTPUT_DIR + "test.binary.bed");
private File binaryBedFile;
private final static File tabixBedFile = new File(TestUtils.DATA_DIR + "test.tabix.bed.gz");

@BeforeClass
public void setup() throws IOException {
ExampleBinaryCodec.convertToBinaryTest(asciiBedFile, binaryBedFile, new BEDCodec());
binaryBedFile = File.createTempFile("htsjdk-test.featurereader", ".bed");
binaryBedFile.deleteOnExit();
ExampleBinaryCodec.convertToBinaryTest(asciiBedFile, binaryBedFile, new BEDCodec());
}

@AfterClass
Expand Down
1 change: 0 additions & 1 deletion src/tests/java/htsjdk/tribble/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@
*/
public class TestUtils {
public static String DATA_DIR = "testdata/htsjdk/tribble/";
public static String OUTPUT_DIR = DATA_DIR + "tmp/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class TabixIndexTest {
public void readWriteTest(final File tabixFile) throws Exception {
final TabixIndex index = new TabixIndex(tabixFile);
final File indexFile = File.createTempFile("TabixIndexTest.", TabixUtils.STANDARD_INDEX_EXTENSION);
indexFile.deleteOnExit();
final LittleEndianOutputStream los = new LittleEndianOutputStream(new BlockCompressedOutputStream(indexFile));
index.write(los);
los.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import htsjdk.tribble.FeatureCodec;
import htsjdk.tribble.FeatureCodecHeader;
import htsjdk.tribble.Tribble;
import htsjdk.tribble.readers.LineIterator;
import htsjdk.tribble.readers.LineIteratorImpl;
import htsjdk.tribble.readers.LineReaderUtil;
Expand All @@ -46,6 +47,7 @@
import htsjdk.variant.vcf.VCFHeaderLineCount;
import htsjdk.variant.vcf.VCFHeaderLineType;
import htsjdk.variant.vcf.VCFInfoHeaderLine;

import org.testng.Assert;

import java.io.BufferedInputStream;
Expand Down Expand Up @@ -645,6 +647,7 @@ public static void testReaderWriterWithMissingGenotypes(final VariantContextIOTe

final File tmpFile = File.createTempFile("testReaderWriter", tester.getExtension());
tmpFile.deleteOnExit();
Tribble.indexFile(tmpFile).deleteOnExit();

// write expected to disk
final EnumSet<Options> options = EnumSet.of(Options.INDEX_ON_THE_FLY);
Expand Down Expand Up @@ -691,6 +694,7 @@ public static void testReaderWriter(final VariantContextIOTest tester,
final boolean recurse) throws IOException {
final File tmpFile = File.createTempFile("testReaderWriter", tester.getExtension());
tmpFile.deleteOnExit();
Tribble.indexFile(tmpFile).deleteOnExit();

// write expected to disk
final EnumSet<Options> options = EnumSet.of(Options.INDEX_ON_THE_FLY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
import htsjdk.variant.vcf.VCFHeader;
import htsjdk.variant.vcf.VCFHeaderLine;
import htsjdk.variant.vcf.VCFHeaderVersion;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -58,6 +55,12 @@
import java.util.Map;
import java.util.Set;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
* @author aaron
* <p/>
Expand All @@ -68,13 +71,31 @@
public class VCFWriterUnitTest extends VariantBaseTest {
private Set<VCFHeaderLine> metaData;
private Set<String> additionalColumns;
private File tempDir;

@BeforeClass
private void createTemporaryDirectory() {
tempDir = TestUtil.getTempDirectory("VCFWriter", "StaleIndex");
}

@AfterClass
private void deleteTemporaryDirectory() {
for (File f : tempDir.listFiles()) {
f.delete();
}
tempDir.delete();
}

/** test, using the writer and reader, that we can output and input a VCF file without problems */
@Test(dataProvider = "vcfExtensionsDataProvider")
public void testBasicWriteAndRead(final String extension) throws IOException {
final File fakeVCFFile = File.createTempFile("testBasicWriteAndRead.", extension);
fakeVCFFile.deleteOnExit();
Tribble.indexFile(fakeVCFFile).deleteOnExit();
if (".vcf.gz".equals(extension)) {
new File(fakeVCFFile.getAbsolutePath() + ".tbi").deleteOnExit();
} else {
Tribble.indexFile(fakeVCFFile).deleteOnExit();
}
metaData = new HashSet<VCFHeaderLine>();
additionalColumns = new HashSet<String>();
final SAMSequenceDictionary sequenceDict = createArtificialSequenceDictionary();
Expand Down Expand Up @@ -189,10 +210,6 @@ public void TestWritingLargeVCF(final String extension) throws FileNotFoundExcep
final SAMSequenceDictionary dict = createArtificialSequenceDictionary();
final VCFHeader header = createFakeHeader(metaData,Columns, dict);

final File tempDir = TestUtil.getTempDirectory("VCFWriter", "StaleIndex");

tempDir.deleteOnExit();

final File vcf = new File(tempDir, "test" + extension);
final String indexExtension;
if (extension.equals(".vcf.gz")) {
Expand All @@ -201,6 +218,7 @@ public void TestWritingLargeVCF(final String extension) throws FileNotFoundExcep
indexExtension = Tribble.STANDARD_INDEX_EXTENSION;
}
final File vcfIndex = new File(vcf.getAbsolutePath() + indexExtension);
vcfIndex.deleteOnExit();

for(int count=1;count<2; count++){
final VariantContextWriter writer = new VariantContextWriterBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,50 @@
import java.util.List;

public class VariantContextWriterBuilderUnitTest extends VariantBaseTest {
private static final String TEST_BASENAME = "htsjdk-test.VariantContextWriterBuilderUnitTest";
private SAMSequenceDictionary dictionary;

private final File vcf = new File("test.vcf");
private final File vcfIdx = new File("test.vcf" + Tribble.STANDARD_INDEX_EXTENSION);
private final File vcfMD5 = new File("test.vcf.md5");
private final File bcf = new File("test.bcf");
private final File bcfIdx = new File("test.bcf" + Tribble.STANDARD_INDEX_EXTENSION);
private final File unknown = new File("test.unknown");
private File vcf;
private File vcfIdx;
private File vcfMD5;
private File bcf;
private File bcfIdx;
private File unknown;

private List<File> blockCompressedVCFs;
private List<File> blockCompressedIndices;

@BeforeSuite
public void before() throws IOException {
dictionary = createArtificialSequenceDictionary();
vcf = File.createTempFile(TEST_BASENAME, ".vcf");
vcf.deleteOnExit();
vcfIdx = Tribble.indexFile(vcf);
vcfIdx.deleteOnExit();
vcfMD5 = new File(vcf.getAbsolutePath() + ".md5");
vcfMD5.deleteOnExit();
bcf = File.createTempFile(TEST_BASENAME, ".bcf");
bcf.deleteOnExit();
bcfIdx = Tribble.indexFile(bcf);
bcfIdx.deleteOnExit();
unknown = File.createTempFile(TEST_BASENAME, ".unknown");
unknown.deleteOnExit();

blockCompressedVCFs = new ArrayList<File>();
blockCompressedIndices = new ArrayList<File>();
for (final String extension : AbstractFeatureReader.BLOCK_COMPRESSED_EXTENSIONS) {
final File blockCompressed = new File("test.vcf" + extension);
final File blockCompressed = File.createTempFile(TEST_BASENAME, ".vcf" + extension);
blockCompressed.deleteOnExit();
blockCompressedVCFs.add(blockCompressed);

final File index = new File("test.vcf" + extension + TabixUtils.STANDARD_INDEX_EXTENSION);
final File index = new File(blockCompressed.getAbsolutePath() + TabixUtils.STANDARD_INDEX_EXTENSION);
index.deleteOnExit();
blockCompressedIndices.add(index);
}
}

@Test
public void testSetOutputFile() {
public void testSetOutputFile() throws IOException {
final VariantContextWriterBuilder builder = new VariantContextWriterBuilder()
.setReferenceDictionary(dictionary);

Expand All @@ -94,9 +101,9 @@ public void testSetOutputFile() {
Assert.assertFalse(((VCFWriter)writer).getOutputStream() instanceof BlockCompressedOutputStream, "testSetOutputFile VCF File was compressed");

for (final String extension : AbstractFeatureReader.BLOCK_COMPRESSED_EXTENSIONS) {
final String filename = "test.vcf" + extension;
final File file = new File(filename);
final File file = File.createTempFile(TEST_BASENAME + ".setoutput", extension);
file.deleteOnExit();
final String filename = file.getAbsolutePath();

writer = builder.setOutputFile(filename).build();
Assert.assertTrue(writer instanceof VCFWriter, "testSetOutputFile " + extension + " String");
Expand Down

0 comments on commit e36afc2

Please sign in to comment.