Skip to content

Commit c4401fa

Browse files
committed
Fix foundry overwrite option
Resolves #14 Change-Id: Iaaeaf499ec3dad1b8c5b33a28ff60e8703217486
1 parent ea46d61 commit c4401fa

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

app/src/main/kotlin/de/ids_mannheim/korapxmltools/KorapXmlTool.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ class KorapXmlTool : Callable<Int> {
12271227

12281228
if (annotateWith.isNotEmpty()) {
12291229
// Detect external foundry label once from annotateWith command
1230-
externalFoundry = detectFoundryFromAnnotateCmd(annotateWith)
1230+
externalFoundry = foundryOverride ?: detectFoundryFromAnnotateCmd(annotateWith)
12311231
// Initialize ZIP output stream BEFORE creating worker pool, if needed
12321232
if (outputFormat == OutputFormat.KORAP_XML) {
12331233
// Determine output filename - respect outputDir consistently
@@ -1943,15 +1943,17 @@ class KorapXmlTool : Callable<Int> {
19431943
dbFactory = DocumentBuilderFactory.newInstance()
19441944
dBuilder = dbFactory!!.newDocumentBuilder()
19451945

1946-
// Respect outputDir option consistently
1946+
// Determine output filename - respect outputDir consistently
19471947
val baseZipName = File(zipFilePath).name.replace(Regex("\\.zip$"), "")
1948-
val outputMorphoZipFileName = File(outputDir, "$baseZipName.$targetFoundry.zip").absolutePath
1949-
targetZipFileName = outputMorphoZipFileName
1950-
LOGGER.info("Output ZIP file: $outputMorphoZipFileName")
1948+
val autoOutputFileName = File(outputDir, "$baseZipName.$targetFoundry.zip").absolutePath
1949+
val finalOutputFileName = outputFile ?: autoOutputFileName
1950+
targetZipFileName = finalOutputFileName
1951+
val outputMorphoZipFileName = finalOutputFileName
1952+
LOGGER.info("Output ZIP file: $targetZipFileName")
19511953

19521954
// Check for existing output file BEFORE redirecting logging, so user sees the message
1953-
if (File(outputMorphoZipFileName).exists() && !overwrite) {
1954-
val errorMsg = "Output file $outputMorphoZipFileName already exists. Use --force to overwrite."
1955+
if (File(targetZipFileName).exists() && !overwrite) {
1956+
val errorMsg = "Output file $targetZipFileName already exists. Use --force to overwrite."
19551957
System.err.println("ERROR: $errorMsg")
19561958
LOGGER.severe(errorMsg)
19571959
exitProcess(1)
@@ -3949,7 +3951,9 @@ class KorapXmlTool : Callable<Int> {
39493951
}
39503952

39513953
// Use extracted foundry from CoNLL-U output if available
3952-
val actualFoundry = if (extractedFoundry != null) {
3954+
val actualFoundry = if (foundryOverride != null) {
3955+
foundryOverride!!
3956+
} else if (extractedFoundry != null) {
39533957
LOGGER.info("Using foundry from CoNLL-U output: $extractedFoundry (was: $foundry)")
39543958
// Update the global externalFoundry variable for consistent naming
39553959
externalFoundry = extractedFoundry
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package de.ids_mannheim.korapxmltools
2+
3+
import org.junit.After
4+
import org.junit.Before
5+
import java.io.ByteArrayOutputStream
6+
import java.io.File
7+
import java.io.PrintStream
8+
import java.net.URL
9+
import kotlin.test.Ignore
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
import kotlin.test.assertTrue
13+
import kotlin.test.assertFalse
14+
15+
class FoundryOverrideTest {
16+
private val outContent = ByteArrayOutputStream(10000000)
17+
private val errContent = ByteArrayOutputStream()
18+
private val originalOut: PrintStream = System.out
19+
private val originalErr: PrintStream = System.err
20+
21+
@Before
22+
fun setUpStreams() {
23+
System.setOut(PrintStream(outContent))
24+
System.setErr(PrintStream(errContent))
25+
}
26+
27+
@After
28+
fun restoreStreams() {
29+
System.setOut(originalOut)
30+
System.setErr(originalErr)
31+
}
32+
33+
private fun loadResource(path: String): URL {
34+
val resource = Thread.currentThread().contextClassLoader.getResource(path)
35+
requireNotNull(resource) { "Resource $path not found" }
36+
return resource
37+
}
38+
39+
@Test
40+
fun testFoundryOverrideWithTagger() {
41+
val outputDir = File.createTempFile("foundry_override_test", "").apply {
42+
delete()
43+
mkdirs()
44+
}
45+
try {
46+
val baseZip = loadResource("wud24_sample.zip").path
47+
val args = arrayOf(
48+
"-f",
49+
"-T", "spacy",
50+
"-q",
51+
"-D", outputDir.path,
52+
"-F", "xyz",
53+
"-t", "zip",
54+
baseZip
55+
)
56+
57+
// Using debug() which calls KorapXmlTool().call()
58+
val exitCode = debug(args)
59+
assertEquals(0, exitCode, "Tool execution should succeed")
60+
61+
// Check if the correct file exists
62+
val expectedFile = File(outputDir, "wud24_sample.xyz.zip")
63+
assertTrue(expectedFile.exists(), "Output file should be named wud24_sample.xyz.zip")
64+
65+
// Check if the wrong file does NOT exist
66+
val wrongFile = File(outputDir, "wud24_sample.spacy.zip")
67+
assertFalse(wrongFile.exists(), "Output file wud24_sample.spacy.zip should NOT exist")
68+
69+
// Check content of the zip
70+
val zipEntries = org.apache.commons.compress.archivers.zip.ZipFile.builder()
71+
.setFile(expectedFile)
72+
.get()
73+
.use { zip ->
74+
zip.entries.asSequence().map { it.name }.toList()
75+
}
76+
77+
assertTrue(zipEntries.any { it.contains("/xyz/morpho.xml") }, "Zip should contain entries with 'xyz' foundry")
78+
assertFalse(zipEntries.any { it.contains("/spacy/morpho.xml") }, "Zip should NOT contain entries with 'spacy' foundry")
79+
80+
} finally {
81+
outputDir.deleteRecursively()
82+
}
83+
}
84+
85+
@Ignore("This test is ignored until -o is fixed")
86+
fun testOutputOptionHasPriority() {
87+
val outputDir = File.createTempFile("output_option_test", "").apply {
88+
delete()
89+
mkdirs()
90+
}
91+
try {
92+
val baseZip = loadResource("wud24_sample.zip").path
93+
val explicitOutputFile = File(outputDir, "my_custom_output.zip")
94+
val args = arrayOf(
95+
"-f",
96+
"-q",
97+
"-T", "spacy",
98+
"-o", explicitOutputFile.path,
99+
"-t", "zip",
100+
baseZip
101+
)
102+
103+
val exitCode = debug(args)
104+
assertEquals(0, exitCode, "Tool execution should succeed")
105+
106+
assertTrue(explicitOutputFile.exists(), "Explicit output file should exist: ${explicitOutputFile.path}")
107+
108+
// Ensure the default-named file does NOT exist
109+
val defaultFile = File(outputDir, "wud24_sample.spacy.zip")
110+
assertFalse(defaultFile.exists(), "Default output file should NOT exist when -o is used")
111+
112+
} finally {
113+
outputDir.deleteRecursively()
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)