Skip to content
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

Issue 2570 devcontainer ruff, black and pytest #2595

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
32 changes: 22 additions & 10 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,43 @@
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
],
"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.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"editor.defaultFormatter": "ms-python.black-formatter",
"ruff.organizeImports": false
},
"black-formatter.importStrategy": "fromEnvironment",
"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
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ test-reports
docker
env
.venv
.devcontainer
.vscode
.vscode-sample
.pytest_cache
.ruff_cache
.pytest.ini
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
21 changes: 21 additions & 0 deletions 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
Loading