Skip to content

Commit

Permalink
Added the checkerLibraryVersion class for compares the local library …
Browse files Browse the repository at this point in the history
…version with the remote version on GitHub
  • Loading branch information
polischuks committed Apr 16, 2023
1 parent 21c942c commit 5520e73
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
49 changes: 49 additions & 0 deletions hstest/stage/checkerLibraryVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import packageJson from "../../package.json";

/**
* CheckerLibraryVersion is a class responsible for comparing the local library
* version with the remote version available on GitHub.
*
* It helps to ensure that the user is using the latest version of the library.
*/
class CheckerLibraryVersion {

constructor() {}

/**
* Compares the local library version with the remote version on GitHub.
*
* If the versions are different, an error message will be logged to the console,
* or an Error will be thrown if the throwError flag is set to true.
*
* @param throwError {boolean} - Optional flag to indicate if an Error should be
* thrown when versions are different. Default is false.
* @returns {Promise<void>} - A promise that resolves when the check is complete.
*/
async checkLibraryVersion(throwError = false): Promise<void> {

const libraryVersionUrl = "https://github.com/hyperskill/hs-test-web/blob/master/package.json";
try {
const response = await fetch(libraryVersionUrl);
const packageRemoteJson = await response.json();
const remoteVersion = packageRemoteJson.version;
const localVersion = packageJson.version;
if (remoteVersion !== localVersion) {
const errorMsg = `The version of the local library (${localVersion}) is different from the version on GitHub (${remoteVersion}).
Please update your local version.`;
if (throwError) {
throw new Error(errorMsg);
} else {
console.error(errorMsg);
}
}
} catch (error) {
console.error("Error while checking library version:\n", error);
if (throwError) {
throw error;
}
}
}
}

export default CheckerLibraryVersion;
10 changes: 10 additions & 0 deletions hstest/stage/stageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Outcome from "../outcome/outcome.js";
import OutcomeFactory from "../outcome/outcomeFactory.js";
import WrongAnswer from "../exception/outcome/WrongAnswer.js";
import UnexpectedErrorOutcome from "../outcome/unexpectedErrorOutcome.js";
import CheckerLibraryVersion from "./checkerLibraryVersion";
import puppeteer from "puppeteer";

class StageTest {
Expand All @@ -17,6 +18,15 @@ class StageTest {
runner: TestRunner = new JsRunner();
tests: NoArgsFunction[] = [];

checkerLibraryVersion: CheckerLibraryVersion = new CheckerLibraryVersion();

constructor() {
// Perform the library version check upon class instantiation
this.checkerLibraryVersion.checkLibraryVersion().catch((error) => {
console.error("Error while checking library version:", error);
});
}

getPage(url: string, options: puppeteer.WaitForOptions = {}): Page {
return new Page(url, this.runner.browser, options);
}
Expand Down

0 comments on commit 5520e73

Please sign in to comment.