Skip to content

🔧 Add code validation standard config #302

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

Merged
merged 12 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .coverage.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = */tests/*
6 changes: 6 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[flake8]
max-line-length = 99
# E203 is incompatible with pep8 and makes black mad
extend-ignore = E203
# Enforce using single quotes with flake8-quotes
inline-quotes = single
4 changes: 4 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[settings]
profile = black
multi_line_output = 3
line_length = 99
4 changes: 4 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[mypy]
ignore_missing_imports = True
show_error_codes = True
plugins = pydantic.mypy
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ RUN echo "#!/bin/bash\n/python/entrypoint.sh startapp" >> /bin/startapp && chmod
echo "#!/bin/bash\n/python/entrypoint.sh runtests" >> /bin/runtests && chmod a+x /bin/runtests &&\
pip install -r /python/requirements.txt

# Copy standard configurations for code validation
COPY pytest.ini .flake8 .isort.cfg .mypy.ini .coverage.conf /home/python/

# Change users
USER python

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ such as the `entrypoint.sh`. Then the subfolders organize the app files:
* `/python/static` holds the static files served by the app such as static html files
* `/python/logs` is meant for the log files generated by the app
* `/python/files` is for files uploaded through the app
* `/home/python` contains the [standard configuration files](Standard-configurations-for-code-quality)
for linting, formatting and other testing tools

## :toolbox: Notes for maintenance

Expand Down Expand Up @@ -127,6 +129,9 @@ plugin.

This code validation command executes the `/python/test_suite.sh` script which can
be overwritten with custom code validation.
These code quality tools will follow the
[standard configurations](Standard-configurations-for-code-quality) present in `/home/python`
by default.

### `validatecodeonce`

Expand All @@ -145,3 +150,17 @@ and code coverage results in the `/python/app/unittesting.xml` and

A [coverage configuration file](https://pytest-cov.readthedocs.io/en/latest/config.html)
can be provided at `python/app/coverage.conf`.

## Standard configurations for code quality

Standard configuration files for all the code quality tools are located in `/home/python`
where the tools will find them.
These configurations can be overwritten:

* `mypy`: Add the configuration file `/python/app/.mypy.ini` and the standard file in
`/home/python` will be ignored.
* `flake8`: Add the configuration file `/python/app/.flake8` it will be *appended* to
the standard file in `/home/python`. Therefore to turn off all the configurations
in the standard file you must explicitely reverse them in the `/python/app/.flake8` file.
* `isort`: Add the configuration `/python/app/.isort.cfg` and the standard file in
`/home/python` will be ignored.
Empty file added pytest.ini
Empty file.
18 changes: 10 additions & 8 deletions test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ reportvalidation() {
if [[ $1 == "reports" ]]
then
MYPY_REPORTS="--junit-xml ${REPORTS_FOLDER}typing.xml"
if [ -f ./coverage.conf ];
then
$covconf="--cov-config ./coverage.conf"
fi
PYTEST_REPORTS="--junitxml ${REPORTS_FOLDER}unittesting.xml $covconf --cov-report xml:${REPORTS_FOLDER}coverage.xml"
PYTEST_REPORTS="--junitxml ${REPORTS_FOLDER}unittesting.xml --cov-report xml:${REPORTS_FOLDER}coverage.xml"
fi

COVERAGE_CONF="--cov-config /home/python/.coverage.conf"
if [ -f ./coverage.conf ];
then
COVERAGE_CONF="--cov-config ./coverage.conf"
fi

echo -ne "$SECTION_PREFIX RUN TESTS:\n\n"
python -m pytest -vv --durations=3 --cov ./ --cov-report term-missing $PYTEST_REPORTS; STATUS1=$?
python -m pytest --rootdir=/python/app -vv --durations=3 --cov ./ --cov-report term-missing $COVERAGE_CONF $PYTEST_REPORTS; STATUS1=$?

echo -ne "$SECTION_PREFIX CHECK DOCKER USER IS PYTHON: "
USEROUT=`checkuser`
reportvalidation "$USEROUT"; STATUS2=$?

echo -ne "$SECTION_PREFIX CHECK TYPING: "
MYPYOUT=`mypy --no-error-summary . $MYPY_REPORTS`
MYPYOUT=`mypy --cache-dir /home/python --no-error-summary . $MYPY_REPORTS`
reportvalidation "$MYPYOUT"; STATUS3=$?

echo -ne "$SECTION_PREFIX CHECK LINTING: "
FLAKE8OUT=`flake8`
FLAKE8OUT=`flake8 --append-config /home/python/.flake8 --append-config /python/app/.flake8`
reportvalidation "$FLAKE8OUT"; STATUS4=$?

echo -ne "$SECTION_PREFIX CHECK FORMATTING: "
Expand Down
3 changes: 3 additions & 0 deletions tests/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
# FOR TEST PURPOSES ONLY
extend-ignore = F401
12 changes: 12 additions & 0 deletions tests/webapp/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# FLAKE8: This would trigger an F401 error without the local .flake8
from math import tan

# MYPY: This import triggers a missing import error without the standard config
# file
from fastapi import FastAPI

app = FastAPI()
Expand All @@ -6,3 +11,10 @@
@app.get('/')
async def root():
return {'message': 'Hello World'}


def dummy_func():
# FLAKE8: The following two lines would trigger an E501 flake8 error
# without the standard config in /home/python
this_is_a_very_long_variable_name_used_to_test_the_flake8_standard_config = 'ok'
return this_is_a_very_long_variable_name_used_to_test_the_flake8_standard_config
7 changes: 5 additions & 2 deletions tests/webapp/tests/simple_test.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
def test_something():
assert True
from webapp.main import dummy_func


def test_dummy_func():
assert dummy_func() == 'ok'