Skip to content

Commit

Permalink
Merge pull request gradle#20245 Capture updated system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
bot-gradle committed Apr 8, 2022
2 parents 90a611a + d817602 commit 8d89e13
Show file tree
Hide file tree
Showing 27 changed files with 2,447 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ class ConfigurationCacheValueSourceIntegrationTest extends AbstractConfiguration

then:
output.count("ON CI") == 1
output.contains("because a build logic input of type 'IsSystemPropertySet' has changed")
// TODO(mlopatkin) the system property becomes an input itself because it is accessed in the ValueSource implementation.
// This assert should be changed back if ValueSource implementations are allowed to access environment freely.
output.contains("because system property 'ci' has changed")
configurationCache.assertStateStored()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2022 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.configurationcache.inputs.undeclared

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.configurationcache.AbstractConfigurationCacheIntegrationTest

class SystemPropertyInstrumentationInDynamicGroovyIntegrationTest extends AbstractConfigurationCacheIntegrationTest {
def "#method is instrumented in dynamic Groovy"() {
def configurationCache = newConfigurationCacheFixture()

given:
// Why the separate plugin? The Project.getProperties() is available in the build.gradle as getProperties().
// Therefore, it is impossible to call System.getProperties() with static import there, and testing static
// import is important because Groovy generates different code in this case.
file("buildSrc/src/main/groovy/SomePlugin.groovy") << """
import ${Plugin.name}
import ${Project.name}
import static ${System.name}.clearProperty
import static ${System.name}.getProperties
import static ${System.name}.getProperty
import static ${System.name}.setProperty
class SomePlugin implements Plugin<Project> {
void apply(Project project) {
def returned = $method
println("returned = \$returned")
}
}
"""

buildScript("""
apply plugin: SomePlugin
""")

when:
configurationCacheRun("-Dsome.property=some.value")

then:
configurationCache.assertStateStored()
outputContains("returned = some.value")
problems.assertResultHasProblems(result) {
withInput("Plugin class 'SomePlugin': system property 'some.property'")
}

where:
method | _
"System.properties['some.property']" | _
"System.getProperties().get('some.property')" | _
"getProperties().get('some.property')" | _
"System.getProperty('some.property')" | _
"getProperty('some.property')" | _
"System.getProperty('some.property', 'default.value')" | _
"getProperty('some.property', 'default.value')" | _
"System.setProperty('some.property', 'new.value')" | _
"setProperty('some.property', 'new.value')" | _
"System.clearProperty('some.property')" | _
"clearProperty('some.property')" | _
}

def "#setProperties is instrumented in dynamic Groovy"() {
def configurationCache = newConfigurationCacheFixture()

given:
buildScript("""
import static ${System.name}.setProperties
def newProps = new Properties()
System.properties.forEach { k, v -> newProps[k] = v }
newProps.replace("some.property", "new.value")
${setProperties}(newProps)
tasks.register("printProperty") {
doLast {
println("returned = \${System.getProperty("some.property")}")
}
}
""")

when:
configurationCacheRun("-Dsome.property=some.value", "printProperty")

then:
configurationCache.assertStateStored()
outputContains("returned = new.value")

when:
configurationCacheRun("-Dsome.property=some.value", "printProperty")

then:
configurationCache.assertStateLoaded()
outputContains("returned = new.value")

where:
setProperties | _
"System.setProperties" | _
"setProperties" | _
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2022 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.configurationcache.inputs.undeclared


import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.configurationcache.AbstractConfigurationCacheIntegrationTest

class SystemPropertyInstrumentationInJavaIntegrationTest extends AbstractConfigurationCacheIntegrationTest {
def "#method is instrumented in Java"() {
def configurationCache = newConfigurationCacheFixture()

given:
file("buildSrc/src/main/java/SomePlugin.java") << """
import ${Plugin.name};
import ${Project.name};
public class SomePlugin implements Plugin<Project> {
public void apply(Project project) {
Object returned = $method;
System.out.println("returned = " + returned);
}
}
"""

buildScript("""
apply plugin: SomePlugin
""")

when:
configurationCacheRun("-Dsome.property=some.value")

then:
configurationCache.assertStateStored()
outputContains("returned = some.value")
problems.assertResultHasProblems(result) {
withInput("Plugin class 'SomePlugin': system property 'some.property'")
}

where:
method | _
"System.getProperties().get(\"some.property\")" | _
"System.getProperty(\"some.property\")" | _
"System.getProperty(\"some.property\", \"default.value\")" | _
"System.setProperty(\"some.property\", \"new.value\")" | _
"System.clearProperty(\"some.property\")" | _
}

def "setProperties is instrumented in Java"() {
def configurationCache = newConfigurationCacheFixture()

given:
file("buildSrc/src/main/java/SomePlugin.java") << """
import ${Plugin.name};
import ${Project.name};
import ${Properties.name};
public class SomePlugin implements Plugin<Project> {
public void apply(Project project) {
Properties newProps = new Properties();
System.getProperties().forEach(newProps::put);
newProps.replace("some.property", "new.value");
System.setProperties(newProps);
}
}
"""

buildScript("""
apply plugin: SomePlugin
tasks.register("printProperty") {
doLast {
println("returned = \${System.getProperty("some.property")}")
}
}
""")

when:
configurationCacheRun("-Dsome.property=some.value", "printProperty")

then:
configurationCache.assertStateStored()
outputContains("returned = new.value")

when:
configurationCacheRun("-Dsome.property=some.value", "printProperty")

then:
configurationCache.assertStateLoaded()
outputContains("returned = new.value")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2022 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.configurationcache.inputs.undeclared


import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.configurationcache.AbstractConfigurationCacheIntegrationTest

class SystemPropertyInstrumentationInKotlinIntegrationTest extends AbstractConfigurationCacheIntegrationTest {
def "#method is instrumented in Kotlin"() {
def configurationCache = newConfigurationCacheFixture()

given:
// Why the separate plugin? The Project.getProperties() is available in the build.gradle.kts as getProperties().
file("buildSrc/src/main/kotlin/SomePlugin.kt") << """
import ${Plugin.name}
import ${Project.name}
class SomePlugin : Plugin<Project> {
override fun apply(project: Project) {
val returned = $method
println("returned = \$returned")
}
}
"""

file("buildSrc/build.gradle.kts") << """
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
"""

buildScript("""
apply plugin: SomePlugin
""")

when:
configurationCacheRun("-Dsome.property=some.value")

then:
configurationCache.assertStateStored()
outputContains("returned = some.value")
problems.assertResultHasProblems(result) {
withInput("Plugin class 'SomePlugin': system property 'some.property'")
ignoringUnexpectedInputs()
}

where:
method | _
"System.getProperties().get(\"some.property\")" | _
"System.getProperty(\"some.property\")" | _
"System.getProperty(\"some.property\", \"default.value\")" | _
"System.setProperty(\"some.property\", \"new.value\")" | _
"System.clearProperty(\"some.property\")" | _
}
}
Loading

0 comments on commit 8d89e13

Please sign in to comment.