-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/3298/output-error-for-nonexistent-file-params (#3305)
* Add error message if file param nonexistent #3298 * Add check for empty file input #3298 --------- Co-authored-by: Phanlezz <[email protected]>
- Loading branch information
1 parent
020fcc4
commit 118ab61
Showing
5 changed files
with
162 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
analysis/model/src/main/kotlin/de/maibornwolff/codecharta/util/InputHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.maibornwolff.codecharta.util | ||
|
||
import mu.KotlinLogging | ||
import java.io.File | ||
|
||
class InputHelper { | ||
companion object { | ||
private val logger = KotlinLogging.logger {} | ||
|
||
fun getAndCheckAllSpecifiedInputFiles(inputFiles: Array<File>): MutableList<File> { | ||
if (inputFiles.isEmpty()) { | ||
logger.error("Did not find any input files!") | ||
return mutableListOf() | ||
} | ||
|
||
val resultList = mutableListOf<File>() | ||
var doesInputContainNonexistentFile = false | ||
|
||
for (source in inputFiles) { | ||
if (!source.exists()) { | ||
logger.error("Could not find file `${ source.path }` and did not merge!") | ||
doesInputContainNonexistentFile = true | ||
} else { | ||
resultList.addAll(getFilesInFolder(source)) | ||
} | ||
} | ||
|
||
return if (doesInputContainNonexistentFile) { | ||
mutableListOf() | ||
} else { | ||
resultList | ||
} | ||
} | ||
|
||
private fun getFilesInFolder(folder: File): List<File> { | ||
val files = folder.walk().filter { !it.name.startsWith(".") && !it.isDirectory } | ||
return files.toList() | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
analysis/model/src/test/kotlin/de/maibornwolff/codecharta/util/InputHelperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package de.maibornwolff.codecharta.util | ||
|
||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import java.io.ByteArrayOutputStream | ||
import java.io.File | ||
import java.io.PrintStream | ||
|
||
class InputHelperTest { | ||
val outContent = ByteArrayOutputStream() | ||
val originalOut = System.out | ||
val errContent = ByteArrayOutputStream() | ||
val originalErr = System.err | ||
|
||
@Test | ||
fun `should output warning for all files from parameters which were not found`() { | ||
System.setOut(PrintStream(outContent)) | ||
System.setErr(PrintStream(errContent)) | ||
|
||
val inputFiles = arrayOf(File("src/test/resources/example.cc.json"), | ||
File("src/test/resources/example_api_version_1.3.cc.json"), | ||
File("src/test/resources/thisDoesNotExist1.json"), | ||
File("src/test/resources/thisDoesNotExist2.json")) | ||
InputHelper.getAndCheckAllSpecifiedInputFiles(inputFiles) | ||
|
||
System.setOut(originalOut) | ||
System.setErr(originalErr) | ||
|
||
Assertions.assertThat(errContent.toString()) | ||
.contains("thisDoesNotExist1.json` and did not merge!") | ||
Assertions.assertThat(errContent.toString()) | ||
.contains("thisDoesNotExist2.json` and did not merge!") | ||
} | ||
|
||
@Test | ||
fun `should return empty list if input contains one nonexistent file`() { | ||
val inputFiles = arrayOf(File("src/test/resources/example.cc.json"), | ||
File("src/test/resources/example_api_version_1.3.cc.json"), | ||
File("src/test/resources/thisDoesNotExist1.json")) | ||
|
||
val result = InputHelper.getAndCheckAllSpecifiedInputFiles(inputFiles) | ||
Assertions.assertThat(result).isEmpty() | ||
} | ||
|
||
@Test | ||
fun `should return empty list if no files are specified`() { | ||
val inputFiles = arrayOf<File>() | ||
|
||
System.setErr(PrintStream(errContent)) | ||
val result = InputHelper.getAndCheckAllSpecifiedInputFiles(inputFiles) | ||
System.setErr(originalErr) | ||
|
||
Assertions.assertThat(result).isEmpty() | ||
Assertions.assertThat(errContent.toString()) | ||
.contains("Did not find any input files!") | ||
} | ||
|
||
@Test | ||
fun `should return list of all input files if all exist`() { | ||
val validFile1 = File("src/test/resources/example.cc.json") | ||
val validFile2 = File("src/test/resources/example_api_version_1.3.cc.json") | ||
val validFile3 = File("src/test/resources/exampleUncompressed.txt") | ||
|
||
val inputFiles = arrayOf(validFile1, validFile2, validFile3) | ||
|
||
val result = InputHelper.getAndCheckAllSpecifiedInputFiles(inputFiles) | ||
|
||
Assertions.assertThat(result).contains(validFile1) | ||
Assertions.assertThat(result).contains(validFile2) | ||
Assertions.assertThat(result).contains(validFile3) | ||
|
||
Assertions.assertThat(result.size).isEqualTo(inputFiles.size) | ||
} | ||
} |