"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/build-logic/binary-compatibility/src/test/kotlin/gradlebuild/binarycompatibility/AbstractAcceptedApiChangesMaintenanceTaskIntegrationTest.kt" (20 Apr 2023, 5764 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Kotlin source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2022 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 package gradlebuild.binarycompatibility
   18 
   19 import org.gradle.testkit.runner.GradleRunner
   20 import org.gradle.testkit.runner.TaskOutcome
   21 import org.gradle.util.internal.TextUtil
   22 import org.junit.jupiter.api.Assertions
   23 import org.junit.jupiter.api.BeforeEach
   24 import org.junit.jupiter.api.io.TempDir
   25 import java.io.File
   26 import java.io.StringWriter
   27 
   28 
   29 abstract class AbstractAcceptedApiChangesMaintenanceTaskIntegrationTest {
   30     @TempDir
   31     lateinit var projectDir: File
   32     lateinit var acceptedApiChangesFile: File
   33 
   34     @BeforeEach
   35     fun setUp() {
   36         projectDir.resolve("src").resolve("changes").mkdirs()
   37         acceptedApiChangesFile = projectDir.resolve("src/changes/accepted-public-api-changes.json")
   38 
   39         projectDir.resolve("build.gradle.kts")
   40             .writeText(
   41                 """
   42                     plugins {
   43                         id("gradlebuild.binary-compatibility")
   44                     }
   45 
   46                     val verifyAcceptedApiChangesOrdering = tasks.register<gradlebuild.binarycompatibility.AlphabeticalAcceptedApiChangesTask>("verifyAcceptedApiChangesOrdering") {
   47                         group = "verification"
   48                         description = "Ensures the accepted api changes file is kept alphabetically ordered to make merging changes to it easier"
   49                         apiChangesFile.set(layout.projectDirectory.file("${ TextUtil.normaliseFileSeparators(acceptedApiChangesFile.absolutePath) }"))
   50                     }
   51 
   52                     val sortAcceptedApiChanges = tasks.register<gradlebuild.binarycompatibility.SortAcceptedApiChangesTask>("sortAcceptedApiChanges") {
   53                         group = "verification"
   54                         description = "Sort the accepted api changes file alphabetically"
   55                         apiChangesFile.set(layout.projectDirectory.file("${ TextUtil.normaliseFileSeparators(acceptedApiChangesFile.absolutePath) }"))
   56                     }
   57                 """.trimIndent()
   58             )
   59 
   60         setupPluginRequirements()
   61     }
   62 
   63     /**
   64      * Create projects and files required for plugins applied to the project (including transitively applied plugins).
   65      * These files are not required to run the task itself, but are required to apply the binary-compatibility plugin.
   66      */
   67     protected
   68     fun setupPluginRequirements() {
   69         projectDir.resolve("version.txt").writeText("9999999.0") // All released versions should be lower than this
   70         projectDir.resolve("released-versions.json").writeText(
   71             """
   72                 {
   73                   "latestReleaseSnapshot": {
   74                     "version": "7.6-20220831090819+0000",
   75                     "buildTime": "20220831090819+0000"
   76                   },
   77                   "latestRc": {
   78                     "version": "7.5-rc-5",
   79                     "buildTime": "20220712114039+0000"
   80                   },
   81                   "finalReleases": [
   82                     {
   83                       "version": "7.5.1",
   84                       "buildTime": "20220805211756+0000"
   85                     },
   86                     {
   87                       "version": "7.5",
   88                       "buildTime": "20220714124815+0000"
   89                     },
   90                     {
   91                       "version": "7.4.2",
   92                       "buildTime": "20220331152529+0000"
   93                     },
   94                     {
   95                       "version": "6.9.2",
   96                       "buildTime": "20211221172537+0000"
   97                     }
   98                   ]
   99                 }
  100             """.trimIndent()
  101         )
  102     }
  103 
  104     protected
  105     fun run(vararg args: String) = GradleRunner.create()
  106         .withProjectDir(projectDir)
  107         .withTestKitDir(projectDir.resolve("test-kit"))
  108         .withPluginClasspath()
  109         .forwardOutput()
  110         .withArguments(*args)
  111 
  112 
  113     protected
  114     fun assertChangesProperlyOrdered() {
  115         val result = run(":verifyAcceptedApiChangesOrdering").build()
  116         Assertions.assertEquals(TaskOutcome.SUCCESS, result.task(":verifyAcceptedApiChangesOrdering")!!.outcome)
  117     }
  118 
  119     protected
  120     fun assertHasMisorderedChanges(changes: List<Change>? = null) {
  121         val standardError = StringWriter()
  122         run(":verifyAcceptedApiChangesOrdering")
  123             .forwardStdError(standardError)
  124             .buildAndFail()
  125 
  126         val expectedOutput = "API changes in file '${acceptedApiChangesFile.name}' should be in alphabetical order (by type and member), yet these changes were not:\n"
  127         val cleanupHint = "To automatically alphabetize these changes run: 'gradlew :architecture-test:sortAcceptedApiChanges'"
  128         with(standardError) {
  129             assertContains(expectedOutput)
  130             changes?.forEach { assertContains(it.toString()) }
  131             assertContains(cleanupHint)
  132         }
  133     }
  134 
  135     private
  136     fun StringWriter.assertContains(text: String) {
  137         Assertions.assertTrue(toString().contains(text)) {
  138             "Did not find expected error message in $this"
  139         }
  140     }
  141 
  142     protected
  143     data class Change(val type: String, val member: String) {
  144         override fun toString(): String = "Type: '$type', Member: '$member'"
  145     }
  146 }