From 4d1060e9d967da87ef43c0363251d623134726ef Mon Sep 17 00:00:00 2001 From: home1 Date: Thu, 18 May 2023 00:13:23 +0300 Subject: [PATCH 01/11] Added CheckLibraryVersion class for check library version --- build.gradle | 3 + .../hstest/checker/CheckLibraryVersion.java | 129 ++++++++++++++++++ .../hyperskill/hstest/resources/version.txt | 1 + .../hyperskill/hstest/stage/StageTest.java | 6 + 4 files changed, 139 insertions(+) create mode 100644 src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java create mode 100644 src/main/java/org/hyperskill/hstest/resources/version.txt diff --git a/build.gradle b/build.gradle index a33f39fb..05df8dca 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,9 @@ dependencies { compileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22' + + implementation 'javax.json:javax.json-api:1.1.4' + implementation 'org.glassfish:javax.json:1.1.4' } compileJava.options.encoding = 'UTF-8' diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java new file mode 100644 index 00000000..94ef4aa0 --- /dev/null +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -0,0 +1,129 @@ +package org.hyperskill.hstest.checker; + +import org.hyperskill.hstest.exception.outcomes.UnexpectedError; + +import javax.json.Json; +import javax.json.JsonObject; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.time.LocalDate; +import java.util.stream.Collectors; + +/** +* Checks if the current version of the library is the latest one on GitHub releases page of the library. +* If not, throws an exception. + */ +public class CheckLibraryVersion { + + private String VERSION_FILE = "src/main/java/org/hyperskill/hstest/resources/version.txt"; + + private String currentVersion; + private String latestVersion; + private boolean isLatestVersion = true; + + public CheckLibraryVersion() { + } + + /** + * Checks if the current version of the library is the latest one on GitHub releases page of the library. + * If not, throws an exception. + */ + public void checkVersion() { + LocalDate lastChecked = null; + File lastCheckedFile = new File("LastChecked.txt"); + if (lastCheckedFile.exists()) { + try { + FileReader fileReader = new FileReader(lastCheckedFile); + BufferedReader reader = new BufferedReader(fileReader); + lastChecked = LocalDate.parse(reader.readLine()); + reader.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + if (lastChecked != null && lastChecked.equals(LocalDate.now())) { + return; + } + + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(VERSION_FILE); + if (inputStream == null) { + throw new RuntimeException("Unable to find hs-test library version file"); + } + + try { + currentVersion = new BufferedReader(new InputStreamReader(inputStream)).readLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + latestVersion = getLatestHsTestVersionFromGitHub(); + + if (!currentVersion.equals(latestVersion)) { + if (currentVersion.split("\\.")[0] != latestVersion.split("\\.")[0]) { + throw new RuntimeException(getFeedback()); + } else { + isLatestVersion = false; + } + } + + lastChecked = LocalDate.now(); + try (FileWriter writer = new FileWriter(lastCheckedFile)) { + writer.write(lastChecked.toString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Returns latest version of the library from GitHub releases page of the library. + * @return String latest version of the library + */ + private String getLatestHsTestVersionFromGitHub() { + HttpURLConnection connection = null; + try { + URL url = new URL("https://api.github.com/repos/hyperskill/hs-test/releases/latest"); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setRequestProperty("Accept", "application/vnd.github+json"); + + int responseCode = connection.getResponseCode(); + if (responseCode != 200) { + throw new UnexpectedError("Error getting latest version of hs-test library from GitHub"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { + String response = in.lines().collect(Collectors.joining()); + JsonObject jsonObject = Json.createReader(new StringReader(response)).readObject(); + return jsonObject.getString("tag_name"); + } catch (IOException e) { + throw new UnexpectedError("Error getting latest version of hs-test library from GitHub"); + } + } + + /** + * Returns feedback for the user if the current version of the library is not the latest one. + * @return String feedback for the user + */ + public String getFeedback() { + return "\nThe installed hs-test version (" + currentVersion + ") is not the latest version (" + latestVersion + "). " + + "Update the library by following the instructions below:\n\n" + + "1. Open your project's dependency file build.gradle.\n" + + "2. Find the hs-test dependency and change its version to the latest one (" + latestVersion + ").\n" + + "3. Sync the dependencies in your development environment or run the following commands in the terminal:\n" + + " For Gradle:\n" + + " gradle clean build --refresh-dependencies\n\n" + + "4. Restart the tests.\n\n"; + } + + /** + * Returns true if the current version of the library is the latest one. + * @return Boolean true if the current version of the library is the latest one + */ + public Boolean getLatestVersion() { + return isLatestVersion; + } +} diff --git a/src/main/java/org/hyperskill/hstest/resources/version.txt b/src/main/java/org/hyperskill/hstest/resources/version.txt new file mode 100644 index 00000000..6eb2d9f3 --- /dev/null +++ b/src/main/java/org/hyperskill/hstest/resources/version.txt @@ -0,0 +1 @@ +10.0.3 \ No newline at end of file diff --git a/src/main/java/org/hyperskill/hstest/stage/StageTest.java b/src/main/java/org/hyperskill/hstest/stage/StageTest.java index a64309dd..bea991a0 100644 --- a/src/main/java/org/hyperskill/hstest/stage/StageTest.java +++ b/src/main/java/org/hyperskill/hstest/stage/StageTest.java @@ -1,6 +1,7 @@ package org.hyperskill.hstest.stage; import lombok.Getter; +import org.hyperskill.hstest.checker.CheckLibraryVersion; import org.hyperskill.hstest.common.FileUtils; import org.hyperskill.hstest.common.ReflectionUtils; import org.hyperskill.hstest.dynamic.ClassSearcher; @@ -138,6 +139,8 @@ private void printTestNum(int num) { @Test public final void start() { + CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion(); + checkLibraryVersion.checkVersion(); int currTest = 0; boolean needTearDown = false; try { @@ -166,6 +169,9 @@ public final void start() { if (!result.isCorrect()) { String fullFeedback = result.getFeedback() + "\n\n" + testRun.getTestCase().getFeedback(); + if (!checkLibraryVersion.getLatestVersion()) { + fullFeedback += checkLibraryVersion.getFeedback(); + } throw new WrongAnswer(fullFeedback.trim()); } From 68cb13ff58aa33cca87251808865be5e9864cc35 Mon Sep 17 00:00:00 2001 From: home1 Date: Fri, 19 May 2023 12:15:50 +0300 Subject: [PATCH 02/11] Fixed CheckLibraryVersion class --- .../hstest/checker/CheckLibraryVersion.java | 32 ++++++++++--------- .../hyperskill/hstest/stage/StageTest.java | 6 ++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java index 94ef4aa0..120383f7 100644 --- a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -1,13 +1,14 @@ package org.hyperskill.hstest.checker; -import org.hyperskill.hstest.exception.outcomes.UnexpectedError; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; -import javax.json.Json; -import javax.json.JsonObject; import java.io.*; +import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; import java.time.LocalDate; +import java.util.Map; import java.util.stream.Collectors; /** @@ -57,14 +58,10 @@ public void checkVersion() { } catch (IOException e) { throw new RuntimeException(e); } - latestVersion = getLatestHsTestVersionFromGitHub(); + getLatestHsTestVersionFromGitHub(); if (!currentVersion.equals(latestVersion)) { - if (currentVersion.split("\\.")[0] != latestVersion.split("\\.")[0]) { - throw new RuntimeException(getFeedback()); - } else { - isLatestVersion = false; - } + isLatestVersion = false; } lastChecked = LocalDate.now(); @@ -79,7 +76,7 @@ public void checkVersion() { * Returns latest version of the library from GitHub releases page of the library. * @return String latest version of the library */ - private String getLatestHsTestVersionFromGitHub() { + private void getLatestHsTestVersionFromGitHub() { HttpURLConnection connection = null; try { URL url = new URL("https://api.github.com/repos/hyperskill/hs-test/releases/latest"); @@ -89,18 +86,23 @@ private String getLatestHsTestVersionFromGitHub() { int responseCode = connection.getResponseCode(); if (responseCode != 200) { - throw new UnexpectedError("Error getting latest version of hs-test library from GitHub"); + latestVersion = currentVersion; + return; } } catch (IOException e) { - throw new RuntimeException(e); + latestVersion = currentVersion; + return; } try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String response = in.lines().collect(Collectors.joining()); - JsonObject jsonObject = Json.createReader(new StringReader(response)).readObject(); - return jsonObject.getString("tag_name"); + + Gson gson = new Gson(); + Type type = new TypeToken>(){}.getType(); + Map map = gson.fromJson(response, type); + latestVersion = map.get("tag_name").toString().replace("v", ""); } catch (IOException e) { - throw new UnexpectedError("Error getting latest version of hs-test library from GitHub"); + latestVersion = currentVersion; } } diff --git a/src/main/java/org/hyperskill/hstest/stage/StageTest.java b/src/main/java/org/hyperskill/hstest/stage/StageTest.java index bea991a0..8ca60069 100644 --- a/src/main/java/org/hyperskill/hstest/stage/StageTest.java +++ b/src/main/java/org/hyperskill/hstest/stage/StageTest.java @@ -139,8 +139,6 @@ private void printTestNum(int num) { @Test public final void start() { - CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion(); - checkLibraryVersion.checkVersion(); int currTest = 0; boolean needTearDown = false; try { @@ -151,6 +149,9 @@ public final void start() { ReflectionUtils.setupCwd(this); } + CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion(); + checkLibraryVersion.checkVersion(); + List testRuns = initTests(); for (TestRun testRun : testRuns) { @@ -165,7 +166,6 @@ public final void start() { currTestRun = testRun; CheckResult result = testRun.test(); - if (!result.isCorrect()) { String fullFeedback = result.getFeedback() + "\n\n" + testRun.getTestCase().getFeedback(); From cc41f232be22e530062783cf8769ee20a450ec5b Mon Sep 17 00:00:00 2001 From: home1 Date: Fri, 19 May 2023 14:26:03 +0300 Subject: [PATCH 03/11] deleted non-usable dependencies --- build.gradle | 3 --- 1 file changed, 3 deletions(-) diff --git a/build.gradle b/build.gradle index 05df8dca..a33f39fb 100644 --- a/build.gradle +++ b/build.gradle @@ -27,9 +27,6 @@ dependencies { compileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22' - - implementation 'javax.json:javax.json-api:1.1.4' - implementation 'org.glassfish:javax.json:1.1.4' } compileJava.options.encoding = 'UTF-8' From 86f5fdbd5aaabd69742f3ee9544216748c53196c Mon Sep 17 00:00:00 2001 From: home1 Date: Tue, 23 May 2023 17:54:25 +0300 Subject: [PATCH 04/11] Fixed exception --- .../hstest/checker/CheckLibraryVersion.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java index 120383f7..d79b5b14 100644 --- a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -2,6 +2,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import org.hyperskill.hstest.exception.outcomes.UnexpectedError; import java.io.*; import java.lang.reflect.Type; @@ -30,17 +31,14 @@ public CheckLibraryVersion() { * Checks if the current version of the library is the latest one on GitHub releases page of the library. * If not, throws an exception. */ - public void checkVersion() { + public void checkVersion() throws IOException { LocalDate lastChecked = null; File lastCheckedFile = new File("LastChecked.txt"); if (lastCheckedFile.exists()) { - try { - FileReader fileReader = new FileReader(lastCheckedFile); - BufferedReader reader = new BufferedReader(fileReader); + try (BufferedReader reader = new BufferedReader(new FileReader(lastCheckedFile))) { lastChecked = LocalDate.parse(reader.readLine()); - reader.close(); } catch (IOException e) { - throw new RuntimeException(e); + throw new IOException(e); } } @@ -49,15 +47,10 @@ public void checkVersion() { } InputStream inputStream = getClass().getClassLoader().getResourceAsStream(VERSION_FILE); - if (inputStream == null) { - throw new RuntimeException("Unable to find hs-test library version file"); - } - - try { + if (inputStream != null) { currentVersion = new BufferedReader(new InputStreamReader(inputStream)).readLine(); - } catch (IOException e) { - throw new RuntimeException(e); - } + } else return; + getLatestHsTestVersionFromGitHub(); if (!currentVersion.equals(latestVersion)) { @@ -68,7 +61,7 @@ public void checkVersion() { try (FileWriter writer = new FileWriter(lastCheckedFile)) { writer.write(lastChecked.toString()); } catch (IOException e) { - throw new RuntimeException(e); + throw new IOException(e); } } From 6d07b7ada0b3db108826eefe820a2a328643a0dc Mon Sep 17 00:00:00 2001 From: home1 Date: Tue, 23 May 2023 17:56:31 +0300 Subject: [PATCH 05/11] Fixed exception --- .../java/org/hyperskill/hstest/checker/CheckLibraryVersion.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java index d79b5b14..b1dbf3c9 100644 --- a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -2,7 +2,6 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import org.hyperskill.hstest.exception.outcomes.UnexpectedError; import java.io.*; import java.lang.reflect.Type; From 194616a051f8a74a9c1d09eacdfda1155ec21a37 Mon Sep 17 00:00:00 2001 From: home1 Date: Tue, 23 May 2023 18:07:21 +0300 Subject: [PATCH 06/11] Fixed exception/omit catch block --- .../org/hyperskill/hstest/checker/CheckLibraryVersion.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java index b1dbf3c9..289e5237 100644 --- a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -36,8 +36,6 @@ public void checkVersion() throws IOException { if (lastCheckedFile.exists()) { try (BufferedReader reader = new BufferedReader(new FileReader(lastCheckedFile))) { lastChecked = LocalDate.parse(reader.readLine()); - } catch (IOException e) { - throw new IOException(e); } } @@ -59,8 +57,6 @@ public void checkVersion() throws IOException { lastChecked = LocalDate.now(); try (FileWriter writer = new FileWriter(lastCheckedFile)) { writer.write(lastChecked.toString()); - } catch (IOException e) { - throw new IOException(e); } } From d4ce11fd9ba761b8ef654186a0ad8f986ba7f161 Mon Sep 17 00:00:00 2001 From: polischuks Date: Fri, 2 Jun 2023 16:24:26 +0300 Subject: [PATCH 07/11] improvement for CheckLibraryVersion --- .../hstest/checker/CheckLibraryVersion.java | 51 ++++++++----------- .../hyperskill/hstest/stage/StageTest.java | 7 ++- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java index 289e5237..9d74b00a 100644 --- a/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java +++ b/src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java @@ -6,6 +6,7 @@ import java.io.*; import java.lang.reflect.Type; import java.net.HttpURLConnection; +import java.net.SocketTimeoutException; import java.net.URL; import java.time.LocalDate; import java.util.Map; @@ -18,10 +19,11 @@ public class CheckLibraryVersion { private String VERSION_FILE = "src/main/java/org/hyperskill/hstest/resources/version.txt"; - + private String LAST_CHECKED_FILE = "lastCheckedHSTestLibrary.txt"; + private String GITHUB_API = "https://api.github.com/repos/hyperskill/hs-test/releases/latest"; private String currentVersion; private String latestVersion; - private boolean isLatestVersion = true; + public boolean isLatestVersion = true; public CheckLibraryVersion() { } @@ -32,28 +34,24 @@ public CheckLibraryVersion() { */ public void checkVersion() throws IOException { LocalDate lastChecked = null; - File lastCheckedFile = new File("LastChecked.txt"); + String tempDirectoryPath = System.getProperty("java.io.tmpdir"); + File lastCheckedFile = new File(tempDirectoryPath + File.separator + LAST_CHECKED_FILE); if (lastCheckedFile.exists()) { try (BufferedReader reader = new BufferedReader(new FileReader(lastCheckedFile))) { lastChecked = LocalDate.parse(reader.readLine()); } } - - if (lastChecked != null && lastChecked.equals(LocalDate.now())) { + if (LocalDate.now().equals(lastChecked)) { return; } - InputStream inputStream = getClass().getClassLoader().getResourceAsStream(VERSION_FILE); if (inputStream != null) { currentVersion = new BufferedReader(new InputStreamReader(inputStream)).readLine(); } else return; - - getLatestHsTestVersionFromGitHub(); - + latestVersion = getLatestHsTestVersionFromGitHub(); if (!currentVersion.equals(latestVersion)) { isLatestVersion = false; } - lastChecked = LocalDate.now(); try (FileWriter writer = new FileWriter(lastCheckedFile)) { writer.write(lastChecked.toString()); @@ -64,33 +62,32 @@ public void checkVersion() throws IOException { * Returns latest version of the library from GitHub releases page of the library. * @return String latest version of the library */ - private void getLatestHsTestVersionFromGitHub() { + private String getLatestHsTestVersionFromGitHub() { HttpURLConnection connection = null; + int responseCode = -1; try { - URL url = new URL("https://api.github.com/repos/hyperskill/hs-test/releases/latest"); + URL url = new URL(GITHUB_API); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/vnd.github+json"); - - int responseCode = connection.getResponseCode(); - if (responseCode != 200) { - latestVersion = currentVersion; - return; - } + connection.setConnectTimeout(100); + connection.setReadTimeout(500); + responseCode = connection.getResponseCode(); } catch (IOException e) { - latestVersion = currentVersion; - return; + return currentVersion; + } + if (responseCode != 200) { + return currentVersion; } try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String response = in.lines().collect(Collectors.joining()); - Gson gson = new Gson(); Type type = new TypeToken>(){}.getType(); Map map = gson.fromJson(response, type); - latestVersion = map.get("tag_name").toString().replace("v", ""); + return map.get("tag_name").toString().replace("v", ""); } catch (IOException e) { - latestVersion = currentVersion; + return currentVersion; } } @@ -108,12 +105,4 @@ public String getFeedback() { " gradle clean build --refresh-dependencies\n\n" + "4. Restart the tests.\n\n"; } - - /** - * Returns true if the current version of the library is the latest one. - * @return Boolean true if the current version of the library is the latest one - */ - public Boolean getLatestVersion() { - return isLatestVersion; - } } diff --git a/src/main/java/org/hyperskill/hstest/stage/StageTest.java b/src/main/java/org/hyperskill/hstest/stage/StageTest.java index 8ca60069..6ae8f95e 100644 --- a/src/main/java/org/hyperskill/hstest/stage/StageTest.java +++ b/src/main/java/org/hyperskill/hstest/stage/StageTest.java @@ -149,9 +149,6 @@ public final void start() { ReflectionUtils.setupCwd(this); } - CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion(); - checkLibraryVersion.checkVersion(); - List testRuns = initTests(); for (TestRun testRun : testRuns) { @@ -167,9 +164,11 @@ public final void start() { currTestRun = testRun; CheckResult result = testRun.test(); if (!result.isCorrect()) { + CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion(); + checkLibraryVersion.checkVersion(); String fullFeedback = result.getFeedback() + "\n\n" + testRun.getTestCase().getFeedback(); - if (!checkLibraryVersion.getLatestVersion()) { + if (!checkLibraryVersion.isLatestVersion) { fullFeedback += checkLibraryVersion.getFeedback(); } throw new WrongAnswer(fullFeedback.trim()); From 833d52c2373920933646cae2f9d70e1538ee58dc Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 13 Nov 2023 18:59:11 +0200 Subject: [PATCH 08/11] added cpp executor and cpp searcher (#143) * added cpp executor and cpp searcher * improved regular expression for cpp_searcher.java * Update CppExecutor.java reformat compilation command --- .../hyperskill/hstest/stage/StageTest.java | 8 +-- .../execution/process/CppExecutor.java | 70 +++++++++++++++++++ .../execution/searcher/CppSearcher.java | 24 +++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java create mode 100644 src/main/java/org/hyperskill/hstest/testing/execution/searcher/CppSearcher.java diff --git a/src/main/java/org/hyperskill/hstest/stage/StageTest.java b/src/main/java/org/hyperskill/hstest/stage/StageTest.java index 6ae8f95e..c3982826 100644 --- a/src/main/java/org/hyperskill/hstest/stage/StageTest.java +++ b/src/main/java/org/hyperskill/hstest/stage/StageTest.java @@ -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; @@ -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); } diff --git a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java new file mode 100644 index 00000000..af7c245b --- /dev/null +++ b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java @@ -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 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 executionCommand(List args) { + List 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) { } + } +} + diff --git a/src/main/java/org/hyperskill/hstest/testing/execution/searcher/CppSearcher.java b/src/main/java/org/hyperskill/hstest/testing/execution/searcher/CppSearcher.java new file mode 100644 index 00000000..14cb144b --- /dev/null +++ b/src/main/java/org/hyperskill/hstest/testing/execution/searcher/CppSearcher.java @@ -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*\\(.*\\)" + ); + } +} From 474958a743765530bbca3a6b8f550c5e0c9478d9 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 16 Nov 2023 09:22:50 +0200 Subject: [PATCH 09/11] Update CppExecutor.java fixed compilation Command --- .../hstest/testing/execution/process/CppExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java index af7c245b..d67b12e6 100644 --- a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java +++ b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java @@ -42,7 +42,7 @@ public CppExecutor(String sourceName) { @Override protected List compilationCommand() { - return List.of("g++", "-std", "c++20", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName()); + return List.of("g++", "-std=c++20", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName()); } @Override From ae8aeed4349f0a3cf21a9481611c5891f4e8b922 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 16 Nov 2023 15:17:39 +0200 Subject: [PATCH 10/11] Update CppExecutor.java fixed compilation command --- .../hstest/testing/execution/process/CppExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java index d67b12e6..e3751d1e 100644 --- a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java +++ b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java @@ -42,7 +42,7 @@ public CppExecutor(String sourceName) { @Override protected List compilationCommand() { - return List.of("g++", "-std=c++20", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName()); + return List.of("g++", "-o", filename, runnable.getFile().getName()); } @Override From 373212fae10b50d3bf936bc3e8698ab8cc863a48 Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 16 Nov 2023 16:51:36 +0200 Subject: [PATCH 11/11] fixed compilation Command --- .../hstest/testing/execution/process/CppExecutor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java index e3751d1e..ab4d87eb 100644 --- a/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java +++ b/src/main/java/org/hyperskill/hstest/testing/execution/process/CppExecutor.java @@ -42,7 +42,7 @@ public CppExecutor(String sourceName) { @Override protected List compilationCommand() { - return List.of("g++", "-o", filename, runnable.getFile().getName()); + return List.of("g++", "-pipe", "-O2", "-static", "-o", filename, runnable.getFile().getName()); } @Override