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

fixed compilation command #146

Merged
merged 14 commits into from
Nov 16, 2023
Prev Previous commit
Next Next commit
improvement for CheckLibraryVersion
polischuks committed Jun 2, 2023
commit d4ce11fd9ba761b8ef654186a0ad8f986ba7f161
Original file line number Diff line number Diff line change
@@ -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<Map<String, Object>>(){}.getType();
Map<String,Object> 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;
}
}
7 changes: 3 additions & 4 deletions src/main/java/org/hyperskill/hstest/stage/StageTest.java
Original file line number Diff line number Diff line change
@@ -149,9 +149,6 @@ public final void start() {
ReflectionUtils.setupCwd(this);
}

CheckLibraryVersion checkLibraryVersion = new CheckLibraryVersion();
checkLibraryVersion.checkVersion();

List<TestRun> 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());