Skip to content

Commit

Permalink
Merge pull request #1133 from znsio/exact_schema_file_name_match_for_BC
Browse files Browse the repository at this point in the history
Do exact schema file name match in backwardCompatibilityCheckCommand
  • Loading branch information
joelrosario authored Jun 3, 2024
2 parents 39c882b + 866dade commit f6ec594
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import `in`.specmatic.core.git.GitCommand
import `in`.specmatic.core.git.SystemGit
import `in`.specmatic.core.testBackwardCompatibility
import `in`.specmatic.core.utilities.exitWithMessage
import kotlinx.serialization.json.Json
import org.springframework.stereotype.Component
import picocli.CommandLine.Command
import java.io.File
import java.util.concurrent.Callable
import java.util.regex.Pattern

@Component
@Command(
name = "backwardCompatibilityCheck",
mixinStandardHelpOptions = true,
description = ["Checks backward compatibility of a directory across the current HEAD and the main branch"]
)
open class BackwardCompatibilityCheckCommand(
class BackwardCompatibilityCheckCommand(
private val gitCommand: GitCommand = SystemGit(),
) : Callable<Unit> {

Expand Down Expand Up @@ -120,13 +122,17 @@ open class BackwardCompatibilityCheckCommand(
println()
}

internal fun filesReferringToChangedSchemaFiles(schemaFiles: Set<String>): Set<String> {
if (schemaFiles.isEmpty()) return emptySet()
internal fun filesReferringToChangedSchemaFiles(inputFiles: Set<String>): Set<String> {
if (inputFiles.isEmpty()) return emptySet()

val schemaFileBaseNames = schemaFiles.map { File(it).name }
val inputFileNames = inputFiles.map { File(it).name }
val result = allOpenApiSpecFiles().filter {
it.readText().let { specContent ->
schemaFileBaseNames.any { schemaFileBaseName -> schemaFileBaseName in specContent }
it.readText().trim().let { specContent ->
inputFileNames.any { inputFileName ->
val pattern = Pattern.compile("\\b$inputFileName\\b")
val matcher = pattern.matcher(specContent)
matcher.find()
}
}
}.map { it.path }.toSet()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,36 @@ class BackwardCompatibilityCheckCommandTest {
assertEquals(setOf("file1.yaml"), result)
}

@Test
fun `filesReferringToChangedSchemaFiles returns set of files which are referring to a changed schema that is one level down`() {
val command = spyk<BackwardCompatibilityCheckCommand>()
every { command.allOpenApiSpecFiles() } returns listOf(
File("file1.yaml").apply { referTo("schema_file1.yaml") },
File("schema_file2.yaml").apply { referTo("schema_file1.yaml") }, // schema within a schema
File("file2.yaml").apply { referTo("schema_file2.yaml") }
)
val result = command.filesReferringToChangedSchemaFiles(setOf("schema_file1.yaml"))
assertEquals(setOf("file1.yaml", "file2.yaml"), result)
}

@AfterEach
fun `cleanup files`() {
listOf(File("file1.yaml"), File("file2.yaml")).forEach {
it.delete()
listOf("file1.yaml", "file2.yaml", "file3.yaml", "file4.yaml", "schema_file1.yaml", "schema_file2.yaml").forEach {
File(it).delete()
}
}

private fun File.referTo(schemaFileName: String) {
val specContent = """
openapi: 3.1.0 # OpenAPI version specified here
info:
title: My API
version: 1.0.0
components:
schemas:
User:
${"$"}ref: '#/components/schemas/$schemaFileName'
""".trimIndent()
this.writeText(specContent)
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.3.22
version=1.3.23

0 comments on commit f6ec594

Please sign in to comment.