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

Added CheckLibraryVersion class for check library version #142

Merged
merged 7 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
131 changes: 131 additions & 0 deletions src/main/java/org/hyperskill/hstest/checker/CheckLibraryVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.hyperskill.hstest.checker;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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;

/**
* 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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check what this file doesn't put into submission.

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);
Copy link

@aaaaaa2493 aaaaaa2493 May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this method contains a lot of RuntimeException's? That's not a good way of handling checked exceptions making them unchecked exceptions. At the very least they should be UnexpectedError's. Or just make the method as thorows IOException

}
}

if (lastChecked != null && lastChecked.equals(LocalDate.now())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (lastChecked != null && lastChecked.equals(LocalDate.now())) {
if (LocalDate.now().equals(lastChecked)) {

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);
}
getLatestHsTestVersionFromGitHub();

if (!currentVersion.equals(latestVersion)) {
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 void getLatestHsTestVersionFromGitHub() {
HttpURLConnection connection = null;
try {
URL url = new URL("https://api.github.com/repos/hyperskill/hs-test/releases/latest");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check timeout

connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/vnd.github+json");

int responseCode = connection.getResponseCode();
if (responseCode != 200) {
latestVersion = currentVersion;
return;
}
} catch (IOException e) {
latestVersion = currentVersion;
return;
}

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", "");
} catch (IOException e) {
latestVersion = currentVersion;
}
}

/**
* 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;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/hyperskill/hstest/resources/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.0.3
8 changes: 7 additions & 1 deletion src/main/java/org/hyperskill/hstest/stage/StageTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -148,6 +149,9 @@ public final void start() {
ReflectionUtils.setupCwd(this);
}

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

List<TestRun> testRuns = initTests();

for (TestRun testRun : testRuns) {
Expand All @@ -162,10 +166,12 @@ public final void start() {

currTestRun = testRun;
CheckResult result = testRun.test();

if (!result.isCorrect()) {
String fullFeedback = result.getFeedback() + "\n\n"
+ testRun.getTestCase().getFeedback();
if (!checkLibraryVersion.getLatestVersion()) {
fullFeedback += checkLibraryVersion.getFeedback();
}
throw new WrongAnswer(fullFeedback.trim());
}

Expand Down