Skip to content

Commit

Permalink
update devcontainer for pytest+ruff+black
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Sherman <[email protected]>
  • Loading branch information
usingtechnology committed Nov 8, 2023
1 parent ac277ce commit b8f6fd0
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 61 deletions.
32 changes: 21 additions & 11 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,43 @@
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.black-formatter"
],
"ms-python.python",
"ms-python.black-formatter",
"charliermarsh.ruff"
],
"settings": {
"python.testing.pytestArgs": [
".",
"aries_cloudagent",
"--no-cov"
],
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": "pytest",
"editor.defaultFormatter": null,
"editor.formatOnSave": false, // enable per language
"[python]": {
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
"ruff.organizeImports": false
},
"black-formatter.importStrategy": "useBundled",
"black-formatter.showNotifications": "always",
"ruff.codeAction.fixViolation": {
"enable": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.blackArgs": []
"ruff.fixAll": true,
"ruff.format.args": ["--config=./pyproject.toml"],
"ruff.lint.args": ["--config=./pyproject.toml"]
}
}
},

"features": {
"docker-in-docker": "latest",
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
Expand Down
23 changes: 22 additions & 1 deletion .devcontainer/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,25 @@ pip3 install -r demo/requirements.txt -r demo/requirements.behave.txt
pip3 install black

# install a version of aries-cloudagent so the pytests can pick up a version
pip3 install aries-cloudagent
pip3 install aries-cloudagent

# hack/workaround to allow `pytest .` and `poetry run pytest` work.
# need to not run ruff...

cat > .pytest.ini <<EOF
# this is created for the devcontainer so pytests are properly discoverable.
# remove this file for normal operations outside of the devcontainer.
# basically we cannot have ruff (--ruff) in the pytest configuration as it breaks the Testing View.
[pytest]
testpaths = "aries_cloudagent"
addopts = --quiet
markers = [
"askar: Tests specifically relating to Aries-Askar support",
"indy: Tests specifically relating to Hyperledger Indy SDK support",
"indy_credx: Tests specifically relating to Indy-Credx support",
"indy_vdr: Tests specifically relating to Indy-VDR support",
"ursa_bbs_signatures: Tests specificaly relating to BBS Signatures support",
"postgres: Tests relating to the postgres storage plugin for Indy"]
junit_family = "xunit1"
asyncio_mode = auto
EOF
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ test-reports
docker
env
.venv
.devcontainer
.vscode
.vscode-sample
.pytest_cache
.ruff_cache
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,6 @@ _build/

# Open API build
open-api/.build

# devcontainer
.pytest.ini
14 changes: 2 additions & 12 deletions aries_cloudagent/messaging/credential_definitions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,14 @@
request_schema,
response_schema,
)

from marshmallow import fields

from ...admin.request_context import AdminRequestContext
from ...anoncreds.base import AnonCredsResolutionError
from ...anoncreds.issuer import AnonCredsIssuer, AnonCredsIssuerError
from ...anoncreds.registry import AnonCredsRegistry

from ...wallet.base import BaseWallet

from ...admin.request_context import AdminRequestContext

from ...indy.models.cred_def import CredentialDefinitionSchema

from ...ledger.error import BadLedgerRequestError


from ...protocols.endorse_transaction.v1_0.manager import (
TransactionManager,
TransactionManagerError,
Expand All @@ -37,9 +29,8 @@
from ...protocols.endorse_transaction.v1_0.util import (
get_endorser_connection_id,
)

from ...storage.error import StorageError

from ...wallet.base import BaseWallet
from ..models.openapi import OpenAPISchema
from ..valid import (
INDY_CRED_DEF_ID_EXAMPLE,
Expand All @@ -56,7 +47,6 @@
)



class CredentialDefinitionSendRequestSchema(OpenAPISchema):
"""Request schema for schema send request."""

Expand Down
6 changes: 2 additions & 4 deletions aries_cloudagent/messaging/schemas/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Credential schema admin routes."""

import functools

from aiohttp import web
from aiohttp_apispec import (
docs,
Expand All @@ -9,11 +10,10 @@
request_schema,
response_schema,
)

from marshmallow import fields
from marshmallow.validate import Regexp
from aries_cloudagent.anoncreds.base import AnonCredsResolutionError

from aries_cloudagent.anoncreds.base import AnonCredsResolutionError
from aries_cloudagent.anoncreds.issuer import AnonCredsIssuer, AnonCredsIssuerError
from aries_cloudagent.anoncreds.registry import AnonCredsRegistry
from aries_cloudagent.wallet.base import BaseWallet
Expand All @@ -24,7 +24,6 @@
from ...protocols.endorse_transaction.v1_0.models.transaction_record import (
TransactionRecordSchema,
)

from ..models.openapi import OpenAPISchema
from ..valid import (
B58,
Expand All @@ -40,7 +39,6 @@
)



class SchemaSendRequestSchema(OpenAPISchema):
"""Request schema for schema send request."""

Expand Down
23 changes: 22 additions & 1 deletion devcontainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ To open ACA-Py in a devcontainer, we open the *root* of this repository. We can

*NOTE* follow any prompts to install `Python Extension` or reload window for `Pylance` when first building the container.

*ADDITIONAL NOTE* we advise that after each time you rebuild the container that you also perform: `Developer: Reload Window` as some extensions seem to require this in order to work as expected.

#### devcontainer.json

When the [.devcontainer/devcontainer.json](.devcontainer/devcontainer.json) is opened, you will see it building... it is building a Python 3.9 image (bash shell) and loading it with all the ACA-Py requirements (and black). Since this is a Docker container, we will also open ports `9060` and `9061`, allowing you to run/debug ACA-Py with those ports available to your `localhost` (more on those later). We also load a few Visual Studio settings (for running Pytests and formatting with Flake and Black).
Expand All @@ -81,6 +83,23 @@ black . --check

The first command should show you that `aries_cloudagent` module is loaded (ACA-Py). The others are examples of code quality checks that ACA-Py does on commits (if you have [`precommit`](https://pre-commit.com) installed) and Pull Requests.

When running `ruff check .` in the terminal, you may see `error: Failed to initialize cache at /.ruff_cache: Permission denied (os error 13)` - that's ok. If there are actual ruff errors, you should see something like:

```
error: Failed to initialize cache at /.ruff_cache: Permission denied (os error 13)
admin/base_server.py:7:7: D101 Missing docstring in public class
Found 1 error.
```

#### extensions

We have added Black formatter and Ruff extensions. Although we have added launch settings for both `ruff` and `black`, you can also use the extension commands from the command palette.

- `Ruff: Format Document`
- `Ruff: Fix all auto-fixable problems`

More importantly, these extensions are now added to document save, so files will be formatted and checked. We advise that after each time you rebuild the container that you also perform: `Developer: Reload Window` to ensure the extensions are loaded correctly.

## Debugging

To better illustrate debugging pytests and ACA-Py runtime code, let's add some run/debug configurations to VS Code. If you have your own `launch.json` and `settings.json`, please cut and paste what you want/need.
Expand All @@ -99,6 +118,8 @@ See [Python Testing](https://code.visualstudio.com/docs/python/testing) for more

*IMPORTANT*: our pytests include coverage, which will prevent the [debugger from working](https://code.visualstudio.com/docs/python/testing#_debug-tests). One way around this would be to have a `.vscode/settings.json` that says not to use coverage (see above). This will allow you to set breakpoints in the pytest and code under test and use commands such as `Test: Debug Tests in Current File` to start debugging.

*WARNING*: the project configuration found in `pyproject.toml` include performing `ruff` checks when we run `pytest`. Including `ruff` does not play nice with the Testing view. In order to have our pytests discoverable AND available in the Testing view, we create a `.pytest.ini` when we build the devcontainer. This file will not be commited to the repo, nor does it impact `./scripts/run_tests` but it will impact if you manually run the pytest commands locally outside of the devcontainer. Just be aware that the file will stay on your file system after you shutdown the devcontainer.


### ACA-Py

Expand Down Expand Up @@ -130,4 +151,4 @@ For example, open `aries_cloudagent/admin/server.py` and set a breakpoint in `as

## Next Steps

At this point, you now have a development environment where you can add pytests, add ACA-Py code and run and debug it all. Be aware there are limitations with `devcontainer` and other docker networks. You may need to adjust other docker-compose files not to start their own networks, and you may need to reference containers using `host.docker.internal`. This isn't a panacea but should get you going in the right direction and provide you with some development tools.
At this point, you now have a development environment where you can add pytests, add ACA-Py code and run and debug it all. Be aware there are limitations with `devcontainer` and other docker networks. You may need to adjust other docker-compose files not to start their own networks, and you may need to reference containers using `host.docker.internal`. This isn't a panacea but should get you going in the right direction and provide you with some development tools.
Loading

0 comments on commit b8f6fd0

Please sign in to comment.