Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,46 +29,23 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
"""
}

@Unroll
def "can configure spooling #spooling"() {
setup:
buildFile << """
import org.elasticsearch.gradle.LoggedExec
tasks.register('loggedExec', LoggedExec) {
commandLine 'ls', '-lh'
getSpoolOutput().set($spooling)
}
"""
when:
def result = gradleRunner("loggedExec").build()
then:
result.task(':loggedExec').outcome == TaskOutcome.SUCCESS
file("build/buffered-output/loggedExec").exists() == spooling
where:
spooling << [false, true]
}

@Unroll
def "failed tasks output logged to console when spooling #spooling"() {
def "failed tasks output logged to console"() {
setup:
buildFile << """
import org.elasticsearch.gradle.LoggedExec
tasks.register('loggedExec', LoggedExec) {
commandLine 'ls', 'wtf'
getSpoolOutput().set($spooling)
}
"""
when:
def result = gradleRunner("loggedExec").buildAndFail()
then:
result.task(':loggedExec').outcome == TaskOutcome.FAILED
file("build/buffered-output/loggedExec").exists() == spooling
assertOutputContains(result.output, """\
> Task :loggedExec FAILED
Output for ls:""".stripIndent())
assertOutputContains(result.output, "No such file or directory")
where:
spooling << [false, true]
}

def "can capture output"() {
Expand All @@ -91,27 +68,6 @@ class LoggedExecFuncTest extends AbstractGradleFuncTest {
result.getOutput().contains("OUTPUT HELLO")
}

def "capturing output with spooling enabled is not supported"() {
setup:
buildFile << """
import org.elasticsearch.gradle.LoggedExec
tasks.register('loggedExec', LoggedExec) {
commandLine 'echo', 'HELLO'
getCaptureOutput().set(true)
getSpoolOutput().set(true)
}
"""
when:
def result = gradleRunner("loggedExec").buildAndFail()
then:
result.task(':loggedExec').outcome == TaskOutcome.FAILED
assertOutputContains(result.output, '''\
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':loggedExec'.
> Capturing output is not supported when spoolOutput is true.'''.stripIndent())
}


def "can configure output indenting"() {
Expand Down
31 changes: 2 additions & 29 deletions build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -99,9 +98,6 @@ public Provider<String> getWorkingDirPath() {
@Internal
abstract public Property<File> getWorkingDir();

@Internal
abstract public Property<Boolean> getSpoolOutput();

private String output;

@Inject
Expand All @@ -118,7 +114,6 @@ public LoggedExec(
// For now mimic default behaviour of Gradle Exec task here
setupDefaultEnvironment(providerFactory);
getCaptureOutput().convention(false);
getSpoolOutput().convention(false);
}

/**
Expand Down Expand Up @@ -147,34 +142,12 @@ private void setupDefaultEnvironment(ProviderFactory providerFactory) {

@TaskAction
public void run() {
boolean spoolOutput = getSpoolOutput().get();
if (spoolOutput && getCaptureOutput().get()) {
throw new GradleException("Capturing output is not supported when spoolOutput is true.");
}
if (getCaptureOutput().get() && getIndentingConsoleOutput().isPresent()) {
throw new GradleException("Capturing output is not supported when indentingConsoleOutput is configured.");
}
Consumer<Logger> outputLogger;
OutputStream out;
if (spoolOutput) {
File spoolFile = new File(projectLayout.getBuildDirectory().dir("buffered-output").get().getAsFile(), this.getName());
out = new LazyFileOutputStream(spoolFile);
outputLogger = logger -> {
try {
// the file may not exist if the command never output anything
if (Files.exists(spoolFile.toPath())) {
try (var lines = Files.lines(spoolFile.toPath())) {
lines.forEach(logger::error);
}
}
} catch (IOException e) {
throw new RuntimeException("could not log", e);
}
};
} else {
out = new ByteArrayOutputStream();
outputLogger = getIndentingConsoleOutput().isPresent() ? logger -> {} : logger -> logger.error(byteStreamToString(out));
}
OutputStream out = new ByteArrayOutputStream();
outputLogger = getIndentingConsoleOutput().isPresent() ? logger -> {} : logger -> logger.error(byteStreamToString(out));

OutputStream finalOutputStream = getIndentingConsoleOutput().isPresent()
? new IndentingOutputStream(System.out, getIndentingConsoleOutput().get())
Expand Down