--- a/pre-commit-src/pre-commit
+++ b/pre-commit-src/pre-commit
@@ -443,48 +443,59 @@ main() {
local domains_variables_in_included_json_files
extract_domains_variables_in_included_json_files "$template_name" domains_variables_in_included_json_files
check_domain_variables_in_filterlists "$template_name" "$(echo ${domains_variables_in_included_filterlists[@]})" "$(echo ${domains_variables_in_included_json_files[@]})"
done
check_if_domains_variables_are_identical_in_lists_and_jsons
}
-# For testing purposes only if the script has no arguments or the argument is main the process should run
-# thanks to that the script can be tested without running the main function
-if [ -z "${1:-}" ] || [ "$1" = "main" ]; then
- main
+check_unit_tests() {
+ local exit_status=0
+
+
+ ./pre-commit-src/tests/pre-commit-tests.sh || exit_status=1
+ function_exit_code=$?
+ if [ $exit_status -ne 1 ]; then
+ exit_status=$function_exit_code
+ fi
- # Check if this file has unstaged changes:
+ if [ $exit_status -ne 0 ]; then
+ last_error="Unit tests failed with exit code $exit_status"
+ return $exit_status
+ else
+ last_error="Unit tests passed successfully"
+ fi
+}
+
+check_pre_commit_files() {
pre_commit_git_status=$(git status :pre-commit-src/pre-commit)
if grep -q "Changes not staged for commit" <<< $pre_commit_git_status || grep -q "Untracked files" <<< $pre_commit_git_status; then
- echo "Unstaged changes detected in pre-commit file. Stage pre-commit changes before continuing."
- exit 1
+ last_error="Unstaged changes detected in pre-commit file. Stage pre-commit changes before continuing."
+ return 1
fi
pre_commit_tests_git_status=$(git status :pre-commit-src/tests/pre-commit-tests.sh)
if grep -q "Changes not staged for commit" <<< $pre_commit_tests_git_status || grep -q "Untracked files" <<< $pre_commit_tests_git_status; then
- echo "Unstaged changes detected in pre-commit-tests file. Stage pre-commit changes before continuing."
- exit 1
+ last_error="Unstaged changes detected in pre-commit-tests file. Stage pre-commit changes before continuing."
+ return 1
fi
+ if grep -q "Changes to be committed" <<< $pre_commit_tests_git_status || grep -q "Changes to be committed" <<< $pre_commit_git_status; then
+ check_unit_tests
+ fi
+}
- # Run unit tests. The workaround with exit_code is meant to avoid failed test to trigger error_handling
- exit_code=0
- ./pre-commit-src/tests/pre-commit-tests.sh || exit_code=1
- function_exit_code=$?
- if [ $exit_code -ne 1 ]; then
- exit_code=$function_exit_code
- fi
- if [ $exit_code -ne 0 ]; then
- echo "Tests failed with exit code $exit_code"
- else
- echo "Tests passed successfully"
- fi
- exit $exit_code
+# For testing purposes only if the script has no arguments or the argument is main the process should run
+# thanks to that the script can be tested without running the main function
+if [ -z "${1:-}" ] || [ "$1" = "main" ]; then
+ main
+ check_pre_commit_files
+
+ exit 0
elif [ "$1" = "--load-only" ]; then
testing=true
echo "Script loaded successfully"
else
"$@"
fi