mirror of
https://github.com/gradle/gradle-build-action.git
synced 2024-12-26 08:42:08 -05:00
Add test coverage for build-result-capture init script
This commit is contained in:
parent
748dc30fdc
commit
ea24a0ad75
4 changed files with 246 additions and 5 deletions
|
@ -16,6 +16,12 @@ dependencies {
|
|||
testImplementation gradleTestKit()
|
||||
testImplementation 'org.spockframework:spock-core:2.1-groovy-3.0'
|
||||
testImplementation('org.spockframework:spock-junit4:2.1-groovy-3.0')
|
||||
|
||||
testImplementation ('io.ratpack:ratpack-groovy-test:1.9.0') {
|
||||
exclude group: 'org.codehaus.groovy', module: 'groovy-all'
|
||||
}
|
||||
testImplementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.13.3'
|
||||
|
||||
}
|
||||
|
||||
test {
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
package com.gradle.gradlebuildaction
|
||||
|
||||
import com.fasterxml.jackson.core.JsonFactory
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.fasterxml.jackson.dataformat.smile.SmileFactory
|
||||
import org.gradle.testkit.runner.BuildResult
|
||||
import org.gradle.testkit.runner.GradleRunner
|
||||
import org.gradle.testkit.runner.internal.DefaultGradleRunner
|
||||
import org.gradle.util.GradleVersion
|
||||
import ratpack.groovy.test.embed.GroovyEmbeddedApp
|
||||
import spock.lang.AutoCleanup
|
||||
import spock.lang.Specification
|
||||
import spock.lang.TempDir
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.util.zip.GZIPOutputStream
|
||||
|
||||
class BaseInitScriptTest extends Specification {
|
||||
|
||||
|
@ -36,12 +42,60 @@ class BaseInitScriptTest extends Specification {
|
|||
static final List<TestGradleVersion> CONFIGURATION_CACHE_VERSIONS =
|
||||
[GRADLE_7_0, GRADLE_7_4]
|
||||
|
||||
static final String PUBLIC_BUILD_SCAN_ID = 'i2wepy2gr7ovw'
|
||||
static final String DEFAULT_SCAN_UPLOAD_TOKEN = 'scan-upload-token'
|
||||
static final String ROOT_PROJECT_NAME = 'test-init-script'
|
||||
|
||||
File settingsFile
|
||||
File buildFile
|
||||
|
||||
@TempDir
|
||||
File testProjectDir
|
||||
|
||||
@AutoCleanup
|
||||
def mockScansServer = GroovyEmbeddedApp.of {
|
||||
def jsonWriter = new ObjectMapper(new JsonFactory()).writer()
|
||||
def smileWriter = new ObjectMapper(new SmileFactory()).writer()
|
||||
|
||||
handlers {
|
||||
post('in/:gradleVersion/:pluginVersion') {
|
||||
def scanUrlString = "${mockScansServer.address}s/$PUBLIC_BUILD_SCAN_ID"
|
||||
def body = [
|
||||
id : PUBLIC_BUILD_SCAN_ID,
|
||||
scanUrl: scanUrlString.toString(),
|
||||
]
|
||||
def out = new ByteArrayOutputStream()
|
||||
new GZIPOutputStream(out).withStream { smileWriter.writeValue(it, body) }
|
||||
context.response
|
||||
.contentType('application/vnd.gradle.scan-ack')
|
||||
.send(out.toByteArray())
|
||||
}
|
||||
prefix('scans/publish') {
|
||||
post('gradle/:pluginVersion/token') {
|
||||
def pluginVersion = context.pathTokens.pluginVersion
|
||||
def scanUrlString = "${mockScansServer.address}s/$PUBLIC_BUILD_SCAN_ID"
|
||||
def body = [
|
||||
id : PUBLIC_BUILD_SCAN_ID,
|
||||
scanUrl : scanUrlString.toString(),
|
||||
scanUploadUrl : "${mockScansServer.address.toString()}scans/publish/gradle/$pluginVersion/upload".toString(),
|
||||
scanUploadToken: DEFAULT_SCAN_UPLOAD_TOKEN
|
||||
]
|
||||
context.response
|
||||
.contentType('application/vnd.gradle.scan-ack+json')
|
||||
.send(jsonWriter.writeValueAsBytes(body))
|
||||
}
|
||||
post('gradle/:pluginVersion/upload') {
|
||||
context.request.getBody(1024 * 1024 * 10).then {
|
||||
context.response
|
||||
.contentType('application/vnd.gradle.scan-upload-ack+json')
|
||||
.send()
|
||||
}
|
||||
}
|
||||
notFound()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def setup() {
|
||||
settingsFile = new File(testProjectDir, 'settings.gradle')
|
||||
buildFile = new File(testProjectDir, 'build.gradle')
|
||||
|
@ -54,10 +108,63 @@ class BaseInitScriptTest extends Specification {
|
|||
File targetInitScript = new File(targetInitScriptsDir, srcInitScript.name)
|
||||
Files.copy(srcInitScript.toPath(), targetInitScript.toPath())
|
||||
}
|
||||
settingsFile << "rootProject.name = 'test-init-script'\n"
|
||||
settingsFile << "rootProject.name = '${ROOT_PROJECT_NAME}'\n"
|
||||
buildFile << ''
|
||||
}
|
||||
|
||||
def declareGePluginApplication(GradleVersion gradleVersion) {
|
||||
settingsFile.text = maybeAddPluginsToSettings(gradleVersion) + settingsFile.text
|
||||
buildFile.text = maybeAddPluginsToRootProject(gradleVersion) + buildFile.text
|
||||
}
|
||||
|
||||
String maybeAddPluginsToSettings(GradleVersion gradleVersion) {
|
||||
if (gradleVersion < GradleVersion.version('5.0')) {
|
||||
'' // applied in build.gradle
|
||||
} else if (gradleVersion < GradleVersion.version('6.0')) {
|
||||
'' // applied in build.gradle
|
||||
} else {
|
||||
"""
|
||||
plugins {
|
||||
id 'com.gradle.enterprise' version '3.4.1'
|
||||
}
|
||||
gradleEnterprise {
|
||||
server = '$mockScansServer.address'
|
||||
buildScan {
|
||||
publishAlways()
|
||||
}
|
||||
}
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
String maybeAddPluginsToRootProject(GradleVersion gradleVersion) {
|
||||
if (gradleVersion < GradleVersion.version('5.0')) {
|
||||
"""
|
||||
plugins {
|
||||
id 'com.gradle.build-scan' version '1.16'
|
||||
}
|
||||
buildScan {
|
||||
server = '$mockScansServer.address'
|
||||
publishAlways()
|
||||
}
|
||||
"""
|
||||
} else if (gradleVersion < GradleVersion.version('6.0')) {
|
||||
"""
|
||||
plugins {
|
||||
id 'com.gradle.build-scan' version '3.4.1'
|
||||
}
|
||||
gradleEnterprise {
|
||||
server = '$mockScansServer.address'
|
||||
buildScan {
|
||||
publishAlways()
|
||||
}
|
||||
}
|
||||
"""
|
||||
} else {
|
||||
'' // applied in settings.gradle
|
||||
}
|
||||
}
|
||||
|
||||
def addFailingTaskToBuild() {
|
||||
buildFile << '''
|
||||
task expectFailure {
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
package com.gradle.gradlebuildaction
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import spock.lang.Ignore
|
||||
|
||||
import static org.junit.Assume.assumeTrue
|
||||
|
||||
class TestBuildResultOutput extends BaseInitScriptTest {
|
||||
def initScript = 'build-result-capture.init.gradle'
|
||||
|
||||
@Ignore
|
||||
def "produces build results file for build with #testGradleVersion"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
run(['help'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, false)
|
||||
|
||||
where:
|
||||
testGradleVersion << ALL_VERSIONS
|
||||
}
|
||||
|
||||
@Ignore
|
||||
def "produces build results file for failing build with #testGradleVersion"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
addFailingTaskToBuild()
|
||||
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('expectFailure', testGradleVersion, true, false)
|
||||
|
||||
where:
|
||||
testGradleVersion << ALL_VERSIONS
|
||||
}
|
||||
|
||||
@Ignore
|
||||
def "produces build results file for build with --configuration-cache on #testGradleVersion"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, false)
|
||||
assert buildResultFile.delete()
|
||||
|
||||
when:
|
||||
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, false)
|
||||
|
||||
where:
|
||||
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
|
||||
}
|
||||
|
||||
def "produces build results file for #testGradleVersion with build scan published"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
declareGePluginApplication(testGradleVersion.gradleVersion)
|
||||
run(['help'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, true)
|
||||
|
||||
where:
|
||||
testGradleVersion << ALL_VERSIONS
|
||||
}
|
||||
|
||||
def "produces build results file for failing build on #testGradleVersion with build scan published"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
declareGePluginApplication(testGradleVersion.gradleVersion)
|
||||
addFailingTaskToBuild()
|
||||
runAndFail(['expectFailure'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('expectFailure', testGradleVersion, true, true)
|
||||
|
||||
where:
|
||||
testGradleVersion << ALL_VERSIONS
|
||||
}
|
||||
|
||||
def "produces build results file for build with --configuration-cache on #testGradleVersion with build scan published"() {
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
declareGePluginApplication(testGradleVersion.gradleVersion)
|
||||
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, true)
|
||||
assert buildResultFile.delete()
|
||||
|
||||
when:
|
||||
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
|
||||
|
||||
then:
|
||||
assertResults('help', testGradleVersion, false, true)
|
||||
|
||||
where:
|
||||
testGradleVersion << CONFIGURATION_CACHE_VERSIONS
|
||||
}
|
||||
|
||||
void assertResults(String task, TestGradleVersion testGradleVersion, boolean hasFailure, boolean hasBuildScan) {
|
||||
def results = new JsonSlurper().parse(buildResultFile)
|
||||
assert results['rootProject'] == ROOT_PROJECT_NAME
|
||||
assert results['requestedTasks'] == task
|
||||
assert results['gradleVersion'] == testGradleVersion.gradleVersion.version
|
||||
assert results['buildFailed'] == hasFailure
|
||||
assert results['buildScanUri'] == (hasBuildScan ? "${mockScansServer.address}s/${PUBLIC_BUILD_SCAN_ID}" : null)
|
||||
}
|
||||
|
||||
private File getBuildResultFile() {
|
||||
def buildResultsDir = new File(testProjectDir, '.build-results')
|
||||
assert buildResultsDir.directory
|
||||
assert buildResultsDir.listFiles().size() == 1
|
||||
def resultsFile = buildResultsDir.listFiles()[0]
|
||||
assert resultsFile.name.startsWith('github-step-id')
|
||||
return resultsFile
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ class TestProjectRootCapture extends BaseInitScriptTest {
|
|||
def initScript = 'project-root-capture.init.gradle'
|
||||
|
||||
def "captures project root on #testGradleVersion"() {
|
||||
assumeTrue testGradleVersion.isCompatibleWithCurrentJvm()
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
run(['help'], initScript, testGradleVersion.gradleVersion)
|
||||
|
@ -19,7 +19,7 @@ class TestProjectRootCapture extends BaseInitScriptTest {
|
|||
}
|
||||
|
||||
def "captures project root on #testGradleVersion when build fails"() {
|
||||
assumeTrue testGradleVersion.isCompatibleWithCurrentJvm()
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
addFailingTaskToBuild()
|
||||
|
||||
|
@ -34,7 +34,7 @@ class TestProjectRootCapture extends BaseInitScriptTest {
|
|||
}
|
||||
|
||||
def "captures project root on #testGradleVersion with --configuration-cache"() {
|
||||
assumeTrue testGradleVersion.isCompatibleWithCurrentJvm()
|
||||
assumeTrue testGradleVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
run(['help', '--configuration-cache'], initScript, testGradleVersion.gradleVersion)
|
||||
|
@ -54,7 +54,7 @@ class TestProjectRootCapture extends BaseInitScriptTest {
|
|||
}
|
||||
|
||||
def "has no effect on #testVersion"() {
|
||||
assumeTrue testVersion.isCompatibleWithCurrentJvm()
|
||||
assumeTrue testVersion.compatibleWithCurrentJvm
|
||||
|
||||
when:
|
||||
run(['help'], initScript, testVersion.gradleVersion)
|
||||
|
|
Loading…
Reference in a new issue