Skip to content

Commit

Permalink
added cpp executor and cpp searcher (#143)
Browse files Browse the repository at this point in the history
* added cpp executor and cpp searcher

* improved regular expression for cpp_searcher.java

* Update CppExecutor.java

reformat compilation command
  • Loading branch information
polischuks authored Nov 13, 2023
1 parent 94a1452 commit 833d52c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/hyperskill/hstest/stage/StageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import org.hyperskill.hstest.testcase.TestCase;
import org.hyperskill.hstest.testing.TestRun;
import org.hyperskill.hstest.testing.execution.MainMethodExecutor;
import org.hyperskill.hstest.testing.execution.process.GoExecutor;
import org.hyperskill.hstest.testing.execution.process.JavascriptExecutor;
import org.hyperskill.hstest.testing.execution.process.PythonExecutor;
import org.hyperskill.hstest.testing.execution.process.ShellExecutor;
import org.hyperskill.hstest.testing.execution.process.*;
import org.hyperskill.hstest.testing.runner.AsyncDynamicTestingRunner;
import org.hyperskill.hstest.testing.runner.TestRunner;
import org.junit.Test;
Expand Down Expand Up @@ -85,6 +82,9 @@ private TestRunner initRunner() {

for (var folder : walkUserFiles(FileUtils.cwd())) {
for (var file : folder.getFiles()) {
if (file.getName().endsWith(".cpp")) {
return new AsyncDynamicTestingRunner(CppExecutor.class);
}
if (file.getName().endsWith(".go")) {
return new AsyncDynamicTestingRunner(GoExecutor.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.hyperskill.hstest.testing.execution.process;

import org.hyperskill.hstest.testing.execution.ProcessExecutor;
import org.hyperskill.hstest.testing.execution.searcher.CppSearcher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.hyperskill.hstest.common.FileUtils.abspath;
import static org.hyperskill.hstest.common.OsUtils.isWindows;

/**
* Executes C++ runnable files
* (files with main function)
* in the given directory.
*
*/
public class CppExecutor extends ProcessExecutor {
private final String executable;
private final String filename;

public CppExecutor(String sourceName) {
super(new CppSearcher().find(sourceName));

var fileName = runnable.getFile().getName();

var withoutCpp = fileName
.substring(0, fileName.length() - new CppSearcher().extension().length());

if (isWindows()) {
executable = withoutCpp;
filename = executable + ".exe";
} else {
executable = "./" + withoutCpp;
filename = withoutCpp;
}
}

@Override
protected List<String> compilationCommand() {
return List.of("g++", "-std", "c++20", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName());
}

@Override
protected String filterCompilationError(String error) {
// Adapt error filtering if needed
return error;
}

@Override
protected List<String> executionCommand(List<String> args) {
List<String> fullArgs = new ArrayList<>();
fullArgs.add(executable);
fullArgs.addAll(args);

return fullArgs;
}

@Override
protected void cleanup() {
try {
Files.deleteIfExists(Paths.get(abspath(filename)));
} catch (IOException ignored) { }
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.hyperskill.hstest.testing.execution.searcher;

import org.hyperskill.hstest.testing.execution.runnable.RunnableFile;

/**
* Searches for C++ runnable files
* (files with main function)
* in the given directory
* and returns the first one found.
*/
public class CppSearcher extends BaseSearcher {
@Override
public String extension() {
return ".cpp";
}

@Override
public RunnableFile search(String whereToSearch) {
return simpleSearch(whereToSearch,
"int main()",
"(^|\\n)\\s*int\\s+main\\s*\\(.*\\)"
);
}
}

0 comments on commit 833d52c

Please sign in to comment.