Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS: Fix SimpleMakefile when command is wrapped #695

Merged
merged 1 commit into from
Sep 30, 2024
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 @@ -22,7 +22,7 @@ class InvalidMakefileException extends Exception {}
class SimpleMakefile {

// This pattern validates if a command is a make command.
private static final Pattern isMakeCommand = Pattern.compile("^(?<before>.* && )?make(?:\\s+(?<startRule>\\w*))?(?<assignments>(?:.*?=.*?)+)?(?<after> && .*)?$");
private static final Pattern isMakeCommand = Pattern.compile("^(?<before>env CODEOCEAN=true /bin/bash -c \\\"(.* && )?)make(?:\\s+(?<startRule>\\w*))?(?<assignments>(?:.*?=.*?)+)?(?<after>( && .*)?\\\")$");

// This pattern identifies the rules in a makefile.
private static final Pattern makeRules = Pattern.compile("(?<name>.*):\\r?\\n(?<commands>(?:\\t.+\\r?\\n?)*)");
Expand Down Expand Up @@ -110,7 +110,12 @@ private String getAssignmentPart(String assignment, boolean firstPart) {
if (firstPart) {
return parts[0];
} else {
return parts[1].replaceAll("^\\\"|\\\"$", "");
String value = parts[1];
// First, remove one level of escaping: \"HelloWorldTest\" -> "HelloWorldTest"
value = value.replaceAll("\\\\\"", "\"");
// Then, remove the quotes at the beginning and end of string: "HelloWorldTest" -> HelloWorldTest
value = value.replaceAll("^\"|\"$", "");
return value;
}
}

Expand Down
2 changes: 1 addition & 1 deletion deploy/aws/java11Exec/src/test/java/poseidon/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void outputWithoutTrailingNewline() {
public void makefileJustReplacesShellCommand() {
ByteArrayOutputStream out = setupStdOutLogs();
APIGatewayProxyResponseEvent result = getApiGatewayProxyResponse("{\"action\":\"java11Exec\"," +
"\"cmd\":[\"env\", \"TEST_VAR=42\", \"sh\",\"-c\",\"make run\"]," +
"\"cmd\":[\"env\", \"TEST_VAR=42\", \"sh\",\"-c\",\"env CODEOCEAN=true /bin/bash -c \\\"make run\\\"\"]," +
"\"files\":{\"Makefile\":\"" + Base64.getEncoder().encodeToString(("run:\n\t@echo $TEST_VAR\n").getBytes(StandardCharsets.UTF_8)) + "\"}}");
restoreStdOutLogs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ public void sucessfullMakeWithComment() {
parseRunCommandOfMakefile(SuccessfulMakefileWithComment);
}

private String wrapFullCommand(String command) {
return "env CODEOCEAN=true /bin/bash -c \"" + command + "\"";
}

private void parseRunCommandOfMakefile(String makefileB64) {
Map<String, String> files = new HashMap<>();
files.put("Makefile", makefileB64);
files.put("org/example/RecursiveMath.java", RecursiveMathContent);

try {
String command = "make run";
String command = wrapFullCommand("make run");
SimpleMakefile makefile = new SimpleMakefile(files);
String cmd = makefile.parseCommand(command);

assertEquals("javac org/example/RecursiveMath.java && java org/example/RecursiveMath", cmd);
assertEquals(wrapFullCommand("javac org/example/RecursiveMath.java && java org/example/RecursiveMath"), cmd);
} catch (NoMakefileFoundException | InvalidMakefileException | NoMakeCommandException ignored) {
fail();
}
Expand Down Expand Up @@ -122,12 +126,12 @@ public void sucessfullMakeWithAssignments() {
files.put("org/example/RecursiveMath.java", RecursiveMathContent);

try {
String command = "make test CLASS_NAME=\"RecursiveMath\" FILENAME=\"RecursiveMath-Test.java\"";
String command = wrapFullCommand("make test CLASS_NAME=\"RecursiveMath\" FILENAME=\"RecursiveMath-Test.java\"");
SimpleMakefile make = new SimpleMakefile(files);
String cmd = make.parseCommand(command);

assertEquals("javac -encoding utf8 RecursiveMath-Test.java && " +
"java -Dfile.encoding=UTF8 RecursiveMath", cmd);
assertEquals(wrapFullCommand("javac -encoding utf8 RecursiveMath-Test.java && " +
"java -Dfile.encoding=UTF8 RecursiveMath"), cmd);
} catch (NoMakefileFoundException | InvalidMakefileException | NoMakeCommandException ignored) {
fail();
}
Expand All @@ -140,7 +144,7 @@ public void withNotSupportedMakefile() {
files.put("org/example/RecursiveMath.java", RecursiveMathContent);

try {
String command = "make run";
String command = wrapFullCommand("make run");
SimpleMakefile makefile = new SimpleMakefile(files);
makefile.parseCommand(command);
fail();
Expand All @@ -155,11 +159,11 @@ public void withBeforeAndAfterStatements() {
files.put("Makefile", Base64.getEncoder().encodeToString(("run:\n\t@echo TRAAAIIN\n").getBytes(StandardCharsets.UTF_8)));

try {
String command = "echo \"Look it's a\" && sl && make run && echo WOW";
String command = wrapFullCommand("echo \"Look it's a\" && sl && make run && echo WOW");
SimpleMakefile makefile = new SimpleMakefile(files);
String cmd = makefile.parseCommand(command);

assertEquals("echo \"Look it's a\" && sl && echo TRAAAIIN && echo WOW", cmd);
assertEquals(wrapFullCommand("echo \"Look it's a\" && sl && echo TRAAAIIN && echo WOW"), cmd);
} catch (NoMakefileFoundException | InvalidMakefileException | NoMakeCommandException ignored) {
fail();
}
Expand Down
Loading