Skip to content

Commit c8600fd

Browse files
committed
refactor BCVApiCheckTask.kt to try and make it more legible
1 parent 25d8db1 commit c8600fd

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

modules/bcv-gradle-plugin-functional-tests/src/functionalTest/kotlin/kotlinx/validation/test/JavaTestFixturesTest.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ class JavaTestFixturesTest : FunSpec({
2828
}
2929

3030
test("expect :apiCheck task passes") {
31-
project.runner.withArguments("apiCheck").build {
32-
withClue(output) {
33-
task(":apiCheck") shouldHaveOutcome SUCCESS
31+
project.runner
32+
.withArguments(
33+
"apiCheck",
34+
"--info",
35+
"--stacktrace",
36+
)
37+
.build {
38+
withClue(output) {
39+
task(":apiCheck") shouldHaveOutcome SUCCESS
40+
}
3441
}
35-
}
3642
}
3743

3844
test("expect correct kotlinJvm api declaration is generated") {
@@ -74,11 +80,17 @@ class JavaTestFixturesTest : FunSpec({
7480
}
7581

7682
test("expect :apiCheck task passes") {
77-
project.runner.withArguments("apiCheck").build {
78-
withClue(output) {
79-
task(":apiCheck") shouldHaveOutcome SUCCESS
83+
project.runner
84+
.withArguments(
85+
"apiCheck",
86+
"--info",
87+
"--stacktrace",
88+
)
89+
.build {
90+
withClue(output) {
91+
task(":apiCheck") shouldHaveOutcome SUCCESS
92+
}
8093
}
81-
}
8294
}
8395

8496
test("expect correct kotlinJvm api declaration is generated") {

modules/bcv-gradle-plugin/src/main/kotlin/tasks/BCVApiCheckTask.kt

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,58 +55,45 @@ abstract class BCVApiCheckTask @Inject constructor(
5555
""".trimIndent()
5656
)
5757

58-
val expectedApiDeclarations = setOfRelativePaths()
59-
objects.fileTree().from(projectApiDir)
60-
.visit {
61-
if (!isDirectory) expectedApiDeclarations += relativePath
62-
}
63-
logger.info("expectedApiDeclarations: $expectedApiDeclarations")
58+
val checkApiDeclarationPaths = projectApiDir.relativePathsOfContent { !isDirectory }
59+
logger.info("checkApiDeclarationPaths: $checkApiDeclarationPaths")
6460

65-
expectedApiDeclarations.forEach { expectedApiDeclaration ->
61+
checkApiDeclarationPaths.forEach { checkApiDeclarationPath ->
6662
logger.info("---------------------------")
6763
checkTarget(
68-
projectApiDir.resolve(expectedApiDeclaration.pathString),
69-
apiBuildDir.get().asFile.resolve(expectedApiDeclaration.pathString),
64+
checkApiDeclaration = checkApiDeclarationPath.getFile(projectApiDir),
65+
builtApiDeclaration = checkApiDeclarationPath.getFile(apiBuildDir.get().asFile)
7066
)
7167
logger.info("---------------------------")
7268
}
7369
}
7470

7571
private fun checkTarget(
76-
projectApiDeclaration: File,
77-
buildApiDeclaration: File,
72+
checkApiDeclaration: File,
73+
builtApiDeclaration: File,
7874
) {
79-
logger.info("projectApiDeclaration: $projectApiDeclaration")
80-
logger.info("buildApiDeclaration: $buildApiDeclaration")
75+
logger.info("checkApiDeclaration: $checkApiDeclaration")
76+
logger.info("builtApiDeclaration: $builtApiDeclaration")
8177

82-
val apiBuildDirFiles = setOfRelativePaths()
83-
objects.fileTree().from(buildApiDeclaration.parent)
84-
.visit {
85-
if (!isDirectory) apiBuildDirFiles += relativePath
86-
}
87-
val expectedApiFiles = setOfRelativePaths()
88-
objects.fileTree().from(projectApiDeclaration.parent)
89-
.visit {
90-
if (!isDirectory) expectedApiFiles += relativePath
91-
}
78+
val allBuiltFilePaths = builtApiDeclaration.parentFile.relativePathsOfContent()
79+
val allCheckFilePaths = checkApiDeclaration.parentFile.relativePathsOfContent()
9280

93-
logger.info("apiBuildDirFiles: $apiBuildDirFiles")
94-
logger.info("expectedApiFiles: $expectedApiFiles")
81+
logger.info("allBuiltPaths: $allBuiltFilePaths")
82+
logger.info("allCheckFiles: $allCheckFilePaths")
9583

96-
val expectedApiDeclaration = apiBuildDirFiles.singleOrNull()
97-
?: error("Expected a single file ${expectedProjectName.get()}.api, but found ${apiBuildDirFiles.size}: $apiBuildDirFiles")
84+
val builtFilePath = allBuiltFilePaths.singleOrNull()
85+
?: error("Expected a single file ${expectedProjectName.get()}.api, but found ${allBuiltFilePaths.size}: $allBuiltFilePaths")
9886

99-
if (expectedApiDeclaration !in expectedApiFiles) {
87+
if (builtFilePath !in allCheckFilePaths) {
10088
val relativeDirPath = projectApiDir.get().toRelativeString(rootDir) + File.separator
10189
error(
102-
"File ${expectedApiDeclaration.lastName} is missing from ${relativeDirPath}, please run '$apiDumpTaskPath' task to generate one"
90+
"File ${builtFilePath.lastName} is missing from ${relativeDirPath}, please run '$apiDumpTaskPath' task to generate one"
10391
)
10492
}
10593

10694
val diff = compareFiles(
107-
// use RelativePath.getFile() to fetch the files, because it's case-insensitive
108-
checkFile = expectedApiDeclaration.getFile(projectApiDeclaration.parentFile),
109-
builtFile = expectedApiDeclaration.getFile(buildApiDeclaration.parentFile),
95+
checkFile = checkApiDeclaration,
96+
builtFile = builtApiDeclaration,
11097
)
11198
val diffSet = mutableSetOf<String>()
11299
if (diff != null) diffSet.add(diff)
@@ -123,6 +110,19 @@ abstract class BCVApiCheckTask @Inject constructor(
123110
}
124111
}
125112

113+
/** Get the relative paths of all files and folders inside a directory */
114+
private fun File?.relativePathsOfContent(
115+
filter: FileVisitDetails.() -> Boolean = { true },
116+
): TreeSet<RelativePath> {
117+
val contents = setOfRelativePaths()
118+
if (this != null) {
119+
objects.fileTree().from(this).visit {
120+
if (filter()) contents += relativePath
121+
}
122+
}
123+
return contents
124+
}
125+
126126
private fun compareFiles(checkFile: File, builtFile: File): String? {
127127
val checkText = checkFile.readText()
128128
val builtText = builtFile.readText()

0 commit comments

Comments
 (0)