Skip to content

Commit 8f752a0

Browse files
author
Anthony Hillairet
authored
🔧 Add code validation standard config (#302)
* Add standard configs to docker image * Make code validation use std conf or local * Test if code validation confs are working * Add documentation
1 parent 334c376 commit 8f752a0

File tree

11 files changed

+68
-10
lines changed

11 files changed

+68
-10
lines changed

‎.coverage.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = */tests/*

‎.flake8

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[flake8]
2+
max-line-length = 99
3+
# E203 is incompatible with pep8 and makes black mad
4+
extend-ignore = E203
5+
# Enforce using single quotes with flake8-quotes
6+
inline-quotes = single

‎.isort.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[settings]
2+
profile = black
3+
multi_line_output = 3
4+
line_length = 99

‎.mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
ignore_missing_imports = True
3+
show_error_codes = True
4+
plugins = pydantic.mypy

‎Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ RUN echo "#!/bin/bash\n/python/entrypoint.sh startapp" >> /bin/startapp && chmod
2424
echo "#!/bin/bash\n/python/entrypoint.sh runtests" >> /bin/runtests && chmod a+x /bin/runtests &&\
2525
pip install -r /python/requirements.txt
2626

27+
# Copy standard configurations for code validation
28+
COPY pytest.ini .flake8 .isort.cfg .mypy.ini .coverage.conf /home/python/
29+
2730
# Change users
2831
USER python
2932

‎README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ such as the `entrypoint.sh`. Then the subfolders organize the app files:
4444
* `/python/static` holds the static files served by the app such as static html files
4545
* `/python/logs` is meant for the log files generated by the app
4646
* `/python/files` is for files uploaded through the app
47+
* `/home/python` contains the [standard configuration files](Standard-configurations-for-code-quality)
48+
for linting, formatting and other testing tools
4749

4850
## :toolbox: Notes for maintenance
4951

@@ -127,6 +129,9 @@ plugin.
127129

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

131136
### `validatecodeonce`
132137

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

146151
A [coverage configuration file](https://pytest-cov.readthedocs.io/en/latest/config.html)
147152
can be provided at `python/app/coverage.conf`.
153+
154+
## Standard configurations for code quality
155+
156+
Standard configuration files for all the code quality tools are located in `/home/python`
157+
where the tools will find them.
158+
These configurations can be overwritten:
159+
160+
* `mypy`: Add the configuration file `/python/app/.mypy.ini` and the standard file in
161+
`/home/python` will be ignored.
162+
* `flake8`: Add the configuration file `/python/app/.flake8` it will be *appended* to
163+
the standard file in `/home/python`. Therefore to turn off all the configurations
164+
in the standard file you must explicitely reverse them in the `/python/app/.flake8` file.
165+
* `isort`: Add the configuration `/python/app/.isort.cfg` and the standard file in
166+
`/home/python` will be ignored.

‎pytest.ini

Whitespace-only changes.

‎test_suite.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,28 @@ reportvalidation() {
2727
if [[ $1 == "reports" ]]
2828
then
2929
MYPY_REPORTS="--junit-xml ${REPORTS_FOLDER}typing.xml"
30-
if [ -f ./coverage.conf ];
31-
then
32-
$covconf="--cov-config ./coverage.conf"
33-
fi
34-
PYTEST_REPORTS="--junitxml ${REPORTS_FOLDER}unittesting.xml $covconf --cov-report xml:${REPORTS_FOLDER}coverage.xml"
30+
PYTEST_REPORTS="--junitxml ${REPORTS_FOLDER}unittesting.xml --cov-report xml:${REPORTS_FOLDER}coverage.xml"
31+
fi
32+
33+
COVERAGE_CONF="--cov-config /home/python/.coverage.conf"
34+
if [ -f ./coverage.conf ];
35+
then
36+
COVERAGE_CONF="--cov-config ./coverage.conf"
3537
fi
3638

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

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

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

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

5254
echo -ne "$SECTION_PREFIX CHECK FORMATTING: "

‎tests/.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
# FOR TEST PURPOSES ONLY
3+
extend-ignore = F401

‎tests/webapp/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# FLAKE8: This would trigger an F401 error without the local .flake8
2+
from math import tan
3+
4+
# MYPY: This import triggers a missing import error without the standard config
5+
# file
16
from fastapi import FastAPI
27

38
app = FastAPI()
@@ -6,3 +11,10 @@
611
@app.get('/')
712
async def root():
813
return {'message': 'Hello World'}
14+
15+
16+
def dummy_func():
17+
# FLAKE8: The following two lines would trigger an E501 flake8 error
18+
# without the standard config in /home/python
19+
this_is_a_very_long_variable_name_used_to_test_the_flake8_standard_config = 'ok'
20+
return this_is_a_very_long_variable_name_used_to_test_the_flake8_standard_config

0 commit comments

Comments
 (0)