Skip to content

Commit

Permalink
Adding basedDir optional argument in central-contract-repo-report com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
harikrishnan83 committed May 29, 2024
1 parent dbbf0d4 commit e03c262
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class CentralContractRepoReportCommand : Callable<Unit> {
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.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -67,7 +99,7 @@ class CentralContractRepoReportCommandTestE2E {
reportFile.delete()
}

private fun createSpecFiles() {
private fun createSpecFiles(specFilePath: String) {
val service1spec = """
openapi: 3.0.0
info:
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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(
Expand Down
17 changes: 3 additions & 14 deletions core/src/test/kotlin/reports/CentralContractRepoReportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -40,18 +40,7 @@ class CentralContractRepoReportTest {
400
)
)
),
SpecificationRow(
"service2/service2.yaml",
"HTTP",
listOf(
SpecificationOperation(
"/products/{id}",
"GET",
200
)
)
),
)
)
)
)
Expand Down

0 comments on commit e03c262

Please sign in to comment.