Skip to content

Commit

Permalink
Merge pull request quipucords#2133 from bruno-fs/improve-linting
Browse files Browse the repository at this point in the history
Improve linting
  • Loading branch information
bruno-fs committed Feb 24, 2022
2 parents e901edf + 92c877e commit bea0cd0
Show file tree
Hide file tree
Showing 8 changed files with 705 additions and 44 deletions.
617 changes: 617 additions & 0 deletions .flakeheaven-baseline

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
steps:
- name: Check out repository code
uses: actions/checkout@v2
with:
fetch-depth: 0 # fetches all commits/tags

- name: Set up Python 3.9
uses: actions/setup-python@v2
Expand All @@ -36,7 +38,7 @@ jobs:
run: pip install -r dev-requirements.txt

- name: Run linters
run: make lint
run: make lint -k

- name: Run Tests
run: make test-coverage
Expand Down
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ PYTHON = $(shell which python)
TOPDIR = $(shell pwd)
DIRS = test bin locale src
PYDIRS = quipucords
TEST_OPTS := -n auto -ra
QPC_COMPARISON_REVISION = a362b28db064c7a4ee38fe66685ba891f33ee5ba

BINDIR = bin

Expand Down Expand Up @@ -35,25 +37,26 @@ clean:
rm -rf quipucords/quipucords/templates

test:
PYTHONHASHSEED=0 QUIPUCORDS_MANAGER_HEARTBEAT=1 QPC_DISABLE_AUTHENTICATION=True PYTHONPATH=`pwd`/quipucords pytest -ra -n auto
PYTHONHASHSEED=0 QUIPUCORDS_MANAGER_HEARTBEAT=1 QPC_DISABLE_AUTHENTICATION=True PYTHONPATH=`pwd`/quipucords \
pytest $(TEST_OPTS)

test-case:
echo $(pattern)
QUIPUCORDS_MANAGER_HEARTBEAT=1 QPC_DISABLE_AUTHENTICATION=True $(PYTHON) quipucords/manage.py test -v 2 quipucords/ -p $(pattern)
$(MAKE) test -e TEST_OPTS="${TEST_OPTS} $(pattern)"

test-coverage:
PYTHONHASHSEED=0 QUIPUCORDS_MANAGER_HEARTBEAT=1 QPC_DISABLE_AUTHENTICATION=True PYTHONPATH=`pwd`/quipucords pytest -ra -n auto --cov=quipucords
$(MAKE) test TEST_OPTS="${TEST_OPTS} --cov=quipucords"

swagger-valid:
node_modules/swagger-cli/swagger-cli.js validate docs/swagger.yml

lint-flake8:
flake8 . --ignore D203,W504,W605,Q000 --exclude quipucords/api/migrations,docs,build,.vscode,client,venv,deploy,quipucords/local_gunicorn.conf.py
git diff $(QPC_COMPARISON_REVISION) | flakeheaven lint --diff .

lint-pylint:
find . -name "*.py" -not -name "*0*.py" -not -path "./build/*" -not -path "./docs/*" -not -path "./.vscode/*" -not -path "./client/*" -not -path "./venv/*" -not -path "./deploy/*" -not -path "./quipucords/local_gunicorn.conf.py" | DJANGO_SETTINGS_MODULE=quipucords.settings xargs $(PYTHON) -m pylint --load-plugins=pylint_django --disable=duplicate-code,wrong-import-order,useless-import-alias,unnecessary-pass,too-many-lines,raise-missing-from
lint-black:
darker --check --diff --revision $(QPC_COMPARISON_REVISION) .

lint: lint-flake8 lint-pylint
lint: lint-black lint-flake8

server-makemigrations:
$(PYTHON) quipucords/manage.py makemigrations api --settings quipucords.settings
Expand Down
26 changes: 16 additions & 10 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
coverage==5.3.1

# Test runner/testing utils
pytest==6.2.1
pytest-django==4.1.0
requests-mock==1.8.0
pytest-xdist==2.5.0
pytest-cov==3.0.0
pytest-django==4.5.2
pytest-xdist==2.5.0
pytest==7.0.1
requests-mock==1.8.0

# Linting
pydocstyle==5.1.1
flake8==3.8.4
black==22.1.0
darker==1.4.1
flake8-docstrings==1.5.0
flake8-import-order==0.18.1
flake8-pylint==0.1.3
flake8-quotes==3.2.0
pylint==2.6.0
pylint-django==2.3.0
pylint-plugin-utils==0.6
pyflakes==2.2.0
flake8==4.0.1
flakeheaven==0.11.1
isort==5.10.1
pydocstyle==5.1.1
pyflakes==2.4.0
pylint-django==2.5.2
pylint-plugin-utils==0.7
pylint-pytest==1.1.2
pylint==2.12.2

