Skip to content

Commit d9030e9

Browse files
committed
Allow warning mode to be configured
Uses WarningMode.Fail by default allowing us to remove our own warning detection logic. Requires Gradle 5.6 and later.
1 parent a4f85a0 commit d9030e9

File tree

3 files changed

+44
-69
lines changed

3 files changed

+44
-69
lines changed

src/main/groovy/nebula/test/BaseIntegrationSpec.groovy

+4-43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package nebula.test
1818
import groovy.transform.CompileStatic
1919
import groovy.transform.TypeCheckingMode
2020
import org.gradle.api.logging.LogLevel
21+
import org.gradle.api.logging.configuration.WarningMode
2122
import org.junit.Rule
2223
import org.junit.rules.TestName
2324
import spock.lang.Specification
@@ -80,39 +81,6 @@ abstract class BaseIntegrationSpec extends Specification {
8081
file
8182
}
8283

83-
protected static void checkForDeprecations(String output) {
84-
def deprecations = output.readLines().findAll {
85-
it.contains("has been deprecated and is scheduled to be removed in Gradle") ||
86-
it.contains("Deprecated Gradle features were used in this build") ||
87-
it.contains("has been deprecated. This is scheduled to be removed in Gradle") ||
88-
it.contains("This behaviour has been deprecated and is scheduled to be removed in Gradle")
89-
}
90-
// temporary for known issue with overwriting task
91-
// overridden task expected to not be needed in future version
92-
if (deprecations.size() == 1 && deprecations.first().contains("Creating a custom task named 'dependencyInsight' has been deprecated and is scheduled to be removed in Gradle 5.0.")) {
93-
return
94-
}
95-
if (!System.getProperty("ignoreDeprecations") && !deprecations.isEmpty()) {
96-
throw new IllegalArgumentException("Deprecation warnings were found (Set the ignoreDeprecations system property during the test to ignore):\n" + deprecations.collect {
97-
" - $it"
98-
}.join("\n"))
99-
}
100-
}
101-
102-
protected static void checkForMutableProjectState(String output) {
103-
def mutableProjectStateWarnings = output.readLines().findAll {
104-
it.contains("was resolved without accessing the project in a safe manner") ||
105-
it.contains("This may happen when a configuration is resolved from a thread not managed by Gradle or from a different project")
106-
107-
}
108-
109-
if (!System.getProperty("ignoreMutableProjectStateWarnings") && !mutableProjectStateWarnings.isEmpty()) {
110-
throw new IllegalArgumentException("Mutable Project State warnings were found (Set the ignoreMutableProjectStateWarnings system property during the test to ignore):\n" + mutableProjectStateWarnings.collect {
111-
" - $it"
112-
}.join("\n"))
113-
}
114-
}
115-
11684
protected void writeHelloWorld(File baseDir = getProjectDir()) {
11785
writeHelloWorld('nebula', baseDir)
11886
}
@@ -210,15 +178,8 @@ abstract class BaseIntegrationSpec extends Specification {
210178
getProjectDir().getName().replaceAll(/_\d+/, '')
211179
}
212180

213-
protected List<String> calculateArguments(String... args) {
181+
protected List<String> calculateArguments(WarningMode warningMode, String... args) {
214182
List<String> arguments = []
215-
// Gradle will use these files name from the PWD, instead of the project directory. It's easier to just leave
216-
// them out and let the default find them, since we're not changing their default names.
217-
//arguments += '--build-file'
218-
//arguments += (buildFile.canonicalPath - projectDir.canonicalPath).substring(1)
219-
//arguments += '--settings-file'
220-
//arguments += (settingsFile.canonicalPath - projectDir.canonicalPath).substring(1)
221-
//arguments += '--no-daemon'
222183
switch (getLogLevel()) {
223184
case LogLevel.INFO:
224185
arguments += '--info'
@@ -228,9 +189,9 @@ abstract class BaseIntegrationSpec extends Specification {
228189
break
229190
}
230191
arguments += '--stacktrace'
231-
arguments += '-Dorg.gradle.warning.mode=all'
192+
arguments += '--warning-mode=' + warningMode.name().toLowerCase()
232193
arguments.addAll(args)
233194
arguments.addAll(initScripts.collect { file -> '-I' + file.absolutePath })
234195
arguments
235196
}
236-
}
197+
}

src/main/groovy/nebula/test/IntegrationSpec.groovy

+19-11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import nebula.test.functional.internal.GradleHandle
2626
import nebula.test.multiproject.MultiProjectIntegrationHelper
2727
import org.apache.commons.io.FileUtils
2828
import org.gradle.api.logging.LogLevel
29+
import org.gradle.api.logging.configuration.WarningMode
2930

