From e03c26229a4403cf5e442f1a76d32344b3fdb7cb Mon Sep 17 00:00:00 2001 From: Hari Krishnan Date: Wed, 29 May 2024 15:31:30 +0530 Subject: [PATCH] Adding basedDir optional argument in central-contract-repo-report command --- .../CentralContractRepoReportCommand.kt | 5 ++- ...CentralContractRepoReportCommandTestE2E.kt | 38 +++++++++++++++++-- .../reports/CentralContractRepoReport.kt | 4 +- .../reports/CentralContractRepoReportTest.kt | 17 ++------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/application/src/main/kotlin/application/CentralContractRepoReportCommand.kt b/application/src/main/kotlin/application/CentralContractRepoReportCommand.kt index db1b232bb..c31990dbe 100644 --- a/application/src/main/kotlin/application/CentralContractRepoReportCommand.kt +++ b/application/src/main/kotlin/application/CentralContractRepoReportCommand.kt @@ -20,8 +20,11 @@ class CentralContractRepoReportCommand : Callable { const val REPORT_FILE_NAME = "central_contract_repo_report.json" } + @CommandLine.Option(names = ["--baseDir"], description = ["Directory to treated as the root for API specifications"], defaultValue = "") + lateinit var baseDir: String + override fun call() { - val report = CentralContractRepoReport().generate() + val report = CentralContractRepoReport().generate(baseDir) if(report.specifications.isEmpty()) { logger.log("No specifications found, hence the Central Contract Repo Report has not been generated.") } diff --git a/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt b/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt index bebf2683c..978368d7a 100644 --- a/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt +++ b/application/src/test/kotlin/application/CentralContractRepoReportCommandTestE2E.kt @@ -51,13 +51,45 @@ class CentralContractRepoReportCommandTestE2E { assertThat(reportJson.specifications.contains(expectedSpecificationRow)).isTrue() } + @Test + fun `report only contains specifications within baseDir`() { + createSpecFiles("./specifications/service2/service2.yaml") + centralContractRepoReportCommand.baseDir = "specifications/service2" + centralContractRepoReportCommand.call() + val reportJson: CentralContractRepoReportJson = Json.decodeFromString(reportFile.readText()) + + val expectedSpecificationRow = SpecificationRow( + osAgnosticPath("specifications/service2/service2.yaml"), + "HTTP", + listOf( + SpecificationOperation( + "/hello/{id}", + "GET", + 200 + ), + SpecificationOperation( + "/hello/{id}", + "GET", + 404 + ), + SpecificationOperation( + "/hello/{id}", + "GET", + 400 + ) + ) + ) + + assertThat(reportJson.specifications).containsOnly(expectedSpecificationRow) + } + companion object { private val reportFile = File(osAgnosticPath("./build/reports/specmatic/central_contract_repo_report.json")) @JvmStatic @BeforeAll fun setupBeforeAll() { - createSpecFiles() + createSpecFiles("./specifications/service1/service1.yaml") } @JvmStatic @@ -67,7 +99,7 @@ class CentralContractRepoReportCommandTestE2E { reportFile.delete() } - private fun createSpecFiles() { + private fun createSpecFiles(specFilePath: String) { val service1spec = """ openapi: 3.0.0 info: @@ -111,7 +143,7 @@ paths: schema: type: string """ - val service1File = File(osAgnosticPath("./specifications/service1/service1.yaml")) + val service1File = File(osAgnosticPath(specFilePath)) service1File.parentFile.mkdirs() service1File.writeText(service1spec) } diff --git a/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt b/core/src/main/kotlin/in/specmatic/reports/CentralContractRepoReport.kt index 6f2c3b716..d07efc52c 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() } ?: File("").canonicalPath + val searchPath = currentWorkingDir.takeIf { it.isNotEmpty() }.let { File(it).canonicalPath } ?: File("").canonicalPath logger.log("Searching for specification files at: $searchPath") val specifications = findSpecifications(searchPath) return CentralContractRepoReportJson(getSpecificationRows(specifications.sorted(), searchPath)) @@ -37,7 +37,7 @@ class CentralContractRepoReport { feature.scenarios.isNotEmpty() }.map { (spec, feature) -> SpecificationRow( - spec.relativeTo(currentWorkingDirPath).path, + spec.relativeTo(File("").canonicalFile).path, feature.serviceType, feature.scenarios.map { SpecificationOperation( diff --git a/core/src/test/kotlin/reports/CentralContractRepoReportTest.kt b/core/src/test/kotlin/reports/CentralContractRepoReportTest.kt index 2263aef05..d7ecb2cfa 100644 --- a/core/src/test/kotlin/reports/CentralContractRepoReportTest.kt +++ b/core/src/test/kotlin/reports/CentralContractRepoReportTest.kt @@ -15,13 +15,13 @@ class CentralContractRepoReportTest { @Test fun `test generates report based on all the open api specifications present in the specified dir`() { - val report = CentralContractRepoReport().generate("./specifications") + val report = CentralContractRepoReport().generate("./specifications/service1") assertThat(osAgnosticPaths(report)).isEqualTo( osAgnosticPaths( CentralContractRepoReportJson( listOf( SpecificationRow( - "service1/service1.yaml", + "specifications/service1/service1.yaml", "HTTP", listOf( SpecificationOperation( @@ -40,18 +40,7 @@ class CentralContractRepoReportTest { 400 ) ) - ), - SpecificationRow( - "service2/service2.yaml", - "HTTP", - listOf( - SpecificationOperation( - "/products/{id}", - "GET", - 200 - ) - ) - ), + ) ) ) )