# Editor
autopep8==1.5.4
Expand Down
45 changes: 45 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[tool.darker]
revision = "a362b28db064c7a4ee38fe66685ba891f33ee5ba"
isort = true

[tool.flakeheaven]
baseline = ".flakeheaven-baseline"
exclude = [
"docs",
"build",
".vscode",
"client",
"venv",
]
max_line_length = 88

[tool.flakeheaven.plugins]
pycodestyle = ["+*","-W504","-W605"]
pyflakes = ["+*"]
"flake8-*" = ["+*", "-Q000"]

[tool.isort]
profile = "black"

[tool.pylint.format]
max-line-length = "88"

[tool.pylint.master]
init-hook = "import sys; sys.path.append('./quipucords')"
django-settings-module = "quipucords.settings"
load-plugins = [
"pylint_django",
"pylint_pytest",
]

[tool.pytest.ini_options]
pythonpath = "quipucords/"
addopts = [
"--ds=quipucords.settings",
]
python_files = [
"tests.py",
"test_*.py",
"*_tests.py",
"tests_*.py",
]
3 changes: 0 additions & 3 deletions pytest.ini

This file was deleted.

8 changes: 4 additions & 4 deletions quipucords/quipucords/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ def is_int(value):
'db.sqlite3')
DB_PATH = PROD_DB if PRODUCTION else DEV_DB
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': DB_PATH,
'TEST': {'NAME': ':memory:'}
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": DB_PATH,
"TEST": {"NAME": ":memory:"},
}
}
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
("QPC_FEATURE_TEST", "1", "TEST", True),
),
)
def test_get_feature_flags_from_env(
env_name, env_value, feature_name, feature_value
):
def test_get_feature_flags_from_env(env_name, env_value, feature_name, feature_value):
"""Tests if function retrieves new variables set in .env."""
with mock.patch.dict(os.environ, {env_name: env_value}):
dict_with_test_flags = FeatureFlag.get_feature_flags_from_env()
Expand Down Expand Up @@ -74,7 +72,8 @@ def test_when_value_cant_be_cast_to_int():
),
)
def test_when_int_is_not_valid_value_for_env(
env_name, env_value,
env_name,
env_value,
):
"""Test when int is not a valid value for env."""
with mock.patch.dict(os.environ, ({env_name: env_value})), pytest.raises(
Expand All @@ -93,9 +92,7 @@ def test_when_int_is_not_valid_value_for_env(
("QPC_TEST1_FEATURE_", "0", "TEST1_"),
),
)
def test_function_only_adds_names_follow_standard(
env_name, env_value, feature_name
):
def test_function_only_adds_names_follow_standard(env_name, env_value, feature_name):
"""Tests if function only adds variables that start with QPC_FEATURE_."""
with mock.patch.dict(os.environ, ({env_name: env_value})):
dict_with_test_flags = FeatureFlag.get_feature_flags_from_env()
Expand Down Expand Up @@ -130,33 +127,27 @@ def setup_feature_flag_instance_for_tests():


def test_if_instance_contains_all_attributes(
setup_feature_flag_instance_for_tests, # pylint: disable=redefined-outer-name # noqa: E501
setup_feature_flag_instance_for_tests,
):
"""Tests if the constructor loads all attributes correctly."""
assert hasattr(setup_feature_flag_instance_for_tests, "TEST")
assert hasattr(setup_feature_flag_instance_for_tests, "OVERALL_STATUS")


def test_if_instance_attributes_values_are_correct(
setup_feature_flag_instance_for_tests, # pylint: disable=redefined-outer-name # noqa: E501
setup_feature_flag_instance_for_tests,
):
"""Tests if the right values are attributed to attribute."""
assert setup_feature_flag_instance_for_tests.TEST is True
assert setup_feature_flag_instance_for_tests.OVERALL_STATUS is False


def test_is_feature_active(setup_feature_flag_instance_for_tests): # pylint: disable=redefined-outer-name # noqa: E501
def test_is_feature_active(setup_feature_flag_instance_for_tests):
"""Tests method is_feature_active."""
assert setup_feature_flag_instance_for_tests.is_feature_active("TEST") is True
assert (
setup_feature_flag_instance_for_tests.is_feature_active("TEST") is True
)
assert (
setup_feature_flag_instance_for_tests.is_feature_active(
"OVERALL_STATUS"
)
setup_feature_flag_instance_for_tests.is_feature_active("OVERALL_STATUS")
is False
)
with pytest.raises(ValueError):
setup_feature_flag_instance_for_tests.is_feature_active(
"FALSE_ATTRIBUTE"
)
setup_feature_flag_instance_for_tests.is_feature_active("FALSE_ATTRIBUTE")

0 comments on commit bea0cd0

Please sign in to comment.