Skip to content

Commit

Permalink
Merge pull request #389 from gdgib/G2-1661-BasicTests
Browse files Browse the repository at this point in the history
G2-1661  Create basic tests & design changes
  • Loading branch information
gdgib authored Oct 30, 2024
2 parents 98889eb + e04a49f commit 9d9ef64
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CMDCommandRunner extends AShellCommandRunner {
@Override
public <I, O> CommandInvocation<I, O> wrap(CommandInvocation<I, O> invocation) {
final List<? extends String> shellArguments = getShellArguments();
final List<String> retVal = new ArrayList<>(shellArguments.size() + 1 + invocation.getArguments().size());
final List<String> retVal = new ArrayList<>(1 + shellArguments.size() + invocation.getArguments().size());
retVal.add(getShellExecutable());
retVal.addAll(shellArguments);
retVal.addAll(invocation.getArguments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PosixShellCommandRunner extends AShellCommandRunner {
@Override
public <I, O> CommandInvocation<I, O> wrap(CommandInvocation<I, O> invocation) {
final List<? extends String> shellArguments = getShellArguments();
final List<String> retVal = new ArrayList<>(shellArguments.size() + 2);
final List<String> retVal = new ArrayList<>(1 + shellArguments.size() + 1);
retVal.add(getShellExecutable());
retVal.addAll(shellArguments);
retVal.add(invocation.getArguments().stream().collect(Collectors.joining(" ")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected boolean isAllowAmbiguousCommands() {

protected boolean isQuoted(String string) {
final int last = string.length() - 1;
return (last >= 1) && (string.charAt(0) == '"') && (string.charAt(last) == '"');
return (last > 1) && (string.charAt(0) == '"') && (string.charAt(last) == '"');
}

public boolean isQuoteRequired(SpecialCharacterSet specialCharacterSet, String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.g2forge.alexandria.command.invocation.CommandInvocation;
Expand All @@ -27,6 +29,7 @@
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.java.platform.PlatformCategory;
import com.g2forge.alexandria.test.HAssert;

import lombok.Getter;
Expand All @@ -39,6 +42,14 @@ public class TestICommandRunner {
@Getter
protected Path cliReport;

protected void assumeMicrosoft() {
Assume.assumeTrue(PlatformCategory.Microsoft.equals(HPlatform.getPlatform().getCategory()));
}

protected void assumePosix() {
Assume.assumeTrue(PlatformCategory.Posix.equals(HPlatform.getPlatform().getCategory()));
}

@Before
public void before() {
cliReport = Paths.get(HPlatform.getPlatform().getExeSpecs()[0].fromBase(CLIREPORT_FILENAME));
Expand All @@ -62,17 +73,35 @@ public void before() {
}

@Test
public void test() throws IOException, InterruptedException {
final List<String> arguments = HCollection.asList(HPlatform.getPlatform().getCategory().convertExecutablePathToString(getCliReport()), "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);
public void microsoftBasic() throws IOException, InterruptedException {
assumeMicrosoft();
test("a", "'", "%VAR%", "$env:VAR");
}

@Test
@Ignore
public void microsoftQuote() throws IOException, InterruptedException {
assumeMicrosoft();
test("a", "\"\\\"\"", "b");
}

protected List<String> test(final List<String> arguments) throws IOException, InterruptedException {
@Ignore
@Test
public void posix() throws IOException, InterruptedException {
assumePosix();
test("a", "\"", "'", "${VAR}");
}

@Test
public void simple() throws IOException, InterruptedException {
test("argument");
}

protected void test(String... arguments) throws IOException, InterruptedException {
final CommandInvocation.CommandInvocationBuilder<ProcessBuilder.Redirect, ProcessBuilder.Redirect> invocationBuilder = CommandInvocation.builder();
invocationBuilder.format(ICommandFormat.getDefault());
invocationBuilder.arguments(arguments);
invocationBuilder.argument(HPlatform.getPlatform().getCategory().convertExecutablePathToString(getCliReport()));
invocationBuilder.arguments(HCollection.asList(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());
Expand All @@ -99,6 +128,8 @@ protected List<String> test(final List<String> arguments) throws IOException, In
}
}
HAssert.assertEquals(0, exitCode);
return output;

final List<String> expected = HCollection.concatenate(HCollection.asList(String.format("CLIReport: %1$d arguments", invocation.getArguments().size())), invocation.getArguments().stream().map(argument -> String.format("%1$04d: %2$s", argument.length(), argument)).collect(Collectors.toList()));
HAssert.assertEquals(expected, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import com.g2forge.alexandria.java.text.escape.IEscapeType;

public interface IQuoteType {
public default String escape(final String string) {
return getEscapeType().getEscaper().escape(string);
}

public IEscapeType getEscapeType();

/**
Expand Down Expand Up @@ -40,20 +36,16 @@ public default String quote(final QuoteControl option, final String string, IQuo
if ((option != QuoteControl.IfNeeded) || isQuoteNeeded(string)) {
final StringBuilder builder = new StringBuilder();
builder.append(getPrefix());
builder.append(escape(string));
builder.append(getEscapeType().getEscaper().escape(string));
builder.append(getPostfix());
return builder.toString();
}

return string;
}

public default String unescape(final String string) {
return getEscapeType().getEscaper().unescape(string);
}

public default String unquote(final String string) {
if (!isQuoted(string)) return string;
return unescape(string.substring(getPrefix().length(), string.length() - getPostfix().length()));
return getEscapeType().getEscaper().unescape(string.substring(getPrefix().length(), string.length() - getPostfix().length()));
}
}

0 comments on commit 9d9ef64

Please sign in to comment.