3031
/**
3132
* @author Justin Ryan
@@ -34,7 +35,7 @@ import org.gradle.api.logging.LogLevel
3435
@CompileStatic
3536
abstract class IntegrationSpec extends BaseIntegrationSpec {
3637
private static final String DEFAULT_REMOTE_DEBUG_JVM_ARGUMENTS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
37-
private static final Integer DEFAULT_DAEMON_MAX_IDLE_TIME_IN_SECONDS_IN_MEMORY_SAFE_MODE = 15;
38+
private static final Integer DEFAULT_DAEMON_MAX_IDLE_TIME_IN_SECONDS_IN_MEMORY_SAFE_MODE = 15
3839

3940
// Holds State of last run
4041
private ExecutionResult result
@@ -72,8 +73,8 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {
7273
helper = new MultiProjectIntegrationHelper(getProjectDir(), settingsFile)
7374
}
7475

75-
protected GradleHandle launcher(String... args) {
76-
List<String> arguments = calculateArguments(args)
76+
protected GradleHandle launcher(WarningMode warningMode, String ... args) {
77+
List<String> arguments = calculateArguments(warningMode, args)
7778
List<String> jvmArguments = calculateJvmArguments()
7879
Integer daemonMaxIdleTimeInSeconds = calculateMaxIdleDaemonTimeoutInSeconds()
7980

@@ -144,7 +145,11 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {
144145

145146
/* Execution */
146147
protected ExecutionResult runTasksSuccessfully(String... tasks) {
147-
ExecutionResult result = runTasks(tasks)
148+
return runTasksSuccessfully(WarningMode.Fail, tasks)
149+
}
150+
151+
protected ExecutionResult runTasksSuccessfully(WarningMode warningMode, String... tasks) {
152+
ExecutionResult result = runTasks(warningMode, tasks)
148153
if (result.failure) {
149154
result.rethrowFailure()
150155
}
@@ -153,20 +158,23 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {
153158

154159
@CompileStatic(TypeCheckingMode.SKIP)
155160
protected ExecutionResult runTasksWithFailure(String... tasks) {
156-
ExecutionResult result = runTasks(tasks)
161+
return runTasksWithFailure(WarningMode.Fail, tasks)
162+
}
163+
164+
@CompileStatic(TypeCheckingMode.SKIP)
165+
protected ExecutionResult runTasksWithFailure(WarningMode warningMode, String... tasks) {
166+
ExecutionResult result = runTasks(warningMode, tasks)
157167
assert result.failure
158168
result
159169
}
160170

161171
protected ExecutionResult runTasks(String... tasks) {
162-
ExecutionResult result = launcher(tasks).run()
163-
this.result = result
164-
return checkForDeprecations(result)
172+
return runTasks(WarningMode.Fail, tasks)
165173
}
166174

167-
protected ExecutionResult checkForDeprecations(ExecutionResult result) {
168-
checkForMutableProjectState(result.standardOutput)
169-
checkForDeprecations(result.standardOutput)
175+
protected ExecutionResult runTasks(WarningMode warningMode, String... tasks) {
176+
ExecutionResult result = launcher(warningMode, tasks).run()
177+
this.result = result
170178
return result
171179
}
172180

src/main/groovy/nebula/test/IntegrationTestKitSpec.groovy

+21-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package nebula.test
1717

18+
1819
import nebula.test.functional.internal.classpath.ClasspathAddingInitScriptBuilder
20+
import org.gradle.api.logging.configuration.WarningMode
1921
import org.gradle.testkit.runner.BuildResult
2022
import org.gradle.testkit.runner.GradleRunner
2123
import org.gradle.util.GFileUtils
@@ -41,7 +43,7 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
4143
boolean definePluginOutsideOfPluginBlock = false
4244

4345
def setup() {
44-
if (! settingsFile) {
46+
if (!settingsFile) {
4547
settingsFile = new File(projectDir, "settings.gradle")
4648
settingsFile.text = "rootProject.name='${moduleName}'\n"
4749
}
@@ -68,15 +70,19 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
6870
}
6971

7072
BuildResult runTasks(String... tasks) {
71-
BuildResult result = createRunner(tasks)
72-
.build()
73-
return checkForDeprecations(result)
73+
return runTasks(WarningMode.Fail, tasks)
74+
}
75+
76+
BuildResult runTasks(WarningMode warningMode, String... tasks) {
77+
return createRunner(warningMode, tasks).build()
7478
}
7579

7680
BuildResult runTasksAndFail(String... tasks) {
77-
BuildResult result = createRunner(tasks)
78-
.buildAndFail()
79-
return checkForDeprecations(result)
81+
return runTasksAndFail(WarningMode.Fail, tasks)
82+
}
83+
84+
BuildResult runTasksAndFail(WarningMode warningMode, String... tasks) {
85+
return createRunner(warningMode, tasks).buildAndFail()
8086
}
8187

8288
def tasksWereSuccessful(BuildResult result, String... tasks) {
@@ -90,14 +96,19 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
9096
}
9197

9298
GradleRunner createRunner(String... tasks) {
93-
def pluginArgs = definePluginOutsideOfPluginBlock ? createGradleTestKitInitArgs() : new ArrayList<>()
99+
return createRunner(WarningMode.Fail, tasks)
100+
}
101+
102+
GradleRunner createRunner(WarningMode warningMode, String... tasks) {
103+
def arguments = calculateArguments(warningMode, tasks)
104+
def pluginArgs = definePluginOutsideOfPluginBlock ? createGradleTestKitInitArgs() : new ArrayList<String>()
94105
def gradleRunnerBuilder = GradleRunner.create()
95106
.withProjectDir(projectDir)
96-
.withArguments(calculateArguments(tasks) + pluginArgs)
107+
.withArguments(arguments + pluginArgs)
97108
.withDebug(debug)
98109
.withPluginClasspath()
99110

100-
if(forwardOutput) {
111+
if (forwardOutput) {
101112
gradleRunnerBuilder.forwardOutput()
102113
}
103114
if (gradleVersion != null) {
@@ -122,9 +133,4 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
122133

123134
return Arrays.asList("--init-script", initScript.getAbsolutePath())
124135
}
125-
126-
protected BuildResult checkForDeprecations(BuildResult result) {
127-
checkForDeprecations(result.output)
128-
return result
129-
}
130136
}

0 commit comments

Comments
 (0)