Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log error when invalid contract paths are selected by user #1140

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/kotlin/in/specmatic/core/utilities/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ fun examplesDirFor(openApiFilePath: String, alternateSuffix: String): File {
getExamplesDir(openApiFilePath, alternateSuffix)
}

fun checkIfContractPathsAreValid(contractPaths: List<String>) {
val validContractPaths = contractPaths.filter { path ->
File(path).extension in CONTRACT_EXTENSIONS
}
if (validContractPaths.isEmpty()) {
throw ContractException("No valid specification file found at given paths : ${contractPaths.joinToString(", ")}")
}
}

private fun getExamplesDir(openApiFilePath: String, suffix: String): File =
File(openApiFilePath).canonicalFile.let {
it.parentFile.resolve("${it.parent}/${it.nameWithoutExtension}$suffix")
Expand Down
9 changes: 5 additions & 4 deletions core/src/main/kotlin/in/specmatic/stub/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import `in`.specmatic.core.git.SystemGit
import `in`.specmatic.core.log.StringLog
import `in`.specmatic.core.log.consoleLog
import `in`.specmatic.core.log.logger
import `in`.specmatic.core.utilities.ContractPathData
import `in`.specmatic.core.utilities.contractStubPaths
import `in`.specmatic.core.utilities.examplesDirFor
import `in`.specmatic.core.utilities.exitIfDoesNotExist
import `in`.specmatic.core.utilities.*
import `in`.specmatic.core.value.StringValue
import `in`.specmatic.mock.NoMatchingScenario
import `in`.specmatic.mock.ScenarioStub
Expand Down Expand Up @@ -70,6 +67,8 @@ fun createStubFromContracts(
host: String = "localhost",
port: Int = 9000
): ContractStub {
checkIfContractPathsAreValid(contractPaths)

return createStubFromContracts(
contractPaths,
dataDirPaths,
Expand All @@ -81,6 +80,8 @@ fun createStubFromContracts(

// Used by stub client code
fun createStubFromContracts(contractPaths: List<String>, host: String = "localhost", port: Int = 9000): ContractStub {
checkIfContractPathsAreValid(contractPaths)

return createStubFromContracts(
contractPaths,
host,
Expand Down
29 changes: 25 additions & 4 deletions core/src/test/kotlin/utilities/UtilitiesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import `in`.specmatic.core.git.GitCommand
import `in`.specmatic.core.git.SystemGit
import `in`.specmatic.core.git.checkout
import `in`.specmatic.core.git.clone
import `in`.specmatic.core.pattern.ContractException
import `in`.specmatic.core.pattern.parsedJSON
import `in`.specmatic.core.pattern.parsedJSONObject
import `in`.specmatic.core.utilities.*
Expand All @@ -22,10 +23,7 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.mockk.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.*
import java.io.File
import java.net.ServerSocket

Expand Down Expand Up @@ -535,6 +533,29 @@ internal class UtilitiesTest {
}
}

@Nested
inner class CheckIfContractPathsAreValidTests {
@Test
fun `should not throw an exception if the contract paths are valid`() {
val contractPaths = listOf("first.yaml", "second.yml", "third.json")

assertDoesNotThrow {
checkIfContractPathsAreValid(contractPaths)
}
}

@Test
fun `should throw an exception if none of the contract paths is valid`() {
val contractPaths = listOf("first.random", "second.random", "third.random")

val e = assertThrows<ContractException> {
checkIfContractPathsAreValid(contractPaths)
}

assertThat(e.errorMessage == "No valid specification file found at given paths: first.random, second.random, third.random")
}
}

private fun deleteGitIgnoreFile(){
File(".gitignore").delete()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ open class SpecmaticJUnitSupport {
if(!mbs.isRegistered(name))
mbs.registerMBean(statistics, name)

val contractPaths = System.getProperty(CONTRACT_PATHS)
val contractPaths = System.getProperty(CONTRACT_PATHS)?.split(",").orEmpty()
checkIfContractPathsAreValid(contractPaths)

val givenWorkingDirectory = System.getProperty(WORKING_DIRECTORY)
val filterName: String? = System.getProperty(FILTER_NAME_PROPERTY) ?: System.getenv(FILTER_NAME_ENVIRONMENT_VARIABLE)
val filterNotName: String? = System.getProperty(FILTER_NOT_NAME_PROPERTY) ?: System.getenv(FILTER_NOT_NAME_ENVIRONMENT_VARIABLE)
Expand All @@ -206,10 +208,8 @@ open class SpecmaticJUnitSupport {
}
val testScenarios = try {
val (testScenarios, allEndpoints) = when {
contractPaths != null -> {
val testScenariosAndEndpointsPairList = contractPaths.split(",").filter {
File(it).extension in CONTRACT_EXTENSIONS
}.map {
(contractPaths != null) -> {
val testScenariosAndEndpointsPairList = contractPaths.map {
loadTestScenarios(
it,
suggestionsPath,
Expand Down