org.gradle.api.tasks.TaskPropertiesIntegrationTest.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
class TaskPropertiesIntegrationTest extends AbstractIntegrationSpec {
def "can define task with abstract read-only Property property"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@Internal
abstract Property getCount()
@TaskAction
void go() {
println("count = \${count.get()}")
}
}
tasks.create("thing", MyTask) {
println("property = \$count")
count = 12
}
"""
when:
succeeds("thing")
then:
outputContains("property = task ':thing' property 'count'")
outputContains("count = 12")
}
def "reports failure to query managed Property with no value"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@Internal
abstract Property getCount()
@TaskAction
void go() {
println("count = \${count.get()}")
}
}
tasks.create("thing", MyTask) {
}
"""
when:
fails("thing")
then:
failure.assertHasCause("No value has been specified for task ':thing' property 'count'")
}
def "reports failure to query read-only unmanaged Property with final getter"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@Internal
final Property count = project.objects.property(Integer)
@TaskAction
void go() {
println("count = \${count.get()}")
}
}
tasks.create("thing", MyTask) {
println("property = \$count")
}
"""
when:
fails("thing")
then:
outputContains("property = task ':thing' property 'count'")
failure.assertHasCause("No value has been specified for task ':thing' property 'count'")
}
def "reports failure to query read-only unmanaged Property"() {
given:
file("buildSrc/src/main/java/MyTask.java") << """
import org.gradle.api.*;
import org.gradle.api.provider.*;
import org.gradle.api.tasks.*;
public abstract class MyTask extends DefaultTask {
private final Property count = getProject().getObjects().property(Integer.class);
@Internal
public Property getCount() {
return count;
}
@TaskAction
void go() {
System.out.println("count = " + count.get());
}
}
"""
buildFile << """
tasks.create("thing", MyTask) {
println("property = \$count")
}
"""
when:
fails("thing")
then:
outputContains("property = task ':thing' property 'count'")
failure.assertHasCause("No value has been specified for task ':thing' property 'count'")
}
def "can define task with abstract read-only ConfigurableFileCollection property"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@InputFiles
abstract ConfigurableFileCollection getSource()
@TaskAction
void go() {
println("files = \${source.files.name}")
}
}
tasks.create("thing", MyTask) {
source.from("a", "b", "c")
}
"""
when:
succeeds("thing")
then:
outputContains("files = [a, b, c]")
}
def "can define task with abstract read-only ConfigurableFileTree property"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@InputFiles
abstract ConfigurableFileTree getSource()
@TaskAction
void go() {
println("files = \${source.files.name.sort()}")
}
}
tasks.create("thing", MyTask) {
source.from("dir")
}
"""
file("dir/sub/a.txt").createFile()
file("dir/b.txt").createFile()
when:
succeeds("thing")
then:
outputContains("files = [a.txt, b.txt]")
}
def "can define task with abstract read-only NamedDomainObjectContainer property"() {
given:
buildFile << """
abstract class Bean {
@Internal
final String name
@Internal
abstract Property getProp()
Bean(String name) {
this.name = name
}
}
abstract class MyTask extends DefaultTask {
@Nested
abstract NamedDomainObjectContainer getBeans()
@TaskAction
void go() {
println("beans = \${beans.collect { it.prop.get() } }")
}
}
tasks.create("thing", MyTask) {
beans {
one { prop = '1' }
two { prop = '2' }
}
}
"""
when:
succeeds("thing")
then:
outputContains("beans = [1, 2]")
}
def "can define task with abstract read-only @Nested property"() {
given:
buildFile << """
interface Params {
@Internal
Property getCount()
}
abstract class MyTask extends DefaultTask {
@Nested
abstract Params getParams()
@TaskAction
void go() {
println("count = \${params.count.get()}")
}
}
tasks.create("thing", MyTask) {
println("params = \$params")
println("params.count = \$params.count")
params.count = 12
}
"""
when:
succeeds("thing")
then:
outputContains("params = task ':thing' property 'params'")
outputContains("params.count = task ':thing' property 'params.count'")
outputContains("count = 12")
}
def "can query generated read only property in constructor"() {
given:
buildFile << """
abstract class MyTask extends DefaultTask {
@Internal
abstract Property getParam()
MyTask() {
param.convention("from convention")
}
@TaskAction
void go() {
println("param = \${param.get()}")
}
}
tasks.create("thing", MyTask) {
param.set("from configuration")
}
"""
when:
succeeds("thing")
then:
outputContains("param = from configuration")
}
def "cannot modify task's input properties via returned map"() {
given:
buildFile << """
tasks.create("thing") {
inputs.properties.put("Won't", "happen")
}
"""
when:
fails("thing")
then:
errorOutput.contains("java.lang.UnsupportedOperationException")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy