-
Notifications
You must be signed in to change notification settings - Fork 27
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
refactor: do not use built-in random and add proper linter check #698
Merged
Merged
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
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,102 @@ | ||
#!/bin/bash | ||
|
||
# Define colors | ||
RED='\033[0;31m' | ||
GREEN='\033[0;32m' | ||
NC='\033[0m' # No Color | ||
|
||
# Source dirs | ||
SOURCE_DIRS=(hathor tests) | ||
|
||
# Define your custom linter check functions here | ||
# Each function should return 0 if everything is OK, and 1 if something is wrong. | ||
|
||
function check_version_match() { | ||
# This function will check all source files containing the project version and return 1 in case | ||
# they don't match. When a version is provided as an environment variable, it is checked against the package version. | ||
|
||
OPENAPI_FILE="hathor/cli/openapi_files/openapi_base.json" | ||
SRC_FILE="hathor/version.py" | ||
PACKAGE_FILE="pyproject.toml" | ||
|
||
OPENAPI_VERSION=`grep "version\":" ${OPENAPI_FILE} | cut -d'"' -f4` | ||
SRC_VERSION=`grep "BASE_VERSION =" ${SRC_FILE} | cut -d "'" -f2` | ||
PACKAGE_VERSION=`grep '^version' ${PACKAGE_FILE} | cut -d '"' -f2` | ||
|
||
# For debugging: | ||
# echo x${SRC_VERSION}x | ||
# echo x${OPENAPI_VERSION}x | ||
# echo x${PACKAGE_VERSION}x | ||
|
||
EXITCODE=0 | ||
|
||
if [[ x${PACKAGE_VERSION}x != x${SRC_VERSION}x ]]; then | ||
echo "Version different in ${PACKAGE_FILE} and ${SRC_FILE}" | ||
EXITCODE=1 | ||
fi | ||
|
||
if [[ x${PACKAGE_VERSION}x != x${OPENAPI_VERSION}x ]]; then | ||
echo "Version different in ${PACKAGE_FILE} and ${OPENAPI_FILE}" | ||
EXITCODE=1 | ||
fi | ||
|
||
# We expect an optional environment variable containing a version string to be checked against the others | ||
if [[ -n ${VERSION} ]]; then | ||
if [[ x${PACKAGE_VERSION}x != x${VERSION}x ]]; then | ||
echo "Version different in ${PACKAGE_FILE} and VERSION environment variable" | ||
EXITCODE=1 | ||
fi | ||
fi | ||
|
||
return $EXITCODE | ||
} | ||
|
||
function check_do_not_use_builtin_random_in_tests() { | ||
# If the check fails, return 1 | ||
# If the check passes, return 0 | ||
exclude=( | ||
hathor/merged_mining/debug_api.py | ||
hathor/client.py | ||
hathor/cli/tx_generator.py | ||
) | ||
exclude_params=() | ||
for item in "${exclude[@]}"; do | ||
exclude_params+=(-not -path "*$item*") | ||
done | ||
if find "${SOURCE_DIRS[@]}" "${exclude_params[@]}" -type f -print0 | xargs -0 grep -l '\<import .*\<random\>'; then | ||
echo '"import random" found in the files above' | ||
echo 'use `self.rng` or `hathor.util.Random` instead of `random`' | ||
return 1 | ||
fi | ||
return 0 | ||
} | ||
|
||
# List of functions to be executed | ||
checks=( | ||
check_version_match | ||
check_do_not_use_builtin_random_in_tests | ||
) | ||
|
||
# Initialize a variable to track if any check fails | ||
any_check_failed=0 | ||
|
||
# Loop over all checks | ||
for check in "${checks[@]}"; do | ||
$check | ||
result=$? | ||
if [ $result -ne 0 ]; then | ||
echo -e "${RED}Check $check FAILED${NC}" | ||
any_check_failed=1 | ||
else | ||
echo -e "${GREEN}Check $check PASSED${NC}" | ||
fi | ||
done | ||
|
||
# Exit with code 0 if no check failed, otherwise exit with code 1 | ||
if [ $any_check_failed -eq 0 ]; then | ||
echo -e "${GREEN}All checks PASSED${NC}" | ||
exit 0 | ||
else | ||
echo -e "${RED}Some checks FAILED${NC}" | ||
exit 1 | ||
fi |
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
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
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.
I think we will need to adapt the
.github/workflows/docker.yml
, because we used this arg there.Since you ported it to be an env var, we should set it as an env var there instead. Besides migrating to the new
check-custom
there as well.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.
Oh, right, I'll fix it.
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.
Done. I've fixed it and tested with
act
with a wrong and correct tag and both work as expected.