1+ #! /bin/bash
2+
3+ declare -a RESULT
4+
5+ # specifies a set of variables to declare CLI output color
6+ FAILED_OUT=" \033[0;31m"
7+ PASSED_OUT=" \033[0;32m"
8+ NONE_OUT=" \033[0m"
9+
10+ # specifies a set of variables to declare files to be used for code assessment
11+ PROJECT_FILES=" ./"
12+ TESTS_FILES=" tests"
13+
14+
15+ function store-failures {
16+ RESULT+=(" $1 " )
17+ }
18+
19+
20+ function remove-pycache-trash {
21+ local PYCACHE_DIR=" __pycache__"
22+ echo " Removing ${PYCACHE_DIR} directories if present ..."
23+ ( find . -d -name ${PYCACHE_DIR} | xargs rm -r ) || echo -e " No ${PYCACHE_DIR} found"
24+ }
25+
26+
27+ function remove-analysis-trash {
28+ local PYTEST_CACHE_DIR=' .pytest_cache'
29+ local MYPY_CACHE_DIR=' .mypy_cache'
30+ echo " Removing code analysis trash if present ..."
31+ [[ -d " $PYTEST_CACHE_DIR " ]] && rm -rf ${PYTEST_CACHE_DIR} && echo " pytest trash is removed"
32+ [[ -d " $MYPY_CACHE_DIR " ]] && rm -rf ${MYPY_CACHE_DIR} && echo " mypy trash is removed"
33+ }
34+
35+
36+ function install-dependencies {
37+ echo " Installing python code analysis packages ..." \
38+ && ( pip install --no-cache-dir --upgrade pip ) \
39+ && ( pip install --no-cache-dir -r requirements.txt )
40+ }
41+
42+
43+ function run-unittests {
44+ echo " Running unittests ..." && ( pytest -m unittest )
45+ }
46+
47+
48+ function run-black-analysis() {
49+ echo " Running black analysis ..." && ( black --check " ${PROJECT_FILES} " )
50+ }
51+
52+
53+ function run-code-analysis {
54+ echo " Running code analysis ..."
55+ remove-pycache-trash
56+ run-unittests || store-failures " Unittests are failed!"
57+ run-black-analysis || store-failures " black analysis is failed!"
58+
59+ if [[ ${# RESULT[@]} -ne 0 ]];
60+ then echo -e " ${FAILED_OUT} Some errors occurred while analysing the code quality.${NONE_OUT} "
61+ for failed_item in " ${RESULT[@]} " ; do
62+ echo -e " ${FAILED_OUT} - ${failed_item}${NONE_OUT} "
63+ done
64+ remove-analysis-trash
65+ exit 1
66+ fi
67+ remove-analysis-trash
68+ echo -e " ${PASSED_OUT} Code analysis is passed${NONE_OUT} "
69+ }
70+
71+
72+ function main() {
73+ if [[ " $1 " == " install-dependencies" ]];
74+ then install-dependencies || store-failures " Python packages installation is failed!" ;
75+ fi
76+ run-code-analysis
77+ }
78+
79+
80+ main " $@ "
0 commit comments