Skip to content

Commit

Permalink
tests: lint and test using Tox (#4005)
Browse files Browse the repository at this point in the history
Configures Tox >=4.0 and adds it as a dev requirement, adds a GitHub
workflow for tox, and updates the Makefile to use tox environments.

The tox configuration includes labels for CI, linting, auto-fixing (fix)
and tests.
  • Loading branch information
lengau authored Jan 14, 2023
1 parent c1e240c commit 5e14f6d
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
sudo apt update
sudo apt install -y libapt-pkg-dev libyaml-dev xdelta3 shellcheck
pip install -U -r requirements.txt -r requirements-devel.txt
pip install .
pip install .[dev]
- name: Run black
run: |
make test-black
Expand Down
89 changes: 89 additions & 0 deletions .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Tox
on:
push:
branches:
- "main"
- "snapcraft/7.0"
- "release/*"
- "hotfix/*"
pull_request:

jobs:
linters:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
echo "::group::Begin snap install"
echo "Installing snaps in the background while running apt and pip..."
sudo snap install --no-wait --classic pyright
sudo snap install --no-wait shellcheck
echo "::endgroup::"
echo "::group::apt-get update"
sudo apt-get update
echo "::endgroup::"
echo "::group::apt-get install..."
sudo apt-get install --yes libapt-pkg-dev libyaml-dev xdelta3
echo "::endgroup::"
echo "::group::pip install"
python -m pip install 'tox>=4.0.11<5.0' tox-gh
echo "::endgroup::"
echo "::group::Create virtual environments for linting processes."
tox run-parallel --parallel all --parallel-no-spinner -m lint --notest
echo "::endgroup::"
echo "::group::Wait for snap to complete"
snap watch --last=install
echo "::endgroup::"
- name: Run Linters
run: tox run --skip-pkg-install -m lint
tests:
strategy:
fail-fast: false # Run all the tests to their conclusions.
matrix:
platform: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Python versions on ${{ matrix.platform }}
uses: actions/setup-python@v4
with:
python-version: |
3.8
3.10
- name: Install dependencies
run: |
echo "::group::apt-get update"
sudo apt-get update
echo "::endgroup::"
echo "::group::apt-get install..."
sudo apt-get install -y libapt-pkg-dev libyaml-dev xdelta3
echo "::endgroup::"
echo "::group::pip install"
python -m pip install 'tox>=4.0.11<5.0' tox-gh
echo "::endgroup::"
mkdir -p results
- name: Setup Tox environments
run: tox run-parallel --parallel auto --parallel-no-spinner --parallel-live -m ci --notest
- name: Test with tox
run: tox run-parallel --parallel all --parallel-no-spinner --result-json results/tox-${{ matrix.platform }}.json -m ci --skip-pkg-install -- --no-header --quiet -rN
- name: Upload code coverage
uses: codecov/codecov-action@v3
with:
directory: ./results/
files: coverage*.xml
- name: Upload test results
if: always()
uses: actions/upload-artifact@v3
with:
name: test-results-${{ matrix.platform }}
path: results/
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
build
Cargo.lock
.coverage**
coverage-*
demos/*/parts/
demos/*/prime/
demos/**/*.snap
Expand All @@ -21,6 +22,7 @@ pip-wheel-metadata/
prime
*.pyc
__pycache__
.pytest_cache
*.snap
snap/.snapcraft/
.spread-reuse.*
Expand All @@ -29,6 +31,8 @@ stage
target
tests/unit/snap/
tests/unit/stage/
test-results*
.tox
.vscode
venv
.venv
25 changes: 11 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,39 @@ SOURCES_LEGACY=snapcraft_legacy tests/legacy

.PHONY: autoformat-black
autoformat-black:
black $(SOURCES) $(SOURCES_LEGACY)
tox run -e format

.PHONY: freeze-requirements
freeze-requirements:
tools/freeze-requirements.sh

.PHONY: test-black
test-black:
black --check --diff $(SOURCES) $(SOURCES_LEGACY)
tox run -e black

.PHONY: test-codespell
test-codespell:
codespell
tox run -e codespell

.PHONY: test-isort
test-isort:
isort --check $(SOURCES) $(SOURCES_LEGACY)
tox run -e isort

.PHONY: test-mypy
test-mypy:
mypy $(SOURCES)
tox run -e mypy

.PHONY: test-pydocstyle
test-pydocstyle:
pydocstyle snapcraft
tox run -e docstyle

.PHONY: test-pylint
test-pylint:
pylint snapcraft
pylint tests/*.py tests/unit --disable=invalid-name,missing-module-docstring,missing-function-docstring,duplicate-code,protected-access,unspecified-encoding,too-many-public-methods,too-many-arguments,too-many-lines
tox run -e pylint

.PHONY: test-pyright
test-pyright:
pyright $(SOURCES)
tox run -e pyright

.PHONY: test-ruff
test-ruff:
Expand All @@ -45,17 +44,15 @@ test-ruff:

.PHONY: test-shellcheck
test-shellcheck:
# Skip third-party gradlew script.
find . \( -name .git -o -name gradlew \) -prune -o -print0 | xargs -0 file -N | grep shell.script | cut -f1 -d: | xargs shellcheck
./tools/spread-shellcheck.py spread.yaml tests/spread/
tox run -e spellcheck

.PHONY: test-legacy-units
test-legacy-units:
pytest --cov-report=xml --cov=snapcraft tests/legacy/unit
tox run -e py38-withreq-legacy

.PHONY: test-units
test-units: test-legacy-units
pytest --cov-report=xml --cov=snapcraft tests/unit
tox run -e py38-withreq-unit

.PHONY: tests
tests: tests-static test-units
Expand Down
32 changes: 31 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ exclude = '''
| ^/prime
)/
'''
# Targeting future versions as well so we don't have black reformatting code
# en masse later.
target_version = ["py38", "py310", "py311"]

[tool.isort]
# black-compatible isort configuration
Expand All @@ -30,6 +33,9 @@ use_parentheses = true
ensure_newline_before_comments = true
line_length = 88

[tool.pylint.main]
ignore-paths = ["tests/legacy"]

[tool.pylint.messages_control]
# duplicate-code can't be disabled locally: https://github.com/PyCQA/pylint/issues/214
disable = "too-few-public-methods,fixme,use-implicit-booleaness-not-comparison,duplicate-code,unnecessary-lambda-assignment"
Expand All @@ -52,12 +58,36 @@ load-plugins = "pylint_fixme_info,pylint_pytest"
[tool.pylint.SIMILARITIES]
min-similarity-lines=10

[tool.mypy]
python_version = 3.8
ignore_missing_imports = true
follow_imports = "silent"
exclude = [
"build",
"snapcraft_legacy",
"tests/spread",
"tests/legacy",
"tools",
]

[tool.pyright]
include = ["snapcraft", "tests"]
exclude = ["tests/legacy", "tests/spread", "build"]
pythonVersion = "3.8"

[tool.pytest.ini_options]
minversion = 7.0
required_plugins = ["pytest-cov>=4.0", "pytest-mock>=3.10", "pytest-subprocess>=1.4"]
addopts = ["--cov=snapcraft"]

# Most of this ruff configuration comes from craft-parts
[tool.ruff]
line-length = 88
extend-exclude = [
"docs",
"__pycache__"
"__pycache__",
"legacy",
"tests/legacy",
]
select = [
"E", "F", # The rules built into Flake8
Expand Down
28 changes: 15 additions & 13 deletions requirements-devel.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
astroid==2.13.2
attrs==22.2.0
black==22.12.0
cachetools==5.2.0
catkin-pkg==0.5.2
certifi==2022.12.7
certifi==2022.9.24
cffi==1.15.1
chardet==5.1.0
charset-normalizer==2.1.1
click==8.1.3
codespell==2.2.2
colorama==0.4.6
coverage==7.0.4
craft-cli==1.2.0
craft-grammar==1.1.1
craft-parts==1.17.1
craft-providers==1.6.2
craft-providers==1.6.1
craft-store==2.3.0
cryptography==3.4
Deprecated==1.2.13
dill==0.3.6
dill==0.3.5.1
distro==1.8.0
docutils==0.19
exceptiongroup==1.1.0
Expand All @@ -40,8 +42,8 @@ lazy-object-proxy==1.9.0
lxml==4.9.2
macaroonbakery==1.3.1
mccabe==0.7.0
more-itertools==9.0.0
mypy==0.991
more-itertools==8.14.0
mypy==0.982
mypy-extensions==0.4.3
oauthlib==3.2.2
overrides==7.3.1
Expand All @@ -56,7 +58,7 @@ platformdirs==2.6.2
pluggy==1.0.0
progressbar==2.5
protobuf==3.20.3
psutil==5.9.4
psutil==5.9.2
ptyprocess==0.7.0
pycodestyle==2.10.0
pycparser==2.21
Expand All @@ -74,7 +76,7 @@ pymacaroons==0.13.0
pyparsing==3.0.9
pyramid==2.0
pyRFC3339==1.1
pytest==7.2.0
pytest==7.1.3
pytest-cov==4.0.0
pytest-mock==3.10.0
pytest-subprocess==1.4.2
Expand All @@ -85,15 +87,15 @@ pyxdg==0.28
PyYAML==6.0
raven==6.10.0
requests==2.28.1
requests-toolbelt==0.10.1
requests-toolbelt==0.10.0
requests-unixsocket==0.3.0
ruff==0.0.215
SecretStorage==3.3.3
semantic-version==2.10.0
semver==2.13.0
simplejson==3.18.1
six==1.16.0
snap-helpers==0.3.1
snap-helpers==0.2.0
snowballstemmer==2.2.0
tabulate==0.9.0
testscenarios==0.5.0
Expand All @@ -102,24 +104,24 @@ tinydb==4.7.0
toml==0.10.2
tomli==2.0.1
tomlkit==0.11.6
tox==4.0.11
translationstring==1.4
types-Deprecated==1.2.9
types-docutils==0.19.1.1
types-PyYAML==6.0.12.2
types-requests==2.28.11.7
types-setuptools==65.6.0.3
types-tabulate==0.9.0.0
types-urllib3==1.26.25.4
types-urllib3==1.26.25
typing_extensions==4.4.0
urllib3==1.26.13
urllib3==1.26.12
venusian==3.0.0
wadllib==1.3.6
WebOb==1.8.7
wrapt==1.14.1
ws4py==0.5.1
zipp==3.11.0
zope.deprecation==4.4.0
zope.interface==5.5.2
zope.interface==5.5.0
python-apt @ https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/python-apt/2.0.0ubuntu0.20.04.6/python-apt_2.0.0ubuntu0.20.04.6.tar.xz; sys.platform == "linux"
PyNaCl==1.4.0; sys.platform != "linux"
PyNaCl @ https://files.pythonhosted.org/packages/61/ab/2ac6dea8489fa713e2b4c6c5b549cc962dd4a842b5998d9e80cf8440b7cd/PyNaCl-1.3.0.tar.gz; sys.platform == "linux"
Expand Down
Loading

0 comments on commit 5e14f6d

Please sign in to comment.