Skip to content

Commit

Permalink
#793 better error message on file not found (#795)
Browse files Browse the repository at this point in the history
* release_notes, move file loading to function with custom exception

* Rename Yaml Loader to more generic File Loader

* Made changes requested by prs
  • Loading branch information
adamfilipow92 authored May 20, 2020
1 parent 71aac52 commit 08b7866
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 31 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [#784](https://github.com/Flank/flank/pull/784) Add output-style option. ([jan-gogo](https://github.com/jan-gogo))
- [#779](https://github.com/Flank/flank/pull/779) Print retries & display additional info. ([jan-gogo](https://github.com/jan-gogo))
- [#793](https://github.com/Flank/flank/issues/793) Better error message on file not found. ([adamfilipow92](https://github.com/adamfilipow92))
-
-

Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/AndroidArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import ftl.args.yml.GcloudYml
import ftl.args.yml.AppTestPair
import ftl.args.yml.AndroidGcloudYmlParams
import ftl.args.yml.YamlDeprecated
import ftl.util.loadFile
import ftl.cli.firebase.test.android.AndroidRunCommand
import ftl.config.Device
import ftl.config.FtlConstants
import ftl.config.parseRoboDirectives
import ftl.run.status.asOutputStyle
import ftl.util.FlankFatalError
import java.io.Reader
import java.nio.file.Files
import java.nio.file.Path

// set default values, init properties, etc.
Expand Down Expand Up @@ -197,7 +197,7 @@ AndroidArgs
mergeYmlMaps(GcloudYml, AndroidGcloudYml, FlankYml, AndroidFlankYml)
}

fun load(yamlPath: Path, cli: AndroidRunCommand? = null): AndroidArgs = load(Files.newBufferedReader(yamlPath), cli)
fun load(yamlPath: Path, cli: AndroidRunCommand? = null): AndroidArgs = load(loadFile(yamlPath), cli)

@VisibleForTesting
internal fun load(yamlReader: Reader, cli: AndroidRunCommand? = null): AndroidArgs {
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/IosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ftl.args.yml.IosFlankYml
import ftl.args.yml.IosGcloudYml
import ftl.args.yml.IosGcloudYmlParams
import ftl.args.yml.YamlDeprecated
import ftl.util.loadFile
import ftl.cli.firebase.test.ios.IosRunCommand
import ftl.config.Device
import ftl.config.FtlConstants
Expand All @@ -27,7 +28,6 @@ import ftl.run.status.asOutputStyle
import ftl.util.FlankFatalError
import ftl.util.FlankTestMethod
import java.io.Reader
import java.nio.file.Files
import java.nio.file.Path

class IosArgs(
Expand Down Expand Up @@ -162,7 +162,7 @@ IosArgs
mergeYmlMaps(GcloudYml, IosGcloudYml, FlankYml, IosFlankYml)
}

fun load(yamlPath: Path, cli: IosRunCommand? = null): IosArgs = load(Files.newBufferedReader(yamlPath), cli)
fun load(yamlPath: Path, cli: IosRunCommand? = null): IosArgs = load(loadFile(yamlPath), cli)

@VisibleForTesting
internal fun load(yamlReader: Reader, cli: IosRunCommand? = null): IosArgs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package ftl.args.yml

import com.fasterxml.jackson.databind.JsonNode
import ftl.args.yml.errors.ConfigurationErrorMessageBuilder
import ftl.util.FlankConfigurationException
import ftl.util.FlankFatalError

fun convertConfigurationErrorExceptions(missingParameterError: Exception, yaml: JsonNode): Throwable {
val errorMessageBuilder = ConfigurationErrorMessageBuilder
val errorMessage = missingParameterError.message
return if (errorMessage != null) {
FlankConfigurationException(errorMessageBuilder(errorMessage, yaml))
FlankFatalError(errorMessageBuilder(errorMessage, yaml))
} else {
missingParameterError
}
Expand Down
3 changes: 2 additions & 1 deletion test_runner/src/main/kotlin/ftl/args/yml/YamlDeprecated.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.MissingNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.common.annotations.VisibleForTesting
import ftl.args.ArgsHelper.yamlMapper
import ftl.util.loadFile
import ftl.util.FlankFatalError
import java.io.Reader
import java.nio.file.Files
Expand Down Expand Up @@ -125,7 +126,7 @@ object YamlDeprecated {
fun modify(yamlPath: Path): Boolean {
if (yamlPath.toFile().exists().not()) throw FlankFatalError("Flank yml doesn't exist at path $yamlPath")

val (errorDetected, string) = modify(Files.newBufferedReader(yamlPath))
val (errorDetected, string) = modify(loadFile(yamlPath))

Files.write(yamlPath, string.toByteArray())
println("\nUpdated ${yamlPath.fileName} file")
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/doctor/Doctor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package ftl.doctor
import com.google.common.annotations.VisibleForTesting
import ftl.args.ArgsHelper
import ftl.args.IArgsCompanion
import ftl.util.loadFile
import java.io.Reader
import java.nio.file.Files
import java.nio.file.Path

object Doctor {
fun validateYaml(args: IArgsCompanion, data: Path): String {
if (!data.toFile().exists()) return "Skipping yaml validation. No file at path $data"
return validateYaml(args, Files.newBufferedReader(data))
return validateYaml(args, loadFile(data))
}

@VisibleForTesting
Expand Down
12 changes: 12 additions & 0 deletions test_runner/src/main/kotlin/ftl/util/FileLoader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ftl.util

import java.io.Reader
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path

fun loadFile(filePath: Path): Reader = try {
Files.newBufferedReader(filePath)
} catch (fileNotFound: NoSuchFileException) {
throw FlankFatalError("File not found: ${fileNotFound.message}")
}
9 changes: 0 additions & 9 deletions test_runner/src/main/kotlin/ftl/util/FlankException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,3 @@ class FlankFatalError(message: String) : FlankException(message)
* @param message [String] message to be printed to [System.err]
*/
class FlankCommonException(message: String) : FlankException(message)

/**
* Exception throws when required parameters is missing
*
* Exit code: 1
*
* @param message [String] message to be printed to [System.err]
*/
class FlankConfigurationException(message: String) : FlankException(message)
4 changes: 0 additions & 4 deletions test_runner/src/main/kotlin/ftl/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ fun withGlobalExceptionHandling(block: () -> Int) {
}
exitProcess(1)
}
is FlankConfigurationException -> {
System.err.println(t)
exitProcess(1)
}
is FTLError -> {
t.matrix.logError("not finished")
exitProcess(3)
Expand Down
4 changes: 2 additions & 2 deletions test_runner/src/test/kotlin/ftl/MainTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class MainTest {
}

@Test
fun `should terminate jvm with exit status 1 if yml parsing error occurs`() {
systemExit.expectSystemExitWithStatus(1)
fun `should terminate jvm with exit status 2 if yml parsing error occurs`() {
systemExit.expectSystemExitWithStatus(2)
Main.main(arrayOf(
"firebase",
"test",
Expand Down
14 changes: 7 additions & 7 deletions test_runner/src/test/kotlin/ftl/args/yml/ErrorParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ftl.args.AndroidArgs
import ftl.args.yml.errors.ConfigurationErrorMessageBuilder
import ftl.test.util.TestHelper
import ftl.test.util.TestHelper.getThrowable
import ftl.util.FlankConfigurationException
import ftl.util.FlankFatalError
import org.junit.Assert
import org.junit.Test

Expand Down Expand Up @@ -41,8 +41,8 @@ At line: 23, column: 3
Assert.assertEquals(expected, buildErrorMessage(instantionError))
}

@Test(expected = FlankConfigurationException::class)
fun `should throw FlankConfigException without device version`() {
@Test(expected = FlankFatalError::class)
fun `should throw FlankFatalError without device version`() {
AndroidArgs.load(yamlWithoutDeviceVersion)
}

Expand Down Expand Up @@ -78,13 +78,13 @@ Error node: {
Assert.assertEquals(exceptedMessage, actualMessage)
}

@Test(expected = FlankConfigurationException::class)
fun `should throw FlankConfigException without model name`() {
@Test(expected = FlankFatalError::class)
fun `should throw FlankFatalError without model name`() {
AndroidArgs.load(yamlNoModelName)
}

@Test(expected = FlankConfigurationException::class)
fun `should throw FlankConfigException without model node`() {
@Test(expected = FlankFatalError::class)
fun `should throw FlankFatalError without model node`() {
AndroidArgs.load(yamlNoModelNode)
}
}
29 changes: 29 additions & 0 deletions test_runner/src/test/kotlin/ftl/args/yml/FileLoaderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ftl.args.yml

import ftl.util.loadFile
import ftl.test.util.TestHelper.getThrowable
import ftl.util.FlankFatalError
import org.junit.Assert
import org.junit.Test
import java.nio.file.Paths
import java.util.UUID

class FileLoaderTest {
@Test(expected = FlankFatalError::class)
fun `should throws FlankFatalError when file not found`() {
val filePath = Paths.get("${UUID.randomUUID()}.yml")
loadFile(filePath)
}

@Test
fun `should throws FlankFatalError with specific message when file not found`() {
val filePath = Paths.get("${UUID.randomUUID()}.yml")
val thrownException = getThrowable { loadFile(filePath) }

val expectedExceptionMessage = "File not found: $filePath"
Assert.assertEquals(expectedExceptionMessage, thrownException.message)

val expectedExceptionType = FlankFatalError::class
Assert.assertEquals(expectedExceptionType, thrownException::class)
}
}

0 comments on commit 08b7866

Please sign in to comment.