diff --git a/gortools/src/main/scala/gorsat/Analysis/ForkWrite.scala b/gortools/src/main/scala/gorsat/Analysis/ForkWrite.scala index 6f8cb5b5..92835e32 100644 --- a/gortools/src/main/scala/gorsat/Analysis/ForkWrite.scala +++ b/gortools/src/main/scala/gorsat/Analysis/ForkWrite.scala @@ -30,7 +30,7 @@ import org.apache.commons.lang3.StringUtils import org.gorpipe.exceptions.GorResourceException import org.gorpipe.gor.binsearch.GorIndexType import org.gorpipe.gor.driver.linkfile.{LinkFile, LinkFileEntryV1} -import org.gorpipe.gor.driver.meta.DataType +import org.gorpipe.gor.driver.meta.{DataType, SourceReference} import org.gorpipe.gor.driver.providers.stream.sources.StreamSource import org.gorpipe.gor.model.{DriverBackedFileReader, Row} import org.gorpipe.gor.session.{GorSession, ProjectContext} @@ -159,8 +159,10 @@ case class ForkWrite(forkCol: Int, fullFileName.replace("#{fork}", forkValue).replace("""${fork}""", forkValue) } else { if (fullFileName.isEmpty && options.linkFile.nonEmpty) { + val linkSourceRef = new SourceReference(options.linkFile, null, projectContext.getFileReader.getCommonRoot, null, null, true); // Infer the full file name from the link (and defautl locations) - LinkFile.inferDataFileNameFromLinkFile(projectContext.getFileReader.resolveUrl(options.linkFile).asInstanceOf[StreamSource]) + LinkFile.inferDataFileNameFromLinkFile( + projectContext.getFileReader.resolveDataSource(linkSourceRef).asInstanceOf[StreamSource]); } else { fullFileName } diff --git a/gortools/src/test/java/gorsat/UTestGorWrite.java b/gortools/src/test/java/gorsat/UTestGorWrite.java index 4c0470d2..3e78f21b 100644 --- a/gortools/src/test/java/gorsat/UTestGorWrite.java +++ b/gortools/src/test/java/gorsat/UTestGorWrite.java @@ -249,6 +249,24 @@ public void testWriteLinkFileWithInferFileName() throws IOException { } + @Test + public void testWriteLinkFileWithInferFileNameForExistingLink() throws IOException { + + environmentVariables.set(GorDriverConfig.GOR_DRIVER_LINK_MANAGED_DATA_FILES_URL, workDirPath.resolve("managed_data").toString()); + TestUtils.runGorPipe("gorrow chr1,1,100 | write -link ltest.gor", "-gorroot", workDirPath.toString()); + TestUtils.runGorPipe("gorrow chr1,1,101 | write -link ltest.gor", "-gorroot", workDirPath.toString()); + + var linkFile = LinkFile.load(new FileSource(workDirPath.resolve("ltest.gor.link").toString())); + + Assert.assertEquals(2, linkFile.getEntriesCount()); + Assert.assertTrue(linkFile.getLatestEntry().url().startsWith(workDirPath.resolve("managed_data/" + workDirPath.getFileName() + "/ltest").toString())); + Assert.assertTrue(linkFile.getLatestEntry().url().endsWith(".gor")); + Assert.assertTrue(Files.exists(Path.of(linkFile.getLatestEntry().url()))); + Assert.assertEquals("#chrom\tbpStart\tbpStop\nchr1\t1\t101\n", + Files.readString(Path.of(linkFile.getLatestEntry().url()))); + + } + @Test public void testTxtWriteServer() throws IOException { Path p = Paths.get("../tests/data/nor/simple.nor");