Allow shortening the command line using a pathing JAR by lacasseio · Pull Request #10544 · gradle/gradle

Expand Up @@ -29,20 +29,8 @@ class JavaExecWIthLongCommandLineIntegrationTest extends AbstractIntegrationSpec file("src/main/java/Driver.java").text = """ package driver;
import java.io.*; import java.lang.System;
public class Driver { public static void main(String[] args) { try { FileWriter out = new FileWriter("out.txt"); out.write(System.getenv("CLASSPATH")); out.write("\\n"); out.close(); } catch (IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) {} } """
Expand All @@ -52,7 +40,6 @@ class JavaExecWIthLongCommandLineIntegrationTest extends AbstractIntegrationSpec task run(type: JavaExec) { classpath = project.layout.files(compileJava) main "driver.Driver" args "1" } """ } Expand Down Expand Up @@ -84,8 +71,10 @@ class JavaExecWIthLongCommandLineIntegrationTest extends AbstractIntegrationSpec failure.assertThatCause(containsText("could not be started because the command line exceed operating system limits.")) }
def "does not suggest long command line failures when execution fails for short command line"() { @Requires(TestPrecondition.NOT_WINDOWS) def "does not suggest long command line failures when execution fails on non-Windows system"() { buildFile << """ run.classpath += project.files('${'a' * ARG_MAX_WINDOWS}') run.executable 'some-java' """
Expand All @@ -96,108 +85,53 @@ class JavaExecWIthLongCommandLineIntegrationTest extends AbstractIntegrationSpec failure.assertThatCause(containsText("A problem occurred starting process")) }
@Requires(TestPrecondition.WINDOWS) def "succeeds when long classpath fit inside environment variable"() { def fileName = 'a' * (ARG_MAX_WINDOWS / 2) def "does not suggest long command line failures when execution fails for short command line"() { buildFile << """ run.classpath += project.files('${fileName}') run.args '${fileName}' run.executable 'some-java' """
when: succeeds("run") def failure = fails("run")
then: executedAndNotSkipped(":run") failure.assertThatCause(containsText("A problem occurred starting process")) }
@Requires(TestPrecondition.WINDOWS) def "fails when long classpath fit inside environment variable but CLASSPATH was explicitly defined (no inheritance)"() { def fileName = 'a' * (ARG_MAX_WINDOWS / 2) def "succeeds when long classpath"() { def fileName = 'a' * ((ARG_MAX_WINDOWS / 2) * 3) buildFile << """ run.classpath += project.files('${fileName}') run.args '${fileName}' run.environment('CLASSPATH', 'some-classpath-from-task') """
when: executer.withEnvironmentVars([:]) // Currently this has no effect because the gradle[w].bat script leaks a CLASSPATH environment variable, see https://github.com/gradle/gradle/issues/10463 def failure = fails("run", "-i") succeeds("run", "-i")
then: failure.assertThatCause(containsText("could not be started because the command line exceed operating system limits.")) failure.assertOutputContains("CLASSPATH environment variable was explicitly overwritten, Gradle cannot shorten the command line") }
@Requires(TestPrecondition.WINDOWS) def "fails when long classpath fit inside environment variable but CLASSPATH was explicitly overwritten"() { def fileName = 'a' * (ARG_MAX_WINDOWS / 2) buildFile << """ run.classpath += project.files('${fileName}') run.args '${fileName}' run.environment('CLASSPATH', 'some-classpath-from-task') """
when: executer.withEnvironmentVars([CLASSPATH: 'some-classpath']) def failure = fails("run", "-i")
then: failure.assertThatCause(containsText("could not be started because the command line exceed operating system limits.")) failure.assertOutputContains("CLASSPATH environment variable was explicitly overwritten, Gradle cannot shorten the command line") executedAndNotSkipped(":run") assertOutputContainsShorteningMessage(fileName) }
@Requires(TestPrecondition.WINDOWS) def "fails when long classpath fit inside environment variable but CLASSPATH was explicitly cleared"() { def "still fail when classpath doesn't shorten the command line enough"() { def fileName = 'a' * (ARG_MAX_WINDOWS / 2) buildFile << """ run.classpath += project.files('${fileName}') run.args '${fileName}' def newEnvironment = run.environment newEnvironment.remove('CLASSPATH') run.environment = newEnvironment run.args "${'b' * ARG_MAX_WINDOWS}" """
when: executer.withEnvironmentVars([CLASSPATH: 'some-classpath']) def failure = fails("run", "-i")
then: failure.assertThatCause(containsText("could not be started because the command line exceed operating system limits.")) failure.assertOutputContains("CLASSPATH environment variable was explicitly cleared, Gradle cannot shorten the command line") }
@Requires(TestPrecondition.WINDOWS) def "succeeds when long classpath fit inside environment variable and CLASSPATH was inherited"() { def fileName = 'a' * (ARG_MAX_WINDOWS / 2) buildFile << """ run.classpath += project.files('${fileName}') run.args '${fileName}' """
when: executer.withEnvironmentVars([CLASSPATH: 'some-classpath']) succeeds("run")
then: executedAndNotSkipped(":run") assertOutputFileIs("${file('build/classes/java/main')};${file('build/generated/sources/annotationProcessor/java/main')};${testDirectory.absolutePath}\\${fileName}\n") }
@Requires(TestPrecondition.WINDOWS) def "fails when long classpath exceed environment variable value length"() { buildFile << """ run.classpath += project.files('${'a' * ((ARG_MAX_WINDOWS / 2) * 3)}') """
when: def failure = fails("run")
then: failure.assertThatCause(containsText("could not be started because the command line exceed operating system limits.")) private void assertOutputContainsShorteningMessage(String fileName) { assert output =~ /^Shortening Java classpath ${classpath(fileName)} with .+\\/gradle-javaexec-classpath.+\.jar$/ }
private void assertOutputFileIs(String text) { assert file("out.txt").text == text private String classpath(String fileName) { return "${file('build/classes/java/main')};${file('build/generated/sources/annotationProcessor/java/main')};${testDirectory.absolutePath}\\${fileName}" } }