-
Notifications
You must be signed in to change notification settings - Fork 41
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
Feature - Version check evaluator #205
Open
AkashS0510
wants to merge
4
commits into
main
Choose a base branch
from
version-check-evaluator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from .base_evaluator import BaseEvaluator | ||
from packaging.version import Version | ||
import re | ||
|
||
|
||
# Compares two version strings based on the provided operation. | ||
|
||
# Args: | ||
# evaluator_input (str): The first version string to compare. | ||
# evaluator_data (str): The second version string to compare. | ||
# operation (str): The comparison operation to perform. | ||
# Supported values: "LessThan", "LessThanOrEquals", "Equals", "GreaterThan", "GreaterThanOrEquals". | ||
|
||
# Returns: | ||
# dict: A dictionary containing the comparison result (`passed`) and a descriptive message. | ||
|
||
# Example: | ||
# | ||
# >>> comparator = VersionCompare() | ||
# >>> comparator.evaluate("1.0.0", {"value": "1.0.1", "operation": "lessThan"}) | ||
# {'passed': True, 'message': '1.0.0 is less than 1.0.1'} | ||
# >>> comparator.evaluate("1.0.0", {"value": "1.0.0", "operation": "equal"}) | ||
# {'passed': True, 'message': '1.0.0 is equal to 1.0.0'} | ||
# >>> comparator.evaluate("2.0.0", {"value": "1.0.0", "operation": "greaterThan"}) | ||
# {'passed': True, 'message': '2.0.0 is greater than 1.0.0'} | ||
|
||
|
||
# .. versionadded:: 1.0.0 | ||
|
||
|
||
class VersionCompare(BaseEvaluator): | ||
"""Compares two version strings based on the provided operation.""" | ||
|
||
def parse_version(self, version_string): | ||
"""Parses a version string and returns a comparable version object.""" | ||
# Extract the version part from the string | ||
match = re.search(r"[\d]+(\.[\d]+)*(\-[a-zA-Z0-9]+)?$", version_string) | ||
if match: | ||
try: | ||
return Version(match.group(0)) | ||
except Exception as e: | ||
raise ValueError(f"Invalid version format: {version_string}. Error: {str(e)}") | ||
else: | ||
raise ValueError(f"Invalid version format: {version_string}") | ||
|
||
def evaluate(self, evaluator_input, evaluator_data): | ||
"""Compares two version strings based on the provided operation.""" | ||
evaluation_result = {"passed": False, "message": "Not evaluated"} | ||
try: | ||
v1 = self.parse_version(evaluator_input) | ||
v2 = self.parse_version(evaluator_data["value"]) | ||
operation = evaluator_data["operation"] | ||
|
||
if operation == "LessThan": | ||
evaluation_result["passed"] = v1 < v2 | ||
elif operation == "LessThanOrEquals": | ||
evaluation_result["passed"] = v1 <= v2 | ||
elif operation == "Equals": | ||
evaluation_result["passed"] = v1 == v2 | ||
elif operation == "GreaterThan": | ||
evaluation_result["passed"] = v1 > v2 | ||
elif operation == "GreaterThanOrEquals": | ||
evaluation_result["passed"] = v1 >= v2 | ||
|
||
else: | ||
raise ValueError(f"Unsupported operation: {operation}") | ||
|
||
if evaluation_result["passed"]: | ||
evaluation_result["message"] = f"{evaluator_input} is {operation.replace('_', ' ')} {evaluator_data}" | ||
else: | ||
evaluation_result["message"] = ( | ||
f"{evaluator_input} is not {operation.replace('_', ' ')} {evaluator_data}" | ||
) | ||
|
||
return evaluation_result | ||
|
||
except ValueError as e: | ||
evaluation_result["message"] = str(e) | ||
return evaluation_result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from tirith.core.evaluators import VersionCompare | ||
from pytest import mark | ||
|
||
# Define test cases | ||
checks_passing = [ | ||
("1.0.0", {"value": "1.0.1", "operation": "LessThan"}), | ||
("1.0.0", {"value": "1.0.0", "operation": "Equals"}), | ||
("2.0.0", {"value": "1.0.0", "operation": "GreaterThan"}), | ||
("1.0.0", {"value": "1.1.0", "operation": "LessThanOrEquals"}), | ||
("1.1.0", {"value": "1.1.0", "operation": "LessThanOrEquals"}), | ||
("1.2.0", {"value": "1.1.0", "operation": "GreaterThanOrEquals"}), | ||
("1.1.0", {"value": "1.1.0", "operation": "GreaterThanOrEquals"}), | ||
] | ||
|
||
checks_failing = [ | ||
("1.0.0", {"value": "1.0.1", "operation": "GreaterThan"}), | ||
("1.0.0", {"value": "1.0.0", "operation": "LessThan"}), | ||
("1.0.1", {"value": "1.0.0", "operation": "LessThan"}), | ||
("1.1.0", {"value": "1.0.0", "operation": "Equals"}), | ||
] | ||
|
||
|
||
evaluator = VersionCompare() | ||
|
||
|
||
# pytest -v -m passing | ||
@mark.passing | ||
@mark.parametrize("evaluator_input,evaluator_data", checks_passing) | ||
def test_evaluate_passing(evaluator_input, evaluator_data): | ||
result = evaluator.evaluate(evaluator_input, evaluator_data) | ||
operation = evaluator_data["operation"] | ||
assert result == {"passed": True, "message": f"{evaluator_input} is {operation} {evaluator_data}"} | ||
|
||
|
||
# pytest -v -m failing | ||
@mark.failing | ||
@mark.parametrize("evaluator_input,evaluator_data", checks_failing) | ||
def test_evaluate_failing(evaluator_input, evaluator_data): | ||
result = evaluator.evaluate(evaluator_input, evaluator_data) | ||
operation = evaluator_data["operation"] | ||
assert result == {"passed": False, "message": f"{evaluator_input} is not {operation} {evaluator_data}"} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should make core as agnostic as possible towards the available condition types so that it's loosely coupled
Will discus this tomorrow