Skip to content

Commit

Permalink
Allow warning mode to be configured
Browse files Browse the repository at this point in the history
Uses WarningMode.Fail by default allowing us to remove our own warning detection logic. Requires Gradle 5.6 and later.
  • Loading branch information
DanielThomas committed Jan 31, 2020
1 parent a4f85a0 commit d9030e9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 69 deletions.
47 changes: 4 additions & 43 deletions src/main/groovy/nebula/test/BaseIntegrationSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package nebula.test
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.WarningMode
import org.junit.Rule
import org.junit.rules.TestName
import spock.lang.Specification
Expand Down Expand Up @@ -80,39 +81,6 @@ abstract class BaseIntegrationSpec extends Specification {
file
}

protected static void checkForDeprecations(String output) {
def deprecations = output.readLines().findAll {
it.contains("has been deprecated and is scheduled to be removed in Gradle") ||
it.contains("Deprecated Gradle features were used in this build") ||
it.contains("has been deprecated. This is scheduled to be removed in Gradle") ||
it.contains("This behaviour has been deprecated and is scheduled to be removed in Gradle")
}
// temporary for known issue with overwriting task
// overridden task expected to not be needed in future version
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.")) {
return
}
if (!System.getProperty("ignoreDeprecations") && !deprecations.isEmpty()) {
throw new IllegalArgumentException("Deprecation warnings were found (Set the ignoreDeprecations system property during the test to ignore):\n" + deprecations.collect {
" - $it"
}.join("\n"))
}
}

protected static void checkForMutableProjectState(String output) {
def mutableProjectStateWarnings = output.readLines().findAll {
it.contains("was resolved without accessing the project in a safe manner") ||
it.contains("This may happen when a configuration is resolved from a thread not managed by Gradle or from a different project")

}

if (!System.getProperty("ignoreMutableProjectStateWarnings") && !mutableProjectStateWarnings.isEmpty()) {
throw new IllegalArgumentException("Mutable Project State warnings were found (Set the ignoreMutableProjectStateWarnings system property during the test to ignore):\n" + mutableProjectStateWarnings.collect {
" - $it"
}.join("\n"))
}
}

protected void writeHelloWorld(File baseDir = getProjectDir()) {
writeHelloWorld('nebula', baseDir)
}
Expand Down Expand Up @@ -210,15 +178,8 @@ abstract class BaseIntegrationSpec extends Specification {
getProjectDir().getName().replaceAll(/_\d+/, '')
}

protected List<String> calculateArguments(String... args) {
protected List<String> calculateArguments(WarningMode warningMode, String... args) {
List<String> arguments = []
// Gradle will use these files name from the PWD, instead of the project directory. It's easier to just leave
// them out and let the default find them, since we're not changing their default names.
//arguments += '--build-file'
//arguments += (buildFile.canonicalPath - projectDir.canonicalPath).substring(1)
//arguments += '--settings-file'
//arguments += (settingsFile.canonicalPath - projectDir.canonicalPath).substring(1)
//arguments += '--no-daemon'
switch (getLogLevel()) {
case LogLevel.INFO:
arguments += '--info'
Expand All @@ -228,9 +189,9 @@ abstract class BaseIntegrationSpec extends Specification {
break
}
arguments += '--stacktrace'
arguments += '-Dorg.gradle.warning.mode=all'
arguments += '--warning-mode=' + warningMode.name().toLowerCase()
arguments.addAll(args)
arguments.addAll(initScripts.collect { file -> '-I' + file.absolutePath })
arguments
}
}
}
30 changes: 19 additions & 11 deletions src/main/groovy/nebula/test/IntegrationSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import nebula.test.functional.internal.GradleHandle
import nebula.test.multiproject.MultiProjectIntegrationHelper
import org.apache.commons.io.FileUtils
import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.WarningMode

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

// Holds State of last run
private ExecutionResult result
Expand Down Expand Up @@ -72,8 +73,8 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {
helper = new MultiProjectIntegrationHelper(getProjectDir(), settingsFile)
}

