Skip to content

Commit

Permalink
G2-1660 Create basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
gdgib committed Oct 25, 2024
1 parent 57db69d commit d06d992
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions ax-command/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.classpath
/.project
/.factorypath
/clireport*
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.g2forge.alexandria.command.invocation.runner;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Before;
import org.junit.Test;

import com.g2forge.alexandria.command.invocation.CommandInvocation;
import com.g2forge.alexandria.command.invocation.environment.SystemEnvironment;
import com.g2forge.alexandria.command.invocation.format.ICommandFormat;
import com.g2forge.alexandria.command.process.HProcess;
import com.g2forge.alexandria.command.stdio.StandardIO;
import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.io.HBinaryIO;
import com.g2forge.alexandria.java.io.HIO;
import com.g2forge.alexandria.java.io.HTextIO;
import com.g2forge.alexandria.java.io.RuntimeIOException;
import com.g2forge.alexandria.java.platform.HPlatform;
import com.g2forge.alexandria.test.HAssert;

import lombok.Getter;

public class TestICommandRunner {
protected static final String CLIREPORT_VERSION = "v0.0.1";

protected static final String CLIREPORT_FILENAME = "clireport";

@Getter
protected Path cliReport;

@Before
public void before() {
cliReport = Paths.get(HPlatform.getPlatform().getExeSpecs()[0].fromBase(CLIREPORT_FILENAME));
if (!Files.exists(cliReport)) {
try (final InputStream input = new URL(String.format("https://github.com/g2forge/clireport/releases/download/%1$s/%2$s", CLIREPORT_VERSION, cliReport.getFileName().toString())).openStream();
final OutputStream output = Files.newOutputStream(cliReport)) {
HBinaryIO.copy(input, output);
} catch (IOException e) {
throw new RuntimeIOException("Failed to download clireport", e);
}
HAssert.assertTrue(Files.exists(cliReport));
}
}

@Test
public void test() throws IOException, InterruptedException {
final List<String> arguments = HCollection.asList(getCliReport().toString(), "argument");
final List<String> output = test(arguments);
final List<String> expected = HCollection.concatenate(HCollection.asList(String.format("CLIReport: %1$d arguments", arguments.size())), arguments.stream().map(argument -> String.format("%1$04d: %2$s", argument.length(), argument)).collect(Collectors.toList()));
HAssert.assertEquals(expected, output);
}

protected List<String> test(final List<String> arguments) throws IOException, InterruptedException {
final CommandInvocation.CommandInvocationBuilder<ProcessBuilder.Redirect, ProcessBuilder.Redirect> invocationBuilder = CommandInvocation.builder();
invocationBuilder.format(ICommandFormat.getDefault());
invocationBuilder.arguments(arguments);
invocationBuilder.io(new StandardIO<>(ProcessBuilder.Redirect.INHERIT, ProcessBuilder.Redirect.PIPE, ProcessBuilder.Redirect.DISCARD));
invocationBuilder.working(Paths.get(System.getProperty("user.dir")));
invocationBuilder.environment(SystemEnvironment.create());
final CommandInvocation<ProcessBuilder.Redirect, ProcessBuilder.Redirect> invocation = invocationBuilder.build();
final CommandInvocation<ProcessBuilder.Redirect, ProcessBuilder.Redirect> wrapped = ICommandRunner.create(null).wrap(invocation);
final ProcessBuilder processBuilder = HProcess.createProcessBuilder(wrapped);

final List<String> output = new ArrayList<>();
final int exitCode;
final Process process = processBuilder.start();
try {
final Thread thread = new Thread(() -> {
HTextIO.readAll(process.getInputStream(), output::add);
});
thread.run();

exitCode = process.waitFor();
thread.interrupt();
} finally {
if (process != null) {
process.descendants().forEach(handle -> handle.destroy());
process.destroy();
HIO.closeAll(process.getInputStream(), process.getErrorStream(), process.getOutputStream());
}
}
HAssert.assertEquals(0, exitCode);
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public enum ExecutableSpec implements IPlatformNamed {
protected final String prefix;

protected final String suffix;

public String fromBase(String base) {
final StringBuilder retVal = new StringBuilder();
if (getPrefix() != null) retVal.append(getPrefix());
retVal.append(base);
if (getSuffix() != null) retVal.append(getSuffix());
return retVal.toString();
}
}

0 comments on commit d06d992

Please sign in to comment.