Initial implementation of validation script

This commit is contained in:
VakarisZ 2020-11-05 14:57:50 +02:00
parent 53f3625172
commit 2e35b9ac43
9 changed files with 73 additions and 7 deletions

View File

@ -60,20 +60,17 @@ before_script:
script: script:
# Check Python code # Check Python code
## Check syntax errors and fail the build if any are found. - flake8 ./monkey --config ./ci_scripts/flake8_syntax_check.config
- flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics
## Warn about linter issues. ## Warn about linter issues.
### --exit-zero forces Flake8 to use the exit status code 0 even if there are errors, which means this will NOT fail the build. ### --exit-zero forces Flake8 to use the exit status code 0 even if there are errors, which means this will NOT fail the build.
### --count will print the total number of errors.
### --statistics Count the number of occurrences of each error/warning code and print a report.
### The output is redirected to a file. ### The output is redirected to a file.
- flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics > flake8_warnings.txt - flake8 ./monkey --exit-zero --config ./ci_scripts/flake8_linter_check.config > ./ci_scripts/flake8_warnings.txt
## Display the linter issues ## Display the linter issues
- cat flake8_warnings.txt - cat ./ci_scripts/flake8_warnings.txt
## Make sure that we haven't increased the amount of warnings. ## Make sure that we haven't increased the amount of warnings.
- PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT=80 - PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT=80
- if [ $(tail -n 1 flake8_warnings.txt) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT ]; then echo "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " && exit 1; fi - if [ $(tail -n 1 ./ci_scripts/flake8_warnings.txt) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT ]; then echo "Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. " && exit 1; fi
## Check import order ## Check import order
- python -m isort . -c -p common -p infection_monkey -p monkey_island - python -m isort . -c -p common -p infection_monkey -p monkey_island

2
ci_scripts/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
./validation-env
./flake8_warnings.txt

7
ci_scripts/README.md Normal file
View File

@ -0,0 +1,7 @@
# About
Run this script to validate your code locally and auto fix/format the problems before pushing.
# Usage
run from `infection_monkey` directory: `powershell .\ci_scripts\validate.ps1`

View File

@ -0,0 +1,15 @@
[flake8]
## Warn about linter issues.
exclude = ../monkey/monkey_island/cc/ui,
../monkey/common/cloud
show-source = True
max-complexity = 10
max-line-length = 127
### --statistics Count the number of occurrences of each error/warning code and print a report.
statistics = True
### --count will print the total number of errors.
count = True

View File

@ -0,0 +1,9 @@
[flake8]
## Check syntax errors and fail the build if any are found.
exclude = ../monkey/monkey_island/cc/ui,
../monkey/common/cloud
select = E901,E999,F821,F822,F823
count = True
show-source = True
statistics = True

View File

@ -0,0 +1,5 @@
python -m venv validation-env
.\validation-env\Scripts\activate.ps1
python -m pip install -r .\requirements.txt
npm i -g eslint
deactivate

5
ci_scripts/isort.cfg Normal file
View File

@ -0,0 +1,5 @@
[settings]
project=common,infection_monkey,monkey_island
skip=./monkey/common/cloud/scoutsuite,./monkey/monkey_island/cc/services/zero_trust/scoutsuite/data_parsing/rule_path_building/rule_path_creators_list.py,./monkey/monkey_island/cc/ui
line-length=120
wrap-length=120

View File

@ -0,0 +1,6 @@
flake8
pytest
dlint
isort
coverage
black

20
ci_scripts/validate.ps1 Normal file
View File

@ -0,0 +1,20 @@
.\ci_scripts\validation-env\Scripts\activate.ps1
flake8 ./monkey --config ./ci_scripts/flake8_syntax_check.cfg
flake8 ./monkey --exit-zero --config ./ci_scripts/flake8_linter_check.cfg | Out-File -FilePath .\ci_scripts\flake8_warnings.txt
Get-Content -Path .\ci_scripts\flake8_warnings.txt
$PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT = 80
if ((Get-Item -Path .\ci_scripts\flake8_warnings.txt | Get-Content -Tail 1) -gt $PYTHON_WARNINGS_AMOUNT_UPPER_LIMIT){
"Too many python linter warnings! Failing this build. Lower the amount of linter errors in this and try again. "
exit
}
python -m isort ./monkey -c --settings-file ./ci_scripts/isort.cfg
# python -m isort ./monkey --settings-file ./ci_scripts/isort.cfg
python monkey_island/cc/environment/set_server_config.py testing
python -m pytest
Push-Location -Path .\monkey_island\cc\ui
eslint ./src -c ./.eslintrc
Pop-Location
Pop-Location
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
deactivate