protected GradleHandle launcher(String... args) {
List<String> arguments = calculateArguments(args)
protected GradleHandle launcher(WarningMode warningMode, String ... args) {
List<String> arguments = calculateArguments(warningMode, args)
List<String> jvmArguments = calculateJvmArguments()
Integer daemonMaxIdleTimeInSeconds = calculateMaxIdleDaemonTimeoutInSeconds()

Expand Down Expand Up @@ -144,7 +145,11 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {

/* Execution */
protected ExecutionResult runTasksSuccessfully(String... tasks) {
ExecutionResult result = runTasks(tasks)
return runTasksSuccessfully(WarningMode.Fail, tasks)
}

protected ExecutionResult runTasksSuccessfully(WarningMode warningMode, String... tasks) {
ExecutionResult result = runTasks(warningMode, tasks)
if (result.failure) {
result.rethrowFailure()
}
Expand All @@ -153,20 +158,23 @@ abstract class IntegrationSpec extends BaseIntegrationSpec {

@CompileStatic(TypeCheckingMode.SKIP)
protected ExecutionResult runTasksWithFailure(String... tasks) {
ExecutionResult result = runTasks(tasks)
return runTasksWithFailure(WarningMode.Fail, tasks)
}

@CompileStatic(TypeCheckingMode.SKIP)
protected ExecutionResult runTasksWithFailure(WarningMode warningMode, String... tasks) {
ExecutionResult result = runTasks(warningMode, tasks)
assert result.failure
result
}

protected ExecutionResult runTasks(String... tasks) {
ExecutionResult result = launcher(tasks).run()
this.result = result
return checkForDeprecations(result)
return runTasks(WarningMode.Fail, tasks)
}

protected ExecutionResult checkForDeprecations(ExecutionResult result) {
checkForMutableProjectState(result.standardOutput)
checkForDeprecations(result.standardOutput)
protected ExecutionResult runTasks(WarningMode warningMode, String... tasks) {
ExecutionResult result = launcher(warningMode, tasks).run()
this.result = result
return result
}

Expand Down
36 changes: 21 additions & 15 deletions src/main/groovy/nebula/test/IntegrationTestKitSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package nebula.test


import nebula.test.functional.internal.classpath.ClasspathAddingInitScriptBuilder
import org.gradle.api.logging.configuration.WarningMode
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.gradle.util.GFileUtils
Expand All @@ -41,7 +43,7 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
boolean definePluginOutsideOfPluginBlock = false

def setup() {
if (! settingsFile) {
if (!settingsFile) {
settingsFile = new File(projectDir, "settings.gradle")
settingsFile.text = "rootProject.name='${moduleName}'\n"
}
Expand All @@ -68,15 +70,19 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {
}

BuildResult runTasks(String... tasks) {
BuildResult result = createRunner(tasks)
.build()
return checkForDeprecations(result)
return runTasks(WarningMode.Fail, tasks)
}

BuildResult runTasks(WarningMode warningMode, String... tasks) {
return createRunner(warningMode, tasks).build()
}

BuildResult runTasksAndFail(String... tasks) {
BuildResult result = createRunner(tasks)
.buildAndFail()
return checkForDeprecations(result)
return runTasksAndFail(WarningMode.Fail, tasks)
}

BuildResult runTasksAndFail(WarningMode warningMode, String... tasks) {
return createRunner(warningMode, tasks).buildAndFail()
}

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

GradleRunner createRunner(String... tasks) {
def pluginArgs = definePluginOutsideOfPluginBlock ? createGradleTestKitInitArgs() : new ArrayList<>()
return createRunner(WarningMode.Fail, tasks)
}

GradleRunner createRunner(WarningMode warningMode, String... tasks) {
def arguments = calculateArguments(warningMode, tasks)
def pluginArgs = definePluginOutsideOfPluginBlock ? createGradleTestKitInitArgs() : new ArrayList<String>()
def gradleRunnerBuilder = GradleRunner.create()
.withProjectDir(projectDir)
.withArguments(calculateArguments(tasks) + pluginArgs)
.withArguments(arguments + pluginArgs)
.withDebug(debug)
.withPluginClasspath()

if(forwardOutput) {
if (forwardOutput) {
gradleRunnerBuilder.forwardOutput()
}
if (gradleVersion != null) {
Expand All @@ -122,9 +133,4 @@ abstract class IntegrationTestKitSpec extends BaseIntegrationSpec {

return Arrays.asList("--init-script", initScript.getAbsolutePath())
}

protected BuildResult checkForDeprecations(BuildResult result) {
checkForDeprecations(result.output)
return result
}
}

0 comments on commit d9030e9

Please sign in to comment.