From 7c94aaba5ea409e2384edb57081de37a752be561 Mon Sep 17 00:00:00 2001 From: Joel Rosario Date: Wed, 29 May 2024 17:43:21 +0530 Subject: [PATCH] Added support for recursive check of referred-to files and fixed bug in backward compatibility check --- .../BackwardCompatibilityCheckCommand.kt | 66 +++++++++++-------- ...CentralContractRepoReportCommandTestE2E.kt | 1 + .../reports/CentralContractRepoReport.kt | 2 +- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt b/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt index 01c75977f..3268c626e 100644 --- a/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt +++ b/application/src/main/kotlin/application/BackwardCompatibilityCheckCommand.kt @@ -32,12 +32,13 @@ open class BackwardCompatibilityCheckCommand( override fun call() { val filesChangedInCurrentBranch: Set = getOpenAPISpecFilesChangedInCurrentBranch() - if (filesChangedInCurrentBranch.isEmpty()) exitWithMessage("$newLine OpenAPI spec files were changed, skipping the check.$newLine") + if (filesChangedInCurrentBranch.isEmpty()) exitWithMessage("${newLine}No OpenAPI spec files were changed, skipping the check.$newLine") val filesReferringToChangedSchemaFiles = filesReferringToChangedSchemaFiles(filesChangedInCurrentBranch) val filesToCheck: Set = filesChangedInCurrentBranch + filesReferringToChangedSchemaFiles + logFilesToBeCheckedForBackwardCompatibility( filesChangedInCurrentBranch, filesReferringToChangedSchemaFiles @@ -52,43 +53,54 @@ open class BackwardCompatibilityCheckCommand( } private fun runBackwardCompatibilityCheckFor(files: Set): String { - val currentBranch = gitCommand.currentBranch() - val currentTreeish = if (currentBranch == HEAD) gitCommand.detachedHEAD() else currentBranch + val branchWithChanges = gitCommand.currentBranch() + val treeishWithChanges = if (branchWithChanges == HEAD) gitCommand.detachedHEAD() else branchWithChanges try { val failures = files.mapIndexed { index, specFilePath -> - println("${index.inc()}. Running the check for $specFilePath:") + try { + println("${index.inc()}. Running the check for $specFilePath:") - // newer => the file with changes on the branch - val newer = OpenApiSpecification.fromFile(specFilePath).toFeature() + // newer => the file with changes on the branch + val newer = OpenApiSpecification.fromFile(specFilePath).toFeature() - val olderFile = gitCommand.getFileInTheDefaultBranch(specFilePath, currentTreeish) - if (olderFile == null) { - println("$specFilePath is a new file.$newLine") - return@mapIndexed SUCCESS - } - // older => the same file on the default (e.g. main) branch - val older = OpenApiSpecification.fromFile(olderFile.path).toFeature() - - val backwardCompatibilityResult = testBackwardCompatibility(older, newer) - - if (backwardCompatibilityResult.success()) { - println("$newLine The file $specFilePath is backward compatible.$newLine".prependIndent(MARGIN_SPACE)) - SUCCESS - } else { - println("$newLine ${backwardCompatibilityResult.report().prependIndent(MARGIN_SPACE)}") - println( - "$newLine *** The file $specFilePath is NOT backward compatible. ***$newLine".prependIndent( - MARGIN_SPACE + val olderFile = gitCommand.getFileInTheDefaultBranch(specFilePath, treeishWithChanges) + if (olderFile == null) { + println("$specFilePath is a new file.$newLine") + return@mapIndexed SUCCESS + } + + gitCommand.checkout(gitCommand.defaultBranch()) + + // older => the same file on the default (e.g. main) branch + val older = OpenApiSpecification.fromFile(olderFile.path).toFeature() + + val backwardCompatibilityResult = testBackwardCompatibility(older, newer) + + if (backwardCompatibilityResult.success()) { + println( + "$newLine The file $specFilePath is backward compatible.$newLine".prependIndent( + MARGIN_SPACE + ) + ) + SUCCESS + } else { + println("$newLine ${backwardCompatibilityResult.report().prependIndent(MARGIN_SPACE)}") + println( + "$newLine *** The file $specFilePath is NOT backward compatible. ***$newLine".prependIndent( + MARGIN_SPACE + ) ) - ) - FAILED + FAILED + } + } finally { + gitCommand.checkout(treeishWithChanges) } }.filter { it == FAILED } return if (failures.isNotEmpty()) FAILED else SUCCESS } finally { - gitCommand.checkout(currentTreeish) + gitCommand.checkout(treeishWithChanges) } } diff --git a/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt b/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt index 978368d7a..cfac1918f 100644 --- a/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt +++ b/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt @@ -23,6 +23,7 @@ class CentralContractRepoReportCommandTestE2E { @Test fun `test generates report json file`() { + centralContractRepoReportCommand.baseDir = "" centralContractRepoReportCommand.call() val reportJson: CentralContractRepoReportJson = Json.decodeFromString(reportFile.readText()) diff --git a/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt b/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt index d07efc52c..14c3da330 100644 --- a/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt +++ b/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt @@ -10,7 +10,7 @@ import java.io.File class CentralContractRepoReport { fun generate(currentWorkingDir: String = ""): CentralContractRepoReportJson { - val searchPath = currentWorkingDir.takeIf { it.isNotEmpty() }.let { File(it).canonicalPath } ?: File("").canonicalPath + val searchPath = File(currentWorkingDir).canonicalPath logger.log("Searching for specification files at: $searchPath") val specifications = findSpecifications(searchPath) return CentralContractRepoReportJson(getSpecificationRows(specifications.sorted(), searchPath))