Skip to content

Commit

Permalink
Merge pull request #1368 from znsio/config_file_from_classpath
Browse files Browse the repository at this point in the history
Loading config file from classpath Also in case of env property or classpath checking if the file exists before returning
  • Loading branch information
harikrishnan83 authored Oct 20, 2024
2 parents 5196bbb + 8cf687d commit f0600c7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
26 changes: 16 additions & 10 deletions core/src/main/kotlin/io/specmatic/core/Configuration.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package io.specmatic.core

import io.specmatic.core.utilities.Flags
import io.specmatic.core.utilities.Flags.Companion.CONFIG_FILE_PATH
import io.specmatic.core.utilities.Flags.Companion.getStringValue
import java.io.File

//TODO: This is a temporary solution need to delete this from Kafka and other dependencies
fun getConfigFileName(): String = getConfigFilePath()
fun getConfigFilePath(): String {
Flags.getStringValue(CONFIG_FILE_PATH)?.let {
return it
}
val configFileNameWithoutExtension = ".${File.separator}${APPLICATION_NAME_LOWER_CASE}"
val supportedExtensions = listOf(JSON, YAML, YML)
return getStringValue(CONFIG_FILE_PATH)?.takeIf { File(it).exists() }
?: getConfigFilePathFromClasspath()?.takeIf { File(it).exists() }
?: getConfigFilePath(".${File.separator}")
}

val configFileExtension = supportedExtensions.firstOrNull { extension ->
File("$configFileNameWithoutExtension.$extension").exists()
} ?: JSON
private fun getConfigFilePathFromClasspath(): String? {
return CONFIG_EXTENSIONS.firstNotNullOfOrNull {
Configuration::class.java.getResource("/$CONFIG_FILE_NAME_WITHOUT_EXT.$it")?.path
}
}

return "$configFileNameWithoutExtension.$configFileExtension"
private fun getConfigFilePath(filePathPrefix: String): String {
val configFileNameWithoutExtension = "$filePathPrefix${CONFIG_FILE_NAME_WITHOUT_EXT}"
return CONFIG_EXTENSIONS.firstNotNullOfOrNull {
val filePath = "$configFileNameWithoutExtension.$it"
filePath.takeIf { File(filePath).exists() }
} ?: "$configFileNameWithoutExtension.$YAML"
}

class Configuration {
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/kotlin/io/specmatic/core/SpecmaticConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ import java.io.File

const val APPLICATION_NAME = "Specmatic"
const val APPLICATION_NAME_LOWER_CASE = "specmatic"
const val CONFIG_FILE_NAME_WITHOUT_EXT = "specmatic"
const val DEFAULT_TIMEOUT_IN_MILLISECONDS: Long = 6000L
const val CONTRACT_EXTENSION = "spec"
const val YAML = "yaml"
const val WSDL = "wsdl"
const val YML = "yml"
const val JSON = "json"
val CONFIG_EXTENSIONS = listOf(YAML, YML, JSON)
val OPENAPI_FILE_EXTENSIONS = listOf(YAML, YML, JSON)
val CONTRACT_EXTENSIONS = listOf(CONTRACT_EXTENSION, WSDL) + OPENAPI_FILE_EXTENSIONS
const val DATA_DIR_SUFFIX = "_data"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import io.specmatic.core.log.ignoreLog
import io.specmatic.core.log.logger
import io.specmatic.core.pattern.*
import io.specmatic.core.utilities.*
import io.specmatic.core.utilities.Flags.Companion.CONFIG_FILE_PATH
import io.specmatic.core.utilities.Flags.Companion.SPECMATIC_TEST_TIMEOUT
import io.specmatic.core.utilities.Flags.Companion.getLongValue
import io.specmatic.core.value.JSONArrayValue
import io.specmatic.core.value.JSONObjectValue
import io.specmatic.core.value.Value
Expand Down Expand Up @@ -160,7 +161,7 @@ open class SpecmaticJUnitSupport {
}
}

val configFile get() = Flags.getStringValue(CONFIG_FILE_PATH) ?: getConfigFilePath()
val configFile get() = getConfigFilePath()

private fun getConfigFileWithAbsolutePath() = File(configFile).canonicalPath
}
Expand Down Expand Up @@ -207,7 +208,11 @@ open class SpecmaticJUnitSupport {

specmaticConfig = getSpecmaticConfig()

val timeoutInMilliseconds = specmaticConfig?.test?.timeoutInMilliseconds ?: try { Flags.getLongValue(Flags.SPECMATIC_TEST_TIMEOUT) } catch(e: NumberFormatException) { throw ContractException("${Flags.SPECMATIC_TEST_TIMEOUT} should be a value of type long") } ?: DEFAULT_TIMEOUT_IN_MILLISECONDS
val timeoutInMilliseconds = specmaticConfig?.test?.timeoutInMilliseconds ?: try {
getLongValue(SPECMATIC_TEST_TIMEOUT)
} catch (e: NumberFormatException) {
throw ContractException("$SPECMATIC_TEST_TIMEOUT should be a value of type long")
} ?: DEFAULT_TIMEOUT_IN_MILLISECONDS

val suggestionsData = System.getProperty(INLINE_SUGGESTIONS) ?: ""
val suggestionsPath = System.getProperty(SUGGESTIONS_PATH) ?: ""
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.0.28
version=2.0.29

0 comments on commit f0600c7

Please sign in to comment.